-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #393 from BBMRI-ERIC/feat/submit_additional_inform…
…ation feat: add functionality to submit additional information
- Loading branch information
Showing
28 changed files
with
1,346 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
81 changes: 81 additions & 0 deletions
81
...main/java/eu/bbmri_eric/negotiator/api/controller/v3/InformationSubmissionController.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package eu.bbmri_eric.negotiator.api.controller.v3; | ||
|
||
import eu.bbmri_eric.negotiator.dto.InformationSubmissionDTO; | ||
import eu.bbmri_eric.negotiator.dto.SubmittedInformationDTO; | ||
import eu.bbmri_eric.negotiator.service.InformationRequirementService; | ||
import eu.bbmri_eric.negotiator.service.InformationSubmissionService; | ||
import eu.bbmri_eric.negotiator.service.NegotiationService; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.io.IOException; | ||
import org.springframework.hateoas.EntityModel; | ||
import org.springframework.hateoas.MediaTypes; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.server.ServerErrorException; | ||
|
||
@RestController | ||
@RequestMapping(value = InformationSubmissionController.BASE_URL) | ||
@Tag( | ||
name = "Submit required information", | ||
description = "Submit required information on behalf of a resource in a Negotiation.") | ||
@SecurityRequirement(name = "security_auth") | ||
public class InformationSubmissionController { | ||
private final InformationRequirementService requirementService; | ||
private final NegotiationService negotiationService; | ||
private final InformationSubmissionService submissionService; | ||
public static final String BASE_URL = "/v3"; | ||
|
||
public InformationSubmissionController( | ||
InformationRequirementService requirementService, | ||
NegotiationService negotiationService, | ||
InformationSubmissionService submissionService) { | ||
this.requirementService = requirementService; | ||
this.negotiationService = negotiationService; | ||
this.submissionService = submissionService; | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping( | ||
value = "/negotiations/{negotiationId}/info-requirements/{requirementId}", | ||
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) | ||
public ResponseEntity<byte[]> getSummaryInformation( | ||
@PathVariable String negotiationId, @PathVariable Long requirementId) { | ||
MultipartFile file = submissionService.createSummary(requirementId, negotiationId); | ||
try { | ||
return ResponseEntity.ok() | ||
.header("Content-Disposition", "attachment; filename=\"%s\"".formatted(file.getName())) | ||
.contentType(MediaType.valueOf("text/csv")) | ||
.body(file.getBytes()); | ||
} catch (IOException e) { | ||
throw new ServerErrorException("Failed to create summary information", e); | ||
} | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@PostMapping( | ||
value = "/negotiations/{negotiationId}/info-requirements/{requirementId}", | ||
consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaTypes.HAL_JSON_VALUE) | ||
public EntityModel<SubmittedInformationDTO> submitInformation( | ||
@PathVariable String negotiationId, | ||
@PathVariable Long requirementId, | ||
@RequestBody InformationSubmissionDTO dto) { | ||
return EntityModel.of(submissionService.submit(dto, requirementId, negotiationId)); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping(value = "/info-submissions/{id}", produces = MediaTypes.HAL_JSON_VALUE) | ||
public EntityModel<SubmittedInformationDTO> getInfoSubmission(@PathVariable Long id) { | ||
return EntityModel.of(submissionService.findById(id)); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
64 changes: 64 additions & 0 deletions
64
src/main/java/eu/bbmri_eric/negotiator/database/model/InformationSubmission.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package eu.bbmri_eric.negotiator.database.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.JdbcTypeCode; | ||
import org.hibernate.type.SqlTypes; | ||
|
||
/** Represents a submission of additional information by the resource representative. */ | ||
@Setter | ||
@Getter | ||
@Entity | ||
public class InformationSubmission { | ||
|
||
protected InformationSubmission() {} | ||
|
||
protected InformationSubmission( | ||
Long id, | ||
InformationRequirement requirement, | ||
Resource resource, | ||
Negotiation negotiation, | ||
String payload) { | ||
this.id = id; | ||
this.requirement = requirement; | ||
this.resource = resource; | ||
this.negotiation = negotiation; | ||
this.payload = payload; | ||
} | ||
|
||
public InformationSubmission( | ||
InformationRequirement requirement, | ||
Resource resource, | ||
Negotiation negotiation, | ||
String payload) { | ||
this.requirement = requirement; | ||
this.resource = resource; | ||
this.negotiation = negotiation; | ||
this.payload = payload; | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "requirement_id") | ||
private InformationRequirement requirement; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "resource_id") | ||
private Resource resource; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "negotiation_id") | ||
private Negotiation negotiation; | ||
|
||
@JdbcTypeCode(SqlTypes.JSON) | ||
private String payload; | ||
} |
19 changes: 19 additions & 0 deletions
19
...in/java/eu/bbmri_eric/negotiator/database/repository/InformationSubmissionRepository.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package eu.bbmri_eric.negotiator.database.repository; | ||
|
||
import eu.bbmri_eric.negotiator.database.model.InformationSubmission; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface InformationSubmissionRepository | ||
extends JpaRepository<InformationSubmission, Long> { | ||
boolean existsByResource_SourceIdAndNegotiation_Id(String sourceId, String negotiationId); | ||
|
||
boolean existsByResource_SourceIdAndNegotiation_IdAndRequirement_Id( | ||
String sourceId, String negotiationId, Long requirementId); | ||
|
||
Set<InformationSubmission> findAllByNegotiation_Id(String negotiationId); | ||
|
||
List<InformationSubmission> findAllByRequirement_IdAndNegotiation_Id( | ||
Long requirementId, String negotiationId); | ||
} |
This file contains 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
19 changes: 19 additions & 0 deletions
19
src/main/java/eu/bbmri_eric/negotiator/dto/InformationSubmissionDTO.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package eu.bbmri_eric.negotiator.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonInclude(value = JsonInclude.Include.NON_NULL) | ||
public class InformationSubmissionDTO { | ||
@NotNull private Long resourceId; | ||
@NotNull private JsonNode payload; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/eu/bbmri_eric/negotiator/dto/SubmittedInformationDTO.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package eu.bbmri_eric.negotiator.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonInclude(value = JsonInclude.Include.NON_NULL) | ||
public class SubmittedInformationDTO { | ||
@NotNull private Long id; | ||
@NotNull private Long resourceId; | ||
private Long requirementId; | ||
@NotNull private JsonNode payload; | ||
} |
This file contains 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
14 changes: 14 additions & 0 deletions
14
src/main/java/eu/bbmri_eric/negotiator/events/InformationSubmissionEvent.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package eu.bbmri_eric.negotiator.events; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class InformationSubmissionEvent extends ApplicationEvent { | ||
private final String negotiationId; | ||
|
||
public InformationSubmissionEvent(Object source, String negotiationId) { | ||
super(source); | ||
this.negotiationId = negotiationId; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/eu/bbmri_eric/negotiator/mappers/InformationSubmissionMapper.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package eu.bbmri_eric.negotiator.mappers; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import eu.bbmri_eric.negotiator.database.model.InformationSubmission; | ||
import eu.bbmri_eric.negotiator.dto.SubmittedInformationDTO; | ||
import jakarta.annotation.PostConstruct; | ||
import org.modelmapper.Converter; | ||
import org.modelmapper.ModelMapper; | ||
import org.modelmapper.TypeMap; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class InformationSubmissionMapper { | ||
ModelMapper modelMapper; | ||
|
||
public InformationSubmissionMapper(ModelMapper modelMapper) { | ||
this.modelMapper = modelMapper; | ||
} | ||
|
||
@PostConstruct | ||
public void addMappings() { | ||
TypeMap<InformationSubmission, SubmittedInformationDTO> typeMap = | ||
modelMapper.createTypeMap(InformationSubmission.class, SubmittedInformationDTO.class); | ||
Converter<String, JsonNode> payloadConverter = | ||
p -> { | ||
try { | ||
return payloadConverter(p.getSource()); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); // TODO: raise the correct exception | ||
} | ||
}; | ||
typeMap.addMappings( | ||
mapper -> | ||
mapper | ||
.using(payloadConverter) | ||
.map(InformationSubmission::getPayload, SubmittedInformationDTO::setPayload)); | ||
} | ||
|
||
private JsonNode payloadConverter(String jsonPayload) throws JsonProcessingException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
if (jsonPayload == null) { | ||
jsonPayload = "{}"; | ||
} | ||
return mapper.readTree(jsonPayload); | ||
} | ||
} |
Oops, something went wrong.