Search in sources :

Example 1 with DataFileCategory

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

the class MocksFactory method makeDataset.

public static Dataset makeDataset() {
    Dataset ds = new Dataset();
    ds.setId(nextId());
    ds.setIdentifier("sample-ds-" + ds.getId());
    ds.setCategoriesByName(Arrays.asList("CatOne", "CatTwo", "CatThree"));
    final List<DataFile> files = makeFiles(10);
    final List<FileMetadata> metadatas = new ArrayList<>(10);
    final List<DataFileCategory> categories = ds.getCategories();
    Random rand = new Random();
    files.forEach(df -> {
        df.getFileMetadata().addCategory(categories.get(rand.nextInt(categories.size())));
        metadatas.add(df.getFileMetadata());
    });
    ds.setFiles(files);
    final DatasetVersion initialVersion = ds.getVersions().get(0);
    initialVersion.setFileMetadatas(metadatas);
    List<DatasetField> fields = new ArrayList<>();
    DatasetField field = new DatasetField();
    field.setId(nextId());
    field.setSingleValue("Sample Field Value");
    field.setDatasetFieldType(makeDatasetFieldType());
    fields.add(field);
    initialVersion.setDatasetFields(fields);
    ds.setOwner(makeDataverse());
    return ds;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataFileCategory(edu.harvard.iq.dataverse.DataFileCategory) Random(java.util.Random) DatasetField(edu.harvard.iq.dataverse.DatasetField) Dataset(edu.harvard.iq.dataverse.Dataset) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion)

Example 2 with DataFileCategory

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

the class JsonPrinterTest method testGetFileCategories.

@Test
public void testGetFileCategories() {
    FileMetadata fmd = new FileMetadata();
    DatasetVersion dsVersion = new DatasetVersion();
    DataFile dataFile = new DataFile();
    List<DataFileTag> dataFileTags = new ArrayList<>();
    DataFileTag tag = new DataFileTag();
    tag.setTypeByLabel("Survey");
    dataFileTags.add(tag);
    dataFile.setTags(dataFileTags);
    fmd.setDatasetVersion(dsVersion);
    fmd.setDataFile(dataFile);
    List<DataFileCategory> fileCategories = new ArrayList<>();
    DataFileCategory dataFileCategory = new DataFileCategory();
    dataFileCategory.setName("Data");
    fileCategories.add(dataFileCategory);
    fmd.setCategories(fileCategories);
    JsonObjectBuilder job = JsonPrinter.json(fmd);
    assertNotNull(job);
    JsonObject jsonObject = job.build();
    System.out.println("json: " + jsonObject);
    assertEquals("", jsonObject.getString("description"));
    assertEquals("", jsonObject.getString("label"));
    assertEquals("Data", jsonObject.getJsonArray("categories").getString(0));
    assertEquals("", jsonObject.getJsonObject("dataFile").getString("filename"));
    assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("filesize"));
    assertEquals("UNKNOWN", jsonObject.getJsonObject("dataFile").getString("originalFormatLabel"));
    assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("rootDataFileId"));
    assertEquals("Survey", jsonObject.getJsonObject("dataFile").getJsonArray("tabularTags").getString(0));
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataFileCategory(edu.harvard.iq.dataverse.DataFileCategory) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder) DataFileTag(edu.harvard.iq.dataverse.DataFileTag) Test(org.junit.Test)

Example 3 with DataFileCategory

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

the class JsonParser method getCategories.

private List<DataFileCategory> getCategories(JsonObject filemetadataJson, Dataset dataset) {
    JsonArray categories = filemetadataJson.getJsonArray(OptionalFileParams.CATEGORIES_ATTR_NAME);
    if (categories == null || categories.isEmpty() || dataset == null) {
        return null;
    }
    List<DataFileCategory> dataFileCategories = new ArrayList<>();
    for (Object category : categories.getValuesAs(JsonString.class)) {
        JsonString categoryAsJsonString;
        try {
            categoryAsJsonString = (JsonString) category;
        } catch (ClassCastException ex) {
            logger.info("ClassCastException caught in getCategories: " + ex);
            return null;
        }
        DataFileCategory dfc = new DataFileCategory();
        dfc.setDataset(dataset);
        dfc.setName(categoryAsJsonString.getString());
        dataFileCategories.add(dfc);
    }
    return dataFileCategories;
}
Also used : JsonArray(javax.json.JsonArray) DataFileCategory(edu.harvard.iq.dataverse.DataFileCategory) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Aggregations

DataFileCategory (edu.harvard.iq.dataverse.DataFileCategory)3 ArrayList (java.util.ArrayList)3 DataFile (edu.harvard.iq.dataverse.DataFile)2 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)2 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)2 JsonObject (javax.json.JsonObject)2 DataFileTag (edu.harvard.iq.dataverse.DataFileTag)1 Dataset (edu.harvard.iq.dataverse.Dataset)1 DatasetField (edu.harvard.iq.dataverse.DatasetField)1 Random (java.util.Random)1 JsonArray (javax.json.JsonArray)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 JsonString (javax.json.JsonString)1 Test (org.junit.Test)1