Skip to content

Commit

Permalink
Merge pull request #11071 from lubitchv/9871-termsofuse_license
Browse files Browse the repository at this point in the history
Adding terms of use in DDI
  • Loading branch information
ofahimIQSS authored Dec 20, 2024
2 parents f7aabc8 + 32de244 commit 75ba6d6
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 5 deletions.
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
}

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
}
}
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

0 comments on commit 75ba6d6

Please sign in to comment.