#79 year generation

This commit is contained in:
ASH 2019-04-28 23:58:35 +04:00
parent a352f561fa
commit b4c11a7f90
3 changed files with 63 additions and 0 deletions

View File

@ -15,4 +15,9 @@ public interface TaskRepository extends JpaRepository<Task, Integer> {
@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<Task> 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<Task> findToGenerate(@Param("day") Integer day, @Param("month") Integer month, @Param("year") Integer year);
}

View File

@ -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");
}
}

View File

@ -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<Task> 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<Task.TaskStatus> getTaskStatuses() {
return Arrays.asList(Task.TaskStatus.values());
}