work with different version of entity

pull/244/head
Anton Romanov 5 years ago
parent a1d268a8c2
commit 0f9f591c6b

@ -4,6 +4,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import java.util.Optional;
public interface ActivityRepository<T> {
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
@ -14,4 +16,8 @@ public interface ActivityRepository<T> {
Page<T> findAll(Pageable pageable);
void deleteById(Integer id);
Optional<T> findById(Integer id);
void detach(T t);
}

@ -1,8 +1,10 @@
package ru.ulstu.activity.common.model;
import ru.ulstu.activity.timeline.model.Event;
import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.user.model.User;
import java.util.List;
import java.util.Set;
public abstract class AbstractActivity extends BaseEntity {
@ -17,4 +19,6 @@ public abstract class AbstractActivity extends BaseEntity {
}
public abstract Set<User> getActivityMembers();
public abstract List<Event> getEvents();
}

@ -13,6 +13,7 @@ import ru.ulstu.activity.timeline.service.EventService;
import ru.ulstu.core.jpa.OffsetablePageRequest;
import ru.ulstu.core.model.response.PageableItems;
import javax.persistence.EntityNotFoundException;
import java.io.IOException;
import java.util.List;
@ -47,23 +48,31 @@ public abstract class ActivityService<L extends ActivityListDto, T extends Abstr
}
@Transactional
public T update(T entity) {
public T update(T oldEntity, T entity) {
entity.getEvents().clear();
eventService.deleteAll(getEvents(entity));
eventService.createFromObject((EventSource) entity);
doActionsOnDiffObjects(oldEntity, entity);
return activityRepository.save(entity);
}
protected abstract void doActionsOnDiffObjects(T oldEntity, T entity);
protected abstract List<Event> getEvents(T entity);
@Transactional
public D update(D entityDto) {
T oldEntity = activityRepository.getById(entityDto.getId());
//to init lazy collection
oldEntity.getActivityMembers();
activityRepository.detach(oldEntity);
T newEntity;
try {
newEntity = copyFromDto(activityRepository.getById(entityDto.getId()), entityDto);
} catch (IOException e) {
throw new RuntimeException(e);
}
return getNewActivityDto(update(newEntity));
return getNewActivityDto(update(oldEntity, newEntity));
}
@Transactional
@ -79,8 +88,13 @@ public abstract class ActivityService<L extends ActivityListDto, T extends Abstr
return true;
}
public D findDtoById(Integer id) {
return getNewActivityDto(findById(id));
}
public T findById(Integer id) {
return activityRepository.getById(id);
return activityRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Entity with id=" + id + " not found"));
}
public PageableItems<T> findAll(int offset, int count) {
@ -92,7 +106,6 @@ public abstract class ActivityService<L extends ActivityListDto, T extends Abstr
return convertPageable(findAll(offset, count), entity -> getActivityListDto(entity));
}
public abstract PageableItems findAllActiveDto(int offset, int count);
public abstract PageableItems<T> findAllActive(int offset, int count);

@ -25,6 +25,7 @@ import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
@ -177,4 +178,9 @@ public class Conference extends AbstractActivity implements EventSource {
public Set<User> getActivityMembers() {
return users.stream().map(conferenceUser -> conferenceUser.getUser()).collect(Collectors.toSet());
}
@Override
public List<Event> getEvents() {
return Collections.emptyList();
}
}

