Search in sources :

Example 11 with DatasetFieldCompoundValue

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

the class JsonParser method parseField.

public DatasetField parseField(JsonObject json) throws JsonParseException {
    if (json == null) {
        return null;
    }
    DatasetField ret = new DatasetField();
    DatasetFieldType type = datasetFieldSvc.findByNameOpt(json.getString("typeName", ""));
    if (type == null) {
        throw new JsonParseException("Can't find type '" + json.getString("typeName", "") + "'");
    }
    if (type.isAllowMultiples() != json.getBoolean("multiple")) {
        throw new JsonParseException("incorrect multiple   for field " + json.getString("typeName", ""));
    }
    if (type.isCompound() && !json.getString("typeClass").equals("compound")) {
        throw new JsonParseException("incorrect  typeClass for field " + json.getString("typeName", "") + ", should be compound.");
    }
    if (!type.isControlledVocabulary() && type.isPrimitive() && !json.getString("typeClass").equals("primitive")) {
        throw new JsonParseException("incorrect  typeClass for field: " + json.getString("typeName", "") + ", should be primitive");
    }
    if (type.isControlledVocabulary() && !json.getString("typeClass").equals("controlledVocabulary")) {
        throw new JsonParseException("incorrect  typeClass for field " + json.getString("typeName", "") + ", should be controlledVocabulary");
    }
    ret.setDatasetFieldType(type);
    if (type.isCompound()) {
        List<DatasetFieldCompoundValue> vals = parseCompoundValue(type, json);
        for (DatasetFieldCompoundValue dsfcv : vals) {
            dsfcv.setParentDatasetField(ret);
        }
        ret.setDatasetFieldCompoundValues(vals);
    } else if (type.isControlledVocabulary()) {
        List<ControlledVocabularyValue> vals = parseControlledVocabularyValue(type, json);
        for (ControlledVocabularyValue cvv : vals) {
            cvv.setDatasetFieldType(type);
        }
        ret.setControlledVocabularyValues(vals);
    } else {
        // primitive
        List<DatasetFieldValue> values = parsePrimitiveValue(json);
        for (DatasetFieldValue val : values) {
            val.setDatasetField(ret);
        }
        ret.setDatasetFieldValues(values);
    }
    return ret;
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue)

Example 12 with DatasetFieldCompoundValue

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

the class JsonParser method parseCompoundValue.

public List<DatasetFieldCompoundValue> parseCompoundValue(DatasetFieldType compoundType, JsonObject json) throws JsonParseException {
    List<ControlledVocabularyException> vocabExceptions = new ArrayList<>();
    List<DatasetFieldCompoundValue> vals = new LinkedList<>();
    if (json.getBoolean("multiple")) {
        int order = 0;
        for (JsonObject obj : json.getJsonArray("value").getValuesAs(JsonObject.class)) {
            DatasetFieldCompoundValue cv = new DatasetFieldCompoundValue();
            List<DatasetField> fields = new LinkedList<>();
            for (String fieldName : obj.keySet()) {
                JsonObject childFieldJson = obj.getJsonObject(fieldName);
                DatasetField f = null;
                try {
                    f = parseField(childFieldJson);
                } catch (ControlledVocabularyException ex) {
                    vocabExceptions.add(ex);
                }
                if (f != null) {
                    if (!compoundType.getChildDatasetFieldTypes().contains(f.getDatasetFieldType())) {
                        throw new JsonParseException("field " + f.getDatasetFieldType().getName() + " is not a child of " + compoundType.getName());
                    }
                    f.setParentDatasetFieldCompoundValue(cv);
                    fields.add(f);
                }
            }
            if (!fields.isEmpty()) {
                cv.setChildDatasetFields(fields);
                cv.setDisplayOrder(order);
                vals.add(cv);
            }
            order++;
        }
    } else {
        DatasetFieldCompoundValue cv = new DatasetFieldCompoundValue();
        List<DatasetField> fields = new LinkedList<>();
        JsonObject value = json.getJsonObject("value");
        for (String key : value.keySet()) {
            JsonObject childFieldJson = value.getJsonObject(key);
            DatasetField f = null;
            try {
                f = parseField(childFieldJson);
            } catch (ControlledVocabularyException ex) {
                vocabExceptions.add(ex);
            }
            if (f != null) {
                f.setParentDatasetFieldCompoundValue(cv);
                fields.add(f);
            }
        }
        if (!fields.isEmpty()) {
            cv.setChildDatasetFields(fields);
            vals.add(cv);
        }
    }
    if (!vocabExceptions.isEmpty()) {
        throw new CompoundVocabularyException("Invalid controlled vocabulary in compound field ", vocabExceptions, vals);
    }
    return vals;
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) LinkedList(java.util.LinkedList)

Example 13 with DatasetFieldCompoundValue

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

the class DatasetFieldWalker method walk.

public void walk(DatasetField fld, SettingsServiceBean settingsService) {
    l.startField(fld);
    DatasetFieldType datasetFieldType = fld.getDatasetFieldType();
    if (datasetFieldType.isControlledVocabulary()) {
        for (ControlledVocabularyValue cvv : sort(fld.getControlledVocabularyValues(), ControlledVocabularyValue.DisplayOrder)) {
            l.controledVocabularyValue(cvv);
        }
    } else if (datasetFieldType.isPrimitive()) {
        for (DatasetFieldValue pv : sort(fld.getDatasetFieldValues(), DatasetFieldValue.DisplayOrder)) {
            if (settingsService != null && settingsService.isTrueForKey(SettingsServiceBean.Key.ExcludeEmailFromExport, false) && DatasetFieldType.FieldType.EMAIL.equals(pv.getDatasetField().getDatasetFieldType().getFieldType())) {
                continue;
            }
            l.primitiveValue(pv);
        }
    } else if (datasetFieldType.isCompound()) {
        for (DatasetFieldCompoundValue dsfcv : sort(fld.getDatasetFieldCompoundValues(), DatasetFieldCompoundValue.DisplayOrder)) {
            l.startCompoundValue(dsfcv);
            for (DatasetField dsf : sort(dsfcv.getChildDatasetFields(), DatasetField.DisplayOrder)) {
                walk(dsf, settingsService);
            }
            l.endCompoundValue(dsfcv);
        }
    }
    l.endField(fld);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) 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