Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding terms of use in DDI #11071

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
import edu.harvard.iq.dataverse.DatasetFieldType;
import edu.harvard.iq.dataverse.DatasetVersion;
import edu.harvard.iq.dataverse.DatasetVersion.VersionState;
import edu.harvard.iq.dataverse.api.dto.*;
import edu.harvard.iq.dataverse.api.dto.LicenseDTO;
import edu.harvard.iq.dataverse.api.dto.FieldDTO;
import edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO;
import edu.harvard.iq.dataverse.api.dto.DatasetDTO;
import edu.harvard.iq.dataverse.api.dto.DatasetVersionDTO;
import edu.harvard.iq.dataverse.api.dto.FileMetadataDTO;
import edu.harvard.iq.dataverse.api.dto.DataFileDTO;
import edu.harvard.iq.dataverse.api.dto.DataTableDTO;

import edu.harvard.iq.dataverse.api.imports.ImportUtil.ImportType;
import static edu.harvard.iq.dataverse.export.ddi.DdiExportUtil.NOTE_TYPE_CONTENTTYPE;
import static edu.harvard.iq.dataverse.export.ddi.DdiExportUtil.NOTE_TYPE_TERMS_OF_ACCESS;

import edu.harvard.iq.dataverse.license.License;
import edu.harvard.iq.dataverse.license.LicenseServiceBean;
import edu.harvard.iq.dataverse.util.StringUtil;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -32,6 +40,9 @@

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
* @author ellenk
Expand Down Expand Up @@ -103,6 +114,8 @@ public class ImportDDIServiceBean {
@EJB DatasetFieldServiceBean datasetFieldService;

@EJB ImportGenericServiceBean importGenericService;

@EJB LicenseServiceBean licenseService;


// TODO: stop passing the xml source as a string; (it could be huge!) -- L.A. 4.5
Expand Down Expand Up @@ -1180,7 +1193,24 @@ private void processDataAccs(XMLStreamReader xmlr, DatasetVersionDTO dvDTO) thro
String noteType = xmlr.getAttributeValue(null, "type");
if (NOTE_TYPE_TERMS_OF_USE.equalsIgnoreCase(noteType) ) {
if ( LEVEL_DV.equalsIgnoreCase(xmlr.getAttributeValue(null, "level"))) {
dvDTO.setTermsOfUse(parseText(xmlr, "notes"));
String termsOfUseStr = parseText(xmlr, "notes").trim();
Pattern pattern = Pattern.compile("<a href=\"(.*)\">(.*)</a>", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(termsOfUseStr);
boolean matchFound = matcher.find();
if (matchFound) {
String uri = matcher.group(1);
String license = matcher.group(2);
License lic = licenseService.getByNameOrUri(license);
if (lic != null) {
LicenseDTO licenseDTO = new LicenseDTO();
licenseDTO.setName(license);
licenseDTO.setUri(uri);
dvDTO.setLicense(licenseDTO);
}

} else {
dvDTO.setTermsOfUse(termsOfUseStr);
}
}
} else if (NOTE_TYPE_TERMS_OF_ACCESS.equalsIgnoreCase(noteType) ) {
if (LEVEL_DV.equalsIgnoreCase(xmlr.getAttributeValue(null, "level"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import edu.harvard.iq.dataverse.ControlledVocabularyValue;
import edu.harvard.iq.dataverse.DatasetFieldConstant;
import edu.harvard.iq.dataverse.DvObjectContainer;
import edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO;
import edu.harvard.iq.dataverse.api.dto.DatasetDTO;
import edu.harvard.iq.dataverse.api.dto.DatasetVersionDTO;
import edu.harvard.iq.dataverse.api.dto.FieldDTO;
import edu.harvard.iq.dataverse.api.dto.FileDTO;
import edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO;
import edu.harvard.iq.dataverse.api.dto.FieldDTO;
import edu.harvard.iq.dataverse.api.dto.LicenseDTO;


import static edu.harvard.iq.dataverse.export.DDIExportServiceBean.LEVEL_FILE;
import static edu.harvard.iq.dataverse.export.DDIExportServiceBean.NOTE_SUBJECT_TAG;
Expand Down Expand Up @@ -313,15 +315,36 @@ private static void writeDataAccess(XMLStreamWriter xmlw , DatasetVersionDTO ver
XmlWriterUtil.writeFullElement(xmlw, "conditions", version.getConditions());
XmlWriterUtil.writeFullElement(xmlw, "disclaimer", version.getDisclaimer());
xmlw.writeEndElement(); //useStmt

/* any <note>s: */
if (version.getTermsOfUse() != null && !version.getTermsOfUse().trim().equals("")) {
xmlw.writeStartElement("notes");
xmlw.writeAttribute("type", NOTE_TYPE_TERMS_OF_USE);
xmlw.writeAttribute("level", LEVEL_DV);
xmlw.writeCharacters(version.getTermsOfUse());
xmlw.writeEndElement(); //notes
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add to DdiExportUtilTest.java to exercise this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added dataset-finch-terms-of-use.json and dataset-finch-terms-of-use.xml which is used in testJson2DdiNoFilesTermsOfUse() for custom terms of use

if (version.getTermsOfAccess() != null && !version.getTermsOfAccess().trim().equals("")) {
xmlw.writeStartElement("notes");
xmlw.writeAttribute("type", NOTE_TYPE_TERMS_OF_ACCESS);
xmlw.writeAttribute("level", LEVEL_DV);
xmlw.writeCharacters(version.getTermsOfAccess());
xmlw.writeEndElement(); //notes
}

LicenseDTO license = version.getLicense();
if (license != null) {
String name = license.getName();
String uri = license.getUri();
if ((name != null && !name.trim().equals("")) && (uri != null && !uri.trim().equals(""))) {
xmlw.writeStartElement("notes");
xmlw.writeAttribute("type", NOTE_TYPE_TERMS_OF_USE);
xmlw.writeAttribute("level", LEVEL_DV);
xmlw.writeCharacters("<a href=" + '"' + uri + '"' + ">" + name + "</a>");
xmlw.writeEndElement(); //notes
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please add a test to DdiExportUtilTest.java

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standard license was already in dataset-finch1.json and dataset-finch1.xml was updated to add the standard license.

xmlw.writeEndElement(); //dataAccs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public void testJson2DdiNoFiles() throws Exception {
XmlAssert.assertThat(result).and(datasetAsDdi).ignoreWhitespace().areSimilar();
}

@Test
public void testJson2DdiNoFilesTermsOfUse() throws Exception {
// given
Path datasetVersionJson = Path.of("src/test/java/edu/harvard/iq/dataverse/export/ddi/dataset-finch-terms-of-use.json");
String datasetVersionAsJson = Files.readString(datasetVersionJson, StandardCharsets.UTF_8);
Path ddiFile = Path.of("src/test/java/edu/harvard/iq/dataverse/export/ddi/dataset-finch-terms-of-use.xml");
String datasetAsDdi = XmlPrinter.prettyPrintXml(Files.readString(ddiFile, StandardCharsets.UTF_8));
logger.fine(datasetAsDdi);

// when
String result = DdiExportUtil.datasetDtoAsJson2ddi(datasetVersionAsJson);
logger.fine(result);

// then
XmlAssert.assertThat(result).and(datasetAsDdi).ignoreWhitespace().areSimilar();
}

@Test
public void testExportDDI() throws Exception {
// given
Expand Down
Loading
Loading