Search in sources :

Example 6 with DatasetField

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;
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue)

Example 7 with DatasetField

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);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) JsonObject(javax.json.JsonObject) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) Test(org.junit.Test)

Example 8 with DatasetField

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() + "'");
}
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 9 with DatasetField

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);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 10 with DatasetField

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

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