Skip to content

Commit

Permalink
https://github.com/metadatacenter/cedar-project/issues/1198
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila L. Egyedi committed Nov 8, 2023
1 parent 86a9542 commit 9687849
Show file tree
Hide file tree
Showing 31 changed files with 87 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.metadatacenter.bridge.CedarDataServices;
import org.metadatacenter.cedar.bridge.resource.*;
import org.metadatacenter.cedar.bridge.resource.Cedar.MetadataInstance;
import org.metadatacenter.cedar.bridge.resource.DataCiteProperties.Attributes;
import org.metadatacenter.cedar.bridge.resource.DataCiteProperties.DataCiteSchema;
import org.metadatacenter.cedar.bridge.resource.*;
import org.metadatacenter.cedar.bridge.resource.datacite.Attributes;
import org.metadatacenter.cedar.bridge.resource.datacite.DataCiteSchema;
import org.metadatacenter.cedar.util.dw.CedarMicroserviceResource;
import org.metadatacenter.config.CedarConfig;
import org.metadatacenter.constant.HttpConstants;
Expand Down Expand Up @@ -178,16 +178,16 @@ public Response createDOIStart(@QueryParam(QP_SOURCE_ARTIFACT_ID) String sourceA
.errorMessage("The source artifact is not found")
.id(sourceArtifactIdTyped)
.build();
} else if (!(folderServerResource.isOpen() || folderSession.isArtifactOpenImplicitly(sourceArtifactIdTyped))){
return CedarResponse
.badRequest()
.errorMessage("Please make the " + sourceArtifactResourceId.getType().getValue().toLowerCase() + " open to create a DOI")
.build();
}
} else if (!(folderServerResource.isOpen() || folderSession.isArtifactOpenImplicitly(sourceArtifactIdTyped))) {
return CedarResponse
.badRequest()
.errorMessage("Please make the " + sourceArtifactResourceId.getType().getValue().toLowerCase() + " open to create a DOI")
.build();
}

