use of edu.harvard.iq.dataverse.DatasetFieldCompoundValue 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.DatasetFieldCompoundValue in project dataverse by IQSS.
the class JsonPrinterTest method testDatasetContactOutOfBoxNoPrivacy.
@Test
public void testDatasetContactOutOfBoxNoPrivacy() {
MetadataBlock block = new MetadataBlock();
block.setName("citation");
List<DatasetField> fields = new ArrayList<>();
DatasetField datasetContactField = new DatasetField();
DatasetFieldType datasetContactDatasetFieldType = datasetFieldTypeSvc.findByName("datasetContact");
datasetContactDatasetFieldType.setMetadataBlock(block);
datasetContactField.setDatasetFieldType(datasetContactDatasetFieldType);
List<DatasetFieldCompoundValue> vals = new LinkedList<>();
DatasetFieldCompoundValue val = new DatasetFieldCompoundValue();
val.setParentDatasetField(datasetContactField);
val.setChildDatasetFields(Arrays.asList(constructPrimitive("datasetContactEmail", "foo@bar.com"), constructPrimitive("datasetContactName", "Foo Bar"), constructPrimitive("datasetContactAffiliation", "Bar University")));
vals.add(val);
datasetContactField.setDatasetFieldCompoundValues(vals);
fields.add(datasetContactField);
SettingsServiceBean nullServiceBean = null;
JsonPrinter jsonPrinter = new JsonPrinter(nullServiceBean);
JsonObject jsonObject = jsonPrinter.json(block, fields).build();
assertNotNull(jsonObject);
System.out.println("json: " + JsonUtil.prettyPrint(jsonObject.toString()));
assertEquals("Foo Bar", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
assertEquals("Bar University", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
assertEquals("foo@bar.com", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail").getString("value"));
JsonObject byBlocks = jsonPrinter.jsonByBlocks(fields).build();
System.out.println("byBlocks: " + JsonUtil.prettyPrint(byBlocks.toString()));
assertEquals("Foo Bar", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
assertEquals("Bar University", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
assertEquals("foo@bar.com", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail").getString("value"));
}
use of edu.harvard.iq.dataverse.DatasetFieldCompoundValue in project dataverse by IQSS.
the class JsonParserTest method assertFieldsEqual.
public boolean assertFieldsEqual(DatasetField ex, DatasetField act) {
if (ex == act)
return true;
if ((ex == null) ^ (act == null))
return false;
// type
if (!ex.getDatasetFieldType().equals(act.getDatasetFieldType()))
return false;
if (ex.getDatasetFieldType().isPrimitive()) {
List<DatasetFieldValue> exVals = ex.getDatasetFieldValues();
List<DatasetFieldValue> actVals = act.getDatasetFieldValues();
if (exVals.size() != actVals.size())
return false;
Iterator<DatasetFieldValue> exItr = exVals.iterator();
for (DatasetFieldValue actVal : actVals) {
DatasetFieldValue exVal = exItr.next();
if (!exVal.getValue().equals(actVal.getValue())) {
return false;
}
}
return true;
} else if (ex.getDatasetFieldType().isControlledVocabulary()) {
List<ControlledVocabularyValue> exVals = ex.getControlledVocabularyValues();
List<ControlledVocabularyValue> actVals = act.getControlledVocabularyValues();
if (exVals.size() != actVals.size())
return false;
Iterator<ControlledVocabularyValue> exItr = exVals.iterator();
for (ControlledVocabularyValue actVal : actVals) {
ControlledVocabularyValue exVal = exItr.next();
if (!exVal.getId().equals(actVal.getId())) {
return false;
}
}
return true;
} else if (ex.getDatasetFieldType().isCompound()) {
List<DatasetFieldCompoundValue> exVals = ex.getDatasetFieldCompoundValues();
List<DatasetFieldCompoundValue> actVals = act.getDatasetFieldCompoundValues();
if (exVals.size() != actVals.size())
return false;
Iterator<DatasetFieldCompoundValue> exItr = exVals.iterator();
for (DatasetFieldCompoundValue actVal : actVals) {
DatasetFieldCompoundValue exVal = exItr.next();
Iterator<DatasetField> exChildItr = exVal.getChildDatasetFields().iterator();
Iterator<DatasetField> actChildItr = actVal.getChildDatasetFields().iterator();
while (exChildItr.hasNext()) {
assertFieldsEqual(exChildItr.next(), actChildItr.next());
}
}
return true;
}
throw new IllegalArgumentException("Unknown dataset field type '" + ex.getDatasetFieldType() + "'");
}
use of edu.harvard.iq.dataverse.DatasetFieldCompoundValue in project dataverse by IQSS.
the class JsonParser method remapGeographicCoverage.
/**
* Special processing for GeographicCoverage compound field:
* Handle parsing exceptions caused by invalid controlled vocabulary in the "country" field by
* putting the invalid data in "otherGeographicCoverage" in a new compound value.
*
* @param ex - contains the invalid values to be processed
* @return a compound DatasetField that contains the newly created values, in addition to
* the original valid values.
* @throws JsonParseException
*/
private DatasetField remapGeographicCoverage(CompoundVocabularyException ex) throws JsonParseException {
List<HashSet<FieldDTO>> geoCoverageList = new ArrayList<>();
// For each exception, create HashSet of otherGeographic Coverage and add to list
for (ControlledVocabularyException vocabEx : ex.getExList()) {
HashSet<FieldDTO> set = new HashSet<>();
set.add(FieldDTO.createPrimitiveFieldDTO(DatasetFieldConstant.otherGeographicCoverage, vocabEx.getStrValue()));
geoCoverageList.add(set);
}
FieldDTO geoCoverageDTO = FieldDTO.createMultipleCompoundFieldDTO(DatasetFieldConstant.geographicCoverage, geoCoverageList);
// convert DTO to datasetField so we can back valid values.
Gson gson = new Gson();
String jsonString = gson.toJson(geoCoverageDTO);
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
JsonObject obj = jsonReader.readObject();
DatasetField geoCoverageField = parseField(obj);
// add back valid values
for (DatasetFieldCompoundValue dsfcv : ex.getValidValues()) {
if (!dsfcv.getChildDatasetFields().isEmpty()) {
dsfcv.setParentDatasetField(geoCoverageField);
geoCoverageField.getDatasetFieldCompoundValues().add(dsfcv);
}
}
return geoCoverageField;
}
use of edu.harvard.iq.dataverse.DatasetFieldCompoundValue in project dataverse by IQSS.
the class JsonParser method convertKeywordsToSubjects.
/**
* Special processing of keywords and subjects. All keywords and subjects will be input
* from foreign formats (DDI, dcterms, etc) as keywords.
* As part of the parsing, we will move keywords that match subject controlled vocabulary values
* into the subjects datasetField.
* @param fields - the parsed datasetFields
*/
public void convertKeywordsToSubjects(List<DatasetField> fields) {
DatasetField keywordField = null;
for (DatasetField field : fields) {
if (field.getDatasetFieldType().getName().equals("keyword")) {
keywordField = field;
break;
}
}
if (keywordField == null) {
// nothing to do.
return;
}
DatasetFieldType type = datasetFieldSvc.findByNameOpt(DatasetFieldConstant.subject);
// new list to hold subjects that we find
List<ControlledVocabularyValue> subjects = new ArrayList<>();
// Make new list to hold the non-subject keywords
List<DatasetFieldCompoundValue> filteredValues = new ArrayList<>();
for (DatasetFieldCompoundValue compoundVal : keywordField.getDatasetFieldCompoundValues()) {
// Loop through the child fields to find the "keywordValue" field
for (DatasetField childField : compoundVal.getChildDatasetFields()) {
if (childField.getDatasetFieldType().getName().equals(DatasetFieldConstant.keywordValue)) {
// check if this value is a subject
ControlledVocabularyValue cvv = datasetFieldSvc.findControlledVocabularyValueByDatasetFieldTypeAndStrValue(type, childField.getValue(), lenient);
if (cvv == null) {
// the keyword was not found in the subject list, so retain it in filtered list
filteredValues.add(compoundVal);
} else {
// save the value for our subject field
if (!subjects.contains(cvv)) {
subjects.add(cvv);
}
}
}
}
}
// if we have found any subjects in the keyword list, then update the keyword and subject fields appropriately.
if (subjects.size() > 0) {
keywordField.setDatasetFieldCompoundValues(filteredValues);
DatasetField subjectField = new DatasetField();
subjectField.setDatasetFieldType(type);
for (ControlledVocabularyValue val : subjects) {
int order = 0;
val.setDisplayOrder(order);
val.setDatasetFieldType(type);
order++;
}
subjectField.setControlledVocabularyValues(subjects);
fields.add(subjectField);
}
}
Aggregations