From b4c11a7f90cd8dd6733959a8e15dd7ea478f389c Mon Sep 17 00:00:00 2001 From: ASH Date: Sun, 28 Apr 2019 23:58:35 +0400 Subject: [PATCH 1/6] #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()); } From 7ce8b20841e8a16740aa1ec319737ee1283637a6 Mon Sep 17 00:00:00 2001 From: ASH Date: Tue, 30 Apr 2019 22:05:57 +0400 Subject: [PATCH 2/6] #79 fixing bug --- src/main/java/ru/ulstu/students/service/TaskService.java | 9 ++++++--- src/main/resources/public/js/tasks.js | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index d35720d..0fab0be 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -130,12 +130,15 @@ public class TaskService { return newTask; } + @Transactional 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))); + List tasks = taskRepository.findToGenerate(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR) - 1); + tasks.forEach(task -> { + Task newTask = copyYearTask(task); + taskRepository.save(newTask); + }); } public List getTaskStatuses() { diff --git a/src/main/resources/public/js/tasks.js b/src/main/resources/public/js/tasks.js index f756199..0eb2da6 100644 --- a/src/main/resources/public/js/tasks.js +++ b/src/main/resources/public/js/tasks.js @@ -21,6 +21,14 @@ $(document).ready(function () { $("#input-tag").keyup(function (event) { if(event.keyCode == 13 || event.keyCode == 188) { var tagNumber = $("#tags .tag").length; + if(length != 0) { + tagNumber = $("#tags .tag").last() + .children('input') + .attr("name") + .split(']')[0] + .split('[')[1]; + tagNumber++; + } var tagName = $.trim($(this).val()); var addTag = true; // проверка, добавлен ли этот тег From 65dea9bce2c211126114386768ed6940fbbaee9f Mon Sep 17 00:00:00 2001 From: ASH Date: Mon, 6 May 2019 22:39:08 +0400 Subject: [PATCH 3/6] #79 adding task planer generation --- .../ru/ulstu/students/model/Scheduler.java | 49 +++++++++ .../repository/SchedulerRepository.java | 8 ++ .../students/repository/TaskRepository.java | 2 + .../students/service/SchedulerService.java | 99 +++++++++++++++++++ .../service/TaskGenerationService.java | 19 +++- .../ulstu/students/service/TaskService.java | 81 +++++++++++++-- src/main/java/ru/ulstu/tags/model/Tag.java | 14 +++ .../db/changelog-20190505_000000-schema.xml | 24 +++++ src/main/resources/db/changelog-master.xml | 1 + src/main/resources/public/js/tasks.js | 2 +- 10 files changed, 285 insertions(+), 14 deletions(-) create mode 100644 src/main/java/ru/ulstu/students/model/Scheduler.java create mode 100644 src/main/java/ru/ulstu/students/repository/SchedulerRepository.java create mode 100644 src/main/java/ru/ulstu/students/service/SchedulerService.java create mode 100644 src/main/resources/db/changelog-20190505_000000-schema.xml diff --git a/src/main/java/ru/ulstu/students/model/Scheduler.java b/src/main/java/ru/ulstu/students/model/Scheduler.java new file mode 100644 index 0000000..67005b1 --- /dev/null +++ b/src/main/java/ru/ulstu/students/model/Scheduler.java @@ -0,0 +1,49 @@ +package ru.ulstu.students.model; + +import org.springframework.format.annotation.DateTimeFormat; +import ru.ulstu.core.model.BaseEntity; + +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import java.util.Date; + +@Entity +@Table(name = "scheduler") +public class Scheduler extends BaseEntity { + + @OneToOne(optional = false) + @JoinColumn(name = "task_id") + private Task task; + + @Temporal(value = TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date date; + + public Scheduler() { + } + + public Scheduler(Task task, Date date) { + this.task = task; + this.date = date; + } + + public Task getTask() { + return task; + } + + public void setTask(Task task) { + this.task = task; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } +} diff --git a/src/main/java/ru/ulstu/students/repository/SchedulerRepository.java b/src/main/java/ru/ulstu/students/repository/SchedulerRepository.java new file mode 100644 index 0000000..ff25e98 --- /dev/null +++ b/src/main/java/ru/ulstu/students/repository/SchedulerRepository.java @@ -0,0 +1,8 @@ +package ru.ulstu.students.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.students.model.Scheduler; + +public interface SchedulerRepository extends JpaRepository { + +} diff --git a/src/main/java/ru/ulstu/students/repository/TaskRepository.java b/src/main/java/ru/ulstu/students/repository/TaskRepository.java index 0b17691..adcc43a 100644 --- a/src/main/java/ru/ulstu/students/repository/TaskRepository.java +++ b/src/main/java/ru/ulstu/students/repository/TaskRepository.java @@ -20,4 +20,6 @@ public interface TaskRepository extends JpaRepository { "(YEAR FROM t.createDate) = :year)") List findToGenerate(@Param("day") Integer day, @Param("month") Integer month, @Param("year") Integer year); + @Query("SELECT t FROM Task t WHERE(:tag IS NULL OR :tag MEMBER OF t.tags) ORDER BY create_date DESC") + List findByTag(@Param("tag") Tag tag); } diff --git a/src/main/java/ru/ulstu/students/service/SchedulerService.java b/src/main/java/ru/ulstu/students/service/SchedulerService.java new file mode 100644 index 0000000..09fbce7 --- /dev/null +++ b/src/main/java/ru/ulstu/students/service/SchedulerService.java @@ -0,0 +1,99 @@ +package ru.ulstu.students.service; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.students.model.Scheduler; +import ru.ulstu.students.model.Task; +import ru.ulstu.students.repository.SchedulerRepository; +import ru.ulstu.tags.model.Tag; + +import java.util.Date; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +@Service +public class SchedulerService { + + private final TaskService taskService; + private final SchedulerRepository schedulerRepository; + + public SchedulerService(TaskService taskService, SchedulerRepository schedulerRepository) { + this.taskService = taskService; + this.schedulerRepository = schedulerRepository; + } + + private void save(Tag tag) { + List taskList = taskService.findTasksByTag(tag); + create(taskList.get(0)); + } + + + @Transactional + private Scheduler create(Task task) { + Scheduler scheduler = new Scheduler(task, task.getDeadlines().get(task.getDeadlines().size() - 1).getDate()); + return schedulerRepository.save(scheduler); + } + + @Transactional + private void delete(Integer schedulerId) { + if (schedulerRepository.exists(schedulerId)) { + schedulerRepository.delete(schedulerId); + } + } + + public void checkPlanToday() { + List schedulerList = schedulerRepository.findAll(); + if (!schedulerList.isEmpty()) { + doTodayPlanIfNeed(schedulerList); + schedulerList = schedulerRepository.findAll(); + } + checkNewPlan(schedulerList); + } + + private void checkNewPlan(List schedulerList) { + Set tags = taskService.checkRepeatingTags(); + Tag newTag = null; + if (!schedulerList.isEmpty()) { + newTag = checkNewTag(tags, schedulerList); + } else { + if (!tags.isEmpty()) { + newTag = tags.iterator().next(); + } + } + if (newTag != null) { + save(newTag); + } + } + + private Tag checkNewTag(Set tags, List schedulerList) { + Set newTags = tags + .stream() + .filter(tag -> schedulerList + .stream() + .anyMatch(scheduler -> + !scheduler.getTask().getTags().contains(tag))) + .collect(Collectors.toSet()); + if (!newTags.isEmpty()) { + return newTags.iterator().next(); + } + return null; + } + + private void doTodayPlanIfNeed(List schedulerList) { + List plan = schedulerList + .stream() + .filter(scheduler -> scheduler.getDate().before(new Date())) + .collect(Collectors.toList()); + doToday(plan); + } + + private void doToday(List plan) { + plan.forEach(scheduler -> { + taskService.createPeriodTask(scheduler); + delete(scheduler.getId()); + }); + } + + +} diff --git a/src/main/java/ru/ulstu/students/service/TaskGenerationService.java b/src/main/java/ru/ulstu/students/service/TaskGenerationService.java index d347140..57060d5 100644 --- a/src/main/java/ru/ulstu/students/service/TaskGenerationService.java +++ b/src/main/java/ru/ulstu/students/service/TaskGenerationService.java @@ -10,16 +10,25 @@ public class TaskGenerationService { private final Logger log = LoggerFactory.getLogger(TaskGenerationService.class); private final TaskService taskService; + private final SchedulerService schedulerService; - public TaskGenerationService(TaskService taskService) { + public TaskGenerationService(TaskService taskService, SchedulerService schedulerService) { this.taskService = taskService; + this.schedulerService = schedulerService; } +// @Scheduled(cron = "0 * * ? * *", zone = "Europe/Samara") +// public void generateYearTasks() { +// log.debug("TaskService.generateYearTasks started"); +// taskService.generateYearTasks(); +// log.debug("TaskService.generateYearTasks finished"); +// } + @Scheduled(cron = "0 * * ? * *", zone = "Europe/Samara") - public void generateYearTasks() { - log.debug("TaskService.generateYearTasks started"); - taskService.generateYearTasks(); - log.debug("TaskService.generateYearTasks finished"); + public void checkPlanToday() { + log.debug("SchedulerService.checkPlanToday started"); + schedulerService.checkPlanToday(); + log.debug("SchedulerService.checkPlanToday finished"); } } diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index 0fab0be..c7f750c 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -4,8 +4,10 @@ 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.core.util.DateUtils; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; +import ru.ulstu.students.model.Scheduler; import ru.ulstu.students.model.Task; import ru.ulstu.students.model.TaskDto; import ru.ulstu.students.model.TaskFilterDto; @@ -18,6 +20,10 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TimeZone; +import java.util.TreeMap; import java.util.stream.Collectors; import static org.springframework.util.ObjectUtils.isEmpty; @@ -108,14 +114,46 @@ public class TaskService { } } - public Task copyYearTask(Task task) { - - Task newTask = new Task(); + public void copyMainPart(Task newTask, Task 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() + } + + public Task copyTaskWithNewDates(Task task) { + Task newTask = new Task(); + copyMainPart(newTask, task); + Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); + cal1.setTime(newTask.getCreateDate()); + Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); + cal2.setTime(task.getCreateDate()); + Integer interval = cal1.get(Calendar.DAY_OF_YEAR) - cal2.get(Calendar.DAY_OF_YEAR); + newTask.setDeadlines(newDatesDeadlines(task.getDeadlines(), interval)); + return newTask; + } + + private List newDatesDeadlines(List deadlines, Integer interval) { + return deadlines + .stream() + .map(deadline -> { + Deadline newDeadline = new Deadline(); + Date newDate = DateUtils.addDays(deadline.getDate(), interval); + newDeadline.setDescription(deadline.getDescription()); + newDeadline.setDate(newDate); + return deadlineService.create(newDeadline); + }).collect(Collectors.toList()); + } + + public Task copyTaskWithNewYear(Task task) { + Task newTask = new Task(); + copyMainPart(newTask, task); + newTask.setDeadlines(newYearDeadlines(task.getDeadlines())); + return newTask; + } + + private List newYearDeadlines(List deadlines) { + return deadlines .stream() .map(deadline -> { Deadline newDeadline = new Deadline(); @@ -125,22 +163,40 @@ public class TaskService { newDeadline.setDescription(deadline.getDescription()); newDeadline.setDate(cal.getTime()); return deadlineService.create(newDeadline); - }).collect(Collectors.toList())); - - return newTask; + }).collect(Collectors.toList()); } + @Transactional 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) - 1); tasks.forEach(task -> { - Task newTask = copyYearTask(task); + Task newTask = copyTaskWithNewYear(task); taskRepository.save(newTask); }); } + @Transactional + public Set checkRepeatingTags() { + Map tagsCount = new TreeMap<>(); + List tags = tagService.getTags(); + List tasks = taskRepository.findAll(); + tags.forEach(tag -> + tagsCount.put(tag, tasks + .stream() + .filter(task -> task.getTags().contains(tag)) + .count())); + + return tagsCount + .entrySet() + .stream() + .filter(tagLongEntry -> tagLongEntry.getValue() >= 2) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + .keySet(); + } + public List getTaskStatuses() { return Arrays.asList(Task.TaskStatus.values()); } @@ -149,4 +205,13 @@ public class TaskService { return tagService.getTags(); } + public List findTasksByTag(Tag tag) { + return taskRepository.findByTag(tag); + } + + @Transactional + public void createPeriodTask(Scheduler scheduler) { + Task newTask = copyTaskWithNewDates(scheduler.getTask()); + taskRepository.save(newTask); + } } diff --git a/src/main/java/ru/ulstu/tags/model/Tag.java b/src/main/java/ru/ulstu/tags/model/Tag.java index aed000f..933b3ef 100644 --- a/src/main/java/ru/ulstu/tags/model/Tag.java +++ b/src/main/java/ru/ulstu/tags/model/Tag.java @@ -9,6 +9,7 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.Size; +import java.util.Objects; @Entity @Table(name = "tag") @@ -41,4 +42,17 @@ public class Tag extends BaseEntity { public void setTagName(String tagName) { this.tagName = tagName; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Tag tag = (Tag) o; + return tagName.equals(tag.tagName); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), tagName); + } } diff --git a/src/main/resources/db/changelog-20190505_000000-schema.xml b/src/main/resources/db/changelog-20190505_000000-schema.xml new file mode 100644 index 0000000..a47476e --- /dev/null +++ b/src/main/resources/db/changelog-20190505_000000-schema.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 68b294a..a28952a 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -34,4 +34,5 @@ + \ No newline at end of file diff --git a/src/main/resources/public/js/tasks.js b/src/main/resources/public/js/tasks.js index 0eb2da6..be24d5b 100644 --- a/src/main/resources/public/js/tasks.js +++ b/src/main/resources/public/js/tasks.js @@ -21,7 +21,7 @@ $(document).ready(function () { $("#input-tag").keyup(function (event) { if(event.keyCode == 13 || event.keyCode == 188) { var tagNumber = $("#tags .tag").length; - if(length != 0) { + if(tagNumber > 0) { tagNumber = $("#tags .tag").last() .children('input') .attr("name") From 0de34c89fea802355890f8d0fcecaaf0719c5d51 Mon Sep 17 00:00:00 2001 From: ASH Date: Sat, 11 May 2019 22:58:57 +0400 Subject: [PATCH 4/6] #79 fixing planner and year generation --- .../repository/SchedulerRepository.java | 3 + .../students/repository/TaskRepository.java | 10 ++- .../students/service/SchedulerService.java | 33 ++++++-- .../service/TaskGenerationService.java | 16 ++-- .../ulstu/students/service/TaskService.java | 76 ++++++++++++++----- src/main/java/ru/ulstu/tags/model/Tag.java | 8 +- src/main/resources/public/css/tasks.css | 23 ++++++ src/main/resources/public/js/tasks.js | 2 +- .../resources/templates/students/task.html | 2 +- 9 files changed, 131 insertions(+), 42 deletions(-) diff --git a/src/main/java/ru/ulstu/students/repository/SchedulerRepository.java b/src/main/java/ru/ulstu/students/repository/SchedulerRepository.java index ff25e98..7481692 100644 --- a/src/main/java/ru/ulstu/students/repository/SchedulerRepository.java +++ b/src/main/java/ru/ulstu/students/repository/SchedulerRepository.java @@ -2,7 +2,10 @@ package ru.ulstu.students.repository; import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.students.model.Scheduler; +import ru.ulstu.students.model.Task; public interface SchedulerRepository extends JpaRepository { + Scheduler findOneByTask(Task task); + } diff --git a/src/main/java/ru/ulstu/students/repository/TaskRepository.java b/src/main/java/ru/ulstu/students/repository/TaskRepository.java index adcc43a..af277d1 100644 --- a/src/main/java/ru/ulstu/students/repository/TaskRepository.java +++ b/src/main/java/ru/ulstu/students/repository/TaskRepository.java @@ -6,6 +6,7 @@ import org.springframework.data.repository.query.Param; import ru.ulstu.students.model.Task; import ru.ulstu.tags.model.Tag; +import java.util.Date; import java.util.List; public interface TaskRepository extends JpaRepository { @@ -16,10 +17,11 @@ 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); - @Query("SELECT t FROM Task t WHERE(:tag IS NULL OR :tag MEMBER OF t.tags) ORDER BY create_date DESC") List findByTag(@Param("tag") Tag tag); + + @Query("SELECT t FROM Task t WHERE (t.createDate >= :date) ORDER BY create_date DESC") + List findAllYear(@Param("date") Date date); + + } diff --git a/src/main/java/ru/ulstu/students/service/SchedulerService.java b/src/main/java/ru/ulstu/students/service/SchedulerService.java index 09fbce7..989255e 100644 --- a/src/main/java/ru/ulstu/students/service/SchedulerService.java +++ b/src/main/java/ru/ulstu/students/service/SchedulerService.java @@ -23,6 +23,7 @@ public class SchedulerService { this.schedulerRepository = schedulerRepository; } + private void save(Tag tag) { List taskList = taskService.findTasksByTag(tag); create(taskList.get(0)); @@ -52,21 +53,37 @@ public class SchedulerService { } private void checkNewPlan(List schedulerList) { - Set tags = taskService.checkRepeatingTags(); - Tag newTag = null; + Set tags = taskService.checkRepeatingTags(false); + Set newTags = null; if (!schedulerList.isEmpty()) { - newTag = checkNewTag(tags, schedulerList); + newTags = checkNewTags(tags, schedulerList); } else { if (!tags.isEmpty()) { - newTag = tags.iterator().next(); + newTags = tags; } } - if (newTag != null) { - save(newTag); + + if (newTags != null) { + newTags.forEach(tag -> { + if (!hasNewTag(tag, schedulerList)) { + save(tag); + Task task = taskService.findTasksByTag(tag).get(0); + schedulerList.add(new Scheduler(task, task.getDeadlines().get(task.getDeadlines().size() - 1).getDate())); + } + }); } } - private Tag checkNewTag(Set tags, List schedulerList) { + private boolean hasNewTag(Tag tag, List schedulerList) { + + return schedulerList + .stream() + .anyMatch(scheduler -> scheduler.getTask().getTags().contains(tag)); + + } + + + private Set checkNewTags(Set tags, List schedulerList) { Set newTags = tags .stream() .filter(tag -> schedulerList @@ -75,7 +92,7 @@ public class SchedulerService { !scheduler.getTask().getTags().contains(tag))) .collect(Collectors.toSet()); if (!newTags.isEmpty()) { - return newTags.iterator().next(); + return newTags; } return null; } diff --git a/src/main/java/ru/ulstu/students/service/TaskGenerationService.java b/src/main/java/ru/ulstu/students/service/TaskGenerationService.java index 57060d5..1c04c20 100644 --- a/src/main/java/ru/ulstu/students/service/TaskGenerationService.java +++ b/src/main/java/ru/ulstu/students/service/TaskGenerationService.java @@ -18,17 +18,15 @@ public class TaskGenerationService { this.schedulerService = schedulerService; } -// @Scheduled(cron = "0 * * ? * *", zone = "Europe/Samara") -// public void generateYearTasks() { -// log.debug("TaskService.generateYearTasks started"); -// taskService.generateYearTasks(); -// log.debug("TaskService.generateYearTasks finished"); -// } - - @Scheduled(cron = "0 * * ? * *", zone = "Europe/Samara") - public void checkPlanToday() { + @Scheduled(cron = "0 0 0 * * ?", zone = "Europe/Samara") + public void generateTasks() { log.debug("SchedulerService.checkPlanToday started"); schedulerService.checkPlanToday(); log.debug("SchedulerService.checkPlanToday finished"); + + 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 c7f750c..f57c084 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -11,11 +11,13 @@ import ru.ulstu.students.model.Scheduler; import ru.ulstu.students.model.Task; import ru.ulstu.students.model.TaskDto; import ru.ulstu.students.model.TaskFilterDto; +import ru.ulstu.students.repository.SchedulerRepository; import ru.ulstu.students.repository.TaskRepository; import ru.ulstu.tags.model.Tag; import ru.ulstu.tags.service.TagService; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; @@ -36,14 +38,17 @@ public class TaskService { private final static int MAX_DISPLAY_SIZE = 40; private final TaskRepository taskRepository; + private final SchedulerRepository schedulerRepository; private final DeadlineService deadlineService; private final TagService tagService; + public TaskService(TaskRepository taskRepository, - DeadlineService deadlineService, TagService tagService) { + DeadlineService deadlineService, TagService tagService, SchedulerRepository schedulerRepository) { this.taskRepository = taskRepository; this.deadlineService = deadlineService; this.tagService = tagService; + this.schedulerRepository = schedulerRepository; } public List findAll() { @@ -101,6 +106,11 @@ public class TaskService { @Transactional public void delete(Integer taskId) throws IOException { if (taskRepository.exists(taskId)) { + Task scheduleTask = taskRepository.findOne(taskId); + Scheduler sch = schedulerRepository.findOneByTask(scheduleTask); + if (sch != null) { + schedulerRepository.delete(sch.getId()); + } taskRepository.delete(taskId); } @@ -124,9 +134,9 @@ public class TaskService { public Task copyTaskWithNewDates(Task task) { Task newTask = new Task(); copyMainPart(newTask, task); - Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); + Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Samara")); cal1.setTime(newTask.getCreateDate()); - Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); + Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Samara")); cal2.setTime(task.getCreateDate()); Integer interval = cal1.get(Calendar.DAY_OF_YEAR) - cal2.get(Calendar.DAY_OF_YEAR); newTask.setDeadlines(newDatesDeadlines(task.getDeadlines(), interval)); @@ -169,32 +179,64 @@ public class TaskService { @Transactional 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) - 1); - tasks.forEach(task -> { - Task newTask = copyTaskWithNewYear(task); - taskRepository.save(newTask); + Set tags = checkRepeatingTags(true); + List tasks = new ArrayList<>(); + tags.forEach(tag -> + { + Task singleTask = findTasksByTag(tag).get(0); + Calendar taskDate = Calendar.getInstance(); + Calendar nowDate = Calendar.getInstance(); + taskDate.setTime(singleTask.getCreateDate()); + nowDate.setTime(new Date()); + if (taskDate.get(Calendar.DAY_OF_MONTH) == nowDate.get(Calendar.DAY_OF_MONTH) && + taskDate.get(Calendar.MONTH) + 1 == nowDate.get(Calendar.MONTH) + 1 && + taskDate.get(Calendar.YEAR) + 1 == nowDate.get(Calendar.YEAR)) { + if (!tasks.contains(singleTask)) { + tasks.add(singleTask); + } + } }); + if (tasks != null) { + tasks.forEach(task -> { + Task newTask = copyTaskWithNewYear(task); + taskRepository.save(newTask); + }); + } } + @Transactional - public Set checkRepeatingTags() { + public Set checkRepeatingTags(Boolean param) { //param: true = year task; false = period task Map tagsCount = new TreeMap<>(); List tags = tagService.getTags(); - List tasks = taskRepository.findAll(); + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.YEAR, -1); + cal.set(Calendar.HOUR_OF_DAY, 0); + System.out.println(cal.getTime()); + List tasks = taskRepository.findAllYear(cal.getTime()); + ; tags.forEach(tag -> tagsCount.put(tag, tasks .stream() .filter(task -> task.getTags().contains(tag)) .count())); + if (param) { + return tagsCount + .entrySet() + .stream() + .filter(tagLongEntry -> tagLongEntry.getValue() == 1) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + .keySet(); + } else { + return tagsCount + .entrySet() + .stream() + .filter(tagLongEntry -> tagLongEntry.getValue() >= 2) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + .keySet(); + } - return tagsCount - .entrySet() - .stream() - .filter(tagLongEntry -> tagLongEntry.getValue() >= 2) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) - .keySet(); } public List getTaskStatuses() { diff --git a/src/main/java/ru/ulstu/tags/model/Tag.java b/src/main/java/ru/ulstu/tags/model/Tag.java index 933b3ef..e1c63d2 100644 --- a/src/main/java/ru/ulstu/tags/model/Tag.java +++ b/src/main/java/ru/ulstu/tags/model/Tag.java @@ -45,8 +45,12 @@ public class Tag extends BaseEntity { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } Tag tag = (Tag) o; return tagName.equals(tag.tagName); } diff --git a/src/main/resources/public/css/tasks.css b/src/main/resources/public/css/tasks.css index 7e350ed..eadf073 100644 --- a/src/main/resources/public/css/tasks.css +++ b/src/main/resources/public/css/tasks.css @@ -38,6 +38,29 @@ width: auto; max-width: inherit; } +.tag-info{ + font-size: 10px; + color: white; + padding: 5px 15px; + background-color: black; + display: none; + margin-left: 5px; + border-radius: 5px; + opacity: 0.8; + font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; +} + +.fa-question-circle{ + + font-size: 15px; + color: #212529; + cursor:pointer; +} + +.fa-question-circle:hover .tag-info{ + display:inline-block; + +} .tag { display: inline-block; diff --git a/src/main/resources/public/js/tasks.js b/src/main/resources/public/js/tasks.js index be24d5b..74b5a88 100644 --- a/src/main/resources/public/js/tasks.js +++ b/src/main/resources/public/js/tasks.js @@ -19,7 +19,7 @@ $(document).ready(function () { $("#input-tag").keyup(function (event) { - if(event.keyCode == 13 || event.keyCode == 188) { + if(event.keyCode == 13) { var tagNumber = $("#tags .tag").length; if(tagNumber > 0) { tagNumber = $("#tags .tag").last() diff --git a/src/main/resources/templates/students/task.html b/src/main/resources/templates/students/task.html index 968c7a0..3642221 100644 --- a/src/main/resources/templates/students/task.html +++ b/src/main/resources/templates/students/task.html @@ -52,7 +52,7 @@
- +
From c59bb45c70bdab32f1ce88a63006e0cc81c77ece Mon Sep 17 00:00:00 2001 From: ASH Date: Sat, 11 May 2019 23:06:30 +0400 Subject: [PATCH 5/6] #79 fixing codestyle --- src/main/java/ru/ulstu/students/service/TaskService.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index f57c084..487818d 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -181,8 +181,7 @@ public class TaskService { public void generateYearTasks() { Set tags = checkRepeatingTags(true); List tasks = new ArrayList<>(); - tags.forEach(tag -> - { + tags.forEach(tag -> { Task singleTask = findTasksByTag(tag).get(0); Calendar taskDate = Calendar.getInstance(); Calendar nowDate = Calendar.getInstance(); @@ -215,7 +214,6 @@ public class TaskService { cal.set(Calendar.HOUR_OF_DAY, 0); System.out.println(cal.getTime()); List tasks = taskRepository.findAllYear(cal.getTime()); - ; tags.forEach(tag -> tagsCount.put(tag, tasks .stream() From bc9cca62d68751716b77a75a78c1b6ed6e9de6ad Mon Sep 17 00:00:00 2001 From: ASH Date: Wed, 15 May 2019 23:47:05 +0400 Subject: [PATCH 6/6] #79 refactoring --- .../java/ru/ulstu/core/util/DateUtils.java | 6 +++ .../students/service/SchedulerService.java | 2 +- .../ulstu/students/service/TaskService.java | 44 ++++++++----------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/java/ru/ulstu/core/util/DateUtils.java b/src/main/java/ru/ulstu/core/util/DateUtils.java index 6122583..3a38452 100644 --- a/src/main/java/ru/ulstu/core/util/DateUtils.java +++ b/src/main/java/ru/ulstu/core/util/DateUtils.java @@ -54,4 +54,10 @@ public class DateUtils { cal.add(Calendar.DAY_OF_MONTH, count); return cal.getTime(); } + + public static Date addYears(Date date, int count) { + Calendar cal = getCalendar(date); + cal.add(Calendar.YEAR, count); + return cal.getTime(); + } } diff --git a/src/main/java/ru/ulstu/students/service/SchedulerService.java b/src/main/java/ru/ulstu/students/service/SchedulerService.java index 989255e..9d14926 100644 --- a/src/main/java/ru/ulstu/students/service/SchedulerService.java +++ b/src/main/java/ru/ulstu/students/service/SchedulerService.java @@ -53,7 +53,7 @@ public class SchedulerService { } private void checkNewPlan(List schedulerList) { - Set tags = taskService.checkRepeatingTags(false); + Set tags = taskService.checkRepeatingTags(true); Set newTags = null; if (!schedulerList.isEmpty()) { newTags = checkNewTags(tags, schedulerList); diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index 487818d..e79af68 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -24,7 +24,6 @@ import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.TimeZone; import java.util.TreeMap; import java.util.stream.Collectors; @@ -134,10 +133,8 @@ public class TaskService { public Task copyTaskWithNewDates(Task task) { Task newTask = new Task(); copyMainPart(newTask, task); - Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Samara")); - cal1.setTime(newTask.getCreateDate()); - Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Samara")); - cal2.setTime(task.getCreateDate()); + Calendar cal1 = DateUtils.getCalendar(newTask.getCreateDate()); + Calendar cal2 = DateUtils.getCalendar(task.getCreateDate()); Integer interval = cal1.get(Calendar.DAY_OF_YEAR) - cal2.get(Calendar.DAY_OF_YEAR); newTask.setDeadlines(newDatesDeadlines(task.getDeadlines(), interval)); return newTask; @@ -167,29 +164,31 @@ public class TaskService { .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()); + newDeadline.setDate(DateUtils.addYears(deadline.getDate(), 1)); return deadlineService.create(newDeadline); }).collect(Collectors.toList()); } + private boolean equalsDate(Task task) { + Calendar taskDate = DateUtils.getCalendar(task.getCreateDate()); + Calendar nowDate = DateUtils.getCalendar(new Date()); + if (taskDate.get(Calendar.DAY_OF_MONTH) == nowDate.get(Calendar.DAY_OF_MONTH) && + taskDate.get(Calendar.MONTH) + 1 == nowDate.get(Calendar.MONTH) + 1 && + taskDate.get(Calendar.YEAR) + 1 == nowDate.get(Calendar.YEAR)) { + return true; + } else { + return false; + } + } @Transactional public void generateYearTasks() { - Set tags = checkRepeatingTags(true); + Set tags = checkRepeatingTags(false); List tasks = new ArrayList<>(); tags.forEach(tag -> { Task singleTask = findTasksByTag(tag).get(0); - Calendar taskDate = Calendar.getInstance(); - Calendar nowDate = Calendar.getInstance(); - taskDate.setTime(singleTask.getCreateDate()); - nowDate.setTime(new Date()); - if (taskDate.get(Calendar.DAY_OF_MONTH) == nowDate.get(Calendar.DAY_OF_MONTH) && - taskDate.get(Calendar.MONTH) + 1 == nowDate.get(Calendar.MONTH) + 1 && - taskDate.get(Calendar.YEAR) + 1 == nowDate.get(Calendar.YEAR)) { + if (equalsDate(singleTask)) { if (!tasks.contains(singleTask)) { tasks.add(singleTask); } @@ -205,21 +204,16 @@ public class TaskService { @Transactional - public Set checkRepeatingTags(Boolean param) { //param: true = year task; false = period task + public Set checkRepeatingTags(Boolean createPeriodTask) { //param: false = year task; true = period task Map tagsCount = new TreeMap<>(); List tags = tagService.getTags(); - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date()); - cal.add(Calendar.YEAR, -1); - cal.set(Calendar.HOUR_OF_DAY, 0); - System.out.println(cal.getTime()); - List tasks = taskRepository.findAllYear(cal.getTime()); + List tasks = taskRepository.findAllYear(DateUtils.clearTime(DateUtils.addYears(new Date(), -1))); tags.forEach(tag -> tagsCount.put(tag, tasks .stream() .filter(task -> task.getTags().contains(tag)) .count())); - if (param) { + if (!createPeriodTask) { return tagsCount .entrySet() .stream()