|
|
|
@ -1,22 +1,29 @@
|
|
|
|
|
package ru.ulstu.activity.students.service;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import ru.ulstu.activity.common.service.AbstractActivityService;
|
|
|
|
|
import ru.ulstu.activity.deadline.model.Deadline;
|
|
|
|
|
import ru.ulstu.activity.deadline.service.DeadlineService;
|
|
|
|
|
import ru.ulstu.activity.ping.service.PingService;
|
|
|
|
|
import ru.ulstu.activity.students.model.Scheduler;
|
|
|
|
|
import ru.ulstu.activity.students.model.Task;
|
|
|
|
|
import ru.ulstu.activity.students.model.TaskDashboardDto;
|
|
|
|
|
import ru.ulstu.activity.students.model.TaskDto;
|
|
|
|
|
import ru.ulstu.activity.students.model.TaskFilterDto;
|
|
|
|
|
import ru.ulstu.activity.students.model.TaskListDto;
|
|
|
|
|
import ru.ulstu.activity.students.repository.SchedulerRepository;
|
|
|
|
|
import ru.ulstu.activity.students.repository.TaskRepository;
|
|
|
|
|
import ru.ulstu.activity.tags.model.Tag;
|
|
|
|
|
import ru.ulstu.activity.tags.service.TagService;
|
|
|
|
|
import ru.ulstu.activity.timeline.service.EventService;
|
|
|
|
|
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
|
|
|
|
import ru.ulstu.core.model.response.PageableItems;
|
|
|
|
|
import ru.ulstu.core.util.DateUtils;
|
|
|
|
|
|
|
|
|
|
import javax.persistence.EntityNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
@ -29,12 +36,12 @@ import java.util.Set;
|
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import static org.springframework.util.ObjectUtils.isEmpty;
|
|
|
|
|
import static ru.ulstu.activity.students.model.Task.TaskStatus.IN_WORK;
|
|
|
|
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
|
|
|
|
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class TaskService {
|
|
|
|
|
public class TaskService extends AbstractActivityService<TaskListDto, Task, TaskDto> {
|
|
|
|
|
|
|
|
|
|
private final static int MAX_DISPLAY_SIZE = 40;
|
|
|
|
|
|
|
|
|
@ -43,29 +50,35 @@ public class TaskService {
|
|
|
|
|
private final DeadlineService deadlineService;
|
|
|
|
|
private final TagService tagService;
|
|
|
|
|
private final EventService eventService;
|
|
|
|
|
private final PingService pingService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TaskService(TaskRepository taskRepository,
|
|
|
|
|
DeadlineService deadlineService, TagService tagService, SchedulerRepository schedulerRepository, EventService eventService) {
|
|
|
|
|
DeadlineService deadlineService,
|
|
|
|
|
TagService tagService,
|
|
|
|
|
SchedulerRepository schedulerRepository,
|
|
|
|
|
EventService eventService,
|
|
|
|
|
PingService pingService) {
|
|
|
|
|
this.taskRepository = taskRepository;
|
|
|
|
|
this.deadlineService = deadlineService;
|
|
|
|
|
this.tagService = tagService;
|
|
|
|
|
this.eventService = eventService;
|
|
|
|
|
this.schedulerRepository = schedulerRepository;
|
|
|
|
|
this.pingService = pingService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Task> findAll() {
|
|
|
|
|
return taskRepository.findAll(new Sort(Sort.Direction.DESC, "createDate"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<TaskDto> findAllDto() {
|
|
|
|
|
List<TaskDto> tasks = convert(findAll(), TaskDto::new);
|
|
|
|
|
tasks.forEach(taskDto -> taskDto.setTitle(StringUtils.abbreviate(taskDto.getTitle(), MAX_DISPLAY_SIZE)));
|
|
|
|
|
return tasks;
|
|
|
|
|
public PageableItems<Task> findAll(int offset, int count) {
|
|
|
|
|
final Page<Task> page = taskRepository.findAll(new OffsetablePageRequest(offset, count));
|
|
|
|
|
return new PageableItems<>(page.getTotalElements(), page.getContent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TaskDto findOneDto(Integer id) {
|
|
|
|
|
return new TaskDto(taskRepository.getOne(id));
|
|
|
|
|
@Override
|
|
|
|
|
protected TaskListDto getActivityListDto(Task entity) {
|
|
|
|
|
return new TaskListDto(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<TaskDto> filter(TaskFilterDto filterDto) {
|
|
|
|
@ -81,11 +94,21 @@ public class TaskService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public Integer create(TaskDto taskDto) throws IOException {
|
|
|
|
|
Task newTask = copyFromDto(new Task(), taskDto);
|
|
|
|
|
newTask = taskRepository.save(newTask);
|
|
|
|
|
eventService.createFromObject(newTask, Collections.emptyList(), true, "задачи");
|
|
|
|
|
return newTask.getId();
|
|
|
|
|
public TaskDto create(TaskDto taskDto) {
|
|
|
|
|
Task newTask = null;
|
|
|
|
|
try {
|
|
|
|
|
newTask = copyFromDto(new Task(), taskDto);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return new TaskDto(create(newTask));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public Task create(Task task) {
|
|
|
|
|
task = taskRepository.save(task);
|
|
|
|
|
eventService.createFromObject(task, Collections.emptyList(), true, "задачи");
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task copyFromDto(Task task, TaskDto taskDto) throws IOException {
|
|
|
|
@ -101,15 +124,20 @@ public class TaskService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
private Integer update(TaskDto taskDto) throws IOException {
|
|
|
|
|
public TaskDto update(TaskDto taskDto) {
|
|
|
|
|
Task task = taskRepository.getOne(taskDto.getId());
|
|
|
|
|
taskRepository.save(copyFromDto(task, taskDto));
|
|
|
|
|
return new TaskDto(update(task));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public Task update(Task task) {
|
|
|
|
|
task = taskRepository.save(task);
|
|
|
|
|
eventService.updateTaskDeadlines(task);
|
|
|
|
|
return task.getId();
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public boolean delete(Integer taskId) throws IOException {
|
|
|
|
|
public boolean delete(Integer taskId) {
|
|
|
|
|
if (taskRepository.existsById(taskId)) {
|
|
|
|
|
Task scheduleTask = taskRepository.getOne(taskId);
|
|
|
|
|
Scheduler sch = schedulerRepository.findOneByTask(scheduleTask);
|
|
|
|
@ -120,15 +148,6 @@ public class TaskService {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void save(TaskDto taskDto) throws IOException {
|
|
|
|
|
if (isEmpty(taskDto.getId())) {
|
|
|
|
|
create(taskDto);
|
|
|
|
|
} else {
|
|
|
|
|
update(taskDto);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void copyMainPart(Task newTask, Task task) {
|
|
|
|
@ -255,4 +274,27 @@ public class TaskService {
|
|
|
|
|
taskRepository.save(newTask);
|
|
|
|
|
return newTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PageableItems<TaskDashboardDto> findAllActiveDto(int offset, int count) {
|
|
|
|
|
return convertPageable(findAllActive(offset, count), TaskDashboardDto::new);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PageableItems<Task> findAllActive(int offset, int count) {
|
|
|
|
|
return findAll(offset, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TaskDto findById(Integer taskId) {
|
|
|
|
|
return new TaskDto(findTaskById(taskId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task findTaskById(Integer taskId) {
|
|
|
|
|
return taskRepository.findById(taskId)
|
|
|
|
|
.orElseThrow(() -> new EntityNotFoundException("Paper with id=" + taskId + " not found"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public void ping(int taskId) {
|
|
|
|
|
pingService.addPing(findTaskById(taskId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|