From 5773882b9939e91b9548866c4141535c819fe1d5 Mon Sep 17 00:00:00 2001 From: juarez Date: Wed, 10 Jan 2024 09:24:39 +0100 Subject: [PATCH] Added: Frontend DTO converters --- CHANGELOG.md | 1 + .../de/samply/db/model/ProjectBridgehead.java | 2 +- .../de/samply/document/DocumentService.java | 14 ++-- .../de/samply/frontend/dto/DtoFactory.java | 65 +++++++++++++++---- .../de/samply/frontend/dto/Notification.java | 14 +++- .../de/samply/frontend/dto/OtherDocument.java | 4 -- .../java/de/samply/frontend/dto/Project.java | 28 +++++++- .../frontend/dto/ProjectBridgehead.java | 11 +++- .../samply/frontend/dto/ProjectDocument.java | 18 +++++ .../de/samply/frontend/dto/Publication.java | 4 -- .../notification/NotificationService.java | 8 ++- .../project/ProjectBridgeheadService.java | 7 +- .../de/samply/project/ProjectService.java | 9 +-- 13 files changed, 146 insertions(+), 39 deletions(-) delete mode 100644 src/main/java/de/samply/frontend/dto/OtherDocument.java create mode 100644 src/main/java/de/samply/frontend/dto/ProjectDocument.java delete mode 100644 src/main/java/de/samply/frontend/dto/Publication.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d19d6a..07ec009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,3 +75,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Frontend DTO - Email as Mime message - EmailSenderIfError annotation +- Frontend DTO converters diff --git a/src/main/java/de/samply/db/model/ProjectBridgehead.java b/src/main/java/de/samply/db/model/ProjectBridgehead.java index 6c36368..7f2f3ac 100644 --- a/src/main/java/de/samply/db/model/ProjectBridgehead.java +++ b/src/main/java/de/samply/db/model/ProjectBridgehead.java @@ -33,6 +33,6 @@ public class ProjectBridgehead { private ProjectBridgeheadState state = ProjectBridgeheadState.CREATED; @Column(name = "modified_at", nullable = false) - private Instant modifiedAt; + private Instant modifiedAt = Instant.now(); } diff --git a/src/main/java/de/samply/document/DocumentService.java b/src/main/java/de/samply/document/DocumentService.java index e19c2f0..d9ef97a 100644 --- a/src/main/java/de/samply/document/DocumentService.java +++ b/src/main/java/de/samply/document/DocumentService.java @@ -5,6 +5,7 @@ import de.samply.db.model.ProjectDocument; import de.samply.db.repository.ProjectDocumentRepository; import de.samply.db.repository.ProjectRepository; +import de.samply.frontend.dto.DtoFactory; import de.samply.security.SessionUser; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -192,12 +193,17 @@ public Optional fetchApplicationForm() { return fetchPublicDocument(applicationFormFile); } - public List fetchPublications(String projectCode) { - return fetchDocuments(projectCode, Optional.empty(), DocumentType.PUBLICATION); + public List fetchPublications(String projectCode) { + return convertToDto(fetchDocuments(projectCode, Optional.empty(), DocumentType.PUBLICATION)); + + } + + private List convertToDto(List projectDocumentList){ + return projectDocumentList.stream().map(DtoFactory::convert).toList(); } - public List fetchOtherDocuments(String projectCode, Optional bridgehead) { - return fetchDocuments(projectCode, bridgehead, DocumentType.OTHERS); + public List fetchOtherDocuments(String projectCode, Optional bridgehead) { + return convertToDto(fetchDocuments(projectCode, bridgehead, DocumentType.OTHERS)); } diff --git a/src/main/java/de/samply/frontend/dto/DtoFactory.java b/src/main/java/de/samply/frontend/dto/DtoFactory.java index 8d6034d..44b7aba 100644 --- a/src/main/java/de/samply/frontend/dto/DtoFactory.java +++ b/src/main/java/de/samply/frontend/dto/DtoFactory.java @@ -1,27 +1,66 @@ package de.samply.frontend.dto; -import de.samply.db.model.ProjectDocument; +import de.samply.project.state.ProjectBridgeheadState; import jakarta.validation.constraints.NotNull; -import org.springframework.stereotype.Service; -@Service +import java.time.Instant; + public class DtoFactory { - public Project convert (@NotNull de.samply.db.model.Project project){ - return new Project(); - //TODO + public static Project convert(@NotNull de.samply.db.model.Project project) { + return new Project( + project.getCode(), + project.getCreatorEmail(), + project.getCreatedAt(), + project.getExpiresAt(), + project.getArchivedAt(), + project.getModifiedAt(), + project.getState(), + project.getType(), + project.getQuery().getQuery(), + project.getQuery().getHumanReadable(), + project.getQuery().getQueryFormat(), + project.getQuery().getOutputFormat(), + project.getQuery().getTemplateId(), + project.getQuery().getLabel(), + project.getQuery().getDescription(), + project.getQuery().getExplorerUrl(), + project.getQuery().getContext() + ); } - public Notification convert (@NotNull de.samply.db.model.Notification notification){ - return new Notification(); - //TODO + public static Notification convert(@NotNull de.samply.db.model.Notification notification) { + return new Notification( + notification.getEmail(), + notification.getTimestamp(), + notification.getProject().getCode(), + notification.getBridgehead(), + notification.getOperationType(), + notification.getDetails(), + notification.getError() + ); } - public OtherDocument convert(@NotNull ProjectDocument projectDocument){ - return new OtherDocument(); - //TODO + public static ProjectDocument convert(@NotNull de.samply.db.model.ProjectDocument projectDocument) { + return new ProjectDocument( + projectDocument.getProject().getCode(), + projectDocument.getOriginalFilename(), + projectDocument.getUrl(), + projectDocument.getCreatedAt(), + projectDocument.getBridgehead(), + projectDocument.getCreatorEmail(), + projectDocument.getLabel(), + projectDocument.getDocumentType() + ); } - + public static ProjectBridgehead convert(@NotNull de.samply.db.model.ProjectBridgehead projectBridgehead) { + return new ProjectBridgehead( + projectBridgehead.getProject().getCode(), + projectBridgehead.getBridgehead(), + projectBridgehead.getState(), + projectBridgehead.getModifiedAt() + ); + } } diff --git a/src/main/java/de/samply/frontend/dto/Notification.java b/src/main/java/de/samply/frontend/dto/Notification.java index a5e3fff..d56ed4b 100644 --- a/src/main/java/de/samply/frontend/dto/Notification.java +++ b/src/main/java/de/samply/frontend/dto/Notification.java @@ -1,4 +1,16 @@ package de.samply.frontend.dto; -public record Notification() { +import de.samply.notification.OperationType; + +import java.time.Instant; + +public record Notification( + String email, + Instant timestamp, + String projectCode, + String bridgehead, + OperationType operationType, + String details, + String error +) { } diff --git a/src/main/java/de/samply/frontend/dto/OtherDocument.java b/src/main/java/de/samply/frontend/dto/OtherDocument.java deleted file mode 100644 index 1136905..0000000 --- a/src/main/java/de/samply/frontend/dto/OtherDocument.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.samply.frontend.dto; - -public class OtherDocument { -} diff --git a/src/main/java/de/samply/frontend/dto/Project.java b/src/main/java/de/samply/frontend/dto/Project.java index 71a0957..c962172 100644 --- a/src/main/java/de/samply/frontend/dto/Project.java +++ b/src/main/java/de/samply/frontend/dto/Project.java @@ -1,4 +1,30 @@ package de.samply.frontend.dto; -public record Project() { +import de.samply.project.ProjectType; +import de.samply.project.state.ProjectState; +import de.samply.query.OutputFormat; +import de.samply.query.QueryFormat; + +import java.time.Instant; +import java.time.LocalDate; + +public record Project( + String code, + String creatorEmail, + Instant createdAt, + LocalDate expiresAt, + Instant archivedAt, + Instant modifiedAt, + ProjectState state, + ProjectType type, + String query, + String humanReadable, + QueryFormat queryFormat, + OutputFormat outputFormat, + String templateId, + String label, + String description, + String explorerUrl, + String queryContext +) { } diff --git a/src/main/java/de/samply/frontend/dto/ProjectBridgehead.java b/src/main/java/de/samply/frontend/dto/ProjectBridgehead.java index 5d27dc8..4c29a88 100644 --- a/src/main/java/de/samply/frontend/dto/ProjectBridgehead.java +++ b/src/main/java/de/samply/frontend/dto/ProjectBridgehead.java @@ -1,4 +1,13 @@ package de.samply.frontend.dto; -public record ProjectBridgehead() { +import de.samply.project.state.ProjectBridgeheadState; + +import java.time.Instant; + +public record ProjectBridgehead( + String projectCode, + String bridgehead, + ProjectBridgeheadState state, + Instant modifiedAt +) { } diff --git a/src/main/java/de/samply/frontend/dto/ProjectDocument.java b/src/main/java/de/samply/frontend/dto/ProjectDocument.java new file mode 100644 index 0000000..173d8b9 --- /dev/null +++ b/src/main/java/de/samply/frontend/dto/ProjectDocument.java @@ -0,0 +1,18 @@ +package de.samply.frontend.dto; + +import de.samply.document.DocumentType; + +import java.time.Instant; + +public record ProjectDocument( + String projectCode, + String originalFilename, + String url, + Instant createdAt, + String bridgehead, + String creatorEmail, + String label, + DocumentType type +) { + +} diff --git a/src/main/java/de/samply/frontend/dto/Publication.java b/src/main/java/de/samply/frontend/dto/Publication.java deleted file mode 100644 index 8e19f87..0000000 --- a/src/main/java/de/samply/frontend/dto/Publication.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.samply.frontend.dto; - -public record Publication() { -} diff --git a/src/main/java/de/samply/notification/NotificationService.java b/src/main/java/de/samply/notification/NotificationService.java index 5127afa..791a702 100644 --- a/src/main/java/de/samply/notification/NotificationService.java +++ b/src/main/java/de/samply/notification/NotificationService.java @@ -4,6 +4,7 @@ import de.samply.db.model.Project; import de.samply.db.repository.NotificationRepository; import de.samply.db.repository.ProjectRepository; +import de.samply.frontend.dto.DtoFactory; import jakarta.validation.constraints.NotNull; import org.springframework.stereotype.Service; @@ -45,11 +46,12 @@ private Project fetchProject(String projectCode) throws NotificationServiceExcep return project.get(); } - public List fetchNotifications(String projectCode, Optional bridghead) throws NotificationServiceException { + public List fetchNotifications(String projectCode, Optional bridghead) throws NotificationServiceException { Project project = fetchProject(projectCode); - return (bridghead.isEmpty()) ? + return ((bridghead.isEmpty()) ? notificationRepository.findAllByProjectOrderByTimestampDesc(project) : - notificationRepository.findAllByProjectAndBridgeheadOrBridgeheadIsNullOrderByTimestampDesc(project, bridghead.get()); + notificationRepository.findAllByProjectAndBridgeheadOrBridgeheadIsNullOrderByTimestampDesc(project, bridghead.get())) + .stream().map(DtoFactory::convert).toList(); } diff --git a/src/main/java/de/samply/project/ProjectBridgeheadService.java b/src/main/java/de/samply/project/ProjectBridgeheadService.java index 087ba5d..3db6220 100644 --- a/src/main/java/de/samply/project/ProjectBridgeheadService.java +++ b/src/main/java/de/samply/project/ProjectBridgeheadService.java @@ -5,6 +5,7 @@ import de.samply.db.repository.ProjectBridgeheadRepository; import de.samply.db.repository.ProjectBridgeheadUserRepository; import de.samply.db.repository.ProjectRepository; +import de.samply.frontend.dto.DtoFactory; import de.samply.project.state.ProjectBridgeheadState; import de.samply.security.SessionUser; import de.samply.user.roles.OrganisationRole; @@ -52,14 +53,14 @@ private void changeProjectBridgeheadState(@NotNull String projectCode, @NotNull projectBridgeheadRepository.save(projectBridgehead.get()); } - public List fetchUserVisibleProjectBridgeheads(@NotNull String projectCode) throws ProjectBridgeheadServiceException { + public List fetchUserVisibleProjectBridgeheads(@NotNull String projectCode) throws ProjectBridgeheadServiceException { Optional project = projectRepository.findByCode(projectCode); if (project.isEmpty()) { throw new ProjectBridgeheadServiceException("Project " + projectCode + " not found"); } Set projectBridgeheads = projectBridgeheadRepository.findByProject(project.get()); if (isProjectManagerAdmin()) { - return new ArrayList<>(projectBridgeheads); + return new ArrayList<>(projectBridgeheads).stream().map(DtoFactory::convert).toList(); } Set tempProjectBridgeheads = new HashSet<>(); projectBridgeheads.forEach(projectBridgehead -> { @@ -67,7 +68,7 @@ public List fetchUserVisibleProjectBridgeheads(@NotNull Strin tempProjectBridgeheads.add(projectBridgehead); } }); - return new ArrayList<>(tempProjectBridgeheads); + return new ArrayList<>(tempProjectBridgeheads).stream().map(DtoFactory::convert).toList(); } private boolean isProjectManagerAdmin() { diff --git a/src/main/java/de/samply/project/ProjectService.java b/src/main/java/de/samply/project/ProjectService.java index 4061a2d..0629d1a 100644 --- a/src/main/java/de/samply/project/ProjectService.java +++ b/src/main/java/de/samply/project/ProjectService.java @@ -4,6 +4,7 @@ import de.samply.db.model.ProjectBridgehead; import de.samply.db.repository.ProjectBridgeheadRepository; import de.samply.db.repository.ProjectRepository; +import de.samply.frontend.dto.DtoFactory; import de.samply.project.state.ProjectBridgeheadState; import de.samply.project.state.ProjectState; import de.samply.security.SessionUser; @@ -73,18 +74,18 @@ private void createProjectBridgehead(Project project, String bridgehead) { projectBridgeheadRepository.save(projectBridgehead); } - public Page fetchUserVisibleProjects( + public Page fetchUserVisibleProjects( Optional projectState, Optional archived, int page, int pageSize, boolean modifiedDescendant) { PageRequest pageRequest = PageRequest.of(page, pageSize); if (isProjectManagerAdmin()) { - return fetchProjectManagerAdminProjects(projectState, archived, pageRequest, modifiedDescendant); + return fetchProjectManagerAdminProjects(projectState, archived, pageRequest, modifiedDescendant).map(DtoFactory::convert); } Set bridgeheads = sessionUser.getBridgeheads(); // We make an assumption: A bridgehead admin is bridgehead admin in all of their bridgeheads. if (isBridgeheadAdmin()) { - return fetchBridgeheadAdminProjects(bridgeheads, projectState, archived, pageRequest, modifiedDescendant); + return fetchBridgeheadAdminProjects(bridgeheads, projectState, archived, pageRequest, modifiedDescendant).map(DtoFactory::convert); } - return fetchResearcherProjects(sessionUser.getEmail(), bridgeheads, projectState, archived, pageRequest, modifiedDescendant); + return fetchResearcherProjects(sessionUser.getEmail(), bridgeheads, projectState, archived, pageRequest, modifiedDescendant).map(DtoFactory::convert); } private boolean isProjectManagerAdmin() {