use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class UpdateDataverseCommand method execute.
@Override
public Dataverse execute(CommandContext ctxt) throws CommandException {
Dataverse result = ctxt.dataverses().save(editedDv);
if (facetList != null) {
ctxt.facets().deleteFacetsFor(result);
int i = 0;
for (DatasetFieldType df : facetList) {
ctxt.facets().create(i++, df.getId(), result.getId());
}
}
if (featuredDataverseList != null) {
ctxt.featuredDataverses().deleteFeaturedDataversesFor(result);
int i = 0;
for (Object obj : featuredDataverseList) {
Dataverse dv = (Dataverse) obj;
ctxt.featuredDataverses().create(i++, dv.getId(), result.getId());
}
}
if (inputLevelList != null) {
ctxt.fieldTypeInputLevels().deleteFacetsFor(result);
for (DataverseFieldTypeInputLevel obj : inputLevelList) {
ctxt.fieldTypeInputLevels().create(obj);
}
}
ctxt.index().indexDataverse(result);
return result;
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class SwordServiceBean method addDatasetContact.
/**
* Mutate the dataset version, adding a datasetContact (email address) from
* the dataverse that will own the dataset.
*/
public void addDatasetContact(DatasetVersion newDatasetVersion, User user) {
DatasetFieldType emailDatasetFieldType = datasetFieldService.findByNameOpt(DatasetFieldConstant.datasetContact);
DatasetField emailDatasetField = DatasetField.createNewEmptyDatasetField(emailDatasetFieldType, newDatasetVersion);
for (DatasetField childField : emailDatasetField.getDatasetFieldCompoundValues().get(0).getChildDatasetFields()) {
if (DatasetFieldConstant.datasetContactEmail.equals(childField.getDatasetFieldType().getName())) {
// set the value to the in user's email
childField.getSingleValue().setValue(user.getDisplayInfo().getEmailAddress());
}
// We don't see any error from EZID but when using DataCite, we were seeing this error: Response code: 400, [xml] xml error: cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_contributorNamecontributorcontributorsresource'.
if (DatasetFieldConstant.datasetContactName.equals(childField.getDatasetFieldType().getName())) {
childField.getSingleValue().setValue(user.getDisplayInfo().getTitle());
}
}
newDatasetVersion.getDatasetFields().add(emailDatasetField);
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class SwordServiceBean method addDatasetSubjectIfMissing.
/**
* If no subject exists, mutate the dataset version, adding "N/A" for the
* subject. Otherwise, leave the dataset alone.
*/
public void addDatasetSubjectIfMissing(DatasetVersion datasetVersion) {
DatasetFieldType subjectDatasetFieldType = datasetFieldService.findByNameOpt(DatasetFieldConstant.subject);
boolean subjectFieldExists = false;
List<DatasetField> datasetFields = datasetVersion.getDatasetFields();
for (DatasetField datasetField : datasetFields) {
logger.fine("datasetField: " + datasetField.getDisplayValue() + " ... " + datasetField.getDatasetFieldType().getName());
if (datasetField.getDatasetFieldType().getName().equals(subjectDatasetFieldType.getName())) {
subjectFieldExists = true;
logger.fine("subject field exists already");
break;
}
}
if (subjectFieldExists) {
// return early. nothing to do. dataset already has a subject
logger.fine("returning early because subject exists already");
return;
}
// if we made it here, we must not have a subject, so let's add one
DatasetField subjectDatasetField = DatasetField.createNewEmptyDatasetField(subjectDatasetFieldType, datasetVersion);
/**
* @todo Once dataverse has subject
* (https://github.com/IQSS/dataverse/issues/769), we should get subject
* from there for now, we'll use the global NA value. However, there is
* currently oddness in that if you go to edit the title of a dataset
* via the GUI you can not save the dataset without selecting a Subject:
* https://github.com/IQSS/dataverse/issues/1296#issuecomment-70146314
*/
ControlledVocabularyValue cvv = datasetFieldService.findNAControlledVocabularyValue();
subjectDatasetField.setSingleControlledVocabularyValue(cvv);
datasetVersion.getDatasetFields().add(subjectDatasetField);
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class ImportDDIServiceBean method processCustomField.
private void processCustomField(XMLStreamReader xmlr, DatasetVersionDTO dvDTO) throws XMLStreamException, ImportException {
String subject = xmlr.getAttributeValue(null, "subject");
if (!subject.isEmpty()) {
// Syntax of subject attribute:
// TEMPLATE:Contains Custom Fields;FIELD:Customfield1
// first parse by semicolon
String template = subject.substring(subject.indexOf(":") + 1, subject.indexOf(";"));
String sourceField = subject.substring(subject.lastIndexOf(":") + 1);
String fieldValue = parseText(xmlr);
CustomFieldMap map = customFieldService.findByTemplateField(template.trim(), sourceField.trim());
if (map == null) {
throw new ImportException("Did not find mapping for template: " + template + ", sourceField: " + sourceField);
}
if (map.getTargetDatasetField().endsWith("#IGNORE")) {
// copy this field from 3.6 to 4.0
return;
}
// 1. Get datasetFieldType for the targetField
// 2. find the metadatablock for this field type
// 3. If this metadatablock doesn't exist in DTO, create it
// 4. add field to mdatadatablock
DatasetFieldType dsfType = datasetFieldService.findByName(map.getTargetDatasetField());
if (dsfType == null) {
throw new ImportException("Did not find datasetField for target: " + map.getTargetDatasetField());
}
String metadataBlockName = dsfType.getMetadataBlock().getName();
MetadataBlockDTO customBlock = dvDTO.getMetadataBlocks().get(metadataBlockName);
if (customBlock == null) {
customBlock = new MetadataBlockDTO();
customBlock.setDisplayName(metadataBlockName);
dvDTO.getMetadataBlocks().put(metadataBlockName, customBlock);
}
if (dsfType.isChild()) {
handleChildField(customBlock, dsfType, fieldValue);
} else {
if (dsfType.isAllowMultiples()) {
List<String> valList = new ArrayList<>();
valList.add(fieldValue);
if (dsfType.isAllowControlledVocabulary()) {
customBlock.addField(FieldDTO.createMultipleVocabFieldDTO(dsfType.getName(), valList));
} else if (dsfType.isPrimitive()) {
customBlock.addField(FieldDTO.createMultiplePrimitiveFieldDTO(dsfType.getName(), valList));
} else {
throw new ImportException("Unsupported custom field type: " + dsfType);
}
} else {
if (dsfType.isAllowControlledVocabulary()) {
customBlock.addField(FieldDTO.createVocabFieldDTO(dsfType.getName(), fieldValue));
} else if (dsfType.isPrimitive()) {
customBlock.addField(FieldDTO.createPrimitiveFieldDTO(dsfType.getName(), fieldValue));
} else {
throw new ImportException("Unsupported custom field type: " + dsfType);
}
}
}
}
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class JsonPrinterTest method testDatasetContactWithPrivacy.
@Test
public void testDatasetContactWithPrivacy() {
MetadataBlock block = new MetadataBlock();
block.setName("citation");
List<DatasetField> fields = new ArrayList<>();
DatasetField datasetContactField = new DatasetField();
DatasetFieldType datasetContactDatasetFieldType = datasetFieldTypeSvc.findByName("datasetContact");
datasetContactDatasetFieldType.setMetadataBlock(block);
datasetContactField.setDatasetFieldType(datasetContactDatasetFieldType);
List<DatasetFieldCompoundValue> vals = new LinkedList<>();
DatasetFieldCompoundValue val = new DatasetFieldCompoundValue();
val.setParentDatasetField(datasetContactField);
val.setChildDatasetFields(Arrays.asList(constructPrimitive("datasetContactEmail", "foo@bar.com"), constructPrimitive("datasetContactName", "Foo Bar"), constructPrimitive("datasetContactAffiliation", "Bar University")));
vals.add(val);
datasetContactField.setDatasetFieldCompoundValues(vals);
fields.add(datasetContactField);
JsonPrinter jsonPrinter = new JsonPrinter(new MockSettingsSvc());
JsonObject jsonObject = JsonPrinter.json(block, fields).build();
assertNotNull(jsonObject);
System.out.println("json: " + JsonUtil.prettyPrint(jsonObject.toString()));
assertEquals("Foo Bar", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
assertEquals("Bar University", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
assertEquals(null, jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail"));
JsonObject byBlocks = jsonPrinter.jsonByBlocks(fields).build();
System.out.println("byBlocks: " + JsonUtil.prettyPrint(byBlocks.toString()));
assertEquals("Foo Bar", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
assertEquals("Bar University", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
assertEquals(null, byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail"));
}
Aggregations