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;
}
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;
}
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);
}
Aggregations