use of edu.harvard.iq.dataverse.DatasetField in project dataverse by IQSS.
the class JsonParserTest method testControlledVocalNoRepeatsRoundTrip.
@Test
public void testControlledVocalNoRepeatsRoundTrip() throws JsonParseException {
DatasetField expected = new DatasetField();
DatasetFieldType fieldType = datasetFieldTypeSvc.findByName("publicationIdType");
expected.setDatasetFieldType(fieldType);
expected.setControlledVocabularyValues(Collections.singletonList(fieldType.getControlledVocabularyValue("ark")));
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 testCompoundRepeatsRoundtrip.
@Test
public void testCompoundRepeatsRoundtrip() throws JsonParseException {
DatasetField expected = new DatasetField();
expected.setDatasetFieldType(datasetFieldTypeSvc.findByName("coordinate"));
List<DatasetFieldCompoundValue> vals = new LinkedList<>();
for (int i = 0; i < 5; i++) {
DatasetFieldCompoundValue val = new DatasetFieldCompoundValue();
val.setParentDatasetField(expected);
val.setChildDatasetFields(Arrays.asList(latLonField("lat", Integer.toString(i * 10)), latLonField("lon", Integer.toString(3 + i * 10))));
vals.add(val);
}
expected.setDatasetFieldCompoundValues(vals);
JsonObject json = JsonPrinter.json(expected);
System.out.println("json = " + json);
DatasetField actual = sut.parseField(json);
assertFieldsEqual(expected, actual);
}
use of edu.harvard.iq.dataverse.DatasetField in project dataverse by IQSS.
the class FinalizeDatasetPublicationCommand method updateParentDataversesSubjectsField.
/**
* add the dataset subjects to all parent dataverses.
*/
private void updateParentDataversesSubjectsField(Dataset savedDataset, CommandContext ctxt) {
for (DatasetField dsf : savedDataset.getLatestVersion().getDatasetFields()) {
if (dsf.getDatasetFieldType().getName().equals(DatasetFieldConstant.subject)) {
Dataverse dv = savedDataset.getOwner();
while (dv != null) {
if (dv.getDataverseSubjects().addAll(dsf.getControlledVocabularyValues())) {
Dataverse dvWithSubjectJustAdded = ctxt.em().merge(dv);
ctxt.em().flush();
// need to reindex to capture the new subjects
ctxt.index().indexDataverse(dvWithSubjectJustAdded);
}
dv = dv.getOwner();
}
break;
}
}
}
use of edu.harvard.iq.dataverse.DatasetField in project dataverse by IQSS.
the class CreateDatasetVersionCommand method execute.
@Override
public DatasetVersion execute(CommandContext ctxt) throws CommandException {
DatasetVersion latest = dataset.getLatestVersion();
if (latest.isWorkingCopy()) {
// before throwing an Exception
if (newVersion.getVersionState().equals(VersionState.DRAFT)) {
throw new IllegalCommandException("Latest version is already a draft. Cannot add another draft", this);
}
}
newVersion.setDataset(dataset);
newVersion.setDatasetFields(newVersion.initDatasetFields());
Set<ConstraintViolation> constraintViolations = newVersion.validate();
if (!constraintViolations.isEmpty()) {
String validationFailedString = "Validation failed:";
for (ConstraintViolation constraintViolation : constraintViolations) {
validationFailedString += " " + constraintViolation.getMessage();
}
throw new IllegalCommandException(validationFailedString, this);
}
Iterator<DatasetField> dsfIt = newVersion.getDatasetFields().iterator();
while (dsfIt.hasNext()) {
if (dsfIt.next().removeBlankDatasetFieldValues()) {
dsfIt.remove();
}
}
Iterator<DatasetField> dsfItSort = newVersion.getDatasetFields().iterator();
while (dsfItSort.hasNext()) {
dsfItSort.next().setValueDisplayOrder();
}
List<FileMetadata> newVersionMetadatum = new ArrayList<>(latest.getFileMetadatas().size());
for (FileMetadata fmd : latest.getFileMetadatas()) {
FileMetadata fmdCopy = fmd.createCopy();
fmdCopy.setDatasetVersion(newVersion);
newVersionMetadatum.add(fmdCopy);
}
newVersion.setFileMetadatas(newVersionMetadatum);
Timestamp now = new Timestamp(new Date().getTime());
newVersion.setCreateTime(now);
newVersion.setLastUpdateTime(now);
dataset.setModificationTime(now);
newVersion.setDataset(dataset);
final List<DatasetVersion> currentVersions = dataset.getVersions();
ArrayList<DatasetVersion> dsvs = new ArrayList<>(currentVersions.size());
dsvs.addAll(currentVersions);
dsvs.add(0, newVersion);
dataset.setVersions(dsvs);
// ctxt.index().indexDataset(dataset);
return ctxt.datasets().storeVersion(newVersion);
}
use of edu.harvard.iq.dataverse.DatasetField in project dataverse by IQSS.
the class JsonParser method parseField.
public DatasetField parseField(JsonObject json) throws JsonParseException {
if (json == null) {
return null;
}
DatasetField ret = new DatasetField();
DatasetFieldType type = datasetFieldSvc.findByNameOpt(json.getString("typeName", ""));
if (type == null) {
throw new JsonParseException("Can't find type '" + json.getString("typeName", "") + "'");
}
if (type.isAllowMultiples() != json.getBoolean("multiple")) {
throw new JsonParseException("incorrect multiple for field " + json.getString("typeName", ""));
}
if (type.isCompound() && !json.getString("typeClass").equals("compound")) {
throw new JsonParseException("incorrect typeClass for field " + json.getString("typeName", "") + ", should be compound.");
}
if (!type.isControlledVocabulary() && type.isPrimitive() && !json.getString("typeClass").equals("primitive")) {
throw new JsonParseException("incorrect typeClass for field: " + json.getString("typeName", "") + ", should be primitive");
}
if (type.isControlledVocabulary() && !json.getString("typeClass").equals("controlledVocabulary")) {
throw new JsonParseException("incorrect typeClass for field " + json.getString("typeName", "") + ", should be controlledVocabulary");
}
ret.setDatasetFieldType(type);
if (type.isCompound()) {
List<DatasetFieldCompoundValue> vals = parseCompoundValue(type, json);
for (DatasetFieldCompoundValue dsfcv : vals) {
dsfcv.setParentDatasetField(ret);
}
ret.setDatasetFieldCompoundValues(vals);
} else if (type.isControlledVocabulary()) {
List<ControlledVocabularyValue> vals = parseControlledVocabularyValue(type, json);
for (ControlledVocabularyValue cvv : vals) {
cvv.setDatasetFieldType(type);
}
ret.setControlledVocabularyValues(vals);
} else {
// primitive
List<DatasetFieldValue> values = parsePrimitiveValue(json);
for (DatasetFieldValue val : values) {
val.setDatasetField(ret);
}
ret.setDatasetFieldValues(values);
}
return ret;
}
Aggregations