schedule-refactor #45

Merged
sebastian merged 18 commits from schedule-refactor into master 2023-11-11 18:56:16 +01:00
15 changed files with 29 additions and 811 deletions
Showing only changes of commit 27f7a2ad42 - Show all commits

57
.gitignore vendored
View File

@ -27,44 +27,45 @@ replay_pid*
# ---> JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
.idea/
**/.idea/
# User-specific stff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
**/.idea/**/workspace.xml
**/.idea/**/tasks.xml
**/.idea/**/usage.statistics.xml
**/.idea/**/dictionaries
**/.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
**/.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
**/.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
**/.idea/**/dataSources/
**/.idea/**/dataSources.ids
**/.idea/**/dataSources.local.xml
**/.idea/**/sqlDataSources.xml
**/.idea/**/dynamic.xml
**/.idea/**/uiDesigner.xml
**/.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
**/.idea/**/gradle.xml
**/.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# **/.idea/artifacts
# **/.idea/compiler.xml
# **/.idea/jarRepositories.xml
# **/.idea/modules.xml
# **/.idea/*.iml
# **/.idea/modules
# *.iml
# *.ipr
@ -72,7 +73,7 @@ replay_pid*
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
**/.idea/**/mongoSettings.xml
# File-based project format
*.iws
@ -87,10 +88,10 @@ out/
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
**/.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
**/.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
@ -99,10 +100,10 @@ crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
**/.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
**/.idea/caches/build_file_checksums.ser
# ---> Maven
target/

View File

