From b4c11a7f90cd8dd6733959a8e15dd7ea478f389c Mon Sep 17 00:00:00 2001 From: ASH Date: Sun, 28 Apr 2019 23:58:35 +0400 Subject: [PATCH] #79 year generation --- .../students/repository/TaskRepository.java | 5 +++ .../service/TaskGenerationService.java | 25 ++++++++++++++ .../ulstu/students/service/TaskService.java | 33 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/main/java/ru/ulstu/students/service/TaskGenerationService.java diff --git a/src/main/java/ru/ulstu/students/repository/TaskRepository.java b/src/main/java/ru/ulstu/students/repository/TaskRepository.java index ee49ab8..0b17691 100644 --- a/src/main/java/ru/ulstu/students/repository/TaskRepository.java +++ b/src/main/java/ru/ulstu/students/repository/TaskRepository.java @@ -15,4 +15,9 @@ public interface TaskRepository extends JpaRepository { @Query("SELECT t FROM Task t WHERE (t.status = :status OR :status IS NULL) AND (:tag IS NULL OR :tag MEMBER OF t.tags) ORDER BY create_date ASC") List filterOld(@Param("status") Task.TaskStatus status, @Param("tag") Tag tag); + + @Query("SELECT t FROM Task t WHERE (EXTRACT(DAY FROM t.createDate) = :day) AND (EXTRACT(MONTH FROM t.createDate) = :month) AND (EXTRACT" + + "(YEAR FROM t.createDate) = :year)") + List findToGenerate(@Param("day") Integer day, @Param("month") Integer month, @Param("year") Integer year); + } diff --git a/src/main/java/ru/ulstu/students/service/TaskGenerationService.java b/src/main/java/ru/ulstu/students/service/TaskGenerationService.java new file mode 100644 index 0000000..d347140 --- /dev/null +++ b/src/main/java/ru/ulstu/students/service/TaskGenerationService.java @@ -0,0 +1,25 @@ +package ru.ulstu.students.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Service +public class TaskGenerationService { + + private final Logger log = LoggerFactory.getLogger(TaskGenerationService.class); + private final TaskService taskService; + + public TaskGenerationService(TaskService taskService) { + + this.taskService = taskService; + } + + @Scheduled(cron = "0 * * ? * *", zone = "Europe/Samara") + public void generateYearTasks() { + log.debug("TaskService.generateYearTasks started"); + taskService.generateYearTasks(); + log.debug("TaskService.generateYearTasks finished"); + } +} diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index a8747c7..d35720d 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -4,6 +4,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.students.model.Task; import ru.ulstu.students.model.TaskDto; @@ -14,8 +15,10 @@ import ru.ulstu.tags.service.TagService; import java.io.IOException; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -105,6 +108,36 @@ public class TaskService { } } + public Task copyYearTask(Task task) { + + Task newTask = new Task(); + newTask.setTitle(task.getTitle()); + newTask.setTags(tagService.saveOrCreate(task.getTags())); + newTask.setCreateDate(new Date()); + newTask.setStatus(Task.TaskStatus.LOADED_FROM_KIAS); + newTask.setDeadlines(task.getDeadlines() + .stream() + .map(deadline -> { + Deadline newDeadline = new Deadline(); + Calendar cal = Calendar.getInstance(); + cal.setTime(deadline.getDate()); + cal.add(Calendar.YEAR, 1); + newDeadline.setDescription(deadline.getDescription()); + newDeadline.setDate(cal.getTime()); + return deadlineService.create(newDeadline); + }).collect(Collectors.toList())); + + return newTask; + } + + public void generateYearTasks() { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + List tasks = taskRepository.findToGenerate(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR)); + System.out.println(cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.MONTH)); + tasks.forEach(task -> taskRepository.save(copyYearTask(task))); + } + public List getTaskStatuses() { return Arrays.asList(Task.TaskStatus.values()); }