Search in sources :

Example 31 with MetadataBlockDTO

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

Example 32 with MetadataBlockDTO

use of edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO in project dataverse by IQSS.

the class ImportDDIServiceBean method processCitation.

private void processCitation(ImportType importType, XMLStreamReader xmlr, DatasetDTO datasetDTO) throws XMLStreamException, ImportException {
    DatasetVersionDTO dvDTO = datasetDTO.getDatasetVersion();
    MetadataBlockDTO citation = datasetDTO.getDatasetVersion().getMetadataBlocks().get("citation");
    boolean distStatementProcessed = false;
    for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (xmlr.getLocalName().equals("titlStmt"))
                processTitlStmt(xmlr, datasetDTO);
            else if (xmlr.getLocalName().equals("rspStmt"))
                processRspStmt(xmlr, citation);
            else if (xmlr.getLocalName().equals("prodStmt"))
                processProdStmt(xmlr, citation);
            else if (xmlr.getLocalName().equals("distStmt")) {
                if (distStatementProcessed) {
                // We've already encountered one Distribution Statement in
                // this citation, we'll just skip any consecutive ones.
                // This is a defensive check against duplicate distStmt
                // in some DDIs (notably, from ICPSR)
                } else {
                    processDistStmt(xmlr, citation);
                    distStatementProcessed = true;
                }
            } else if (xmlr.getLocalName().equals("serStmt"))
                processSerStmt(xmlr, citation);
            else if (xmlr.getLocalName().equals("verStmt"))
                processVerStmt(importType, xmlr, dvDTO);
            else if (xmlr.getLocalName().equals("notes")) {
                String _note = parseNoteByType(xmlr, NOTE_TYPE_UNF);
                if (_note != null) {
                    datasetDTO.getDatasetVersion().setUNF(parseUNF(_note));
                } else {
                    processNotes(xmlr, dvDTO);
                }
            }
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            if (xmlr.getLocalName().equals("citation"))
                return;
        }
    }
}
Also used : MetadataBlockDTO(edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO)

Aggregations

MetadataBlockDTO (edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO)32 FieldDTO (edu.harvard.iq.dataverse.api.dto.FieldDTO)26 Map (java.util.Map)22 DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)2 DatasetFieldCompoundValue (edu.harvard.iq.dataverse.DatasetFieldCompoundValue)1 ForeignMetadataFieldMapping (edu.harvard.iq.dataverse.ForeignMetadataFieldMapping)1 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 EJBException (javax.ejb.EJBException)1 NoResultException (javax.persistence.NoResultException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1