Search in sources :

Example 26 with DatasetField

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

Example 27 with DatasetField

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

Example 28 with DatasetField

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

Example 29 with DatasetField

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);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Timestamp(java.sql.Timestamp) Date(java.util.Date) ConstraintViolation(javax.validation.ConstraintViolation)

Example 30 with DatasetField

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;
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue)

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