Search in sources :

Example 21 with DatasetFieldType

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

the class UpdateDataverseCommand method execute.

@Override
public Dataverse execute(CommandContext ctxt) throws CommandException {
    Dataverse result = ctxt.dataverses().save(editedDv);
    if (facetList != null) {
        ctxt.facets().deleteFacetsFor(result);
        int i = 0;
        for (DatasetFieldType df : facetList) {
            ctxt.facets().create(i++, df.getId(), result.getId());
        }
    }
    if (featuredDataverseList != null) {
        ctxt.featuredDataverses().deleteFeaturedDataversesFor(result);
        int i = 0;
        for (Object obj : featuredDataverseList) {
            Dataverse dv = (Dataverse) obj;
            ctxt.featuredDataverses().create(i++, dv.getId(), result.getId());
        }
    }
    if (inputLevelList != null) {
        ctxt.fieldTypeInputLevels().deleteFacetsFor(result);
        for (DataverseFieldTypeInputLevel obj : inputLevelList) {
            ctxt.fieldTypeInputLevels().create(obj);
        }
    }
    ctxt.index().indexDataverse(result);
    return result;
}
Also used : DataverseFieldTypeInputLevel(edu.harvard.iq.dataverse.DataverseFieldTypeInputLevel) Dataverse(edu.harvard.iq.dataverse.Dataverse) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType)

Example 22 with DatasetFieldType

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

the class SwordServiceBean method addDatasetContact.

/**
 * Mutate the dataset version, adding a datasetContact (email address) from
 * the dataverse that will own the dataset.
 */
public void addDatasetContact(DatasetVersion newDatasetVersion, User user) {
    DatasetFieldType emailDatasetFieldType = datasetFieldService.findByNameOpt(DatasetFieldConstant.datasetContact);
    DatasetField emailDatasetField = DatasetField.createNewEmptyDatasetField(emailDatasetFieldType, newDatasetVersion);
    for (DatasetField childField : emailDatasetField.getDatasetFieldCompoundValues().get(0).getChildDatasetFields()) {
        if (DatasetFieldConstant.datasetContactEmail.equals(childField.getDatasetFieldType().getName())) {
            // set the value to the  in user's email
            childField.getSingleValue().setValue(user.getDisplayInfo().getEmailAddress());
        }
        // We don't see any error from EZID but when using DataCite, we were seeing this error: Response code: 400, [xml] xml error: cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_contributorNamecontributorcontributorsresource'.
        if (DatasetFieldConstant.datasetContactName.equals(childField.getDatasetFieldType().getName())) {
            childField.getSingleValue().setValue(user.getDisplayInfo().getTitle());
        }
    }
    newDatasetVersion.getDatasetFields().add(emailDatasetField);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType)

Example 23 with DatasetFieldType

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

the class SwordServiceBean method addDatasetSubjectIfMissing.

/**
 * If no subject exists, mutate the dataset version, adding "N/A" for the
 * subject. Otherwise, leave the dataset alone.
 */
public void addDatasetSubjectIfMissing(DatasetVersion datasetVersion) {
    DatasetFieldType subjectDatasetFieldType = datasetFieldService.findByNameOpt(DatasetFieldConstant.subject);
    boolean subjectFieldExists = false;
    List<DatasetField> datasetFields = datasetVersion.getDatasetFields();
    for (DatasetField datasetField : datasetFields) {
        logger.fine("datasetField: " + datasetField.getDisplayValue() + " ... " + datasetField.getDatasetFieldType().getName());
        if (datasetField.getDatasetFieldType().getName().equals(subjectDatasetFieldType.getName())) {
            subjectFieldExists = true;
            logger.fine("subject field exists already");
            break;
        }
    }
    if (subjectFieldExists) {
        // return early. nothing to do. dataset already has a subject
        logger.fine("returning early because subject exists already");
        return;
    }
    // if we made it here, we must not have a subject, so let's add one
    DatasetField subjectDatasetField = DatasetField.createNewEmptyDatasetField(subjectDatasetFieldType, datasetVersion);
    /**
     * @todo Once dataverse has subject
     * (https://github.com/IQSS/dataverse/issues/769), we should get subject
     * from there for now, we'll use the global NA value. However, there is
     * currently oddness in that if you go to edit the title of a dataset
     * via the GUI you can not save the dataset without selecting a Subject:
     * https://github.com/IQSS/dataverse/issues/1296#issuecomment-70146314
     */
    ControlledVocabularyValue cvv = datasetFieldService.findNAControlledVocabularyValue();
    subjectDatasetField.setSingleControlledVocabularyValue(cvv);
    datasetVersion.getDatasetFields().add(subjectDatasetField);
}
Also used : DatasetField(edu.harvard.iq.dataverse.DatasetField) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue)

