use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class DatasetFieldServiceApi method parseControlledVocabulary.
private String parseControlledVocabulary(String[] values) {
DatasetFieldType dsv = datasetFieldService.findByName(values[1]);
// See if it already exists
/*
Matching relies on assumption that only one cv value will exist for a given identifier or display value
If the lookup queries return multiple matches then retval is null
*/
// First see if cvv exists based on display name
ControlledVocabularyValue cvv = datasetFieldService.findControlledVocabularyValueByDatasetFieldTypeAndStrValue(dsv, values[2], true);
// then see if there's a match on identifier
ControlledVocabularyValue cvvi = null;
if (values[3] != null && !values[3].trim().isEmpty()) {
cvvi = datasetFieldService.findControlledVocabularyValueByDatasetFieldTypeAndIdentifier(dsv, values[3]);
}
// if there's a match on identifier use it
if (cvvi != null) {
cvv = cvvi;
}
// if there's no match create a new one
if (cvv == null) {
cvv = new ControlledVocabularyValue();
cvv.setDatasetFieldType(dsv);
// Alt is only for dataload so only add to new
for (int i = 5; i < values.length; i++) {
ControlledVocabAlternate alt = new ControlledVocabAlternate();
alt.setDatasetFieldType(dsv);
alt.setControlledVocabularyValue(cvv);
alt.setStrValue(values[i]);
cvv.getControlledVocabAlternates().add(alt);
}
}
cvv.setStrValue(values[2]);
cvv.setIdentifier(values[3]);
cvv.setDisplayOrder(Integer.parseInt(values[4]));
datasetFieldService.save(cvv);
return cvv.getStrValue();
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class Datasets method setCitationDate.
@PUT
@Path("{id}/citationdate")
public Response setCitationDate(@PathParam("id") String id, String dsfTypeName) {
return response(req -> {
if (dsfTypeName.trim().isEmpty()) {
return badRequest("Please provide a dataset field type in the requst body.");
}
DatasetFieldType dsfType = null;
if (!":publicationDate".equals(dsfTypeName)) {
dsfType = datasetFieldSvc.findByName(dsfTypeName);
if (dsfType == null) {
return badRequest("Dataset Field Type Name " + dsfTypeName + " not found.");
}
}
execCommand(new SetDatasetCitationDateCommand(req, findDatasetOrDie(id), dsfType));
return ok("Citation Date for dataset " + id + " set to: " + (dsfType != null ? dsfType.getDisplayName() : "default"));
});
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class ImportGenericServiceBean method processXMLElement.
private void processXMLElement(XMLStreamReader xmlr, String currentPath, String openingTag, ForeignMetadataFormatMapping foreignFormatMapping, DatasetDTO datasetDTO) throws XMLStreamException {
logger.fine("entering processXMLElement; (" + currentPath + ")");
for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) {
if (event == XMLStreamConstants.START_ELEMENT) {
String currentElement = xmlr.getLocalName();
ForeignMetadataFieldMapping mappingDefined = datasetfieldService.findFieldMapping(foreignFormatMapping.getName(), currentPath + currentElement);
if (mappingDefined != null) {
DatasetFieldType mappingDefinedFieldType = datasetfieldService.findByNameOpt(mappingDefined.getDatasetfieldName());
boolean compound = mappingDefinedFieldType.isCompound();
DatasetFieldCompoundValue cachedCompoundValue = null;
String dataverseFieldName = mappingDefined.getDatasetfieldName();
// Process attributes, if any are defined in the mapping:
if (mappingDefinedFieldType.isCompound()) {
List<HashSet<FieldDTO>> compoundField = new ArrayList<>();
HashSet<FieldDTO> set = new HashSet<>();
for (ForeignMetadataFieldMapping childMapping : mappingDefined.getChildFieldMappings()) {
if (childMapping.isAttribute()) {
String attributeName = childMapping.getForeignFieldXPath();
String attributeValue = xmlr.getAttributeValue(null, attributeName);
if (attributeValue != null) {
String mappedFieldName = childMapping.getDatasetfieldName();
logger.fine("looking up dataset field " + mappedFieldName);
DatasetFieldType mappedFieldType = datasetfieldService.findByNameOpt(mappedFieldName);
if (mappedFieldType != null) {
try {
addToSet(set, attributeName, attributeValue);
// FieldDTO value = FieldDTO.createPrimitiveFieldDTO(attributeName, attributeValue);
// FieldDTO attribute = FieldDTO.createCompoundFieldDTO(attributeName, value);
// MetadataBlockDTO citationBlock = datasetDTO.getDatasetVersion().getMetadataBlocks().get("citation");
// citationBlock.getFields().add(value);
// TO DO replace database output with Json cachedCompoundValue = createDatasetFieldValue(mappedFieldType, cachedCompoundValue, attributeValue, datasetVersion);
} catch (Exception ex) {
logger.warning("Caught unknown exception when processing attribute " + currentPath + currentElement + "{" + attributeName + "} (skipping);");
}
} else {
throw new EJBException("Bad foreign metadata field mapping: no such DatasetField " + mappedFieldName + "!");
}
}
}
}
if (!set.isEmpty()) {
compoundField.add(set);
MetadataBlockDTO citationBlock = datasetDTO.getDatasetVersion().getMetadataBlocks().get(mappingDefinedFieldType.getMetadataBlock().getName());
citationBlock.addField(FieldDTO.createMultipleCompoundFieldDTO(mappingDefined.getDatasetfieldName(), compoundField));
} else {
FieldDTO value = null;
if (mappingDefinedFieldType.isAllowMultiples()) {
List<String> values = new ArrayList<>();
values.add(parseText(xmlr));
value = FieldDTO.createMultiplePrimitiveFieldDTO(dataverseFieldName, values);
} else {
value = FieldDTO.createPrimitiveFieldDTO(dataverseFieldName, parseText(xmlr));
}
value = makeDTO(mappingDefinedFieldType, value, dataverseFieldName);
MetadataBlockDTO citationBlock = datasetDTO.getDatasetVersion().getMetadataBlocks().get(mappingDefinedFieldType.getMetadataBlock().getName());
citationBlock.addField(value);
}
} else // xxString dataverseFieldName = mappingDefined.getDatasetfieldName();
if (dataverseFieldName != null && !dataverseFieldName.isEmpty()) {
DatasetFieldType dataverseFieldType = datasetfieldService.findByNameOpt(dataverseFieldName);
FieldDTO value;
if (dataverseFieldType != null) {
if (dataverseFieldType.isControlledVocabulary()) {
value = FieldDTO.createVocabFieldDTO(dataverseFieldName, parseText(xmlr));
} else {
value = FieldDTO.createPrimitiveFieldDTO(dataverseFieldName, parseText(xmlr));
}
value = makeDTO(dataverseFieldType, value, dataverseFieldName);
// value = FieldDTO.createPrimitiveFieldDTO(dataverseFieldName, parseText(xmlr));
// FieldDTO dataverseField = FieldDTO.createCompoundFieldDTO(dataverseFieldName, value);
MetadataBlockDTO citationBlock = datasetDTO.getDatasetVersion().getMetadataBlocks().get(mappingDefinedFieldType.getMetadataBlock().getName());
citationBlock.addField(value);
// TO DO replace database output with Json createDatasetFieldValue(dataverseFieldType, cachedCompoundValue, elementTextPayload, datasetVersion);
} else {
throw new EJBException("Bad foreign metadata field mapping: no such DatasetField " + dataverseFieldName + "!");
}
}
} else {
// recursively, process the xml stream further down:
processXMLElement(xmlr, currentPath + currentElement + ":", currentElement, foreignFormatMapping, datasetDTO);
}
} else if (event == XMLStreamConstants.END_ELEMENT) {
if (xmlr.getLocalName().equals(openingTag))
return;
}
}
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class SwordServiceBean method addDatasetDepositor.
/**
* Mutate the dataset version, adding a depositor for the dataset.
*/
public void addDatasetDepositor(DatasetVersion newDatasetVersion, User user) {
if (!user.isAuthenticated()) {
logger.info("returning early since user is not authenticated");
return;
}
AuthenticatedUser au = (AuthenticatedUser) user;
DatasetFieldType depositorDatasetFieldType = datasetFieldService.findByNameOpt(DatasetFieldConstant.depositor);
DatasetField depositorDatasetField = DatasetField.createNewEmptyDatasetField(depositorDatasetFieldType, newDatasetVersion);
depositorDatasetField.setSingleValue(au.getLastName() + ", " + au.getFirstName());
newDatasetVersion.getDatasetFields().add(depositorDatasetField);
}
use of edu.harvard.iq.dataverse.DatasetFieldType in project dataverse by IQSS.
the class ImportDDIServiceBean method handleChildField.
private void handleChildField(MetadataBlockDTO customBlock, DatasetFieldType dsfType, String fieldValue) throws ImportException {
DatasetFieldType parent = dsfType.getParentDatasetFieldType();
// Create child Field
FieldDTO child = null;
if (dsfType.isAllowControlledVocabulary()) {
child = FieldDTO.createVocabFieldDTO(dsfType.getName(), fieldValue);
} else if (dsfType.isPrimitive()) {
child = FieldDTO.createPrimitiveFieldDTO(dsfType.getName(), fieldValue);
} else {
throw new ImportException("Unsupported custom child field type: " + dsfType);
}
// Create compound field with this child as its only element
FieldDTO compound = null;
if (parent.isAllowMultiples()) {
compound = FieldDTO.createMultipleCompoundFieldDTO(parent.getName(), child);
} else {
compound = FieldDTO.createCompoundFieldDTO(parent.getName(), child);
}
customBlock.addField(compound);
}
Aggregations