Skip to content

Commit

Permalink
Add more DataCite metadata and update error handling #24 and #2917
Browse files Browse the repository at this point in the history
  • Loading branch information
sekmiller committed Feb 18, 2016
1 parent 470abdf commit 372af97
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ public void close(){
client.close();
}

public String createIdentifier(String identifier, HashMap<String, String> metadata) {
public String createIdentifier(String identifier, HashMap<String, String> 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"));
Expand Down Expand Up @@ -180,6 +184,42 @@ class DataCiteMetadataTemplate {
private String title;
private String publisher;
private String publisherYear;
private List<DatasetAuthor> authors;
private String description;
private List<String[]> contacts;
private List<String[]> producers;

public List<String[]> getProducers() {
return producers;
}

public void setProducers(List<String[]> producers) {
this.producers = producers;
}

public List<String[]> getContacts() {
return contacts;
}

public void setContacts(List<String[]> contacts) {
this.contacts = contacts;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public List<DatasetAuthor> getAuthors() {
return authors;
}

public void setAuthors(List<DatasetAuthor> authors) {
this.authors = authors;
}

public DataCiteMetadataTemplate(){
}
Expand Down Expand Up @@ -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("<creator><creatorName>");
creatorsElement.append(creator);
creatorsElement.append("</creatorName></creator>");
for (DatasetAuthor author : authors) {
creatorsElement.append("<creator><creatorName>");
creatorsElement.append(author.getName().getDisplayValue());
creatorsElement.append("</creatorName>");

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());

This comment has been minimized.

Copy link
@bencomp

bencomp Mar 9, 2016

Contributor

Please use loggers!

creatorsElement.append("<nameIdentifier schemeURI=\"http://orcid.org/\" nameIdentifierScheme=\"ORCID\">" + author.getIdValue() + "</nameIdentifier>");
}
if (author.getIdType().equals("ISNI")) {
System.out.print("isni " + author.getIdType());
creatorsElement.append("<nameIdentifier schemeURI=\"http://isni.org/isni/\" nameIdentifierScheme=\"ISNI\">" + author.getIdValue() + "</nameIdentifier>");
}
if (author.getIdType().equals("LCNA")) {
System.out.print("lcna " + author.getIdType());
creatorsElement.append("<nameIdentifier schemeURI=\"http://id.loc.gov/authorities/names/\" nameIdentifierScheme=\"LCNA\">" + author.getIdValue() + "</nameIdentifier>");
}
}
if (author.getAffiliation() != null && !author.getAffiliation().getDisplayValue().isEmpty()) {
creatorsElement.append("<affiliation>" + author.getAffiliation().getDisplayValue() + "</affiliation>");
}
creatorsElement.append("</creator>");
}
xmlMetadata = xmlMetadata.replace("${creators}", creatorsElement.toString());

StringBuilder contributorsElement = new StringBuilder();
for (String[] contact: this.getContacts()){
contributorsElement.append("<contributor contributorType=\"ContactPerson\"><contributorName>" + contact[0] + "</contributorName>");
if (!contact[1].isEmpty()){
contributorsElement.append("<affiliation>" + contact[1] + "</affiliation>");
}
contributorsElement.append("</contributor>");
}
for (String[] producer: this.getProducers()){
contributorsElement.append("<contributor contributorType=\"Producer\"><contributorName>" + producer[0] + "</contributorName>");
if (!producer[1].isEmpty()){
contributorsElement.append("<affiliation>" + producer[1] + "</affiliation>");
}
contributorsElement.append("</contributor>");
}
System.out.print("Contributors element: " + contributorsElement.toString());
xmlMetadata = xmlMetadata.replace("{$contributors}", contributorsElement.toString());
return xmlMetadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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());
Expand All @@ -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 {

This comment has been minimized.

Copy link
@bencomp

bencomp Mar 9, 2016

Contributor

I don't think you should declare throws RuntimeException, especially this generic one. If you want to warn developers, declare the exception in JavaDoc.

String identifier = getIdentifierFromDataset(datasetIn);
HashMap doiMetadata = new HashMap();
try {
Expand Down Expand Up @@ -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());
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/DatasetAuthor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
89 changes: 87 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/DatasetVersion.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -596,14 +597,92 @@ 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<String[]> getDatasetContacts(){
List retList = new ArrayList();

This comment has been minimized.

Copy link
@bencomp

bencomp Mar 9, 2016

Contributor

Please use type arguments to prevent javac from complaining about unchecked conversions and helping developers' IDEs suggest to work with String[] elements. List<String[]> = 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<String[]> getDatasetProducers(){
List retList = new ArrayList();

This comment has been minimized.

Copy link
@bencomp

bencomp Mar 9, 2016

Contributor

Again, please use type arguments.

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<DatasetAuthor> getDatasetAuthors() {
//todo get "List of Authors" from datasetfieldvalue table
List retList = new ArrayList();
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()) {

This comment has been minimized.

Copy link
@bencomp

bencomp Mar 9, 2016

Contributor

Where did the end-of-line whitespace come from?

DatasetAuthor datasetAuthor = new DatasetAuthor();
for (DatasetField subField : authorValue.getChildDatasetFields()) {
if (subField.getDatasetFieldType().getName().equals(DatasetFieldConstant.authorName)) {
Expand All @@ -615,8 +694,14 @@ public List<DatasetAuthor> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
</titles>
<publisher>${publisher}</publisher>
<publicationYear>${publisherYear}</publicationYear>
<resourceType resourceTypeGeneral="Dataset"/>
<descriptions>
<description descriptionType="Abstract">${description}</description>
</descriptions>
<contributors>{$contributors}</contributors>
</resource>

0 comments on commit 372af97

Please sign in to comment.