use of edu.harvard.iq.dataverse.DatasetField in project dataverse by IQSS.
the class JsonParserTest method latLonField.
DatasetField latLonField(String latLon, String value) {
DatasetField retVal = new DatasetField();
retVal.setDatasetFieldType(datasetFieldTypeSvc.findByName(latLon));
retVal.setDatasetFieldValues(Collections.singletonList(new DatasetFieldValue(retVal, value)));
return retVal;
}
use of edu.harvard.iq.dataverse.DatasetField in project dataverse by IQSS.
the class JsonParserTest method testControlledVocalRepeatsRoundTrip.
@Test
public void testControlledVocalRepeatsRoundTrip() throws JsonParseException {
DatasetField expected = new DatasetField();
DatasetFieldType fieldType = datasetFieldTypeSvc.findByName("subject");
expected.setDatasetFieldType(fieldType);
expected.setControlledVocabularyValues(Arrays.asList(fieldType.getControlledVocabularyValue("mgmt"), fieldType.getControlledVocabularyValue("law"), fieldType.getControlledVocabularyValue("cs")));
JsonObject json = JsonPrinter.json(expected);
DatasetField actual = sut.parseField(json);
assertFieldsEqual(expected, actual);
}
use of edu.harvard.iq.dataverse.DatasetField 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.DatasetField in project dataverse by IQSS.
the class JsonParserTest method testPrimitiveNoRepeatesFieldRoundTrip.
@Test
public void testPrimitiveNoRepeatesFieldRoundTrip() throws JsonParseException {
DatasetField expected = new DatasetField();
expected.setDatasetFieldType(datasetFieldTypeSvc.findByName("description"));
expected.setDatasetFieldValues(Collections.singletonList(new DatasetFieldValue(expected, "This is a description value")));
JsonObject json = JsonPrinter.json(expected);
DatasetField actual = sut.parseField(json);
assertFieldsEqual(actual, expected);
}
use of edu.harvard.iq.dataverse.DatasetField 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;
}
Aggregations