Skip to content

Commit

Permalink
[CST-15084] Refactoring of code in order to work with relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattia Vianelli authored and atarix83 committed Sep 25, 2024
1 parent 5080c70 commit 205f326
Show file tree
Hide file tree
Showing 24 changed files with 749 additions and 1,350 deletions.
2 changes: 1 addition & 1 deletion dspace-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>eu.openaire</groupId>
<artifactId>broker-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.dspace.content.integration.crosswalks.CSLItemDataCrosswalk;
import org.dspace.util.SimpleMapConverter;
import org.orcid.jaxb.model.common.CitationType;
import org.orcid.jaxb.model.common.ContributorRole;
Expand Down Expand Up @@ -87,18 +86,6 @@ public class OrcidPatentWorkFieldMapping {
*/
private SimpleMapConverter languageConverter;

private Map<String, CSLItemDataCrosswalk> citationCrosswalks;

private String fundingField;

private String fundingExternalIdType;

private String fundingExternalId;

private String fundingEntityExternalId;

private String fundingUrlField;

public String convertType(String type) {
return typeConverter != null ? typeConverter.getValue(type) : type;
}
Expand Down Expand Up @@ -187,54 +174,6 @@ public void setSubTitleField(String subTitleField) {
this.subTitleField = subTitleField;
}

public Map<String, CSLItemDataCrosswalk> getCitationCrosswalks() {
return citationCrosswalks;
}

public void setCitationCrosswalks(Map<String, CSLItemDataCrosswalk> citationCrosswalks) {
this.citationCrosswalks = citationCrosswalks;
}

public String getFundingField() {
return fundingField;
}

public void setFundingField(String fundingField) {
this.fundingField = fundingField;
}

public String getFundingExternalIdType() {
return fundingExternalIdType;
}

public void setFundingExternalIdType(String fundingExternalIdType) {
this.fundingExternalIdType = fundingExternalIdType;
}

public String getFundingExternalId() {
return fundingExternalId;
}

public void setFundingExternalId(String fundingExternalId) {
this.fundingExternalId = fundingExternalId;
}

public String getFundingEntityExternalId() {
return fundingEntityExternalId;
}

public void setFundingEntityExternalId(String fundingEntityExternalId) {
this.fundingEntityExternalId = fundingEntityExternalId;
}

public String getFundingUrlField() {
return fundingUrlField;
}

public void setFundingUrlField(String fundingUrlField) {
this.fundingUrlField = fundingUrlField;
}

public CitationType getCitationType() {
return citationType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.dspace.content.integration.crosswalks.CSLItemDataCrosswalk;
import org.dspace.util.SimpleMapConverter;
import org.orcid.jaxb.model.common.CitationType;
import org.orcid.jaxb.model.common.ContributorRole;
Expand Down Expand Up @@ -87,18 +85,6 @@ public class OrcidProductWorkFieldMapping {
*/
private SimpleMapConverter languageConverter;

private Map<String, CSLItemDataCrosswalk> citationCrosswalks;

private String fundingField;

private String fundingExternalIdType;

private String fundingExternalId;

private String fundingEntityExternalId;

private String fundingUrlField;

public String convertType(String type) {
return typeConverter != null ? typeConverter.getValue(type) : type;
}
Expand Down Expand Up @@ -187,75 +173,6 @@ public void setSubTitleField(String subTitleField) {
this.subTitleField = subTitleField;
}

public Map<String, CSLItemDataCrosswalk> getCitationCrosswalks() {
return citationCrosswalks;
}

public void setCitationCrosswalks(Map<String, CSLItemDataCrosswalk> citationCrosswalks) {
this.citationCrosswalks = citationCrosswalks;
}

public String getFundingField() {
return fundingField;
}

public void setFundingField(String fundingField) {
this.fundingField = fundingField;
}

public String getFundingExternalIdType() {
return fundingExternalIdType;
}

public void setFundingExternalIdType(String fundingExternalIdType) {
this.fundingExternalIdType = fundingExternalIdType;
}

public String getFundingExternalId() {
return fundingExternalId;
}

public void setFundingExternalId(String fundingExternalId) {
this.fundingExternalId = fundingExternalId;
}

public String getFundingEntityExternalId() {
return fundingEntityExternalId;
}

public void setFundingEntityExternalId(String fundingEntityExternalId) {
this.fundingEntityExternalId = fundingEntityExternalId;
}

public String getFundingUrlField() {
return fundingUrlField;
}

public void setFundingUrlField(String fundingUrlField) {
this.fundingUrlField = fundingUrlField;
}

public CitationType getCitationType() {
return citationType;
}

public void setCitationType(String citationType) {
this.citationType = parseCitationType(citationType);
}

private CitationType parseCitationType(String citationType) {

if (StringUtils.isBlank(citationType)) {
return null;
}

try {
return CitationType.fromValue(citationType);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("The citation type " + citationType + " is invalid, "
+ "allowed values are " + getAllowedCitationTypes(), ex);
}
}

private Map<String, ContributorRole> parseContributors(String contributors) {
Map<String, String> contributorsMap = parseConfigurations(contributors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,24 @@

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.dspace.core.CrisConstants.PLACEHOLDER_PARENT_METADATA_VALUE;
import static org.orcid.jaxb.model.common.Relationship.FUNDED_BY;
import static org.orcid.jaxb.model.common.Relationship.SELF;

import java.io.ByteArrayOutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.dspace.authority.service.AuthorityValueService;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.integration.crosswalks.CSLItemDataCrosswalk;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.orcid.model.OrcidEntityType;
import org.dspace.orcid.model.OrcidPatentWorkFieldMapping;
import org.dspace.orcid.model.factory.OrcidCommonObjectFactory;
import org.dspace.orcid.model.factory.OrcidEntityFactory;
import org.dspace.util.UUIDUtils;
import org.orcid.jaxb.model.common.CitationType;
import org.orcid.jaxb.model.common.ContributorRole;
import org.orcid.jaxb.model.common.LanguageCode;
import org.orcid.jaxb.model.common.Relationship;
Expand All @@ -48,7 +37,6 @@
import org.orcid.jaxb.model.v3.release.common.Title;
import org.orcid.jaxb.model.v3.release.common.Url;
import org.orcid.jaxb.model.v3.release.record.Activity;
import org.orcid.jaxb.model.v3.release.record.Citation;
import org.orcid.jaxb.model.v3.release.record.ExternalID;
import org.orcid.jaxb.model.v3.release.record.ExternalIDs;
import org.orcid.jaxb.model.v3.release.record.Work;
Expand Down Expand Up @@ -89,7 +77,6 @@ public Activity createOrcidObject(Context context, Item item) {
work.setPublicationDate(getPublicationDate(context, item));
work.setWorkExternalIdentifiers(getWorkExternalIds(context, item));
work.setWorkType(getWorkType(context, item));
work.setWorkCitation(getWorkCitation(context, item));
work.setShortDescription(getShortDescription(context, item));
work.setLanguageCode(getLanguageCode(context, item));
work.setUrl(getUrl(context, item));
Expand Down Expand Up @@ -166,7 +153,6 @@ private PublicationDate getPublicationDate(Context context, Item item) {
private ExternalIDs getWorkExternalIds(Context context, Item item) {
ExternalIDs externalIdentifiers = new ExternalIDs();
externalIdentifiers.getExternalIdentifier().addAll(getWorkSelfExternalIds(context, item));
externalIdentifiers.getExternalIdentifier().addAll(getWorkFundedByExternalIds(context, item));
return externalIdentifiers;
}

Expand Down Expand Up @@ -204,67 +190,6 @@ private ExternalID getSelfExternalId(MetadataValue metadataValue) {
return getExternalId(externalIdentifierFields.get(metadataField), metadataValue.getValue(), SELF);
}

private List<ExternalID> getWorkFundedByExternalIds(Context context, Item item) {

if (isBlank(fieldMapping.getFundingExternalIdType())) {
return Collections.emptyList();
}

return getMetadataValues(context, item, fieldMapping.getFundingField()).stream()
.map(metadataValue -> getWorkFundedByExternalId(context, item, metadataValue))
.flatMap(Optional::stream)
.collect(Collectors.toList());
}

private Optional<ExternalID> getWorkFundedByExternalId(Context context, Item work, MetadataValue fundingMetadata) {
return getFundedByExternalIdFromFunding(context, fundingMetadata)
.or(() -> getFundedByExternalIdFromWork(context, work, fundingMetadata.getPlace()));
}

private Optional<ExternalID> getFundedByExternalIdFromFunding(Context context, MetadataValue fundingMetadata) {

if (isAuthoritySet(fundingMetadata.getAuthority())) {
return findItemById(context, UUIDUtils.fromString(fundingMetadata.getAuthority()))
.map(funding -> getFundingExternalId(context, funding));
}

return Optional.empty();
}

private Optional<ExternalID> getFundedByExternalIdFromWork(Context context, Item work, int fundingPlace) {
List<MetadataValue> externalIdValues = getMetadataValues(context, work, fieldMapping.getFundingExternalId());

if (externalIdValues.size() > fundingPlace && isNotPlaceholder(externalIdValues.get(fundingPlace))) {
String value = externalIdValues.get(fundingPlace).getValue();
return Optional.of(getExternalId(fieldMapping.getFundingExternalIdType(), value, FUNDED_BY));
}

return Optional.empty();
}

private ExternalID getFundingExternalId(Context context, Item funding) {

String externalIdValue = getMetadataValue(context, funding, fieldMapping.getFundingEntityExternalId())
.map(MetadataValue::getValue)
.orElse(null);

if (externalIdValue == null) {
return null;
}

Optional<Url> fundingUrl = getMetadataValue(context, funding, fieldMapping.getFundingUrlField())
.map(fundingUrlMetadata -> new Url(fundingUrlMetadata.getValue()))
.or(() -> orcidCommonObjectFactory.createUrl(context, funding));

ExternalID externalId = getExternalId(fieldMapping.getFundingExternalIdType(), externalIdValue, FUNDED_BY);
fundingUrl.ifPresent(externalId::setUrl);
return externalId;
}

private boolean isAuthoritySet(String authority) {
return isNotBlank(authority) && !StringUtils.startsWith(authority, AuthorityValueService.REFERENCE);
}

/**
* Creates an instance of ExternalID with the given type, value and
* relationship.
Expand Down Expand Up @@ -301,27 +226,6 @@ private Optional<WorkType> getWorkType(String workType) {
}
}

private Citation getWorkCitation(Context context, Item item) {

CSLItemDataCrosswalk citationCrosswalk = getCitationCrosswalk();

if (citationCrosswalk == null || !citationCrosswalk.canDisseminate(context, item)) {
return null;
}

try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
citationCrosswalk.disseminate(context, item, out);
return new Citation(out.toString(), fieldMapping.getCitationType());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private CSLItemDataCrosswalk getCitationCrosswalk() {
CitationType citationType = fieldMapping.getCitationType();
return citationType != null ? fieldMapping.getCitationCrosswalks().get(citationType.value()) : null;
}

private String getShortDescription(Context context, Item item) {
return getMetadataValue(context, item, fieldMapping.getShortDescriptionField())
.map(MetadataValue::getValue)
Expand Down Expand Up @@ -353,32 +257,12 @@ private Url getUrl(Context context, Item item) {
return orcidCommonObjectFactory.createUrl(context, item).orElse(null);
}

private List<MetadataValue> getMetadataValues(Context context, Item item, String metadataField) {
if (isBlank(metadataField)) {
return Collections.emptyList();
}
return itemService.getMetadataByMetadataString(item, metadataField);
}

private boolean isNotPlaceholder(MetadataValue metadata) {
return metadata != null && metadata.getValue() != null
&& !metadata.getValue().equals(PLACEHOLDER_PARENT_METADATA_VALUE);
}

private List<MetadataValue> getMetadataValues(Context context, Item item, Collection<String> metadataFields) {
return metadataFields.stream()
.flatMap(metadataField -> itemService.getMetadataByMetadataString(item, metadataField).stream())
.collect(Collectors.toList());
}

private Optional<Item> findItemById(Context context, UUID id) {
try {
return Optional.ofNullable(itemService.find(context, id));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private Optional<MetadataValue> getMetadataValue(Context context, Item item, String metadataField) {

if (isBlank(metadataField)) {
Expand Down
Loading

0 comments on commit 205f326

Please sign in to comment.