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;
}
}
}
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;
}
}
}
Aggregations