// check if the source artifact is published (version) - if it is a template
if (sourceArtifactResourceId.getType() == CedarResourceType.TEMPLATE){
if (!Objects.equals(sourceArtifactProxyJson.get("bibo:status").asText(), BiboStatus.PUBLISHED.getValue())){
if (sourceArtifactResourceId.getType() == CedarResourceType.TEMPLATE) {
if (!Objects.equals(sourceArtifactProxyJson.get("bibo:status").asText(), BiboStatus.PUBLISHED.getValue())) {
return CedarResponse
.badRequest()
.errorMessage("Please make the template publish to create a DOI")
Expand All @@ -205,7 +205,7 @@ public Response createDOIStart(@QueryParam(QP_SOURCE_ARTIFACT_ID) String sourceA
boolean hasDraftDoi = (boolean) entity.get("hasDraftDoi");
JsonNode dataNode = (JsonNode) entity.get("draftMetadata");

if (hasDraftDoi){
if (hasDraftDoi) {
// if draft DOI is returned, convert the data from dataCite JSON to Cedar Instance JSON-LD, and put it into response
JsonNode attributesNode = dataNode.get(0).get("attributes");
JsonNode draftDoi = attributesNode.get("doi");
Expand All @@ -224,14 +224,13 @@ public Response createDOIStart(@QueryParam(QP_SOURCE_ARTIFACT_ID) String sourceA

String cedarDataCiteInstanceString = mapper.writeValueAsString(cedarExistingDoiMetadata);
System.out.println("Converted Cedar DataCite Instance JSON-LD: " + cedarDataCiteInstanceString);
}
else{
} else {
// if draft DOI is not available, set the url and resourceType fields
MetadataInstance defaultInstance = GenerateInstance.getDefaultInstance(sourceArtifactId);
response.put("existingDataCiteMetadata", defaultInstance);
response.put("draftDoi", null);
}
} catch(IOException | InterruptedException e){
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
response.put("sourceArtifactType", sourceArtifactResourceId.getType().getValue());
Expand All @@ -246,7 +245,8 @@ public Response createDOIStart(@QueryParam(QP_SOURCE_ARTIFACT_ID) String sourceA
@Produces(MediaType.APPLICATION_JSON)
@Timed
@Path("/create-doi")
public Response createDOI(@QueryParam(QP_SOURCE_ARTIFACT_ID) String sourceArtifactId, @QueryParam("state") String state, JsonNode dataCiteInstance) throws CedarException, IOException, InterruptedException {
public Response createDOI(@QueryParam(QP_SOURCE_ARTIFACT_ID) String sourceArtifactId, @QueryParam("state") String state, JsonNode dataCiteInstance) throws CedarException, IOException,
InterruptedException {
CedarRequestContext c = buildRequestContext();

// c.must(c.user()).be(LoggedIn);
Expand Down Expand Up @@ -301,7 +301,7 @@ public Response createDOI(@QueryParam(QP_SOURCE_ARTIFACT_ID) String sourceArtifa
int statusCode = putOrPostResponse.statusCode();
//If the Put or Post response status code is 200 or 201
String jsonResponse = putOrPostResponse.body();
try{
try {
if (statusCode == HttpConstants.CREATED | statusCode == HttpConstants.OK) {
// Deserialize DataCite response json file to DataCiteRequest Class
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -323,7 +323,7 @@ else if (statusCode == CedarResponseStatus.UNPROCESSABLE_ENTITY.getStatusCode())
JsonNode jsonResource = JsonMapper.MAPPER.readTree(jsonResponse);
JsonNode errorsNode = jsonResource.get("errors");
StringBuilder errorMessageBuilder = new StringBuilder();
for(JsonNode errorNode: errorsNode){
for (JsonNode errorNode : errorsNode) {
JsonNode sourceNode = errorNode.get("source");
if (sourceNode != null && sourceNode.isTextual()) {
String source = sourceNode.asText();
Expand All @@ -336,7 +336,7 @@ else if (statusCode == CedarResponseStatus.UNPROCESSABLE_ENTITY.getStatusCode())
if (titleParts.length > 1) {
String titleMessage = String.join(":", Arrays.copyOfRange(titleParts, 1, titleParts.length)).trim();
errorMessageBuilder.append(titleMessage).append("\n");
} else{
} else {
errorMessageBuilder.append(title).append("\n");
}
}
Expand All @@ -354,7 +354,7 @@ else if (statusCode == CedarResponseStatus.UNPROCESSABLE_ENTITY.getStatusCode())
.entity(jsonResource)
.build();
}
} catch (Exception e){
} catch (Exception e) {
return CedarResponse
.internalServerError()
.errorMessage(e.getMessage())
Expand Down Expand Up @@ -464,7 +464,7 @@ private String getRequestJson(JsonNode metadata, String sourceArtifactId, String
// Pass the value from dataCiteInstance to dataCiteRequest
try {
CedarInstanceParser.parseCedarInstance(cedarInstance, dataCiteSchema, sourceArtifactId, state);
} catch (DataCiteInstanceValidationException e){
} catch (DataCiteInstanceValidationException e) {
e.printStackTrace();
throw new DataCiteInstanceValidationException(e.getMessage());
}
Expand All @@ -486,9 +486,9 @@ private String getRequestJson(JsonNode metadata, String sourceArtifactId, String
* @param endPointUrl URL of API call
* @param basicAuth Authentication at heater
* @param jsonData DataCite metadata instance JSON
* @return Http POST Response from DataCite
* @return Http POST Response from DataCite
*/
private HttpResponse<String> httpDataCitePostCall(String endPointUrl, String basicAuth, String jsonData){
private HttpResponse<String> httpDataCitePostCall(String endPointUrl, String basicAuth, String jsonData) {
try {
URI uri = URI.create(endPointUrl);
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -509,13 +509,14 @@ private HttpResponse<String> httpDataCitePostCall(String endPointUrl, String bas
/**
* This function send HTTP Put request to DataCite to update DOI's metadata
*
* @param draftDoi String of draft DOI
* @param basicAuth Authentication at heater
* @param jsonData DataCite metadata instance JSON
* @return Http Put Response from DataCite
* @param draftDoi String of draft DOI
* @param basicAuth Authentication at heater
* @param jsonData DataCite metadata instance JSON
* @return Http Put Response from DataCite
*/
private HttpResponse<String> httpDataCitePutCall(String draftDoi, String basicAuth, String jsonData)throws IOException, InterruptedException{
String url = endpointUrl + "/" + draftDoi.replace("\"", "");;
private HttpResponse<String> httpDataCitePutCall(String draftDoi, String basicAuth, String jsonData) throws IOException, InterruptedException {
String url = endpointUrl + "/" + draftDoi.replace("\"", "");
;
URI uri = URI.create(url);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(uri)
Expand All @@ -532,8 +533,8 @@ private HttpResponse<String> httpDataCitePutCall(String draftDoi, String basicAu
/**
* This function send HTTP Get request to DataCite to query the draft DOI
*
* @param sourceArtifactId ID of source template or instance for which you want to create the DOI
* @return CedarResponse which contains draft DOI's metadata and boolean value of if draft DOI exists
* @param sourceArtifactId ID of source template or instance for which you want to create the DOI
* @return CedarResponse which contains draft DOI's metadata and boolean value of if draft DOI exists
*/
private Response getDraftDoiMetadata(String sourceArtifactId) throws IOException, InterruptedException {
Map<String, Object> response = new HashMap<>();
Expand Down Expand Up @@ -571,8 +572,7 @@ private Boolean hasDraftDoi(JsonNode dataNode) {
private JsonNode getCEDARTemplate(CedarRequestContext c, String templateId) {
try {
CedarTemplateId cedarTemplateId = CedarTemplateId.build(templateId);
String artifactServerUrl = microserviceUrlUtil.getArtifact().getArtifactTypeWithId(CedarResourceType.TEMPLATE,
(CedarArtifactId) cedarTemplateId);
String artifactServerUrl = microserviceUrlUtil.getArtifact().getArtifactTypeWithId(CedarResourceType.TEMPLATE, cedarTemplateId);

HttpEntity currentTemplateEntity = ProxyUtil.proxyGet(artifactServerUrl, c).getEntity();
String currentTemplateEntityContent = EntityUtils.toString(currentTemplateEntity, CharEncoding.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.metadatacenter.cedar.bridge.resource.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.metadatacenter.cedar.bridge.resource.DataCiteProperties.DataCiteSchema;
import org.metadatacenter.cedar.bridge.resource.datacite.DataCiteSchema;
import org.metadatacenter.http.CedarResponseStatus;

import javax.ws.rs.client.Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.metadatacenter.cedar.bridge.resource;

import org.metadatacenter.cedar.bridge.resource.DataCiteProperties.*;
import org.metadatacenter.cedar.bridge.resource.datacite.*;
import org.metadatacenter.id.CedarFQResourceId;
import org.metadatacenter.model.CedarResourceType;

Expand Down Expand Up @@ -235,7 +235,7 @@ private static List<DataCiteAffiliation> parseAffiliationValue(List<?> affiliati
name = a.name() != null ? a.name().value() : null;
affiliationIdentifier = a.affiliationIdentifier() != null ? a.affiliationIdentifier().value():null;
affiliationIdentifierScheme = a.affiliationIdentifierScheme() != null ? a.affiliationIdentifierScheme().value() : null;
if (affiliationIdentifierScheme == null || affiliationIdentifierScheme.equals("")){
if (affiliationIdentifierScheme == null || affiliationIdentifierScheme.isEmpty()){
missedProperties.add("Affiliation Identifier Scheme under Creators");
}
affiliationSchemeURI = a.schemeUri() != null ? a.schemeUri().id() : null;
Expand All @@ -244,7 +244,7 @@ private static List<DataCiteAffiliation> parseAffiliationValue(List<?> affiliati
name = a.name() != null ? a.name().value() : null;
affiliationIdentifier = a.affiliationIdentifier() != null ? a.affiliationIdentifier().value():null;
affiliationIdentifierScheme = a.affiliationIdentifierScheme() != null ? a.affiliationIdentifierScheme().value() : null;
if (affiliationIdentifierScheme == null || affiliationIdentifierScheme.equals("")){
if (affiliationIdentifierScheme == null || affiliationIdentifierScheme.isEmpty()){
missedProperties.add("Affiliation Identifier Scheme under Contributors");
}
affiliationSchemeURI = a.schemeUri() != null ? a.schemeUri().id() : null;
Expand Down Expand Up @@ -275,15 +275,15 @@ private static List<DataCiteNameIdentifier> parseNameIdentifierValue(List<?> nam
CreatorElement.NameIdentifierElement n = (CreatorElement.NameIdentifierElement) obj;
nameIdentifierName = n.name() != null ? n.name().value() : null;
nameIdentifierScheme = n.nameIdentifierScheme() != null ? n.nameIdentifierScheme().value() : null;
if (nameIdentifierScheme == null || nameIdentifierScheme.equals("")){
if (nameIdentifierScheme == null || nameIdentifierScheme.isEmpty()){
missedProperties.add("Name Identifier Scheme under Creators");
}
nameIdentifierSchemeUri = n.schemeUri() != null ? n.schemeUri().id() : null;
} else if(obj instanceof ContributorElement.NameIdentifierElement){
ContributorElement.NameIdentifierElement n = (ContributorElement.NameIdentifierElement) obj;
nameIdentifierName = n.name() != null ? n.name().value() : null;
nameIdentifierScheme = n.nameIdentifierScheme() != null ? n.nameIdentifierScheme().value() : null;
if (nameIdentifierScheme == null || nameIdentifierScheme.equals("")){
if (nameIdentifierScheme == null || nameIdentifierScheme.isEmpty()){
missedProperties.add("Name Identifier Scheme under Contributors");
}
nameIdentifierSchemeUri = n.schemeUri() != null ? n.schemeUri().id() : null;
Expand All @@ -309,7 +309,7 @@ private static List<DataCiteCreator> parseCreatorValue(List<CreatorElement> crea
DataCiteCreator dataCiteCreator = new DataCiteCreator();
// Retrieve values from CEDAR class
String creatorName = c.creatorName().value();
if (creatorName == null || creatorName.equals("")){
if (creatorName == null || creatorName.isEmpty()){
missedProperties.add("Creator Name under Creators");
}

Expand Down Expand Up @@ -421,7 +421,7 @@ private static List<DataCiteContributor> parseContributorValue(List<ContributorE
DataCiteContributor dataCiteContributor = new DataCiteContributor();
// Retrieve values from CEDAR class
String name = c.contributorName()!=null ? c.contributorName().value() : null;
if (name == null || name.equals("")){
if (name == null || name.isEmpty()){
missedProperties.add("Contributor Name under Contributors");
}
String nameType = c.nameType() != null ? c.nameType().label() : null;
Expand Down Expand Up @@ -469,7 +469,7 @@ private static List<DataCiteDate> parseDateValue(List<DateElement> dateList, Has
DataCiteDate dataCiteDate = new DataCiteDate();
String date = d.date() != null ? d.date().value() : null;
String dateType = d.dateType() != null ? d.dateType().label() : null;
if (dateType == null || dateType.equals("")){
if (dateType == null || dateType.isEmpty()){
missedProperties.add("Date Type under Date");
}
String dateInformation = d.dateInformation()!=null ? d.dateInformation().value():null;
Expand All @@ -490,7 +490,7 @@ private static List<DataCiteAlternateIdentifier> parseAlternateIdentifier(List<A
DataCiteAlternateIdentifier dataCiteAlternateIdentifier = new DataCiteAlternateIdentifier();
String alternateIdentifier = a.alternateIdentifier() != null ? a.alternateIdentifier().value() : null;
String alternateIdentifierType = a.alternateIdentifierType() != null ? a.alternateIdentifierType().value() : null;
if (alternateIdentifierType == null || alternateIdentifierType.equals("")){
if (alternateIdentifierType == null || alternateIdentifierType.isEmpty()){
missedProperties.add("Alternate Identifier Type under Alternate Identifiers");
}

Expand Down Expand Up @@ -749,7 +749,7 @@ private static List<DataCiteFundingReference> parseFundingReference(List<Funding
for(FundingReferenceElement f : fundingReferenceList) {
DataCiteFundingReference dataCiteFundingReference = new DataCiteFundingReference();
String funderName = f.funderName() != null ? f.funderName().value() : null;
if (funderName == null || funderName.equals("")){
if (funderName == null || funderName.isEmpty()){
missedProperties.add("Funder Name under Funding Reference");
}
String funderIdentifier = f.funderIdentifier() != null ? f.funderIdentifier().value() : null;
Expand Down Expand Up @@ -862,7 +862,7 @@ private static List<DataCiteCreator> parseRelatedItemCreators(List<RelatedItemCr
DataCiteCreator dataCiteCreator = new DataCiteCreator();
// Retrieve values from CEDAR class
String creatorName = c.creatorName().value();
if (creatorName == null || creatorName.equals("")){
if (creatorName == null || creatorName.isEmpty()){
missedProperties.add("Creator Name under Related Items");
}

Expand All @@ -887,14 +887,14 @@ private static List<DataCiteRelatedItemContributor> parseRelatedItemContributors
DataCiteRelatedItemContributor dataCiteRelatedItemContributor = new DataCiteRelatedItemContributor();
// Retrieve values from CEDAR class
String name = c.contributorName() != null ? c.contributorName().value() : null;
if (name == null || name.equals("")) {
if (name == null || name.isEmpty()) {
missedProperties.add("Contributor Name under Related Items");
}
String nameType = c.nameType() != null ? c.nameType().label() : null;
String givenName = c.givenName() != null ? c.givenName().value() : null;
String familyName = c.familyName() != null ? c.familyName().value() : null;
String contributorType = c.contributorType() != null ? c.contributorType().label() : null;
if (contributorType == null || contributorType.equals("")) {
if (contributorType == null || contributorType.isEmpty()) {
missedProperties.add("Contributor Type under Related Items");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.metadatacenter.cedar.bridge.resource.DataCiteProperties.*;
import org.metadatacenter.cedar.bridge.resource.datacite.*;


import java.util.*;
Expand Down
Loading

0 comments on commit 9687849

Please sign in to comment.