diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/configuration/zenodo/ZenodoProperties.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/configuration/zenodo/ZenodoProperties.java index cc4cc35..85bd543 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/configuration/zenodo/ZenodoProperties.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/configuration/zenodo/ZenodoProperties.java @@ -1,5 +1,6 @@ package eu.eudat.depositinterface.zenodorepository.configuration.zenodo; +import eu.eudat.depositinterface.enums.DepositType; import eu.eudat.depositinterface.repository.RepositoryDepositConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConstructorBinding; @@ -29,7 +30,7 @@ public class ZenodoProperties { } public static class ZenodoConfig extends RepositoryDepositConfiguration { - private final int depositType; + private final DepositType depositType; private final String repositoryId; private final String accessToken; private final String repositoryUrl; @@ -47,7 +48,7 @@ public class ZenodoProperties { private final String domain; @ConstructorBinding - public ZenodoConfig(int depositType, String repositoryId, String accessToken, String repositoryUrl, String repositoryAuthorizationUrl, String repositoryRecordUrl, String repositoryAccessTokenUrl, String repositoryClientId, String repositoryClientSecret, String redirectUri, boolean hasLogo, String logo, String doiFunder, String community, String affiliation, String domain) { + public ZenodoConfig(DepositType depositType, String repositoryId, String accessToken, String repositoryUrl, String repositoryAuthorizationUrl, String repositoryRecordUrl, String repositoryAccessTokenUrl, String repositoryClientId, String repositoryClientSecret, String redirectUri, boolean hasLogo, String logo, String doiFunder, String community, String affiliation, String domain) { this.depositType = depositType; this.repositoryId = repositoryId; this.accessToken = accessToken; @@ -66,7 +67,7 @@ public class ZenodoProperties { this.domain = domain; } - public int getDepositType() { + public DepositType getDepositType() { return depositType; } diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/enums/ZenodoAccessRight.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/enums/ZenodoAccessRight.java new file mode 100644 index 0000000..fe58d5d --- /dev/null +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/enums/ZenodoAccessRight.java @@ -0,0 +1,36 @@ +package eu.eudat.depositinterface.zenodorepository.enums; + +import com.fasterxml.jackson.annotation.JsonValue; +import eu.eudat.depositinterface.enums.EnumUtils; +import eu.eudat.depositinterface.enums.EnumValueProvider; +import eu.eudat.depositinterface.enums.FieldType; + +import java.util.Map; + +public enum ZenodoAccessRight implements EnumValueProvider { + RESTRICTED(Names.Restricted), EMBARGOED(Names.Embargoed), OPEN(Names.Open); + + private final String value; + + public static class Names { + public static final String Restricted = "restricted"; + public static final String Embargoed = "embargoed"; + public static final String Open = "open"; + } + + ZenodoAccessRight(String value) { + this.value = value; + } + + @Override + @JsonValue + public String getValue() { + return value; + } + + private static final Map map = EnumUtils.getEnumValueMap(ZenodoAccessRight.class); + + public static ZenodoAccessRight of(String i) { + return map.get(i); + } +} diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/mapper/DMPToZenodoMapper.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/mapper/ZenodoBuilderMapper.java similarity index 53% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/mapper/DMPToZenodoMapper.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/mapper/ZenodoBuilderMapper.java index 4a2e72e..af681d9 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/mapper/DMPToZenodoMapper.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/mapper/ZenodoBuilderMapper.java @@ -2,18 +2,21 @@ package eu.eudat.depositinterface.zenodorepository.mapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import eu.eudat.depositinterface.enums.FieldType; +import eu.eudat.depositinterface.enums.DmpAccessType; +import eu.eudat.depositinterface.enums.DmpUserRole; +import eu.eudat.depositinterface.enums.ReferenceType; import eu.eudat.depositinterface.models.*; +import eu.eudat.depositinterface.models.reference.FieldDepositModel; import eu.eudat.depositinterface.zenodorepository.configuration.funder.FunderProperties; import eu.eudat.depositinterface.zenodorepository.configuration.identifier.IdentifierProperties; import eu.eudat.depositinterface.zenodorepository.configuration.pid.PidProperties; import eu.eudat.depositinterface.zenodorepository.configuration.zenodo.ZenodoProperties; -import eu.eudat.depositinterface.zenodorepository.models.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import eu.eudat.depositinterface.zenodorepository.enums.ZenodoAccessRight; +import eu.eudat.depositinterface.zenodorepository.model.*; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.time.Instant; @@ -21,32 +24,58 @@ import java.util.*; import java.util.stream.Collectors; @Component -public class DMPToZenodoMapper { - - private static final Logger logger = LoggerFactory.getLogger(DMPToZenodoMapper.class); - private static final ObjectMapper objectMapper = new ObjectMapper(); +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class ZenodoBuilderMapper { private final PidProperties pidProperties; private final IdentifierProperties identifierProperties; private final FunderProperties funderProperties; @Autowired - public DMPToZenodoMapper(PidProperties pidProperties, IdentifierProperties identifierProperties, FunderProperties funderProperties){ + public ZenodoBuilderMapper(PidProperties pidProperties, IdentifierProperties identifierProperties, FunderProperties funderProperties){ this.pidProperties = pidProperties; this.identifierProperties = identifierProperties; this.funderProperties = funderProperties; } + public ZenodoDeposit build(DmpDepositModel dmp, ZenodoProperties.ZenodoConfig zenodoConfig) throws JsonProcessingException { + ZenodoDeposit deposit = new ZenodoDeposit(); + this.applyZenodoRelator(dmp, deposit); + deposit.getMetadata().setTitle(dmp.getLabel()); + deposit.getMetadata().setUploadType("publication"); + deposit.getMetadata().setPublicationType("datamanagementplan"); + deposit.getMetadata().setDescription((dmp.getDescription() != null && !dmp.getDescription().isEmpty() ? dmp.getDescription() : "

")); + deposit.getMetadata().setVersion(String.valueOf(dmp.getVersion())); + String zenodoCommunity = zenodoConfig.getCommunity(); + if(zenodoCommunity != null && !zenodoCommunity.isEmpty()) { + ZenodoComunity community = new ZenodoComunity(); + community.setIdentifier(zenodoCommunity); + deposit.getMetadata().getCommunities().add(community); + } + this.applyAccessRight(dmp, deposit); + this.applyIsIdenticalTo(dmp, deposit, zenodoConfig); + this.applyLicenses(dmp, deposit); + this.applyResearchers(dmp, deposit); + this.applyGrants(dmp, deposit); + this.applyContributors(dmp, deposit, zenodoConfig); + this.applyCreators(dmp, deposit, zenodoConfig); + + + + return deposit; + } + private List findSchemanticValues(String relatedId, List fields){ return fields.stream().filter(f -> f.getSchematics().contains(relatedId)).collect(Collectors.toList()); } - private Set extractSchemanticValues(List fields, List acceptedPidTypes) throws JsonProcessingException{ + private Set extractSchematicValues(List fields, List acceptedPidTypes) throws JsonProcessingException{ + ObjectMapper objectMapper = new ObjectMapper(); Set values = new HashSet<>(); for(DatasetFieldsDepositModel field: fields){ String value = (String) field.getValue(); if(value != null && !value.isEmpty()) { - switch (FieldType.fromName(field.getRenderStyleType())) { + switch (field.getFieldType()) { case FREE_TEXT: case TEXT_AREA: case RICH_TEXT_AREA: @@ -54,7 +83,8 @@ public class DMPToZenodoMapper { case DATE_PICKER: values.add(value); break; - case COMBO_BOX: + case AUTO_COMPLETE: + case WORD_LIST: if (field.isMultiple()) { List selected = objectMapper.readValue(value, new TypeReference>() {}); values.addAll(selected); @@ -63,7 +93,6 @@ public class DMPToZenodoMapper { values.add(value); } break; - case REGISTRIES: case SERVICES: case EXTERNAL_DATASETS: case DATA_REPOSITORIES: @@ -120,155 +149,185 @@ public class DMPToZenodoMapper { } return values; } - - public ZenodoDeposit fromDMP(DMPDepositModel dmp, ZenodoProperties.ZenodoConfig zenodoConfig) throws JsonProcessingException { - Map extraProperties = dmp.getExtraProperties() != null ? new org.json.JSONObject(dmp.getExtraProperties()).toMap() : new HashMap<>(); - ZenodoDeposit deposit = new ZenodoDeposit(); - - Map schematicsMap = new HashMap<>(); - - List relatedIdentifiers = new ArrayList<>(); - List communities = new ArrayList<>(); - List contributors = new ArrayList<>(); - List creators = new ArrayList<>(); - List grants = new ArrayList<>(); - List keywords = new ArrayList<>(); - List references = new ArrayList<>(); - + + private List getReferenceDepositModelOfType(DmpDepositModel dmp, ReferenceType referenceType){ + if (dmp.getReferences() == null) return new ArrayList<>(); + return dmp.getReferences().stream().filter(x -> x.getType().equals(referenceType)).toList(); + } + + private void applyZenodoRelator(DmpDepositModel dmp, ZenodoDeposit deposit) throws JsonProcessingException { + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + List acceptedPidTypes = this.pidProperties.getAcceptedTypes(); - - for(DatasetDepositModel dataset: dmp.getDatasets()){ - + List relatedIdentifiers = new ArrayList<>(); + for(DescriptionDepositModel descriptionDepositModel: dmp.getDescriptions()){ for(String relatedId: this.identifierProperties.getRelated()){ - - List fields = findSchemanticValues(relatedId, dataset.getFields()); - Set values = extractSchemanticValues(fields, acceptedPidTypes); + List fields = findSchemanticValues(relatedId, descriptionDepositModel.getFields()); + Set values = extractSchematicValues(fields, acceptedPidTypes); for(String value: values){ ZenodoRelator relator = new ZenodoRelator(); relator.setRelation(relatedId.substring(relatedId.lastIndexOf(".") + 1)); relator.setIdentifier(value); relatedIdentifiers.add(relator); } - } } - schematicsMap.put("related_identifiers", relatedIdentifiers); - schematicsMap.put("communities", communities); - schematicsMap.put("contributors", contributors); - schematicsMap.put("creators", creators); - schematicsMap.put("grants", grants); - schematicsMap.put("keywords", keywords); - schematicsMap.put("references", references); - - String schematicsString = objectMapper.writeValueAsString(schematicsMap); - objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); - ZenodoDepositMetadata metadata = objectMapper.readValue(schematicsString, new TypeReference(){}); - - deposit.setMetadata(metadata); - deposit.getMetadata().setTitle(dmp.getLabel()); - deposit.getMetadata().setUploadType("publication"); - deposit.getMetadata().setPublicationType("datamanagementplan"); - deposit.getMetadata().setDescription((dmp.getDescription() != null && !dmp.getDescription().isEmpty() ? dmp.getDescription() : "

")); - deposit.getMetadata().setVersion(String.valueOf(dmp.getVersion())); - String zenodoCommunity = zenodoConfig.getCommunity(); - if(zenodoCommunity != null && !zenodoCommunity.isEmpty()) { - ZenodoComunity community = new ZenodoComunity(); - community.setIdentifier(zenodoCommunity); - deposit.getMetadata().getCommunities().add(community); - } - if (extraProperties.get("visible") == null) { + deposit.getMetadata().setRelatedIdentifiers(relatedIdentifiers); + } + + private void applyAccessRight(DmpDepositModel dmp, ZenodoDeposit deposit){ + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + + if (dmp.getAccessType() == null) { deposit.getMetadata().setAccessRight(ZenodoAccessRight.RESTRICTED); deposit.getMetadata().setAccessConditions(""); } else { - if (((Boolean) extraProperties.get("visible"))) { - Instant publicationDate = Instant.parse(extraProperties.get("publicDate").toString()); + if (dmp.getAccessType().equals(DmpAccessType.Public)) { + Instant publicationDate = dmp.getFinalizedAt(); + if (publicationDate == null) publicationDate = Instant.now().minusSeconds(1); + if (publicationDate.isBefore(Instant.now())) { deposit.getMetadata().setAccessRight(ZenodoAccessRight.OPEN); } else { deposit.getMetadata().setAccessRight(ZenodoAccessRight.EMBARGOED); deposit.getMetadata().setEmbargoDate(publicationDate.toString()); } - - if (extraProperties.get("license") != null) { - deposit.getMetadata().setLicense(((Map) extraProperties.get("license")).get("pid").toString()); - } } else { deposit.getMetadata().setAccessRight(ZenodoAccessRight.RESTRICTED); deposit.getMetadata().setAccessConditions(""); } } - if (dmp.isPublic()) { + } + + private void applyIsIdenticalTo(DmpDepositModel dmp, ZenodoDeposit deposit, ZenodoProperties.ZenodoConfig zenodoConfig){ + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + + if (dmp.getAccessType().equals(DmpAccessType.Public)) { ZenodoRelator relator = new ZenodoRelator(); relator.setIdentifier(zenodoConfig.getDomain() + "/external/zenodo/" + dmp.getId().toString()); relator.setRelation("isIdenticalTo"); + if (deposit.getMetadata().getRelatedIdentifiers() == null)deposit.getMetadata().setRelatedIdentifiers(new ArrayList<>()); deposit.getMetadata().getRelatedIdentifiers().add(relator); } - String zenodoAffiliation = zenodoConfig.getAffiliation(); - List contributors1 = dmp.getUsers().stream().map(userDMP -> { - ZenodoContributor contributor = new ZenodoContributor(); - contributor.setName(userDMP.getUser().getName()); - contributor.setType("ProjectMember"); - if (dmp.getOrganisations() != null && !dmp.getOrganisations().isEmpty()) { - contributor.setAffiliation(dmp.getOrganisations() - .stream().map(OrganisationDepositModel::getLabel).collect(Collectors.joining(", "))); - } else { - if(zenodoAffiliation != null && !zenodoAffiliation.isEmpty()) { - contributor.setAffiliation(zenodoAffiliation); + } + + private void applyLicenses(DmpDepositModel dmp, ZenodoDeposit deposit){ + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + + List dmpLicenses = this.getReferenceDepositModelOfType(dmp, ReferenceType.Licenses); + if (dmpLicenses != null && !dmpLicenses.isEmpty()) { + for (ReferenceDepositModel dmpLicense : dmpLicenses) { + String pid = dmpLicense.getDefinition().getFields().stream().filter(x-> "pid".equalsIgnoreCase(x.getCode())).map(FieldDepositModel::getValue).findFirst().orElse(null); + if (pid != null && !pid.isBlank()) { + deposit.getMetadata().setLicense(pid); + break; } } - return contributor; - }).collect(Collectors.toList()); + } + } - List researchers = dmp.getResearchers().stream().map(researcher -> { - ZenodoContributor contributor = new ZenodoContributor(); - contributor.setName(researcher.getLabel()); - contributor.setType("Researcher"); - String referenceHead = researcher.getReference().split(":")[0]; - String referenceTail = researcher.getReference().replace(referenceHead + ":", ""); - contributor.setAffiliation(referenceHead); - if (referenceHead.equalsIgnoreCase("ORCID")) { - contributor.setOrcid(referenceTail); + private void applyResearchers(DmpDepositModel dmp, ZenodoDeposit deposit){ + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + + List researchers = new ArrayList<>(); + List dmpResearchers = this.getReferenceDepositModelOfType(dmp, ReferenceType.Researcher); + if (dmpResearchers != null && !dmpResearchers.isEmpty()) { + for (ReferenceDepositModel researcher : dmpResearchers) { + ZenodoContributor contributor = new ZenodoContributor(); + contributor.setName(researcher.getLabel()); + contributor.setType("Researcher"); + contributor.setAffiliation(researcher.getSource()); + if (researcher.getSource().equalsIgnoreCase("ORCID")) { + contributor.setOrcid(researcher.getReference()); + } + researchers.add(contributor); } - return contributor; - }).collect(Collectors.toList()); + } + + if (deposit.getMetadata().getContributors() == null)deposit.getMetadata().setContributors(new ArrayList<>()); - deposit.getMetadata().getContributors().addAll(contributors1); deposit.getMetadata().getContributors().addAll(researchers); + } - if (dmp.getGrant() != null) { - if (dmp.getGrant().getReference() == null) { - dmp.getGrant().setReference("dmp:" + dmp.getGrant().getId()); - } - String grantReferenceHead = dmp.getGrant().getReference().split(":")[0]; - if (grantReferenceHead.equals("openaire")) { - String grantReferenceTail = dmp.getGrant().getReference().split(":")[3]; + private void applyGrants(DmpDepositModel dmp, ZenodoDeposit deposit){ + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + List dmpGrants = this.getReferenceDepositModelOfType(dmp, ReferenceType.Grants); + List dmpFunders = this.getReferenceDepositModelOfType(dmp, ReferenceType.Funder); + + + if (dmpGrants != null && !dmpGrants.isEmpty()) { + ReferenceDepositModel depositGrant = dmpGrants.stream().filter(x-> x.getSource().equals("openaire")).findFirst().orElse(null); + + if (depositGrant != null) { + String grantReferenceTail = depositGrant.getReference().split(":")[2]; List doiFunders = this.funderProperties.getAvailable(); - if (dmp.getGrant().getFunder() != null && dmp.getGrant().getFunder().getLabel() != null) { + if (dmpFunders != null && !dmpFunders.isEmpty()) { + ReferenceDepositModel depositFunder = dmpFunders.get(0); FunderProperties.DOIFunder doiFunder = doiFunders.stream() - .filter(doiFunder1 -> dmp.getGrant().getFunder().getLabel().contains(doiFunder1.getFunder()) || doiFunder1.getFunder().contains(dmp.getGrant().getFunder().getLabel())) + .filter(doiFunder1 -> depositFunder.getLabel().contains(doiFunder1.getFunder()) || doiFunder1.getFunder().contains(depositFunder.getLabel())) .findFirst().orElse(null); if (doiFunder != null) { String finalId = doiFunder.getDoi() + "::" + grantReferenceTail; ZenodoGrant grant = new ZenodoGrant(); grant.setId(finalId); + if (deposit.getMetadata().getGrants() == null)deposit.getMetadata().setGrants(new ArrayList<>()); deposit.getMetadata().getGrants().add(grant); } } } } + } + + private void applyContributors(DmpDepositModel dmp, ZenodoDeposit deposit, ZenodoProperties.ZenodoConfig zenodoConfig){ + if (dmp.getUsers() == null) return; + + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + + List dmpOrganizations = this.getReferenceDepositModelOfType(dmp, ReferenceType.Organizations); + String zenodoAffiliation = zenodoConfig.getAffiliation(); + + List contributors = new ArrayList<>(); + for (UserDmpDepositModel userDMP: dmp.getUsers()) { + ZenodoContributor contributor = new ZenodoContributor(); + contributor.setName(userDMP.getUser().getName()); + contributor.setType("ProjectMember"); + if (dmpOrganizations != null && !dmpOrganizations.isEmpty()) { + contributor.setAffiliation(dmpOrganizations.stream().map(ReferenceDepositModel::getLabel).collect(Collectors.joining(", "))); + } else { + if(zenodoAffiliation != null && !zenodoAffiliation.isEmpty()) { + contributor.setAffiliation(zenodoAffiliation); + } + } + contributors.add(contributor); + } + if (deposit.getMetadata().getContributors() == null)deposit.getMetadata().setContributors(new ArrayList<>()); + + deposit.getMetadata().getContributors().addAll(contributors); + + } + + private void applyCreators(DmpDepositModel dmp, ZenodoDeposit deposit, ZenodoProperties.ZenodoConfig zenodoConfig){ + if (dmp.getUsers() == null) return; + + if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata()); + + List dmpOrganizations = this.getReferenceDepositModelOfType(dmp, ReferenceType.Organizations); + String zenodoAffiliation = zenodoConfig.getAffiliation(); + ZenodoCreator creator = new ZenodoCreator(); - creator.setName(dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(UserDMPDepositModel.UserDMPRoles.OWNER.getValue())).findFirst().get().getUser().getName()); - if (dmp.getOrganisations() != null && !dmp.getOrganisations().isEmpty()) { - creator.setAffiliation(dmp.getOrganisations() - .stream().map(OrganisationDepositModel::getLabel).collect(Collectors.joining(", "))); + UserDmpDepositModel dmpDepositModel = dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(DmpUserRole.Owner)).findFirst().orElse(null); + if (dmpDepositModel == null || dmpDepositModel.getUser() == null) return; + + creator.setName(dmpDepositModel.getUser().getName()); + if (dmpOrganizations != null && !dmpOrganizations.isEmpty()) { + creator.setAffiliation(dmpOrganizations.stream().map(ReferenceDepositModel::getLabel).collect(Collectors.joining(", "))); } else { if(zenodoAffiliation != null && !zenodoAffiliation.isEmpty()) { creator.setAffiliation(zenodoAffiliation); } } + if (deposit.getMetadata().getCreators() == null)deposit.getMetadata().setCreators(new ArrayList<>()); deposit.getMetadata().getCreators().add(creator); - - return deposit; } } diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoComunity.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoComunity.java similarity index 88% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoComunity.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoComunity.java index a04a19c..28d19a8 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoComunity.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoComunity.java @@ -1,4 +1,4 @@ -package eu.eudat.depositinterface.zenodorepository.models; +package eu.eudat.depositinterface.zenodorepository.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoContributor.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoContributor.java similarity index 93% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoContributor.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoContributor.java index c172d52..7e7f64b 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoContributor.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoContributor.java @@ -1,4 +1,4 @@ -package eu.eudat.depositinterface.zenodorepository.models; +package eu.eudat.depositinterface.zenodorepository.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoCreator.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoCreator.java similarity index 92% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoCreator.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoCreator.java index 2cf5756..8d78ed2 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoCreator.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoCreator.java @@ -1,4 +1,4 @@ -package eu.eudat.depositinterface.zenodorepository.models; +package eu.eudat.depositinterface.zenodorepository.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoDeposit.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoDeposit.java similarity index 88% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoDeposit.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoDeposit.java index c27c253..a76668b 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoDeposit.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoDeposit.java @@ -1,4 +1,4 @@ -package eu.eudat.depositinterface.zenodorepository.models; +package eu.eudat.depositinterface.zenodorepository.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoDepositMetadata.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoDepositMetadata.java similarity index 97% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoDepositMetadata.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoDepositMetadata.java index 87c3596..140cef9 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoDepositMetadata.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoDepositMetadata.java @@ -1,8 +1,9 @@ -package eu.eudat.depositinterface.zenodorepository.models; +package eu.eudat.depositinterface.zenodorepository.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import eu.eudat.depositinterface.zenodorepository.enums.ZenodoAccessRight; import java.util.List; diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoGrant.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoGrant.java similarity index 86% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoGrant.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoGrant.java index d37c657..88da2cf 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoGrant.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoGrant.java @@ -1,4 +1,4 @@ -package eu.eudat.depositinterface.zenodorepository.models; +package eu.eudat.depositinterface.zenodorepository.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoRelator.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoRelator.java similarity index 91% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoRelator.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoRelator.java index 8196f63..36ef0c4 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoRelator.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/model/ZenodoRelator.java @@ -1,4 +1,4 @@ -package eu.eudat.depositinterface.zenodorepository.models; +package eu.eudat.depositinterface.zenodorepository.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoAccessRight.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoAccessRight.java deleted file mode 100644 index 0cf6cd4..0000000 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/models/ZenodoAccessRight.java +++ /dev/null @@ -1,18 +0,0 @@ -package eu.eudat.depositinterface.zenodorepository.models; - -import com.fasterxml.jackson.annotation.JsonValue; - -public enum ZenodoAccessRight { - RESTRICTED("restricted"), EMBARGOED("embargoed"), OPEN("open"); - - private final String value; - - ZenodoAccessRight(String value) { - this.value = value; - } - - @JsonValue - public String getValue() { - return value; - } -} diff --git a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/interfaces/ZenodoDeposit.java b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/service/zenodo/ZenodoDepositService.java similarity index 81% rename from core/src/main/java/eu/eudat/depositinterface/zenodorepository/interfaces/ZenodoDeposit.java rename to core/src/main/java/eu/eudat/depositinterface/zenodorepository/service/zenodo/ZenodoDepositService.java index 16e0b0c..fe4453f 100644 --- a/core/src/main/java/eu/eudat/depositinterface/zenodorepository/interfaces/ZenodoDeposit.java +++ b/core/src/main/java/eu/eudat/depositinterface/zenodorepository/service/zenodo/ZenodoDepositService.java @@ -1,12 +1,12 @@ -package eu.eudat.depositinterface.zenodorepository.interfaces; +package eu.eudat.depositinterface.zenodorepository.service.zenodo; import com.fasterxml.jackson.databind.ObjectMapper; -import eu.eudat.depositinterface.models.DMPDepositModel; +import eu.eudat.depositinterface.models.DmpDepositModel; import eu.eudat.depositinterface.models.FileEnvelope; import eu.eudat.depositinterface.repository.RepositoryDeposit; import eu.eudat.depositinterface.repository.RepositoryDepositConfiguration; import eu.eudat.depositinterface.zenodorepository.configuration.zenodo.ZenodoProperties; -import eu.eudat.depositinterface.zenodorepository.mapper.DMPToZenodoMapper; +import eu.eudat.depositinterface.zenodorepository.mapper.ZenodoBuilderMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -23,36 +23,35 @@ import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; import java.io.*; -import java.nio.file.Files; import java.util.*; import java.util.stream.Collectors; @Component -public class ZenodoDeposit implements RepositoryDeposit { - private static final Logger logger = LoggerFactory.getLogger(ZenodoDeposit.class); +public class ZenodoDepositService implements RepositoryDeposit { + private static final Logger logger = LoggerFactory.getLogger(ZenodoDepositService.class); private static final ObjectMapper objectMapper = new ObjectMapper(); private final ZenodoProperties zenodoProperties; - private final DMPToZenodoMapper mapper; + private final ZenodoBuilderMapper mapper; @Autowired - public ZenodoDeposit(ZenodoProperties zenodoProperties, DMPToZenodoMapper mapper){ + public ZenodoDepositService(ZenodoProperties zenodoProperties, ZenodoBuilderMapper mapper){ this.zenodoProperties = zenodoProperties; this.mapper = mapper; } @Override - public String deposit(String repositoryId, DMPDepositModel dmpDepositModel, String zenodoToken) throws Exception { + public String deposit(String repositoryId, DmpDepositModel dmpDepositModel, String zenodoToken) throws Exception { - RepositoryDepositConfiguration conf = this.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null); + RepositoryDepositConfiguration repositoryDepositConfiguration = this.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null); - if(conf != null) { + if(repositoryDepositConfiguration != null) { if (zenodoToken == null || zenodoToken.isEmpty()) { - zenodoToken = conf.getAccessToken(); + zenodoToken = repositoryDepositConfiguration.getAccessToken(); } - String zenodoUrl = conf.getRepositoryUrl(); + String zenodoUrl = repositoryDepositConfiguration.getRepositoryUrl(); // First step, post call to Zenodo, to create the entry. WebClient zenodoClient = WebClient.builder().build(); @@ -61,7 +60,7 @@ public class ZenodoDeposit implements RepositoryDeposit { headers.setContentType(MediaType.APPLICATION_JSON); ZenodoProperties.ZenodoConfig zenodoConfig = this.zenodoProperties.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null); - eu.eudat.depositinterface.zenodorepository.models.ZenodoDeposit deposit = mapper.fromDMP(dmpDepositModel, zenodoConfig); + eu.eudat.depositinterface.zenodorepository.model.ZenodoDeposit deposit = mapper.build(dmpDepositModel, zenodoConfig); Map createResponse; LinkedHashMap links; @@ -143,20 +142,19 @@ public class ZenodoDeposit implements RepositoryDeposit { String addFileUrl = links.get("bucket") + "/" + pdfEnvelope.getFilename() + "?access_token=" + zenodoToken; zenodoClient.put().uri(addFileUrl) .body(BodyInserters - .fromResource(new ByteArrayResource(Files.readAllBytes(pdfEnvelope.getFile().toPath())))) + .fromResource(new ByteArrayResource(pdfEnvelope.getFile()))) .retrieve().toEntity(Map.class).block(); FileEnvelope rdaJsonEnvelope = dmpDepositModel.getRdaJsonFile(); String jsonFileName = rdaJsonEnvelope.getFilename(); addFileUrl = links.get("bucket") + "/" + jsonFileName + "?access_token=" + zenodoToken; - zenodoClient.put().uri(addFileUrl).headers(httpHeaders -> httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM)).body(BodyInserters.fromResource(new ByteArrayResource(Files.readAllBytes(rdaJsonEnvelope.getFile().toPath())))).retrieve().toEntity(Map.class).block(); + zenodoClient.put().uri(addFileUrl).headers(httpHeaders -> httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM)).body(BodyInserters.fromResource(new ByteArrayResource(rdaJsonEnvelope.getFile()))).retrieve().toEntity(Map.class).block(); if (dmpDepositModel.getSupportingFilesZip() != null) { - File supportinFilesZip = dmpDepositModel.getSupportingFilesZip(); - String supportinFilesZipName = dmpDepositModel.getSupportingFilesZip().getName(); + String supportinFilesZipName = dmpDepositModel.getSupportingFilesZip().getFilename(); addFileUrl = links.get("bucket") + "/" + supportinFilesZipName + "?access_token=" + zenodoToken; - zenodoClient.put().uri(addFileUrl).body(BodyInserters.fromResource(new ByteArrayResource(Files.readAllBytes(supportinFilesZip.toPath())))).retrieve().toEntity(Map.class).block(); + zenodoClient.put().uri(addFileUrl).body(BodyInserters.fromResource(new ByteArrayResource(supportinFilesZipName.getBytes()))).retrieve().toEntity(Map.class).block(); } // Third post call to Zenodo to publish the entry and return the DOI. @@ -179,7 +177,6 @@ public class ZenodoDeposit implements RepositoryDeposit { } private String publish(String publishUrl){ - //RestTemplate restTemplate = new RestTemplate(); WebClient webClient = WebClient.builder().build(); Map publishResponce = webClient.post().uri(publishUrl).bodyValue("").exchangeToMono(mono -> { if (!mono.statusCode().is2xxSuccessful()) { @@ -202,30 +199,25 @@ public class ZenodoDeposit implements RepositoryDeposit { @Override public String authenticate(String repositoryId, String code){ - RepositoryDepositConfiguration conf = this.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null); + RepositoryDepositConfiguration repositoryDepositConfiguration = this.getConfiguration().stream().filter(x -> x.getRepositoryId().equals(repositoryId)).findFirst().orElse(null); - if(conf != null) { + if(repositoryDepositConfiguration != null) { WebClient client = WebClient.builder().defaultHeaders(httpHeaders -> { httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); }).build(); - HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap map = new LinkedMultiValueMap<>(); - map.add("client_id", conf.getRepositoryClientId()); - map.add("client_secret", conf.getRepositoryClientSecret()); + map.add("client_id", repositoryDepositConfiguration.getRepositoryClientId()); + map.add("client_secret", repositoryDepositConfiguration.getRepositoryClientSecret()); map.add("grant_type", "authorization_code"); map.add("code", code); - map.add("redirect_uri", conf.getRedirectUri()); - HttpEntity> request = new HttpEntity<>(map, headers); + map.add("redirect_uri", repositoryDepositConfiguration.getRedirectUri()); try { - Map values = client.post().uri(conf.getRepositoryAccessTokenUrl()).bodyValue(map).retrieve().bodyToMono(Map.class).block(); - Map user = (Map) values.get("user"); - return (String) values.get("access_token"); + Map values = client.post().uri(repositoryDepositConfiguration.getRepositoryAccessTokenUrl()).bodyValue(map).retrieve().bodyToMono(Map.class).block(); + return values != null ? (String) values.get("access_token") : null; } catch (HttpClientErrorException ex) { logger.error(ex.getResponseBodyAsString(), ex); } @@ -244,9 +236,11 @@ public class ZenodoDeposit implements RepositoryDeposit { if(conf.isHasLogo()){ byte[] logo; try { - File logoFile = ResourceUtils.getFile(conf.getLogo()); - InputStream logoStream = new FileInputStream(logoFile); - logo = logoStream.readAllBytes(); + java.io.File logoFile = ResourceUtils.getFile(conf.getLogo()); + if (!logoFile.exists()) return null; + try(InputStream inputStream = new FileInputStream(logoFile)){ + logo = inputStream.readAllBytes(); + }; } catch (IOException e) { throw new RuntimeException(e); } @@ -256,16 +250,13 @@ public class ZenodoDeposit implements RepositoryDeposit { return null; } - private String getUnpublishedDOI(String zenodoUrl, String DOI, String token, Integer version) { + private String getUnpublishedDOI(String zenodoUrl, String doi, String token, Short version) { try { WebClient client = WebClient.builder().build(); - HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - headers.setContentType(MediaType.APPLICATION_JSON); Map createResponse = null; LinkedHashMap links; LinkedHashMap metadata; - String listUrl = zenodoUrl + "deposit/depositions" + "?q=conceptdoi:\"" + DOI + "\"&access_token=" + token; + String listUrl = zenodoUrl + "deposit/depositions" + "?q=conceptdoi:\"" + doi + "\"&access_token=" + token; ResponseEntity> listResponses = client.get().uri(listUrl).retrieve().toEntityList(Map.class).block(); createResponse = listResponses.getBody().get(0); metadata = (LinkedHashMap) createResponse.get("metadata"); diff --git a/web/src/main/java/eu/eudat/deposit/controller/DepositController.java b/web/src/main/java/eu/eudat/deposit/controller/DepositController.java index dc90111..edb961a 100644 --- a/web/src/main/java/eu/eudat/deposit/controller/DepositController.java +++ b/web/src/main/java/eu/eudat/deposit/controller/DepositController.java @@ -1,6 +1,6 @@ package eu.eudat.deposit.controller; -import eu.eudat.depositinterface.models.DMPDepositModel; +import eu.eudat.depositinterface.models.DmpDepositModel; import eu.eudat.depositinterface.repository.RepositoryDeposit; import eu.eudat.depositinterface.repository.RepositoryDepositConfiguration; import org.springframework.beans.factory.annotation.Autowired; @@ -20,7 +20,7 @@ public class DepositController { } @PostMapping("/{id}") - public String deposit(@PathVariable("id") String repositoryId, @RequestBody DMPDepositModel dmpDepositModel, @RequestParam("authToken")String authToken) throws Exception { + public String deposit(@PathVariable("id") String repositoryId, @RequestBody DmpDepositModel dmpDepositModel, @RequestParam("authToken")String authToken) throws Exception { return repositoryDeposit.deposit(repositoryId, dmpDepositModel, authToken); }