From 372af977da58aa396c8f18f2374eae948c38f996 Mon Sep 17 00:00:00 2001 From: Stephen Kraffmiller Date: Thu, 18 Feb 2016 10:01:43 -0500 Subject: [PATCH] Add more DataCite metadata and update error handling #24 and #2917 --- .../dataverse/DOIDataCiteRegisterService.java | 93 +++++++++++++++++-- .../iq/dataverse/DOIDataCiteServiceBean.java | 14 +-- .../harvard/iq/dataverse/DatasetAuthor.java | 11 +-- .../iq/dataverse/DatasetFieldConstant.java | 4 +- .../edu/harvard/iq/dataverse/DatasetPage.java | 8 +- .../harvard/iq/dataverse/DatasetVersion.java | 89 +++++++++++++++++- .../command/impl/PublishDatasetCommand.java | 8 +- .../dataverse/datacite_metadata_template.xml | 5 + 8 files changed, 203 insertions(+), 29 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java index 04c6b53abaf..2b8ad77c97c 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java +++ b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteRegisterService.java @@ -54,10 +54,14 @@ public void close(){ client.close(); } - public String createIdentifier(String identifier, HashMap metadata) { + public String createIdentifier(String identifier, HashMap metadata, Dataset dataset) { DataCiteMetadataTemplate metadataTemplate = new DataCiteMetadataTemplate(); metadataTemplate.setIdentifier(identifier.substring(identifier.indexOf(':')+1)); metadataTemplate.setCreators(Util.getListFromStr(metadata.get("datacite.creator"))); + metadataTemplate.setAuthors(dataset.getLatestVersion().getDatasetAuthors()); + metadataTemplate.setDescription(dataset.getLatestVersion().getDescription()); + metadataTemplate.setContacts(dataset.getLatestVersion().getDatasetContacts()); + metadataTemplate.setProducers(dataset.getLatestVersion().getDatasetProducers()); metadataTemplate.setTitle(metadata.get("datacite.title")); metadataTemplate.setPublisher(metadata.get("datacite.publisher")); metadataTemplate.setPublisherYear(metadata.get("datacite.publicationyear")); @@ -180,6 +184,42 @@ class DataCiteMetadataTemplate { private String title; private String publisher; private String publisherYear; + private List authors; + private String description; + private List contacts; + private List producers; + + public List getProducers() { + return producers; + } + + public void setProducers(List producers) { + this.producers = producers; + } + + public List getContacts() { + return contacts; + } + + public void setContacts(List contacts) { + this.contacts = contacts; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } public DataCiteMetadataTemplate(){ } @@ -207,21 +247,56 @@ public DataCiteMetadataTemplate(String xmlMetaData){ } public String generateXML() { - System.out.print("in generate xml..."); xmlMetadata = template.replace("${identifier}", this.identifier.trim()) .replace("${title}", this.title) .replace("${publisher}", this.publisher) - .replace("${publisherYear}", this.publisherYear); + .replace("${publisherYear}", this.publisherYear) + .replace("${description}", this.description); StringBuilder creatorsElement = new StringBuilder(); - for (String creator : creators) { - creator = creator.trim(); - if (creator.length() > 0) { - creatorsElement.append(""); - creatorsElement.append(creator); - creatorsElement.append(""); + for (DatasetAuthor author : authors) { + creatorsElement.append(""); + creatorsElement.append(author.getName().getDisplayValue()); + creatorsElement.append(""); + + if (author.getIdType() != null && author.getIdValue() != null && !author.getIdType().isEmpty() && !author.getIdValue().isEmpty() && author.getAffiliation() != null && !author.getAffiliation().getDisplayValue().isEmpty()) { + + if (author.getIdType().equals("ORCID")) { + System.out.print("orcid " + author.getIdType()); + creatorsElement.append("" + author.getIdValue() + ""); + } + if (author.getIdType().equals("ISNI")) { + System.out.print("isni " + author.getIdType()); + creatorsElement.append("" + author.getIdValue() + ""); + } + if (author.getIdType().equals("LCNA")) { + System.out.print("lcna " + author.getIdType()); + creatorsElement.append("" + author.getIdValue() + ""); + } + } + if (author.getAffiliation() != null && !author.getAffiliation().getDisplayValue().isEmpty()) { + creatorsElement.append("" + author.getAffiliation().getDisplayValue() + ""); } + creatorsElement.append(""); } xmlMetadata = xmlMetadata.replace("${creators}", creatorsElement.toString()); + + StringBuilder contributorsElement = new StringBuilder(); + for (String[] contact: this.getContacts()){ + contributorsElement.append("" + contact[0] + ""); + if (!contact[1].isEmpty()){ + contributorsElement.append("" + contact[1] + ""); + } + contributorsElement.append(""); + } + for (String[] producer: this.getProducers()){ + contributorsElement.append("" + producer[0] + ""); + if (!producer[1].isEmpty()){ + contributorsElement.append("" + producer[1] + ""); + } + contributorsElement.append(""); + } + System.out.print("Contributors element: " + contributorsElement.toString()); + xmlMetadata = xmlMetadata.replace("{$contributors}", contributorsElement.toString()); return xmlMetadata; } diff --git a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteServiceBean.java index 82d3c0dedd1..53c4d0014b3 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/DOIDataCiteServiceBean.java @@ -5,6 +5,8 @@ */ package edu.harvard.iq.dataverse; +import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException; +import edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand; import edu.harvard.iq.dataverse.settings.SettingsServiceBean; import java.net.InetAddress; import java.net.UnknownHostException; @@ -49,7 +51,7 @@ public String createIdentifier(Dataset dataset) { metadata.put("_status", "reserved"); try { logger.log(Level.INFO, "doiDataCiteRegisterService.toString : " + doiDataCiteRegisterService.toString()); - retString = doiDataCiteRegisterService.createIdentifier(identifier, metadata); + retString = doiDataCiteRegisterService.createIdentifier(identifier, metadata, dataset); logger.log(Level.INFO, "create DOI identifier retString : " + retString); } catch (Exception e) { logger.log(Level.INFO, "Identifier not created: create failed"); @@ -99,7 +101,7 @@ public String getIdentifierForLookup(String protocol, String authority, String s public String modifyIdentifier(Dataset dataset, HashMap metadata) { String identifier = getIdentifierFromDataset(dataset); try { - doiDataCiteRegisterService.createIdentifier(identifier, metadata); + doiDataCiteRegisterService.createIdentifier(identifier, metadata, dataset); } catch (Exception e) { logger.log(Level.INFO, "modifyMetadata failed"); logger.log(Level.INFO, "String " + e.toString()); @@ -111,7 +113,7 @@ public String modifyIdentifier(Dataset dataset, HashMap metadata) { return identifier; } - public void deleteIdentifier(Dataset datasetIn) { + public void deleteIdentifier(Dataset datasetIn) throws RuntimeException { String identifier = getIdentifierFromDataset(datasetIn); HashMap doiMetadata = new HashMap(); try { @@ -240,16 +242,16 @@ private String getIdentifierFromDataset(Dataset dataset) { return dataset.getGlobalId(); } - public void publicizeIdentifier(Dataset studyIn) { + public void publicizeIdentifier(Dataset studyIn) throws IllegalCommandException { updateIdentifierStatus(studyIn, "public"); } - private void updateIdentifierStatus(Dataset dataset, String statusIn) { + private void updateIdentifierStatus(Dataset dataset, String statusIn) throws RuntimeException { String identifier = getIdentifierFromDataset(dataset); HashMap metadata = getUpdateMetadataFromDataset(dataset); metadata.put("_status", statusIn); try { - doiDataCiteRegisterService.createIdentifier(identifier, metadata); + doiDataCiteRegisterService.createIdentifier(identifier, metadata, dataset); } catch (Exception e) { logger.log(Level.INFO, "modifyMetadata failed"); logger.log(Level.INFO, "String " + e.toString()); diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetAuthor.java b/src/main/java/edu/harvard/iq/dataverse/DatasetAuthor.java index 59e4549511f..9fefb98c26a 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetAuthor.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetAuthor.java @@ -60,7 +60,11 @@ public void setAffiliation(DatasetField affiliation) { private String idType; public String getIdType() { - return idType; + if (this.idType.isEmpty() && !this.idValue.isEmpty()){ + return ("ORCID"); + } else { + return idType; + } } public void setIdType(String idType) { @@ -76,11 +80,6 @@ public String getIdValue() { public void setIdValue(String idValue) { this.idValue = idValue; - if (!this.idValue.isEmpty()){ - setIdType("ORCID"); - } else { - setIdType(""); - } } public boolean isEmpty() { diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetFieldConstant.java b/src/main/java/edu/harvard/iq/dataverse/DatasetFieldConstant.java index 008c3e99fa1..f5275873b4c 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetFieldConstant.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetFieldConstant.java @@ -52,8 +52,8 @@ public class DatasetFieldConstant implements java.io.Serializable { public final static String datasetId = "datasetId"; public final static String authorName ="authorName"; public final static String authorAffiliation = "authorAffiliation"; - public final static String authorIdType = "authorIdType"; - public final static String authorIdValue = "authorIdValue"; + public final static String authorIdType = "authorIdentifierScheme"; + public final static String authorIdValue = "authorIdentifier"; public final static String otherIdValue="otherIdValue"; public final static String otherIdAgency= "otherIdAgency"; diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java b/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java index a940170c12e..193c314eafa 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetPage.java @@ -79,6 +79,7 @@ import java.util.logging.Level; import javax.faces.component.UIComponent; import javax.faces.component.UIInput; +import javax.faces.context.ExternalContext; import org.primefaces.component.tabview.TabView; import org.primefaces.event.TabChangeEvent; import org.primefaces.model.LazyDataModel; @@ -1480,7 +1481,7 @@ public String saveGuestbookResponse(String type) { } callDownloadServlet(downloadFormat, this.selectedDownloadFile.getId()); } - + if (type.equals("explore")) { String retVal = getDataExploreURLComplete(this.selectedDownloadFile.getId()); try { @@ -1887,11 +1888,12 @@ private String releaseDataset(boolean minor) { } } } catch (CommandException ex) { - JH.addMessage(FacesMessage.SEVERITY_FATAL, JH.localize("dataset.message.publishFailure")); + + JsfHelper.addErrorMessage(ex.getLocalizedMessage()); logger.severe(ex.getMessage()); } } else { - JH.addMessage(FacesMessage.SEVERITY_ERROR, "Only authenticated users can release Datasets."); + JsfHelper.addErrorMessage("Only authenticated users can release Datasets."); } return returnToDatasetOnly(); } diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetVersion.java b/src/main/java/edu/harvard/iq/dataverse/DatasetVersion.java index a3c748478de..23da7894b89 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetVersion.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetVersion.java @@ -1,5 +1,6 @@ package edu.harvard.iq.dataverse; +import edu.harvard.iq.dataverse.util.MarkupChecker; import edu.harvard.iq.dataverse.util.StringUtil; import java.io.Serializable; import java.sql.Timestamp; @@ -596,6 +597,84 @@ public String getProductionDate() { //todo get "Production Date" from datasetfieldvalue table return "Production Date"; } + + public String getDescription() { + for (DatasetField dsf : this.getDatasetFields()) { + if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.description)) { + String descriptionString = ""; + if (dsf.getDatasetFieldCompoundValues() != null && dsf.getDatasetFieldCompoundValues().get(0) != null) { + DatasetFieldCompoundValue descriptionValue = dsf.getDatasetFieldCompoundValues().get(0); + for (DatasetField subField : descriptionValue.getChildDatasetFields()) { + if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.descriptionText) && !subField.isEmptyForDisplay()) { + descriptionString = subField.getValue(); + } + } + } + return MarkupChecker.sanitizeBasicHTML(descriptionString); + } + } + return ""; + } + + public List getDatasetContacts(){ + List retList = new ArrayList(); + for (DatasetField dsf : this.getDatasetFields()) { + Boolean addContributor = true; + String contributorName = ""; + String contributorAffiliation = ""; + if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.datasetContact)) { + for (DatasetFieldCompoundValue authorValue : dsf.getDatasetFieldCompoundValues()) { + for (DatasetField subField : authorValue.getChildDatasetFields()) { + if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.datasetContactName)) { + if (subField.isEmptyForDisplay()) { + addContributor = false; + } + contributorName = subField.getDisplayValue(); + } + if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.datasetContactAffiliation)) { + contributorAffiliation = subField.getDisplayValue(); + } + + } + if (addContributor) { + String[] datasetContributor = new String[] {contributorName, contributorAffiliation}; + retList.add(datasetContributor); + } + } + } + } + return retList; + } + + public List getDatasetProducers(){ + List retList = new ArrayList(); + for (DatasetField dsf : this.getDatasetFields()) { + Boolean addContributor = true; + String contributorName = ""; + String contributorAffiliation = ""; + if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.producer)) { + for (DatasetFieldCompoundValue authorValue : dsf.getDatasetFieldCompoundValues()) { + for (DatasetField subField : authorValue.getChildDatasetFields()) { + if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.producerName)) { + if (subField.isEmptyForDisplay()) { + addContributor = false; + } + contributorName = subField.getDisplayValue(); + } + if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.producerAffiliation)) { + contributorAffiliation = subField.getDisplayValue(); + } + + } + if (addContributor) { + String[] datasetContributor = new String[] {contributorName, contributorAffiliation}; + retList.add(datasetContributor); + } + } + } + } + return retList; + } public List getDatasetAuthors() { //todo get "List of Authors" from datasetfieldvalue table @@ -603,7 +682,7 @@ public List getDatasetAuthors() { for (DatasetField dsf : this.getDatasetFields()) { Boolean addAuthor = true; if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.author)) { - for (DatasetFieldCompoundValue authorValue : dsf.getDatasetFieldCompoundValues()) { + for (DatasetFieldCompoundValue authorValue : dsf.getDatasetFieldCompoundValues()) { DatasetAuthor datasetAuthor = new DatasetAuthor(); for (DatasetField subField : authorValue.getChildDatasetFields()) { if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.authorName)) { @@ -615,8 +694,14 @@ public List getDatasetAuthors() { if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.authorAffiliation)) { datasetAuthor.setAffiliation(subField); } + if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.authorIdType)){ + datasetAuthor.setIdType(subField.getDisplayValue()); + } + if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.authorIdValue)){ + datasetAuthor.setIdValue(subField.getDisplayValue()); + } } - if (addAuthor) { + if (addAuthor) { retList.add(datasetAuthor); } } diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/PublishDatasetCommand.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/PublishDatasetCommand.java index 0429e46de31..a9503aa19d2 100644 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/PublishDatasetCommand.java +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/PublishDatasetCommand.java @@ -209,7 +209,13 @@ public Dataset execute(CommandContext ctxt) throws CommandException { } if (protocol.equals("doi") && doiProvider.equals("DataCite")) { - ctxt.doiDataCite().publicizeIdentifier(savedDataset); + try{ + ctxt.doiDataCite().publicizeIdentifier(savedDataset); + } catch (Exception e){ + + throw new IllegalCommandException("This dataset may not be published because the DOI update failed. Please contact Dataverse Support for assistance.", this); + } + } return savedDataset; diff --git a/src/main/resources/edu/harvard/iq/dataverse/datacite_metadata_template.xml b/src/main/resources/edu/harvard/iq/dataverse/datacite_metadata_template.xml index e7a83f8a14b..c11e18b49ee 100644 --- a/src/main/resources/edu/harvard/iq/dataverse/datacite_metadata_template.xml +++ b/src/main/resources/edu/harvard/iq/dataverse/datacite_metadata_template.xml @@ -9,4 +9,9 @@ ${publisher} ${publisherYear} + + + ${description} + + {$contributors}