Search in sources :

Example 11 with DatasetFieldType

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

the class JsonPrinterTest method setUp.

@Before
public void setUp() {
    datasetFieldTypeSvc = new JsonParserTest.MockDatasetFieldSvc();
    DatasetFieldType titleType = datasetFieldTypeSvc.add(new DatasetFieldType("title", FieldType.TEXTBOX, false));
    DatasetFieldType authorType = datasetFieldTypeSvc.add(new DatasetFieldType("author", FieldType.TEXT, true));
    Set<DatasetFieldType> authorChildTypes = new HashSet<>();
    authorChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("authorName", FieldType.TEXT, false)));
    authorChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("authorAffiliation", FieldType.TEXT, false)));
    authorChildTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("authorIdentifier", FieldType.TEXT, false)));
    DatasetFieldType authorIdentifierSchemeType = datasetFieldTypeSvc.add(new DatasetFieldType("authorIdentifierScheme", FieldType.TEXT, false));
    authorIdentifierSchemeType.setAllowControlledVocabulary(true);
    authorIdentifierSchemeType.setControlledVocabularyValues(Arrays.asList(// FIXME: Why aren't these enforced? Should be ORCID, etc.
    new ControlledVocabularyValue(1l, "foo", authorIdentifierSchemeType), new ControlledVocabularyValue(2l, "bar", authorIdentifierSchemeType), new ControlledVocabularyValue(3l, "baz", authorIdentifierSchemeType)));
    authorChildTypes.add(datasetFieldTypeSvc.add(authorIdentifierSchemeType));
    for (DatasetFieldType t : authorChildTypes) {
        t.setParentDatasetFieldType(authorType);
    }
    authorType.setChildDatasetFieldTypes(authorChildTypes);
    DatasetFieldType datasetContactType = datasetFieldTypeSvc.add(new DatasetFieldType("datasetContact", FieldType.TEXT, true));
    Set<DatasetFieldType> datasetContactTypes = new HashSet<>();
    datasetContactTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType(DatasetFieldConstant.datasetContactEmail, FieldType.EMAIL, false)));
    datasetContactTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("datasetContactName", FieldType.TEXT, false)));
    datasetContactTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("datasetContactAffiliation", FieldType.TEXT, false)));
    for (DatasetFieldType t : datasetContactTypes) {
        t.setParentDatasetFieldType(datasetContactType);
    }
    datasetContactType.setChildDatasetFieldTypes(datasetContactTypes);
    DatasetFieldType keywordType = datasetFieldTypeSvc.add(new DatasetFieldType("keyword", DatasetFieldType.FieldType.TEXT, true));
    DatasetFieldType descriptionType = datasetFieldTypeSvc.add(new DatasetFieldType("description", DatasetFieldType.FieldType.TEXTBOX, false));
    DatasetFieldType subjectType = datasetFieldTypeSvc.add(new DatasetFieldType("subject", DatasetFieldType.FieldType.TEXT, true));
    subjectType.setAllowControlledVocabulary(true);
    subjectType.setControlledVocabularyValues(Arrays.asList(new ControlledVocabularyValue(1l, "mgmt", subjectType), new ControlledVocabularyValue(2l, "law", subjectType), new ControlledVocabularyValue(3l, "cs", subjectType)));
    DatasetFieldType pubIdType = datasetFieldTypeSvc.add(new DatasetFieldType("publicationIdType", DatasetFieldType.FieldType.TEXT, false));
    pubIdType.setAllowControlledVocabulary(true);
    pubIdType.setControlledVocabularyValues(Arrays.asList(new ControlledVocabularyValue(1l, "ark", pubIdType), new ControlledVocabularyValue(2l, "doi", pubIdType), new ControlledVocabularyValue(3l, "url", pubIdType)));
    DatasetFieldType compoundSingleType = datasetFieldTypeSvc.add(new DatasetFieldType("coordinate", DatasetFieldType.FieldType.TEXT, true));
    Set<DatasetFieldType> childTypes = new HashSet<>();
    childTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("lat", DatasetFieldType.FieldType.TEXT, false)));
    childTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("lon", DatasetFieldType.FieldType.TEXT, false)));
    for (DatasetFieldType t : childTypes) {
        t.setParentDatasetFieldType(compoundSingleType);
    }
    compoundSingleType.setChildDatasetFieldTypes(childTypes);
// settingsSvc = new JsonParserTest.MockSettingsSvc();
// jsonPrinter = new JsonPrinter(settingsSvc);
}
Also used : DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) HashSet(java.util.HashSet) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue) Before(org.junit.Before)

Example 12 with DatasetFieldType

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

the class JsonPrinterTest method testDatasetContactOutOfBoxNoPrivacy.

