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