From bc9cca62d68751716b77a75a78c1b6ed6e9de6ad Mon Sep 17 00:00:00 2001 From: ASH Date: Wed, 15 May 2019 23:47:05 +0400 Subject: [PATCH] #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()