Created
September 4, 2017 12:28
-
-
Save Romain-P/06733baad995c1a0becddef77b0c1c22 to your computer and use it in GitHub Desktop.
Service gérant la logique métier des activités
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @Author: romain.pillot | |
| * @Date: 18/08/2017 | |
| */ | |
| @Service | |
| public class ActivityService extends AbstractCrudService<ActivityDTO, ActivityRepositoryImpl> { | |
| @Autowired | |
| private UserService userService; | |
| /** | |
| * Unused in the new version (on angular). | |
| * This method loaded all activities and sorted it by modification date of children, packing their parent | |
| * and associated children. see: https://stackoverflow.com/questions/45818320/java-8-advanced-sort | |
| */ | |
| @Override | |
| public Set<ActivityDTO> get() { | |
| Set<ActivityDTO> actives = getRepository().findActiveActivities().stream() | |
| .filter(activity -> activity.getParentActivity() == null) | |
| .sorted((a, b) -> Long.compare(b.getSubActivities().stream().max(Comparator.comparing(ActivityDTO::getModificationDate)).orElse(b) | |
| .getModificationDate(), a.getSubActivities().stream().max(Comparator.comparing(ActivityDTO::getModificationDate)).orElse(a).getModificationDate())) | |
| .collect(Collectors.toCollection(LinkedHashSet::new)); | |
| return actives.stream().flatMap(set -> | |
| Stream.concat(Stream.of(set), set.getSubActivities().stream() | |
| .sorted((a, o2) -> Long.compare(o2.getModificationDate(), a.getModificationDate()))) | |
| ).collect(Collectors.toCollection(LinkedHashSet::new)); | |
| } | |
| public Set<ActivityDTO> getParents() { | |
| return getRepository().findParentActivities().stream() | |
| .sorted(Comparator.comparing(ActivityDTO::getModificationDate).reversed()) | |
| .collect(Collectors.toCollection(LinkedHashSet::new)); | |
| } | |
| public Set<ActivityDTO> getChildren(int id) { | |
| return getRepository().findById(id) | |
| .map(activity -> getRepository().findChildrenActivities(activity).stream() | |
| .sorted(Comparator.comparing(ActivityDTO::getModificationDate).reversed()) | |
| .collect(Collectors.toCollection(LinkedHashSet::new))) | |
| .orElseGet(Sets::newLinkedHashSet); | |
| } | |
| @Override | |
| public void update(ActivityDTO dto) { | |
| getRepository().findById(dto.getId()).ifPresent(stored -> | |
| updateDto(dto, stored.getCreationDate(), System.currentTimeMillis(), -1, false)); | |
| } | |
| @Override | |
| public void create(ActivityDTO dto) { | |
| long current = System.currentTimeMillis(); | |
| dto.setActive(true); | |
| updateDto(dto, current, current, -1, true); | |
| } | |
| @Override | |
| public void delete(Integer id) { | |
| getRepository().findById(id).ifPresent(stored -> { | |
| stored.getSubActivities().forEach(x -> delete(x.getId())); | |
| long current = System.currentTimeMillis(); | |
| stored.setActive(false); | |
| updateDto(stored, stored.getCreationDate(), current, current, false); | |
| }); | |
| } | |
| private void updateDto(ActivityDTO dto, long creation, long edit, long delete, boolean create) { | |
| userService.get(SessionUtil.activeUser().getId()).ifPresent(dto::setLastEditor); | |
| dto.setCreationDate(creation) | |
| .setModificationDate(edit) | |
| .setDeletionDate(delete); | |
| if (create) | |
| super.create(dto); | |
| else | |
| super.update(dto); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment