Search in sources :

Example 31 with DatasetField

use of edu.harvard.iq.dataverse.DatasetField 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 32 with DatasetField

use of edu.harvard.iq.dataverse.DatasetField 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

DatasetField (edu.harvard.iq.dataverse.DatasetField)32 DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)16 JsonObject (javax.json.JsonObject)13 DatasetFieldCompoundValue (edu.harvard.iq.dataverse.DatasetFieldCompoundValue)11 DatasetFieldValue (edu.harvard.iq.dataverse.DatasetFieldValue)11 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)8 ControlledVocabularyValue (edu.harvard.iq.dataverse.ControlledVocabularyValue)7 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)7 LinkedList (java.util.LinkedList)7 Dataset (edu.harvard.iq.dataverse.Dataset)6 DataFile (edu.harvard.iq.dataverse.DataFile)4 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)4 ConstraintViolation (javax.validation.ConstraintViolation)4 Gson (com.google.gson.Gson)3 Dataverse (edu.harvard.iq.dataverse.Dataverse)3 MetadataBlock (edu.harvard.iq.dataverse.MetadataBlock)3 StringReader (java.io.StringReader)3 Timestamp (java.sql.Timestamp)3 Date (java.util.Date)3