Search in sources :

Example 6 with DatasetFieldType

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();
}
Also used : ControlledVocabAlternate(edu.harvard.iq.dataverse.ControlledVocabAlternate) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue)

Example 7 with DatasetFieldType

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"));
    });
}
Also used : SetDatasetCitationDateCommand(edu.harvard.iq.dataverse.engine.command.impl.SetDatasetCitationDateCommand) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 8 with DatasetFieldType

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;
        }
    }
}
Also used : DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) NoResultException(javax.persistence.NoResultException) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) EJBException(javax.ejb.EJBException) ForeignMetadataFieldMapping(edu.harvard.iq.dataverse.ForeignMetadataFieldMapping) MetadataBlockDTO(edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO) EJBException(javax.ejb.EJBException) FieldDTO(edu.harvard.iq.dataverse.api.dto.FieldDTO)

Example 9 with DatasetFieldType

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);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType)

Example 10 with DatasetFieldType

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);
}
Also used : DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) FieldDTO(edu.harvard.iq.dataverse.api.dto.FieldDTO)

Aggregations

DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)41 DatasetField (edu.harvard.iq.dataverse.DatasetField)16 ControlledVocabularyValue (edu.harvard.iq.dataverse.ControlledVocabularyValue)12 DatasetFieldCompoundValue (edu.harvard.iq.dataverse.DatasetFieldCompoundValue)9 ArrayList (java.util.ArrayList)9 JsonObject (javax.json.JsonObject)7 Dataverse (edu.harvard.iq.dataverse.Dataverse)6 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 MetadataBlock (edu.harvard.iq.dataverse.MetadataBlock)5 EJBException (javax.ejb.EJBException)5 Path (javax.ws.rs.Path)5 DatasetFieldValue (edu.harvard.iq.dataverse.DatasetFieldValue)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 NoResultException (javax.persistence.NoResultException)4 GET (javax.ws.rs.GET)4 Before (org.junit.Before)4 DataverseFieldTypeInputLevel (edu.harvard.iq.dataverse.DataverseFieldTypeInputLevel)3 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3