diff --git a/CUSTOMISING.md b/CUSTOMISING.md index 6f594d89..29453510 100644 --- a/CUSTOMISING.md +++ b/CUSTOMISING.md @@ -246,3 +246,7 @@ The new filepath will look into the local resource folder and take the file plac This .docx file needs to contain the placeholder keywords found in the sample template provided in the generic project, in order to replace the text at those specific places. By replacing the resource file instead, the default texts used to compose the document can be adapted. + +The template is selected automatically when exporting, if the project has a funder. You may want to override this functionality, +if you have custom templates for example. To override this, write a class that extends [TemplateSelectorServiceImpl](src/main/java/at/ac/tuwien/damap/conversion/TemplateSelectorServiceImpl.java) +and have it override the methods that determine the template. diff --git a/docker/api-mock/data/db.json b/docker/api-mock/data/db.json index 1be5e637..ba010245 100644 --- a/docker/api-mock/data/db.json +++ b/docker/api-mock/data/db.json @@ -11,20 +11,64 @@ "dmpExists": false }, { - "acronym": "RP.20230313.0", - "universityId": "uniProjectIdRecommended0", - "description": "Recommended project description text.", - "title": "Recommended Project Numero Uno", + "acronym": "PROJ.NO-FUNDING", + "universityId": "uniProjectIdNoFunding0", + "description": "This project has no funding and is recommended.", + "title": "No Funding Sample Project", "funding": null, "start": "2022-01-01T00:00:00.000Z", "end": "2024-12-31T00:00:00.000Z", "dmpExists": false }, { - "acronym": "PROJ.FUNDER", - "universityId": "uniProjectIdFunderInfo", - "description": "Funder supported Project description text.", - "title": "Project Title - Funder Supported", + "acronym": "PROJ.HORIZON-EUROPE", + "universityId": "uniProjectIdHorizonEurope0", + "description": "This project is funded by the EU and is recommended.", + "title": "Horizon Europe Sample Project", + "funding": { + "fundingName": null, + "fundingProgram": null, + "funderId": { + "identifier": "501100000780", + "type": "FUNDREF" + }, + "grantId": { + "identifier": null, + "type": null + }, + "fundingStatus": "GRANTED" + }, + "start": "2022-01-01T00:00:00.000Z", + "end": "2024-12-31T00:00:00.000Z", + "dmpExists": false + }, + { + "acronym": "PROJ.FWF", + "universityId": "uniProjectIdFWF0", + "description": "This project is funded by the FWF and is recommended.", + "title": "FWF Sample Project", + "funding": { + "fundingName": null, + "fundingProgram": null, + "funderId": { + "identifier": "501100002428", + "type": "FUNDREF" + }, + "grantId": { + "identifier": null, + "type": null + }, + "fundingStatus": "GRANTED" + }, + "start": "2022-01-01T00:00:00.000Z", + "end": "2024-12-31T00:00:00.000Z", + "dmpExists": false + }, + { + "acronym": "PROJ.UNKNOWN-FUNDING", + "universityId": "uniProjectIdUnknownFunder0", + "description": "This project is funded by an unknown entity and is recommended.", + "title": "Unknown Funder Sample Project", "funding": { "fundingName": null, "fundingProgram": null, diff --git a/src/main/java/at/ac/tuwien/damap/conversion/ExportTemplateBroker.java b/src/main/java/at/ac/tuwien/damap/conversion/ExportTemplateBroker.java index 99d565cc..0f7d87ac 100644 --- a/src/main/java/at/ac/tuwien/damap/conversion/ExportTemplateBroker.java +++ b/src/main/java/at/ac/tuwien/damap/conversion/ExportTemplateBroker.java @@ -20,13 +20,17 @@ public class ExportTemplateBroker { private final ExportFWFTemplate exportFWFTemplate; private final ExportHorizonEuropeTemplate exportHorizonEuropeTemplate; + private final TemplateSelectorServiceImpl templateSelectorService; + @Inject public ExportTemplateBroker(DmpService dmpService, ExportScienceEuropeTemplate exportScienceEuropeTemplate, - ExportFWFTemplate exportFWFTemplate, ExportHorizonEuropeTemplate exportHorizonEuropeTemplate) { + ExportFWFTemplate exportFWFTemplate, ExportHorizonEuropeTemplate exportHorizonEuropeTemplate, + TemplateSelectorServiceImpl templateSelectorService) { this.dmpService = dmpService; this.exportScienceEuropeTemplate = exportScienceEuropeTemplate; this.exportFWFTemplate = exportFWFTemplate; this.exportHorizonEuropeTemplate = exportHorizonEuropeTemplate; + this.templateSelectorService = templateSelectorService; } /** @@ -37,21 +41,7 @@ public ExportTemplateBroker(DmpService dmpService, ExportScienceEuropeTemplate e * @return */ public XWPFDocument exportTemplate(long dmpId) { - DmpDO dmpDO = dmpService.getDmpById(dmpId); - if (dmpDO.getProject() != null) - if (dmpDO.getProject().getFunding() != null) - if (dmpDO.getProject().getFunding().getFunderId() != null) { - IdentifierDO funderIdentifier = dmpDO.getProject().getFunding().getFunderId(); - if (funderIdentifier.getType() != null) - if (funderIdentifier.getType().equals(EIdentifierType.FUNDREF)) - // FWF FUNDREF Identifier 501100002428 - if (funderIdentifier.getIdentifier() != null) - if (funderIdentifier.getIdentifier().equals("501100002428")) - return exportFWFTemplate.exportTemplate(dmpId); - } - - // default export science europe template - return exportScienceEuropeTemplate.exportTemplate(dmpId); + return exportTemplateByType(dmpId, templateSelectorService.selectTemplate(dmpService.getDmpById(dmpId))); } public XWPFDocument exportTemplateByType(long dmpId, ETemplateType type) { diff --git a/src/main/java/at/ac/tuwien/damap/conversion/TemplateSelectorService.java b/src/main/java/at/ac/tuwien/damap/conversion/TemplateSelectorService.java new file mode 100644 index 00000000..dc39a20b --- /dev/null +++ b/src/main/java/at/ac/tuwien/damap/conversion/TemplateSelectorService.java @@ -0,0 +1,14 @@ +package at.ac.tuwien.damap.conversion; + +import at.ac.tuwien.damap.enums.ETemplateType; +import at.ac.tuwien.damap.rest.dmp.domain.DmpDO; +import at.ac.tuwien.damap.rest.dmp.domain.IdentifierDO; + +public interface TemplateSelectorService { + + ETemplateType selectTemplate(DmpDO dmpDO); + + boolean isHorizonEuropeTemplate(IdentifierDO identifierDO); + + boolean isFWFTemplate(IdentifierDO identifierDO); +} diff --git a/src/main/java/at/ac/tuwien/damap/conversion/TemplateSelectorServiceImpl.java b/src/main/java/at/ac/tuwien/damap/conversion/TemplateSelectorServiceImpl.java new file mode 100644 index 00000000..90fbb5eb --- /dev/null +++ b/src/main/java/at/ac/tuwien/damap/conversion/TemplateSelectorServiceImpl.java @@ -0,0 +1,43 @@ +package at.ac.tuwien.damap.conversion; + +import at.ac.tuwien.damap.enums.EFunderIds; +import at.ac.tuwien.damap.enums.EIdentifierType; +import at.ac.tuwien.damap.enums.ETemplateType; +import at.ac.tuwien.damap.rest.dmp.domain.DmpDO; +import at.ac.tuwien.damap.rest.dmp.domain.IdentifierDO; +import io.quarkus.arc.DefaultBean; +import lombok.extern.jbosslog.JBossLog; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +@DefaultBean +@JBossLog +public class TemplateSelectorServiceImpl implements TemplateSelectorService{ + @Override + public ETemplateType selectTemplate(DmpDO dmpDO) { + if (dmpDO.getProject() != null && dmpDO.getProject().getFunding() != null) { + IdentifierDO funderIdentifier = dmpDO.getProject().getFunding().getFunderId(); + if (funderIdentifier != null && EIdentifierType.getFunderIdentifierTypeList().contains(funderIdentifier.getType())) { + if (isHorizonEuropeTemplate(funderIdentifier)) { + return ETemplateType.HORIZON_EUROPE; + } + if (isFWFTemplate(funderIdentifier)) { + return ETemplateType.FWF; + } + } + } + // default export science europe template + return ETemplateType.SCIENCE_EUROPE; + } + + @Override + public boolean isHorizonEuropeTemplate(IdentifierDO identifierDO) { + return (EFunderIds.getEUFunderIds().contains(identifierDO.getIdentifier())); + } + + @Override + public boolean isFWFTemplate(IdentifierDO identifierDO) { + return (EFunderIds.getFWFFunderIds().contains(identifierDO.getIdentifier())); + } +} diff --git a/src/main/java/at/ac/tuwien/damap/enums/EFunderIds.java b/src/main/java/at/ac/tuwien/damap/enums/EFunderIds.java new file mode 100644 index 00000000..ca2ffa4d --- /dev/null +++ b/src/main/java/at/ac/tuwien/damap/enums/EFunderIds.java @@ -0,0 +1,42 @@ +package at.ac.tuwien.damap.enums; + +import java.util.ArrayList; +import java.util.List; + +public enum EFunderIds { + + EU_FUNDREF_ID("501100000780"), + EU_ROR_ID("https://ror.org/032s10s29"), + EU_ISNI_ID("0000 0004 6090 9785"), + + FWF_FUNDREF_ID("501100002428"), + FWF_ROR_ID("https://ror.org/013tf3c58"), + FWF_ISNI_ID("0000 0001 1091 8438"); + + + private final String funderId; + + private static final List EUFunderIds = new ArrayList<>(); + private static final List FWFFunderIds = new ArrayList<>(); + + static { + EUFunderIds.add(EU_FUNDREF_ID.funderId); + EUFunderIds.add(EU_ROR_ID.funderId); + EUFunderIds.add(EU_ISNI_ID.funderId); + + FWFFunderIds.add(FWF_FUNDREF_ID.funderId); + FWFFunderIds.add(FWF_ROR_ID.funderId); + FWFFunderIds.add(FWF_ISNI_ID.funderId); + } + + EFunderIds(String funderId) { + this.funderId = funderId; + } + + public static List getEUFunderIds() {return EUFunderIds;} + public static List getFWFFunderIds() {return FWFFunderIds;} + @Override + public String toString() { + return funderId; + } +} diff --git a/src/main/java/at/ac/tuwien/damap/enums/EIdentifierType.java b/src/main/java/at/ac/tuwien/damap/enums/EIdentifierType.java index d04163ab..1aca1e84 100644 --- a/src/main/java/at/ac/tuwien/damap/enums/EIdentifierType.java +++ b/src/main/java/at/ac/tuwien/damap/enums/EIdentifierType.java @@ -38,6 +38,8 @@ public enum EIdentifierType { funderIdentifierType.add(EIdentifierType.FUNDREF); funderIdentifierType.add(EIdentifierType.URL); + funderIdentifierType.add(EIdentifierType.ROR); + funderIdentifierType.add(EIdentifierType.ISNI); funderIdentifierType.add(EIdentifierType.OTHER); grantIdentifierType.add(EIdentifierType.URL); @@ -55,19 +57,19 @@ public List getPersonIdentifierTypeList() { return personIdentifierType; } - public List getDatasetIdentifierTypeList() { + public static List getDatasetIdentifierTypeList() { return datasetIdentifierType; } - public List getFunderIdentifierTypeList() { + public static List getFunderIdentifierTypeList() { return funderIdentifierType; } - public List getGrantIdentifierTypeList() { + public static List getGrantIdentifierTypeList() { return grantIdentifierType; } - public List getMetadataIdentifierTypeList() { + public static List getMetadataIdentifierTypeList() { return metadataIdentifierType; } diff --git a/src/main/java/at/ac/tuwien/damap/rest/projects/MockProjectRestService.java b/src/main/java/at/ac/tuwien/damap/rest/projects/MockProjectRestService.java index 5e601603..21ecc2e8 100644 --- a/src/main/java/at/ac/tuwien/damap/rest/projects/MockProjectRestService.java +++ b/src/main/java/at/ac/tuwien/damap/rest/projects/MockProjectRestService.java @@ -26,7 +26,7 @@ public interface MockProjectRestService { @GET @Path("/projects") - List getRecommended(@QueryParam("title_like") @DefaultValue("recommend") String title); + List getRecommended(@QueryParam("description_like") @DefaultValue("recommend") String description); @GET @Path("/project-supplement") diff --git a/src/test/java/at/ac/tuwien/damap/conversion/TemplateSelectorServiceImplTest.java b/src/test/java/at/ac/tuwien/damap/conversion/TemplateSelectorServiceImplTest.java new file mode 100644 index 00000000..7fa50001 --- /dev/null +++ b/src/test/java/at/ac/tuwien/damap/conversion/TemplateSelectorServiceImplTest.java @@ -0,0 +1,82 @@ +package at.ac.tuwien.damap.conversion; + +import at.ac.tuwien.damap.TestSetup; +import at.ac.tuwien.damap.enums.ETemplateType; +import at.ac.tuwien.damap.rest.dmp.domain.DmpDO; +import at.ac.tuwien.damap.rest.dmp.domain.FundingDO; +import at.ac.tuwien.damap.rest.dmp.domain.ProjectDO; +import at.ac.tuwien.damap.util.MockDmpService; +import at.ac.tuwien.damap.util.TestDOFactory; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.security.TestSecurity; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import javax.inject.Inject; + +@QuarkusTest +class TemplateSelectorServiceImplTest extends TestSetup { + + @Inject + TestDOFactory testDOFactory; + + @Inject + MockDmpService dmpService; + + @Inject + TemplateSelectorServiceImpl templateSelectorService; + + @Test + @TestSecurity(authorizationEnabled = false) + void givenDmpHasUnknownFunding_whenTemplateIsSelected_thenShouldReturnScienceEurope() { + DmpDO testDMP = testDOFactory.createDmp(this.toString(), true); + + ProjectDO project = testDMP.getProject(); + project.setUniversityId("123456UnknwonFunder"); + project.setDescription("Test unknown funder."); + FundingDO fundingDO = project.getFunding(); + fundingDO.getFunderId().setIdentifier("501100004955"); + project.setFunding(fundingDO); + + testDMP.setProject(project); + DmpDO updatedDMP = dmpService.update(testDMP); + + Assertions.assertEquals(ETemplateType.SCIENCE_EUROPE, templateSelectorService.selectTemplate(updatedDMP)); + } + + @Test + @TestSecurity(authorizationEnabled = false) + void givenDmpHasFWFFunding_whenTemplateIsSelected_thenShouldReturnFWF() { + DmpDO testDMP = testDOFactory.createDmp(this.toString(), true); + + ProjectDO project = testDMP.getProject(); + project.setUniversityId("123456FWFFunder"); + project.setDescription("Test FWF funder."); + FundingDO fundingDO = project.getFunding(); + fundingDO.getFunderId().setIdentifier("501100002428"); + project.setFunding(fundingDO); + + testDMP.setProject(project); + DmpDO updatedDMP = dmpService.update(testDMP); + + Assertions.assertEquals(ETemplateType.FWF, templateSelectorService.selectTemplate(updatedDMP)); + } + + @Test + @TestSecurity(authorizationEnabled = false) + void givenDmpHasUnknownFunding_whenTemplateIsSelected_thenShouldReturnHorizonEurope() { + DmpDO testDMP = testDOFactory.createDmp(this.toString(), true); + + ProjectDO project = testDMP.getProject(); + project.setUniversityId("123456HEFunder"); + project.setDescription("Test HE funder."); + FundingDO fundingDO = project.getFunding(); + fundingDO.getFunderId().setIdentifier("501100000780"); + project.setFunding(fundingDO); + + testDMP.setProject(project); + DmpDO updatedDMP = dmpService.update(testDMP); + + Assertions.assertEquals(ETemplateType.HORIZON_EUROPE, templateSelectorService.selectTemplate(updatedDMP)); + } +}