@ -22,234 +22,5 @@ import java.util.*;
@RequestMapping("/api")
public class ScheduleController {
private final TaskService taskService;
private final TaskScheduleService taskScheduleService;
private final BasicTaskScheduleRepository basicTaskScheduleRepository;
private final UserRepository userRepository;
public ScheduleController(@Autowired TaskService taskService, @Autowired TaskScheduleService taskScheduleService,
BasicTaskScheduleRepository basicTaskScheduleRepository,
UserRepository userRepository) {
this.taskService = taskService;
this.taskScheduleService = taskScheduleService;
this.basicTaskScheduleRepository = basicTaskScheduleRepository;
this.userRepository = userRepository;
}
@GetMapping("/schedules/{taskID}/{scheduleType}")
public ResponseEntity<?> loadSchedulesOfTask(@PathVariable long taskID, @PathVariable ScheduleType scheduleType) {
PermissionResult<Task> taskPermissionResult = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!taskPermissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(taskPermissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
switch (scheduleType) {
case BASIC -> {
List<BasicTaskSchedule> basicTaskScheduleList = new ArrayList<>(taskPermissionResult.getResult().getBasicTaskSchedules());
basicTaskScheduleList.sort(Comparator.comparing(BasicTaskSchedule::getScheduleDate));
return ResponseEntity.ok(basicTaskScheduleList.stream().map(BasicTaskScheduleEntityInfo::new));
}
case ADVANCED, MODERATE -> ResponseEntity.ok(new ArrayList<>());
}
return null;
}
@PutMapping("/schedules/{taskID}/basic")
public ResponseEntity<?> basicScheduleTask(@PathVariable long taskID, @Valid @RequestBody BasicTaskScheduleFieldInfo basicTaskSchedule) {
PermissionResult<Task> taskPermissionResult = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!taskPermissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(taskPermissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
ServiceResult<BasicTaskSchedule> creationResult = taskScheduleService.createBasicTaskSchedule(taskPermissionResult.getResult(), basicTaskSchedule);
return ResponseEntity.ok(new BasicTaskScheduleEntityInfo(creationResult.getResult()));
}
@PostMapping("/schedules/{scheduleID}/basic")
public ResponseEntity<?> editScheduledTask(@PathVariable long scheduleID, @Valid @RequestBody BasicTaskScheduleFieldInfo basicTaskScheduleFieldInfo) {
PermissionResult<BasicTaskSchedule> permissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!permissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(permissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
taskScheduleService.reschedule(permissionResult.getResult(), basicTaskScheduleFieldInfo);
return ResponseEntity.ok(new SimpleStatusResponse("success"));
}
@DeleteMapping("/schedules/{scheduleID}/{scheduleType}")
public ResponseEntity<?> deleteScheduledTask(@PathVariable long scheduleID, @PathVariable ScheduleType scheduleType) {
PermissionResult<BasicTaskSchedule> permissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!permissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(permissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
taskScheduleService.deleteBasicSchedule(permissionResult.getResult());
return ResponseEntity.ok(new SimpleStatusResponse("success"));
}
@GetMapping("/schedules/today/{activateable}")
public ResponseEntity<?> loadTodaysSchedules(@PathVariable boolean activateable) {
ServiceResult<List<BasicTaskSchedule>> todaysSchedules = taskScheduleService.loadTodaysSchedule(SecurityContextHolder.getContext().getAuthentication().getName(), activateable);
if(todaysSchedules.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
} else {
return ResponseEntity.ok(todaysSchedules.getResult().stream().map(ScheduleInfo::new).toList());
}
}
@GetMapping("/schedules")
public ResponseEntity<?> loadSchedules() {
ServiceResult<List<BasicTaskSchedule>> schedules = taskScheduleService.loadSchedules(SecurityContextHolder.getContext().getAuthentication().getName());
if(schedules.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
} else {
return ResponseEntity.ok(schedules.getResult().stream().map(ScheduleInfo::new).toList());
}
}
@PostMapping("/schedules/{taskID}/now")
public ResponseEntity<?> scheduleTaskNow(@PathVariable long taskID) {
PermissionResult<Task> permissionResult = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!permissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(permissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
ServiceResult<BasicTaskSchedule> scheduleResult = taskScheduleService.scheduleTaskNow(permissionResult.getResult());
if(scheduleResult.getExitCode() == ServiceExitCode.ENTITY_ALREADY_EXIST) {
return ResponseEntity.status(409).body(new SimpleStatusResponse("failed"));
} else {
return ResponseEntity.ok(new ScheduleInfo(scheduleResult.getResult()));
}
}
@GetMapping("/schedules/active")
public ResponseEntity<?> getActiveSchedule() {
ServiceResult<BasicTaskSchedule> activeScheduleResult = taskScheduleService.getActiveSchedule(SecurityContextHolder.getContext().getAuthentication().getName());
if(activeScheduleResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.ok(new ScheduleInfo());
} else {
return ResponseEntity.ok(new ScheduleInfo(activeScheduleResult.getResult()));
}
}
@PostMapping("/schedules/{scheduleID}/activate")
public ResponseEntity<?> activateSchedule(@PathVariable long scheduleID) {
PermissionResult<BasicTaskSchedule> schedulePermissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!schedulePermissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(schedulePermissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
ServiceResult<BasicTaskSchedule> scheduleActivateResult = taskScheduleService.activateSchedule(schedulePermissionResult.getResult());
if(scheduleActivateResult.getExitCode() == ServiceExitCode.ENTITY_ALREADY_EXIST) {
return ResponseEntity.status(409).body(new SimpleStatusResponse("failed"));
} else if(scheduleActivateResult.getExitCode() == ServiceExitCode.OK){
return ResponseEntity.ok(new ScheduleActivateInfo(scheduleActivateResult.getResult().getStartTime()));
} else {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
}
@PostMapping("/schedules/{scheduleID}/stop/{finish}")
public ResponseEntity<?> stopSchedule(@PathVariable long scheduleID, @PathVariable boolean finish) {
PermissionResult<BasicTaskSchedule> schedulePermissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!schedulePermissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(schedulePermissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
ServiceResult<Integer> stopResult = taskScheduleService.stopSchedule(schedulePermissionResult.getResult(), finish);
if(stopResult.getExitCode() == ServiceExitCode.INVALID_OPERATION) {
return ResponseEntity.status(400).body(new SimpleStatusResponse("failed"));
} else {
return ResponseEntity.ok(new TaskScheduleStopResponse(stopResult.getResult()));
}
}
@GetMapping("/schedules/status/today")
public ResponseEntity<?> getWorkedMinutesToday() {
Optional<User> user = userRepository.findByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
if(user.isEmpty()) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
int workedMinutes = taskScheduleService.getWorkedMinutes(user.get());
return ResponseEntity.ok(new ScheduleStatus(workedMinutes, taskScheduleService.isAnyScheduleMissed(user.get())));
}
@PostMapping("/schedules/{taskID}/forgotten")
public ResponseEntity<?> registerForgottenActivity(@PathVariable long taskID, @Valid @RequestBody ForgottenActivity forgottenActivity) {
PermissionResult<Task> permissionResult = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!permissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(permissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
ServiceResult<Integer> serviceResult = taskScheduleService.registerForgottenActivity(permissionResult.getResult(), forgottenActivity);
if(serviceResult.getExitCode() == ServiceExitCode.INVALID_OPERATION) {
return ResponseEntity.status(400).body(new SimpleStatusResponse("failed"));
} else {
return ResponseEntity.ok(new TaskScheduleStopResponse(serviceResult.getResult()));
}
}
@GetMapping("/schedules/missed")
public ResponseEntity<?> loadMissedSchedules() {
Optional<User> user = userRepository.findByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
if(user.isEmpty()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
List<BasicTaskSchedule> schedules = taskScheduleService.loadMissedSchedules(user.get());
return ResponseEntity.ok(schedules.stream().map(ScheduleInfo::new).toList());
}
@DeleteMapping("/schedules/{scheduleIDs}/all")
public ResponseEntity<?> deleteSchedules(@PathVariable long[] scheduleIDs) {
List<BasicTaskSchedule> schedulesToDelete = new LinkedList<>();
for(long scheduleID: scheduleIDs) {
PermissionResult<BasicTaskSchedule> permissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName());
if(!permissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(permissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
schedulesToDelete.add(permissionResult.getResult());
}
for(BasicTaskSchedule schedule : schedulesToDelete) {
this.taskScheduleService.deleteBasicSchedule(schedule);
}
return ResponseEntity.ok(new SimpleStatusResponse("success"));
}
}

View File

@ -1,7 +1,6 @@
package core.api.controller;
import core.api.models.auth.SimpleStatusResponse;
import core.api.models.timemanager.taskSchedule.ScheduleInfo;
import core.api.models.timemanager.tasks.*;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
@ -13,7 +12,6 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@CrossOrigin(origins = "*", maxAge = 3600)

View File

@ -1,4 +0,0 @@
package core.api.models.timemanager.taskSchedule;
public abstract class AbstractScheduleShortInfo {
}

View File

@ -1,22 +0,0 @@
package core.api.models.timemanager.taskSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import java.time.LocalDate;
public class BasicScheduleShortInfo extends AbstractScheduleShortInfo {
private LocalDate scheduleDate;
public BasicScheduleShortInfo(BasicTaskSchedule basicTaskSchedule) {
this.scheduleDate = basicTaskSchedule.getScheduleDate();
}
public LocalDate getScheduleDate() {
return scheduleDate;
}
public void setScheduleDate(LocalDate scheduleDate) {
this.scheduleDate = scheduleDate;
}
}

View File

@ -1,53 +0,0 @@
package core.api.models.timemanager.taskSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class BasicTaskScheduleEntityInfo {
private long scheduleID;
private LocalDate scheduleDate;
private LocalDateTime startTime;
private LocalDateTime finishedTime;
public BasicTaskScheduleEntityInfo(BasicTaskSchedule basicTaskSchedule) {
this.scheduleID = basicTaskSchedule.getScheduleID();
this.scheduleDate = basicTaskSchedule.getScheduleDate();
this.startTime = basicTaskSchedule.getStartTime();
this.finishedTime = basicTaskSchedule.getFinishedTime();
}
public long getScheduleID() {
return scheduleID;
}
public void setScheduleID(long scheduleID) {
this.scheduleID = scheduleID;
}
public LocalDate getScheduleDate() {
return scheduleDate;
}
public void setScheduleDate(LocalDate scheduleDate) {
this.scheduleDate = scheduleDate;
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
public LocalDateTime getFinishedTime() {
return finishedTime;
}
public void setFinishedTime(LocalDateTime finishedTime) {
this.finishedTime = finishedTime;
}
}

View File

@ -1,17 +0,0 @@
package core.api.models.timemanager.taskSchedule;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
public class BasicTaskScheduleFieldInfo {
@NotNull
private LocalDate scheduleDate;
public LocalDate getScheduleDate() {
return scheduleDate;
}
public void setScheduleDate(LocalDate scheduleDate) {
this.scheduleDate = scheduleDate;
}
}

View File

@ -1,22 +0,0 @@
package core.api.models.timemanager.taskSchedule;
public class ForgottenActivity {
private ForgottenActivityMode mode;
private int minutesSpent;
public ForgottenActivityMode getMode() {
return mode;
}
public void setMode(ForgottenActivityMode mode) {
this.mode = mode;
}
public int getMinutesSpent() {
return minutesSpent;
}
public void setMinutesSpent(int minutesSpent) {
this.minutesSpent = minutesSpent;
}
}

View File

@ -1,8 +0,0 @@
package core.api.models.timemanager.taskSchedule;
public enum ForgottenActivityMode {
MANUAL,
LAST,
PLANNED
}

View File

@ -1,21 +0,0 @@
package core.api.models.timemanager.taskSchedule;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ScheduleActivateInfo {
private LocalDateTime startTime;
public ScheduleActivateInfo(LocalDateTime startTime) {
this.startTime = startTime;
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
}

View File

@ -1,114 +0,0 @@
package core.api.models.timemanager.taskSchedule;
import core.api.models.timemanager.taskgroup.TaskgroupEntityInfo;
import core.api.models.timemanager.tasks.TaskShortInfo;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.ScheduleType;
import core.entities.timemanager.Taskgroup;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
public class ScheduleInfo {
private long scheduleID;
private ScheduleType scheduleType;
private LocalDateTime startTime;
private LocalDateTime finishTime;
private int activeMinutes;
private AbstractScheduleShortInfo schedule;
private List<TaskgroupEntityInfo> taskgroupPath;
private TaskShortInfo task;
public ScheduleInfo(BasicTaskSchedule basicTaskSchedule) {
this.scheduleID = basicTaskSchedule.getScheduleID();
this.scheduleType = ScheduleType.BASIC;
this.startTime = basicTaskSchedule.getStartTime();
this.finishTime = basicTaskSchedule.getFinishedTime();
if(this.finishTime == null && this.startTime != null) {
this.activeMinutes = (int) Duration.between(basicTaskSchedule.getStartTime(), LocalDateTime.now()).toMinutes();
} else if(this.startTime != null){
this.activeMinutes = (int) Duration.between(basicTaskSchedule.getStartTime(), basicTaskSchedule.getFinishedTime()).toMinutes();
}
this.schedule = new BasicScheduleShortInfo(basicTaskSchedule);
this.task = new TaskShortInfo(basicTaskSchedule.getTask());
List<Taskgroup> taskgroupPath = Taskgroup.getAncestorList(basicTaskSchedule.getTask().getTaskgroup());
taskgroupPath.add(basicTaskSchedule.getTask().getTaskgroup());
this.taskgroupPath = taskgroupPath.stream().map(TaskgroupEntityInfo::new).toList();
}
public ScheduleInfo() {
this.scheduleID = -1;
}
public long getScheduleID() {
return scheduleID;
}
public void setScheduleID(long scheduleID) {
this.scheduleID = scheduleID;
}
public ScheduleType getScheduleType() {
return scheduleType;
}
public void setScheduleType(ScheduleType scheduleType) {
this.scheduleType = scheduleType;
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
public LocalDateTime getFinishTime() {
return finishTime;
}
public void setFinishTime(LocalDateTime finishTime) {
this.finishTime = finishTime;
}
public int getActiveMinutes() {
return activeMinutes;
}
public void setActiveMinutes(int activeMinutes) {
this.activeMinutes = activeMinutes;
}
public AbstractScheduleShortInfo getSchedule() {
return schedule;
}
public void setSchedule(AbstractScheduleShortInfo schedule) {
this.schedule = schedule;
}
public List<TaskgroupEntityInfo> getTaskgroupPath() {
return taskgroupPath;
}
public void setTaskgroupPath(List<TaskgroupEntityInfo> taskgroupPath) {
this.taskgroupPath = taskgroupPath;
}
public TaskShortInfo getTask() {
return task;
}
public void setTask(TaskShortInfo task) {
this.task = task;
}
}

View File

@ -1,28 +0,0 @@
package core.api.models.timemanager.taskSchedule;
public class ScheduleStatus {
private int activeMinutes;
private boolean missedSchedules;
public ScheduleStatus(int activeMinutes, boolean missedSchedules) {
this.activeMinutes = activeMinutes;
this.missedSchedules = missedSchedules;
}
public int getActiveMinutes() {
return activeMinutes;
}
public void setActiveMinutes(int activeMinutes) {
this.activeMinutes = activeMinutes;
}
public boolean isMissedSchedules() {
return missedSchedules;
}
public void setMissedSchedules(boolean missedSchedules) {
this.missedSchedules = missedSchedules;
}
}

View File

@ -1,18 +0,0 @@
package core.api.models.timemanager.taskSchedule;
public class TaskScheduleStopResponse {
private int workTime;
public TaskScheduleStopResponse(int workTime) {
this.workTime = workTime;
}
public int getWorkTime() {
return workTime;
}
public void setWorkTime(int workTime) {
this.workTime = workTime;
}
}

View File

@ -16,30 +16,4 @@ import java.util.Optional;
@Repository
public interface BasicTaskScheduleRepository extends CrudRepository<BasicTaskSchedule, Long> {
@Modifying
@Transactional
@Query(value = "DELETE FROM BasicTaskSchedule bts WHERE bts.scheduleID = ?1")
void deleteBasicTaskScheduleByID(long id);
@Modifying
@Transactional
@Query(value = "DELETE FROM BasicTaskSchedule bts WHERE bts.task = ?1")
void deleteAllByTask(Task task);
@Query(value = "SELECT bts FROM BasicTaskSchedule bts WHERE bts.task.taskgroup.user = ?1")
List<BasicTaskSchedule> findAllByUser(User user);
@Query(value = "SELECT bts FROM BasicTaskSchedule bts WHERE bts.task.taskgroup.user = ?1 AND bts.scheduleDate = ?2")
List<BasicTaskSchedule> findAllByUserAndDate(User user, LocalDate localDate);
@Query(value = "SELECT bts FROM BasicTaskSchedule bts WHERE bts.task.taskgroup.user.username = ?1 AND bts.startTime is not null and bts.finishedTime is null")
Optional<BasicTaskSchedule> findActiveTaskSchedule(String username);
List<BasicTaskSchedule> findAllByStartTimeIsNull();
@Query(value = "SELECT bts FROM BasicTaskSchedule bts WHERE bts.task.taskgroup.user = ?1 AND bts.scheduleDate = ?2 AND bts.finishedTime IS NOT NULL")
List<BasicTaskSchedule> findAllFinishedByUserAndDate(User user, LocalDate now);
@Query(value = "SELECT bts FROM BasicTaskSchedule bts WHERE bts.task.taskgroup.user = ?1 AND bts.startTime IS NULL AND bts.finishedTime IS NULL")
List<BasicTaskSchedule> findAllUnstartedSchedulesOfUser(User user);
}

View File

@ -1,228 +1,9 @@
package core.services;
import core.api.models.timemanager.taskSchedule.BasicTaskScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.ForgottenActivity;
import core.entities.User;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
import core.repositories.UserRepository;
import core.repositories.timemanager.BasicTaskScheduleRepository;
import core.repositories.timemanager.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
@Service
public class TaskScheduleService {
private final BasicTaskScheduleRepository basicTaskScheduleRepository;
private final TaskRepository taskRepository;
private final UserRepository userRepository;
public TaskScheduleService(@Autowired BasicTaskScheduleRepository basicTaskScheduleRepository,
@Autowired TaskRepository taskRepository,
UserRepository userRepository) {
this.basicTaskScheduleRepository = basicTaskScheduleRepository;
this.taskRepository = taskRepository;
this.userRepository = userRepository;
}
public ServiceResult<BasicTaskSchedule> createBasicTaskSchedule(Task task, BasicTaskScheduleFieldInfo basicTaskScheduleFieldInfo) {
BasicTaskSchedule basicTaskSchedule = new BasicTaskSchedule(task, basicTaskScheduleFieldInfo.getScheduleDate());
task.getBasicTaskSchedules().add(basicTaskSchedule);
basicTaskScheduleRepository.save(basicTaskSchedule);
taskRepository.save(task);
return new ServiceResult<>(basicTaskSchedule);
}
public PermissionResult<BasicTaskSchedule> getSchedulePermissions(long scheduleID, String name) {
Optional<BasicTaskSchedule> basicSchedule = basicTaskScheduleRepository.findById(scheduleID);
return basicSchedule.map(value -> new PermissionResult<>(value, value.getTask().getTaskgroup().getUser().getUsername().equals(name))).orElseGet(() ->
new PermissionResult<>(ServiceExitCode.MISSING_ENTITY));
}
public void reschedule(BasicTaskSchedule basicTaskSchedule, BasicTaskScheduleFieldInfo basicTaskScheduleFieldInfo) {
basicTaskSchedule.setScheduleDate(basicTaskScheduleFieldInfo.getScheduleDate());
basicTaskScheduleRepository.save(basicTaskSchedule);
}
public void deleteBasicSchedule(BasicTaskSchedule basicTaskSchedule) {
basicTaskScheduleRepository.deleteBasicTaskScheduleByID(basicTaskSchedule.getScheduleID());
}
public ServiceResult<List<BasicTaskSchedule>> loadTodaysSchedule(String username, boolean onlyActivateable) {
Optional<User> user = userRepository.findByUsername(username);
if(user.isEmpty()) {
return new ServiceResult<>(ServiceExitCode.MISSING_ENTITY);
}
List<BasicTaskSchedule> basicTaskSchedules = basicTaskScheduleRepository.findAllByUserAndDate(user.get(), LocalDate.now());
List<BasicTaskSchedule> activatableSchedules = new LinkedList<>();
if(onlyActivateable) {
for (BasicTaskSchedule basicTaskSchedule : basicTaskSchedules) {
if (basicTaskSchedule.isActivateAble()) {
activatableSchedules.add(basicTaskSchedule);
}
}
}
return new ServiceResult<>(activatableSchedules);
}
public ServiceResult<List<BasicTaskSchedule>> loadSchedules(String username) {
Optional<User> user = userRepository.findByUsername(username);
return user.map(value -> new ServiceResult<>(basicTaskScheduleRepository.findAllByUser(value))).orElseGet(() ->
new ServiceResult<>(ServiceExitCode.MISSING_ENTITY));
}
public ServiceResult<BasicTaskSchedule> scheduleTaskNow(Task task) {
//Check if task has already an active schedule
ServiceResult<BasicTaskSchedule> activeSchedule = getActiveSchedule(task.getTaskgroup().getUser().getUsername());
if(task.hasActiveSchedule() || activeSchedule.getExitCode() != ServiceExitCode.MISSING_ENTITY) {
return new ServiceResult<>(ServiceExitCode.ENTITY_ALREADY_EXIST);
} else {
BasicTaskSchedule basicTaskSchedule = new BasicTaskSchedule(task, LocalDate.now());
basicTaskSchedule.setStartTime(LocalDateTime.now());
task.getBasicTaskSchedules().add(basicTaskSchedule);
basicTaskScheduleRepository.save(basicTaskSchedule);
taskRepository.save(task);
return new ServiceResult<>(basicTaskSchedule);
}
}
public void deleteScheduleByTask(Task task) {
basicTaskScheduleRepository.deleteAllByTask(task);
}
public ServiceResult<BasicTaskSchedule> getActiveSchedule(String username) {
Optional<BasicTaskSchedule> activeTaskScheduleResult = basicTaskScheduleRepository.findActiveTaskSchedule(username);
return activeTaskScheduleResult.map(ServiceResult::new).orElseGet(() -> new ServiceResult<>(ServiceExitCode.MISSING_ENTITY));
}
public ServiceResult<BasicTaskSchedule> activateSchedule(BasicTaskSchedule taskSchedule) {
//Check if taskSchedule can be started
if(taskSchedule.getStartTime() != null) {
return new ServiceResult<>(ServiceExitCode.INVALID_OPERATION);
}
if(getActiveSchedule(taskSchedule.getTask().getTaskgroup().getUser().getUsername()).getExitCode() == ServiceExitCode.MISSING_ENTITY) {
taskSchedule.setStartTime(LocalDateTime.now());
basicTaskScheduleRepository.save(taskSchedule);
return new ServiceResult<>(taskSchedule);
} else {
return new ServiceResult<>(ServiceExitCode.ENTITY_ALREADY_EXIST);
}
}
public ServiceResult<Integer> stopSchedule(BasicTaskSchedule taskSchedule, boolean finish) {
if(taskSchedule.getStartTime() == null || taskSchedule.getFinishedTime() != null) {
return new ServiceResult<>(ServiceExitCode.INVALID_OPERATION);
}
taskSchedule.setFinishedTime(LocalDateTime.now());
long activeTime = Duration.between(taskSchedule.getStartTime(), taskSchedule.getFinishedTime()).toMinutes();
long workTime = taskSchedule.getTask().getWorkTime() + activeTime;
taskSchedule.getTask().setWorkTime((int) workTime);
if(finish) {
taskSchedule.getTask().setFinished(true);
}
basicTaskScheduleRepository.save(taskSchedule);
taskRepository.save(taskSchedule.getTask());
return new ServiceResult<>((int) activeTime);
}
public int getWorkedMinutes(User user) {
List<BasicTaskSchedule> basicTaskSchedules = basicTaskScheduleRepository.findAllByUser(user);
long workedMinutes = 0;
for(BasicTaskSchedule basicTaskSchedule : basicTaskSchedules) {
if(basicTaskSchedule.getFinishedTime() != null && basicTaskSchedule.getFinishedTime().toLocalDate().isEqual(LocalDate.now())) {
workedMinutes += Duration.between(basicTaskSchedule.getStartTime(), basicTaskSchedule.getFinishedTime()).toMinutes();
}
}
return (int) workedMinutes;
}
public boolean isAnyScheduleMissed(User user) {
List<BasicTaskSchedule> unstartedSchedules = basicTaskScheduleRepository.findAllByStartTimeIsNull();
LocalDate dateReference = LocalDate.now();
for(BasicTaskSchedule schedule : unstartedSchedules) {
if(schedule.getScheduleDate().isBefore(dateReference)) {
return true;
}
}
return false;
}
public ServiceResult<Integer> registerForgottenActivity(Task task, ForgottenActivity forgottenActivity) {
switch (forgottenActivity.getMode()) {
case MANUAL -> {
task.increaseActiveTime(forgottenActivity.getMinutesSpent());
BasicTaskSchedule basicTaskSchedule = new BasicTaskSchedule(task, LocalDate.now());
LocalDateTime timeReference = LocalDateTime.now();
basicTaskSchedule.setStartTime(timeReference.minusMinutes(forgottenActivity.getMinutesSpent()));
basicTaskSchedule.setFinishedTime(timeReference);
task.getBasicTaskSchedules().add(basicTaskSchedule);
basicTaskScheduleRepository.save(basicTaskSchedule);
taskRepository.save(task);
return new ServiceResult<>(forgottenActivity.getMinutesSpent());
}
case LAST -> {
List<BasicTaskSchedule> schedules = basicTaskScheduleRepository.findAllFinishedByUserAndDate(task.getTaskgroup().getUser(), LocalDate.now());
if(schedules.isEmpty()) {
return new ServiceResult<>(ServiceExitCode.INVALID_OPERATION);
} else {
LocalDateTime timeReference = LocalDateTime.now();
BasicTaskSchedule nearestSchedule = null;
long nearestDuration = Long.MAX_VALUE;
for(BasicTaskSchedule schedule : schedules) {
if(nearestSchedule == null) {
nearestSchedule = schedule;
nearestDuration = Duration.between(nearestSchedule.getFinishedTime(), timeReference).getSeconds();
} else {
long currentDuration = Duration.between(schedule.getFinishedTime(), timeReference).getSeconds();
if(currentDuration < nearestDuration) {
nearestSchedule = schedule;
nearestDuration = currentDuration;
}
}
}
int minutesSpent = (int) Duration.between(nearestSchedule.getFinishedTime(), timeReference).toMinutes();
task.increaseActiveTime(minutesSpent);
BasicTaskSchedule basicTaskSchedule = new BasicTaskSchedule(task, timeReference.toLocalDate());
basicTaskSchedule.setStartTime(nearestSchedule.getFinishedTime());
basicTaskSchedule.setFinishedTime(timeReference);
basicTaskScheduleRepository.save(basicTaskSchedule);
taskRepository.save(task);
return new ServiceResult<>(minutesSpent);
}
}
case PLANNED -> {
//Does not make sense until advanced schedule/moderate schedule is implemented
return new ServiceResult<>(0);
}
default -> {
throw new RuntimeException("INVALID MODE");
}
}
}
public List<BasicTaskSchedule> loadMissedSchedules(User user) {
List<BasicTaskSchedule> unstartedSchedules = basicTaskScheduleRepository.findAllUnstartedSchedulesOfUser(user);
LocalDate currentDate = LocalDate.now();
List<BasicTaskSchedule> missedSchedules = new LinkedList<>();
for(BasicTaskSchedule schedule : unstartedSchedules) {
if(schedule.getScheduleDate().isBefore(currentDate)) {
missedSchedules.add(schedule);
}
}
return missedSchedules;
}
}