Example 24 with DatasetFieldType

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

the class ImportDDIServiceBean method processCustomField.

private void processCustomField(XMLStreamReader xmlr, DatasetVersionDTO dvDTO) throws XMLStreamException, ImportException {
    String subject = xmlr.getAttributeValue(null, "subject");
    if (!subject.isEmpty()) {
        // Syntax of subject attribute:
        // TEMPLATE:Contains Custom Fields;FIELD:Customfield1
        // first parse by semicolon
        String template = subject.substring(subject.indexOf(":") + 1, subject.indexOf(";"));
        String sourceField = subject.substring(subject.lastIndexOf(":") + 1);
        String fieldValue = parseText(xmlr);
        CustomFieldMap map = customFieldService.findByTemplateField(template.trim(), sourceField.trim());
        if (map == null) {
            throw new ImportException("Did not find mapping for template: " + template + ", sourceField: " + sourceField);
        }
        if (map.getTargetDatasetField().endsWith("#IGNORE")) {
            // copy this field from 3.6 to 4.0
            return;
        }
        // 1. Get datasetFieldType for the targetField
        // 2. find the metadatablock for this field type
        // 3. If this metadatablock doesn't exist in DTO, create it
        // 4. add field to mdatadatablock
        DatasetFieldType dsfType = datasetFieldService.findByName(map.getTargetDatasetField());
        if (dsfType == null) {
            throw new ImportException("Did not find datasetField for target: " + map.getTargetDatasetField());
        }
        String metadataBlockName = dsfType.getMetadataBlock().getName();
        MetadataBlockDTO customBlock = dvDTO.getMetadataBlocks().get(metadataBlockName);
        if (customBlock == null) {
            customBlock = new MetadataBlockDTO();
            customBlock.setDisplayName(metadataBlockName);
            dvDTO.getMetadataBlocks().put(metadataBlockName, customBlock);
        }
        if (dsfType.isChild()) {
            handleChildField(customBlock, dsfType, fieldValue);
        } else {
            if (dsfType.isAllowMultiples()) {
                List<String> valList = new ArrayList<>();
                valList.add(fieldValue);
                if (dsfType.isAllowControlledVocabulary()) {
                    customBlock.addField(FieldDTO.createMultipleVocabFieldDTO(dsfType.getName(), valList));
                } else if (dsfType.isPrimitive()) {
                    customBlock.addField(FieldDTO.createMultiplePrimitiveFieldDTO(dsfType.getName(), valList));
                } else {
                    throw new ImportException("Unsupported custom field type: " + dsfType);
                }
            } else {
                if (dsfType.isAllowControlledVocabulary()) {
                    customBlock.addField(FieldDTO.createVocabFieldDTO(dsfType.getName(), fieldValue));
                } else if (dsfType.isPrimitive()) {
                    customBlock.addField(FieldDTO.createPrimitiveFieldDTO(dsfType.getName(), fieldValue));
                } else {
                    throw new ImportException("Unsupported custom field type: " + dsfType);
                }
            }
        }
    }
}
Also used : MetadataBlockDTO(edu.harvard.iq.dataverse.api.dto.MetadataBlockDTO) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType)

Example 25 with DatasetFieldType

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

the class JsonPrinterTest method testDatasetContactWithPrivacy.

@Test
public void testDatasetContactWithPrivacy() {
    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);
    JsonPrinter jsonPrinter = new JsonPrinter(new MockSettingsSvc());
    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(null, jsonObject.getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail"));
    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(null, byBlocks.getJsonObject("citation").getJsonArray("fields").getJsonObject(0).getJsonArray("value").getJsonObject(0).getJsonObject("datasetContactEmail"));
}
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) Test(org.junit.Test)

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