Search in sources :

Example 1 with ForeignMetadataFieldMapping

use of edu.harvard.iq.dataverse.ForeignMetadataFieldMapping 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 2 with ForeignMetadataFieldMapping

use of edu.harvard.iq.dataverse.ForeignMetadataFieldMapping in project dataverse by IQSS.

the class ForeignMetadataImportServiceBean method processXMLElement.

private void processXMLElement(XMLStreamReader xmlr, String currentPath, String openingTag, ForeignMetadataFormatMapping foreignFormatMapping, DatasetVersion datasetVersion) 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) {
                DatasetFieldCompoundValue cachedCompoundValue = null;
                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 {
                                    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 + "!");
                            }
                        }
                    }
                }
                // Process the payload of this XML element:
                String dataverseFieldName = mappingDefined.getDatasetfieldName();
                if (dataverseFieldName != null && !dataverseFieldName.equals("")) {
                    DatasetFieldType dataverseFieldType = datasetfieldService.findByNameOpt(dataverseFieldName);
                    if (dataverseFieldType != null) {
                        String elementTextPayload = parseText(xmlr);
                        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, datasetVersion);
            }
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            if (xmlr.getLocalName().equals(openingTag))
                return;
        }
    }
}
Also used : EJBException(javax.ejb.EJBException) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) NoResultException(javax.persistence.NoResultException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) EJBException(javax.ejb.EJBException) ForeignMetadataFieldMapping(edu.harvard.iq.dataverse.ForeignMetadataFieldMapping)

Aggregations

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