Skip to content

Commit

Permalink
Added: Frontend DTO converters
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Jan 10, 2024
1 parent 8eb7521 commit 5773882
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/main/java/de/samply/db/model/ProjectBridgehead.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
14 changes: 10 additions & 4 deletions src/main/java/de/samply/document/DocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,12 +193,17 @@ public Optional<Path> fetchApplicationForm() {
return fetchPublicDocument(applicationFormFile);
}

public List<ProjectDocument> fetchPublications(String projectCode) {
return fetchDocuments(projectCode, Optional.empty(), DocumentType.PUBLICATION);
public List<de.samply.frontend.dto.ProjectDocument> fetchPublications(String projectCode) {
return convertToDto(fetchDocuments(projectCode, Optional.empty(), DocumentType.PUBLICATION));

}

private List<de.samply.frontend.dto.ProjectDocument> convertToDto(List<ProjectDocument> projectDocumentList){
return projectDocumentList.stream().map(DtoFactory::convert).toList();
}

public List<ProjectDocument> fetchOtherDocuments(String projectCode, Optional<String> bridgehead) {
return fetchDocuments(projectCode, bridgehead, DocumentType.OTHERS);
public List<de.samply.frontend.dto.ProjectDocument> fetchOtherDocuments(String projectCode, Optional<String> bridgehead) {
return convertToDto(fetchDocuments(projectCode, bridgehead, DocumentType.OTHERS));
}


Expand Down
65 changes: 52 additions & 13 deletions src/main/java/de/samply/frontend/dto/DtoFactory.java
Original file line number Diff line number Diff line change
@@ -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()
);
}

}
14 changes: 13 additions & 1 deletion src/main/java/de/samply/frontend/dto/Notification.java
Original file line number Diff line number Diff line change
@@ -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
) {
}
4 changes: 0 additions & 4 deletions src/main/java/de/samply/frontend/dto/OtherDocument.java

This file was deleted.

28 changes: 27 additions & 1 deletion src/main/java/de/samply/frontend/dto/Project.java
Original file line number Diff line number Diff line change
@@ -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
) {
}
11 changes: 10 additions & 1 deletion src/main/java/de/samply/frontend/dto/ProjectBridgehead.java
Original file line number Diff line number Diff line change
@@ -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
) {
}
18 changes: 18 additions & 0 deletions src/main/java/de/samply/frontend/dto/ProjectDocument.java
Original file line number Diff line number Diff line change
@@ -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
) {

}
4 changes: 0 additions & 4 deletions src/main/java/de/samply/frontend/dto/Publication.java

This file was deleted.

8 changes: 5 additions & 3 deletions src/main/java/de/samply/notification/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -45,11 +46,12 @@ private Project fetchProject(String projectCode) throws NotificationServiceExcep
return project.get();
}

public List<Notification> fetchNotifications(String projectCode, Optional<String> bridghead) throws NotificationServiceException {
public List<de.samply.frontend.dto.Notification> fetchNotifications(String projectCode, Optional<String> 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();
}


Expand Down
7 changes: 4 additions & 3 deletions src/main/java/de/samply/project/ProjectBridgeheadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,22 +53,22 @@ private void changeProjectBridgeheadState(@NotNull String projectCode, @NotNull
projectBridgeheadRepository.save(projectBridgehead.get());
}

public List<ProjectBridgehead> fetchUserVisibleProjectBridgeheads(@NotNull String projectCode) throws ProjectBridgeheadServiceException {
public List<de.samply.frontend.dto.ProjectBridgehead> fetchUserVisibleProjectBridgeheads(@NotNull String projectCode) throws ProjectBridgeheadServiceException {
Optional<Project> project = projectRepository.findByCode(projectCode);
if (project.isEmpty()) {
throw new ProjectBridgeheadServiceException("Project " + projectCode + " not found");
}
Set<ProjectBridgehead> projectBridgeheads = projectBridgeheadRepository.findByProject(project.get());
if (isProjectManagerAdmin()) {
return new ArrayList<>(projectBridgeheads);
return new ArrayList<>(projectBridgeheads).stream().map(DtoFactory::convert).toList();
}
Set<ProjectBridgehead> tempProjectBridgeheads = new HashSet<>();
projectBridgeheads.forEach(projectBridgehead -> {
if (isBridgeheadAdminOfProjectBridgehead(projectBridgehead) || isUserOfProjectBridgehead(projectBridgehead)) {
tempProjectBridgeheads.add(projectBridgehead);
}
});
return new ArrayList<>(tempProjectBridgeheads);
return new ArrayList<>(tempProjectBridgeheads).stream().map(DtoFactory::convert).toList();
}

private boolean isProjectManagerAdmin() {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/de/samply/project/ProjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,18 +74,18 @@ private void createProjectBridgehead(Project project, String bridgehead) {
projectBridgeheadRepository.save(projectBridgehead);
}

public Page<Project> fetchUserVisibleProjects(
public Page<de.samply.frontend.dto.Project> fetchUserVisibleProjects(
Optional<ProjectState> projectState, Optional<Boolean> 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<String> 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() {
Expand Down

0 comments on commit 5773882

Please sign in to comment.