Search in sources :

Example 1 with DatasetFieldCompoundValue

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

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"));
}
Also used : MetadataBlock(edu.harvard.iq.dataverse.MetadataBlock) DatasetField(edu.harvard.iq.dataverse.DatasetField) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) LinkedList(java.util.LinkedList) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) Test(org.junit.Test)

Example 3 with DatasetFieldCompoundValue

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() + "'");
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) Iterator(java.util.Iterator) List(java.util.List) LinkedList(java.util.LinkedList) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue)

Example 4 with DatasetFieldCompoundValue

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;
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) HashSet(java.util.HashSet) FieldDTO(edu.harvard.iq.dataverse.api.dto.FieldDTO)

Example 5 with DatasetFieldCompoundValue

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);
    }
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) ArrayList(java.util.ArrayList) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue)

Aggregations

DatasetFieldCompoundValue (edu.harvard.iq.dataverse.DatasetFieldCompoundValue)13 DatasetField (edu.harvard.iq.dataverse.DatasetField)11 DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)9 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)6 ControlledVocabularyValue (edu.harvard.iq.dataverse.ControlledVocabularyValue)5 DatasetFieldValue (edu.harvard.iq.dataverse.DatasetFieldValue)5 JsonObject (javax.json.JsonObject)5 MetadataBlock (edu.harvard.iq.dataverse.MetadataBlock)3 Test (org.junit.Test)3 ForeignMetadataFieldMapping (edu.harvard.iq.dataverse.ForeignMetadataFieldMapping)2 FieldDTO (edu.harvard.iq.dataverse.api.dto.FieldDTO)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 List (java.util.List)2 EJBException (javax.ejb.EJBException)2 JsonString (javax.json.JsonString)2 NoResultException (javax.persistence.NoResultException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Gson (com.google.gson.Gson)1