#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);
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) {
Set<Tag> tags = taskService.checkRepeatingTags(false);
Set<Tag> tags = taskService.checkRepeatingTags(true);
Set<Tag> newTags = null;
if (!schedulerList.isEmpty()) {
newTags = checkNewTags(tags, schedulerList);

View File

@ -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<Tag> tags = checkRepeatingTags(true);
Set<Tag> tags = checkRepeatingTags(false);
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) &&
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<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<>();
List<Tag> 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<Task> tasks = taskRepository.findAllYear(cal.getTime());
List<Task> 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()