#79 refactoring

This commit is contained in:
ASH 2019-05-15 23:47:05 +04:00
parent 7798508d4f
commit bc9cca62d6
3 changed files with 26 additions and 26 deletions

View File

@ -54,4 +54,10 @@ public class DateUtils {
cal.add(Calendar.DAY_OF_MONTH, count); cal.add(Calendar.DAY_OF_MONTH, count);
return cal.getTime(); return cal.getTime();
} }
public static Date addYears(Date date, int count) {
Calendar cal = getCalendar(date);
cal.add(Calendar.YEAR, count);
return cal.getTime();
}
} }

View File

@ -53,7 +53,7 @@ public class SchedulerService {
} }
private void checkNewPlan(List<Scheduler> schedulerList) { private void checkNewPlan(List<Scheduler> schedulerList) {
Set<Tag> tags = taskService.checkRepeatingTags(false); Set<Tag> tags = taskService.checkRepeatingTags(true);
Set<Tag> newTags = null; Set<Tag> newTags = null;
if (!schedulerList.isEmpty()) { if (!schedulerList.isEmpty()) {
newTags = checkNewTags(tags, schedulerList); newTags = checkNewTags(tags, schedulerList);

View File

@ -24,7 +24,6 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -134,10 +133,8 @@ public class TaskService {
public Task copyTaskWithNewDates(Task task) { public Task copyTaskWithNewDates(Task task) {
Task newTask = new Task(); Task newTask = new Task();
copyMainPart(newTask, task); copyMainPart(newTask, task);
Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Samara")); Calendar cal1 = DateUtils.getCalendar(newTask.getCreateDate());
cal1.setTime(newTask.getCreateDate()); Calendar cal2 = DateUtils.getCalendar(task.getCreateDate());
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); Integer interval = cal1.get(Calendar.DAY_OF_YEAR) - cal2.get(Calendar.DAY_OF_YEAR);
newTask.setDeadlines(newDatesDeadlines(task.getDeadlines(), interval)); newTask.setDeadlines(newDatesDeadlines(task.getDeadlines(), interval));
return newTask; return newTask;
@ -167,29 +164,31 @@ public class TaskService {
.stream() .stream()
.map(deadline -> { .map(deadline -> {
Deadline newDeadline = new Deadline(); Deadline newDeadline = new Deadline();
Calendar cal = Calendar.getInstance();
cal.setTime(deadline.getDate());
cal.add(Calendar.YEAR, 1);
newDeadline.setDescription(deadline.getDescription()); newDeadline.setDescription(deadline.getDescription());
newDeadline.setDate(cal.getTime()); newDeadline.setDate(DateUtils.addYears(deadline.getDate(), 1));
return deadlineService.create(newDeadline); return deadlineService.create(newDeadline);
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
private boolean equalsDate(Task task) {
@Transactional Calendar taskDate = DateUtils.getCalendar(task.getCreateDate());
public void generateYearTasks() { Calendar nowDate = DateUtils.getCalendar(new Date());
Set<Tag> tags = checkRepeatingTags(true);
List<Task> 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) && 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.MONTH) + 1 == nowDate.get(Calendar.MONTH) + 1 &&
taskDate.get(Calendar.YEAR) + 1 == nowDate.get(Calendar.YEAR)) { taskDate.get(Calendar.YEAR) + 1 == nowDate.get(Calendar.YEAR)) {
return true;
} else {
return false;
}
}
@Transactional
public void generateYearTasks() {
Set<Tag> tags = checkRepeatingTags(false);
List<Task> tasks = new ArrayList<>();
tags.forEach(tag -> {
Task singleTask = findTasksByTag(tag).get(0);
if (equalsDate(singleTask)) {
if (!tasks.contains(singleTask)) { if (!tasks.contains(singleTask)) {
tasks.add(singleTask); tasks.add(singleTask);
} }
@ -205,21 +204,16 @@ public class TaskService {
@Transactional @Transactional
public Set<Tag> checkRepeatingTags(Boolean param) { //param: true = year task; false = period task public Set<Tag> checkRepeatingTags(Boolean createPeriodTask) { //param: false = year task; true = period task
Map<Tag, Long> tagsCount = new TreeMap<>(); Map<Tag, Long> tagsCount = new TreeMap<>();
List<Tag> tags = tagService.getTags(); List<Tag> tags = tagService.getTags();
Calendar cal = Calendar.getInstance(); List<Task> tasks = taskRepository.findAllYear(DateUtils.clearTime(DateUtils.addYears(new Date(), -1)));
cal.setTime(new Date());
cal.add(Calendar.YEAR, -1);
cal.set(Calendar.HOUR_OF_DAY, 0);
System.out.println(cal.getTime());
List<Task> tasks = taskRepository.findAllYear(cal.getTime());
tags.forEach(tag -> tags.forEach(tag ->
tagsCount.put(tag, tasks tagsCount.put(tag, tasks
.stream() .stream()
.filter(task -> task.getTags().contains(tag)) .filter(task -> task.getTags().contains(tag))
.count())); .count()));
if (param) { if (!createPeriodTask) {
return tagsCount return tagsCount
.entrySet() .entrySet()
.stream() .stream()