use of edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO 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.api.dto.MetadataBlockDTO in project dataverse by IQSS.
the class ImportDDIServiceBean method initializeDataset.
public DatasetDTO initializeDataset() {
DatasetDTO datasetDTO = new DatasetDTO();
DatasetVersionDTO datasetVersionDTO = new DatasetVersionDTO();
datasetDTO.setDatasetVersion(datasetVersionDTO);
HashMap<String, MetadataBlockDTO> metadataBlocks = new HashMap<>();
datasetVersionDTO.setMetadataBlocks(metadataBlocks);
datasetVersionDTO.getMetadataBlocks().put("citation", new MetadataBlockDTO());
datasetVersionDTO.getMetadataBlocks().get("citation").setFields(new ArrayList<>());
datasetVersionDTO.getMetadataBlocks().put("socialscience", new MetadataBlockDTO());
datasetVersionDTO.getMetadataBlocks().get("socialscience").setFields(new ArrayList<>());
datasetVersionDTO.getMetadataBlocks().put("geospatial", new MetadataBlockDTO());
datasetVersionDTO.getMetadataBlocks().get("geospatial").setFields(new ArrayList<>());
return datasetDTO;
}
use of edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO in project dataverse by IQSS.
the class DdiExportUtil method writeSeriesElement.
private static void writeSeriesElement(XMLStreamWriter xmlw, DatasetVersionDTO datasetVersionDTO) throws XMLStreamException {
for (Map.Entry<String, MetadataBlockDTO> entry : datasetVersionDTO.getMetadataBlocks().entrySet()) {
String key = entry.getKey();
MetadataBlockDTO value = entry.getValue();
if ("citation".equals(key)) {
for (FieldDTO fieldDTO : value.getFields()) {
if (DatasetFieldConstant.series.equals(fieldDTO.getTypeName())) {
xmlw.writeStartElement("serStmt");
String seriesName = "";
String seriesInformation = "";
Set<FieldDTO> foo = fieldDTO.getSingleCompound();
for (Iterator<FieldDTO> iterator = foo.iterator(); iterator.hasNext(); ) {
FieldDTO next = iterator.next();
if (DatasetFieldConstant.seriesName.equals(next.getTypeName())) {
seriesName = next.getSinglePrimitive();
}
if (DatasetFieldConstant.seriesInformation.equals(next.getTypeName())) {
seriesInformation = next.getSinglePrimitive();
}
}
if (!seriesName.isEmpty()) {
xmlw.writeStartElement("serName");
xmlw.writeCharacters(seriesName);
// grantno
xmlw.writeEndElement();
}
if (!seriesInformation.isEmpty()) {
xmlw.writeStartElement("serInfo");
xmlw.writeCharacters(seriesInformation);
// grantno
xmlw.writeEndElement();
}
// serStmt
xmlw.writeEndElement();
}
}
}
}
}
use of edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO in project dataverse by IQSS.
the class DdiExportUtil method writeContactsElement.
private static void writeContactsElement(XMLStreamWriter xmlw, DatasetVersionDTO datasetVersionDTO) throws XMLStreamException {
for (Map.Entry<String, MetadataBlockDTO> entry : datasetVersionDTO.getMetadataBlocks().entrySet()) {
String key = entry.getKey();
MetadataBlockDTO value = entry.getValue();
if ("citation".equals(key)) {
for (FieldDTO fieldDTO : value.getFields()) {
if (DatasetFieldConstant.datasetContact.equals(fieldDTO.getTypeName())) {
String datasetContactName = "";
String datasetContactAffiliation = "";
String datasetContactEmail = "";
for (HashSet<FieldDTO> foo : fieldDTO.getMultipleCompound()) {
for (Iterator<FieldDTO> iterator = foo.iterator(); iterator.hasNext(); ) {
FieldDTO next = iterator.next();
if (DatasetFieldConstant.datasetContactName.equals(next.getTypeName())) {
datasetContactName = next.getSinglePrimitive();
}
if (DatasetFieldConstant.datasetContactAffiliation.equals(next.getTypeName())) {
datasetContactAffiliation = next.getSinglePrimitive();
}
if (DatasetFieldConstant.datasetContactEmail.equals(next.getTypeName())) {
datasetContactEmail = next.getSinglePrimitive();
}
}
// TODO: Since datasetContactEmail is a required field but datasetContactName is not consider not checking if datasetContactName is empty so we can write out datasetContactEmail.
if (!datasetContactName.isEmpty()) {
xmlw.writeStartElement("contact");
if (!datasetContactAffiliation.isEmpty()) {
writeAttribute(xmlw, "affiliation", datasetContactAffiliation);
}
if (!datasetContactEmail.isEmpty()) {
writeAttribute(xmlw, "email", datasetContactEmail);
}
xmlw.writeCharacters(datasetContactName);
// AuthEnty
xmlw.writeEndElement();
}
}
}
}
}
}
}
use of edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO in project dataverse by IQSS.
the class DdiExportUtil method writeAbstractElement.
private static void writeAbstractElement(XMLStreamWriter xmlw, DatasetVersionDTO datasetVersionDTO) throws XMLStreamException {
for (Map.Entry<String, MetadataBlockDTO> entry : datasetVersionDTO.getMetadataBlocks().entrySet()) {
String key = entry.getKey();
MetadataBlockDTO value = entry.getValue();
if ("citation".equals(key)) {
for (FieldDTO fieldDTO : value.getFields()) {
if (DatasetFieldConstant.description.equals(fieldDTO.getTypeName())) {
String descriptionText = "";
String descriptionDate = "";
for (HashSet<FieldDTO> foo : fieldDTO.getMultipleCompound()) {
for (Iterator<FieldDTO> iterator = foo.iterator(); iterator.hasNext(); ) {
FieldDTO next = iterator.next();
if (DatasetFieldConstant.descriptionText.equals(next.getTypeName())) {
descriptionText = next.getSinglePrimitive();
}
if (DatasetFieldConstant.descriptionDate.equals(next.getTypeName())) {
descriptionDate = next.getSinglePrimitive();
}
}
if (!descriptionText.isEmpty()) {
xmlw.writeStartElement("abstract");
if (!descriptionDate.isEmpty()) {
writeAttribute(xmlw, "date", descriptionDate);
}
xmlw.writeCharacters(descriptionText);
// abstract
xmlw.writeEndElement();
}
}
}
}
}
}
}
Aggregations