@ -2,13 +2,13 @@ package ru.ulstu.activity.conference.service;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.activity.common.service.ActivityService;
import ru.ulstu.activity.conference.model.Conference;
import ru.ulstu.activity.conference.model.ConferenceDashboardDto;
import ru.ulstu.activity.conference.model.ConferenceDto;
import ru.ulstu.activity.conference.model.ConferenceFilterDto;
import ru.ulstu.activity.conference.model.ConferenceListDto;
import ru.ulstu.activity.deadline.model.Deadline;
import ru.ulstu.activity.deadline.service.DeadlineService;
import ru.ulstu.activity.paper.service.PaperService;
import ru.ulstu.activity.ping.service.PingService;
@ -57,18 +57,6 @@ public class ConferenceService extends ActivityService<ConferenceListDto, Confer
this.eventService = eventService;
}
@Transactional
@Override
public ConferenceDto update(ConferenceDto conferenceDto) {
Conference conference = conferenceRepository.getOne(conferenceDto.getId());
try {
conferenceRepository.save(copyFromDto(conference, conferenceDto));
} catch (IOException e) {
throw new RuntimeException(e);
}
return new ConferenceDto(update(conference));
}
@Override
protected ConferenceListDto getActivityListDto(Conference entity) {
return new ConferenceListDto(entity);
@ -131,6 +119,34 @@ public class ConferenceService extends ActivityService<ConferenceListDto, Confer
return conferenceRepository.findActiveByUser(user);
}
@Override
protected void doActionsOnDiffObjects(Conference oldEntity, Conference entity) {
if (!entity.getBeginDate().equals(oldEntity.getBeginDate())
|| !entity.getEndDate().equals(oldEntity.getEndDate())) {
conferenceNotificationService.updateConferencesDatesNotification(entity, oldEntity.getBeginDate(), oldEntity.getEndDate());
}
entity.getActivityMembers().forEach(author -> {
if (!oldEntity.getActivityMembers().contains(author)) {
conferenceNotificationService.sendCreateNotification(entity);
}
});
sendNotificationAfterUpdateDeadlines(entity, oldEntity.getDeadlines());
}
private void sendNotificationAfterUpdateDeadlines(Conference conference, List<Deadline> oldDeadlines) {
if (oldDeadlines.size() != conference.getDeadlines().size()) {
conferenceNotificationService.updateDeadlineNotification(conference);
return;
}
if (conference.getDeadlines()
.stream()
.filter(deadline -> !oldDeadlines.contains(deadline))
.count() > 0) {
conferenceNotificationService.updateDeadlineNotification(conference);
}
}
@Override
protected List<Event> getEvents(Conference entity) {
return eventService.findByConference(entity);

@ -53,7 +53,7 @@ public class GrantController implements ActivityController<GrantListDto, GrantDa
@Override
@GetMapping("{grant-id}")
public Response<GrantDto> get(@PathVariable("grant-id") Integer entityId) {
return new Response<>(grantService.findGrantById(entityId));
return new Response<>(grantService.findDtoById(entityId));
}
@Override

@ -2,15 +2,15 @@ package ru.ulstu.activity.grant.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import ru.ulstu.activity.api.ActivityRepository;
import ru.ulstu.activity.grant.model.Grant;
import ru.ulstu.core.repository.JpaDetachableRepository;
import java.util.List;
interface GrantRepository extends JpaRepository<Grant, Integer>, ActivityRepository<Grant> {
interface GrantRepository extends JpaDetachableRepository<Grant, Integer>, ActivityRepository<Grant> {
List<Grant> findByStatus(Grant.GrantStatus status);

@ -116,6 +116,18 @@ public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto>
return grant;
}
@Override
protected void doActionsOnDiffObjects(Grant oldEntity, Grant entity) {
entity.getAuthors().forEach(author -> {
if (!oldEntity.getAuthors().contains(author)) {
grantNotificationService.sendAuthorsChangeNotification(entity, oldEntity.getAuthors());
}
});
if (!entity.getLeader().equals(oldEntity.getLeader())) {
grantNotificationService.sendLeaderChangeNotification(entity, oldEntity.getLeader());
}
}
@Override
protected List<Event> getEvents(Grant entity) {
return eventService.findByGrant(entity);
@ -169,14 +181,6 @@ public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto>
return new PageableItems<>(activeGrantsPage.getTotalElements(), activeGrantsPage.getContent());
}
public GrantDto findGrantById(Integer id) {
return new GrantDto(findById(id));
}
public Grant findById(Integer id) {
return grantRepository.getOne(id);
}
@Transactional
public void ping(int grantId) {
pingService.addPing(findById(grantId));

@ -47,7 +47,7 @@ public class PaperController implements ActivityController<PaperListDto, PaperDa
@GetMapping("{paper-id}")
public Response<PaperDto> get(@PathVariable("paper-id") Integer paperId) {
return new Response<>(paperService.findPaperById(paperId));
return new Response<>(paperService.findDtoById(paperId));
}
@PostMapping

@ -6,12 +6,10 @@ import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.activity.common.service.ActivityService;
import ru.ulstu.activity.deadline.model.Deadline;
import ru.ulstu.activity.deadline.service.DeadlineService;
import ru.ulstu.activity.file.model.FileDataDto;
import ru.ulstu.activity.file.service.FileService;
import ru.ulstu.activity.paper.model.Paper;
import ru.ulstu.activity.paper.model.PaperDashboardDto;
import ru.ulstu.activity.paper.model.PaperDto;
import ru.ulstu.activity.paper.model.PaperFilterListDto;
import ru.ulstu.activity.paper.model.PaperListDto;
import ru.ulstu.activity.paper.model.PaperStatusDto;
import ru.ulstu.activity.paper.model.PaperTypeDto;
@ -23,13 +21,10 @@ import ru.ulstu.core.model.response.PageableItems;
import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService;
import javax.persistence.EntityNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
@ -53,9 +48,6 @@ public class PaperService extends ActivityService<PaperListDto, Paper, PaperDto>
private final EventService eventService;
private final PingService pingService;
private Paper.PaperStatus oldStatus;
private Set<User> oldAuthors;
public PaperService(PaperRepository paperRepository,
FileService fileService,
PaperNotificationService paperNotificationService,
@ -108,21 +100,14 @@ public class PaperService extends ActivityService<PaperListDto, Paper, PaperDto>
return eventService.findByPaper(entity);
}
@Transactional
public PaperDto update(PaperDto paperDto) {
Paper paper = findById(paperDto.getId());
oldStatus = paper.getStatus();
oldAuthors = new HashSet<>(paper.getAuthors());
//TODO: move to service
if (paperDto.getFiles() != null) {
for (FileDataDto file : paperDto.getFiles().stream()
.filter(f -> f.isDeleted() && f.getId() != null)
.collect(toList())) {
fileService.delete(file.getId());
}
@Override
protected void doActionsOnDiffObjects(Paper oldEntity, Paper entity) {
if (oldEntity.getStatus() != entity.getStatus()) {
paperNotificationService.statusChangeNotification(entity, oldEntity.getStatus());
}
if (!oldEntity.getAuthors().equals(entity.getAuthors())) {
paperNotificationService.sendCreateNotification(entity, oldEntity.getAuthors());
}
return new PaperDto(update(copyFromDto(paper, paperDto)));
}
protected Paper copyFromDto(Paper paper, PaperDto paperDto) {
@ -152,13 +137,6 @@ public class PaperService extends ActivityService<PaperListDto, Paper, PaperDto>
return paper;
}
@Transactional
public boolean delete(Integer paperId) {
Paper paper = paperRepository.getOne(paperId);
paperRepository.delete(paper);
return true;
}
public List<PaperStatusDto> getPaperStatuses() {
return convert(Arrays.asList(Paper.PaperStatus.values()), PaperStatusDto::new);
}
@ -181,12 +159,6 @@ public class PaperService extends ActivityService<PaperListDto, Paper, PaperDto>
return create(paper);
}
public List<PaperListDto> filter(PaperFilterListDto filterDto) {
return convert(sortPapers(paperRepository.filter(
filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()),
filterDto.getYear())), PaperListDto::new);
}
private List<Paper> sortPapers(List<Paper> papers) {
return papers.stream().sorted((paper1, paper2) -> {
int statusCompareResult =
@ -223,28 +195,7 @@ public class PaperService extends ActivityService<PaperListDto, Paper, PaperDto>
}
}
public PaperDto findPaperById(Integer paperId) {
return new PaperDto(findById(paperId));
}
public Paper findById(Integer paperId) {
return paperRepository.findById(paperId)
.orElseThrow(() -> new EntityNotFoundException("Paper with id=" + paperId + " not found"));
}
public List<PaperDto> findAllNotCompleted() {
return convert(paperRepository.findByStatusNot(COMPLETED), PaperDto::new);
}
public List<PaperDto> findAllSelect(List<Integer> paperIds) {
return convert(paperRepository.findAllByIdIn(paperIds), PaperDto::new);
}
public List<User> getPaperAuthors() {
return userService.findAll();
}
public List<Paper> findAllCompletedByType(Paper.PaperType type) {
return paperRepository.findByTypeAndStatus(type, Paper.PaperStatus.COMPLETED);
}
}

@ -43,7 +43,7 @@ public class ProjectController implements ActivityController<ProjectListDto, Pro
@GetMapping("{project-id}")
public Response<ProjectDto> get(@PathVariable("project-id") Integer projectId) {
return new Response<>(projectService.findProjectById(projectId));
return new Response<>(projectService.findDtoById(projectId));
}
@PostMapping

@ -56,6 +56,11 @@ public class ProjectService extends ActivityService<ProjectListDto, Project, Pro
return new PageableItems<>(activeProjectPage.getTotalElements(), activeProjectPage.getContent());
}
@Override
protected void doActionsOnDiffObjects(Project oldEntity, Project entity) {
//TODO
}
@Override
protected List<Event> getEvents(Project entity) {
return eventService.findByProject(entity);
@ -88,8 +93,4 @@ public class ProjectService extends ActivityService<ProjectListDto, Project, Pro
protected ProjectDto getNewActivityDto(Project entity) {
return new ProjectDto(entity);
}
public ProjectDto findProjectById(Integer projectId) {
return getNewActivityDto(findById(projectId));
}
}

@ -44,7 +44,7 @@ public class TaskController implements ActivityController<TaskListDto, TaskDashb
@GetMapping("{task-id}")
public Response<TaskDto> get(@PathVariable("task-id") Integer taskId) {
return new Response<>(taskService.findTaskById(taskId));
return new Response<>(taskService.findDtoById(taskId));
}
@PostMapping

@ -110,6 +110,11 @@ public class Task extends AbstractActivity implements EventSource {
return Collections.emptySet();
}
@Override
public List<Event> getEvents() {
return Collections.emptyList();
}
public TaskStatus getStatus() {
return status;
}

@ -1,7 +1,6 @@
package ru.ulstu.activity.students.service;
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.ActivityService;
@ -12,7 +11,6 @@ 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.tags.model.Tag;
import ru.ulstu.activity.tags.service.TagService;
@ -22,7 +20,6 @@ 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.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@ -34,7 +31,6 @@ import java.util.TreeMap;
import java.util.stream.Collectors;
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
@ -65,10 +61,6 @@ public class TaskService extends ActivityService<TaskListDto, Task, TaskDto> {
this.pingService = pingService;
}
public List<Task> findAll() {
return taskRepository.findAll(new Sort(Sort.Direction.DESC, "createDate"));
}
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());
@ -89,18 +81,6 @@ public class TaskService extends ActivityService<TaskListDto, Task, TaskDto> {
return new TaskDto(entity);
}
public List<TaskDto> filter(TaskFilterDto filterDto) {
if (filterDto.getOrder().compareTo("new") == 0) {
return convert(taskRepository.filterNew(
filterDto.getStatus(),
filterDto.getTag() == null ? null : tagService.findById(filterDto.getTag())), TaskDto::new);
} else {
return convert(taskRepository.filterOld(
filterDto.getStatus(),
filterDto.getTag() == null ? null : tagService.findById(filterDto.getTag())), TaskDto::new);
}
}
protected Task copyFromDto(Task task, TaskDto taskDto) {
task.setTitle(taskDto.getTitle());
task.setDescription(taskDto.getDescription());
@ -113,6 +93,11 @@ public class TaskService extends ActivityService<TaskListDto, Task, TaskDto> {
return task;
}
@Override
protected void doActionsOnDiffObjects(Task oldEntity, Task entity) {
//TODO
}
@Override
protected List<Event> getEvents(Task entity) {
return eventService.findByTask(entity);
@ -252,19 +237,4 @@ public class TaskService extends ActivityService<TaskListDto, Task, TaskDto> {
public PageableItems<Task> findAllActive(int offset, int count) {
return findAll(offset, count);
}
public TaskDto findTaskById(Integer taskId) {
return new TaskDto(findById(taskId));
}
public Task findById(Integer taskId) {
return taskRepository.findById(taskId)
.orElseThrow(() -> new EntityNotFoundException("Paper with id=" + taskId + " not found"));
}
@Transactional
public void ping(int taskId) {
pingService.addPing(findById(taskId));
}
}

@ -68,7 +68,6 @@ public class EventService {
+ eventSource.getTitle() + "'");
eventSource.addObjectToEvent(newEvent);
newEvent.setRecipients(eventSource.getRecipients());
newEvents.add(eventRepository.save(newEvent));
}
eventRepository.saveAll(newEvents);
}

Loading…
Cancel
Save