@Test
public void testDatasetContactOutOfBoxNoPrivacy() {
    MetadataBlock block = new MetadataBlock();
    block.setName("citation");
    List<DatasetField> fields = new ArrayList<>();
    DatasetField datasetContactField = new DatasetField();
    DatasetFieldType datasetContactDatasetFieldType = datasetFieldTypeSvc.findByName("datasetContact");
    datasetContactDatasetFieldType.setMetadataBlock(block);
    datasetContactField.setDatasetFieldType(datasetContactDatasetFieldType);
    List<DatasetFieldCompoundValue> vals = new LinkedList<>();
    DatasetFieldCompoundValue val = new DatasetFieldCompoundValue();
    val.setParentDatasetField(datasetContactField);
    val.setChildDatasetFields(Arrays.asList(constructPrimitive("datasetContactEmail", "foo@bar.com"), constructPrimitive("datasetContactName", "Foo Bar"), constructPrimitive("datasetContactAffiliation", "Bar University")));
    vals.add(val);
    datasetContactField.setDatasetFieldCompoundValues(vals);
    fields.add(datasetContactField);
    SettingsServiceBean nullServiceBean = null;
    JsonPrinter jsonPrinter = new JsonPrinter(nullServiceBean);
    JsonObject jsonObject = jsonPrinter.json(block, fields).build();
    assertNotNull(jsonObject);
    System.out.println("json: " + JsonUtil.prettyPrint(jsonObject.toString()));
    assertEquals("Foo Bar", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
    assertEquals("Bar University", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
    assertEquals("foo@bar.com", jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail").getString("value"));
    JsonObject byBlocks = jsonPrinter.jsonByBlocks(fields).build();
    System.out.println("byBlocks: " + JsonUtil.prettyPrint(byBlocks.toString()));
    assertEquals("Foo Bar", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactName").getString("value"));
    assertEquals("Bar University", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactAffiliation").getString("value"));
    assertEquals("foo@bar.com", byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail").getString("value"));
}
Also used : MetadataBlock(edu.harvard.iq.dataverse.MetadataBlock) DatasetField(edu.harvard.iq.dataverse.DatasetField) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) LinkedList(java.util.LinkedList) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) Test(org.junit.Test)

Example 13 with DatasetFieldType

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

the class JsonParserTest method setUp.

@Before
public void setUp() {
    datasetFieldTypeSvc = new MockDatasetFieldSvc();
    keywordType = datasetFieldTypeSvc.add(new DatasetFieldType("keyword", FieldType.TEXT, true));
    descriptionType = datasetFieldTypeSvc.add(new DatasetFieldType("description", FieldType.TEXTBOX, false));
    subjectType = datasetFieldTypeSvc.add(new DatasetFieldType("subject", FieldType.TEXT, true));
    subjectType.setAllowControlledVocabulary(true);
    subjectType.setControlledVocabularyValues(Arrays.asList(new ControlledVocabularyValue(1l, "mgmt", subjectType), new ControlledVocabularyValue(2l, "law", subjectType), new ControlledVocabularyValue(3l, "cs", subjectType)));
    pubIdType = datasetFieldTypeSvc.add(new DatasetFieldType("publicationIdType", FieldType.TEXT, false));
    pubIdType.setAllowControlledVocabulary(true);
    pubIdType.setControlledVocabularyValues(Arrays.asList(new ControlledVocabularyValue(1l, "ark", pubIdType), new ControlledVocabularyValue(2l, "doi", pubIdType), new ControlledVocabularyValue(3l, "url", pubIdType)));
    compoundSingleType = datasetFieldTypeSvc.add(new DatasetFieldType("coordinate", FieldType.TEXT, true));
    Set<DatasetFieldType> childTypes = new HashSet<>();
    childTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("lat", FieldType.TEXT, false)));
    childTypes.add(datasetFieldTypeSvc.add(new DatasetFieldType("lon", FieldType.TEXT, false)));
    for (DatasetFieldType t : childTypes) {
        t.setParentDatasetFieldType(compoundSingleType);
    }
    compoundSingleType.setChildDatasetFieldTypes(childTypes);
    settingsSvc = new MockSettingsSvc();
    sut = new JsonParser(datasetFieldTypeSvc, null, settingsSvc);
}
Also used : DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 14 with DatasetFieldType

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

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

the class JsonParser method parseMetadataBlocks.

public List<DatasetField> parseMetadataBlocks(JsonObject json) throws JsonParseException {
    Set<String> keys = json.keySet();
    List<DatasetField> fields = new LinkedList<>();
    for (String blockName : keys) {
        JsonObject blockJson = json.getJsonObject(blockName);
        JsonArray fieldsJson = blockJson.getJsonArray("fields");
        for (JsonObject fieldJson : fieldsJson.getValuesAs(JsonObject.class)) {
            try {
                fields.add(parseField(fieldJson));
            } catch (CompoundVocabularyException ex) {
                DatasetFieldType fieldType = datasetFieldSvc.findByNameOpt(fieldJson.getString("typeName", ""));
                if (lenient && (DatasetFieldConstant.geographicCoverage).equals(fieldType.getName())) {
                    fields.add(remapGeographicCoverage(ex));
                } else {
                    // if not lenient mode, re-throw exception
                    throw ex;
                }
            }
        }
    }
    convertKeywordsToSubjects(fields);
    return fields;
}
Also used : JsonArray(javax.json.JsonArray) DatasetField(edu.harvard.iq.dataverse.DatasetField) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) LinkedList(java.util.LinkedList)

Aggregations

DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)41 DatasetField (edu.harvard.iq.dataverse.DatasetField)16 ControlledVocabularyValue (edu.harvard.iq.dataverse.ControlledVocabularyValue)12 DatasetFieldCompoundValue (edu.harvard.iq.dataverse.DatasetFieldCompoundValue)9 ArrayList (java.util.ArrayList)9 JsonObject (javax.json.JsonObject)7 Dataverse (edu.harvard.iq.dataverse.Dataverse)6 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 MetadataBlock (edu.harvard.iq.dataverse.MetadataBlock)5 EJBException (javax.ejb.EJBException)5 Path (javax.ws.rs.Path)5 DatasetFieldValue (edu.harvard.iq.dataverse.DatasetFieldValue)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 NoResultException (javax.persistence.NoResultException)4 GET (javax.ws.rs.GET)4 Before (org.junit.Before)4 DataverseFieldTypeInputLevel (edu.harvard.iq.dataverse.DataverseFieldTypeInputLevel)3 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3