Search in sources :

Example 16 with DatasetVersion

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

the class ContainerManagerImpl method replaceMetadata.

@Override
public DepositReceipt replaceMetadata(String uri, Deposit deposit, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
    logger.fine("replaceMetadata called with url: " + uri);
    urlManager.processUrl(uri);
    String targetType = urlManager.getTargetType();
    if (!targetType.isEmpty()) {
        logger.fine("operating on target type: " + urlManager.getTargetType());
        if ("dataverse".equals(targetType)) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Metadata replace of dataverse is not supported.");
        } else if ("study".equals(targetType)) {
            logger.fine("replacing metadata for dataset");
            // do a sanity check on the XML received
            try {
                SwordEntry swordEntry = deposit.getSwordEntry();
                logger.fine("deposit XML received by replaceMetadata():\n" + swordEntry);
            } catch (ParseException ex) {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Can not replace dataset metadata due to malformed Atom entry: " + ex);
            }
            String globalId = urlManager.getTargetIdentifier();
            Dataset dataset = datasetService.findByGlobalId(globalId);
            if (dataset != null) {
                Dataverse dvThatOwnsDataset = dataset.getOwner();
                UpdateDatasetCommand updateDatasetCommand = new UpdateDatasetCommand(dataset, dvReq);
                if (!permissionService.isUserAllowedOn(user, updateDatasetCommand, dataset)) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "User " + user.getDisplayInfo().getTitle() + " is not authorized to modify dataverse " + dvThatOwnsDataset.getAlias());
                }
                DatasetVersion datasetVersion = dataset.getEditVersion();
                // erase all metadata before creating populating dataset version
                List<DatasetField> emptyDatasetFields = new ArrayList<>();
                datasetVersion.setDatasetFields(emptyDatasetFields);
                String foreignFormat = SwordUtil.DCTERMS;
                try {
                    importGenericService.importXML(deposit.getSwordEntry().toString(), foreignFormat, datasetVersion);
                } catch (Exception ex) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "problem calling importXML: " + ex);
                }
                swordService.addDatasetContact(datasetVersion, user);
                swordService.addDatasetDepositor(datasetVersion, user);
                swordService.addDatasetSubjectIfMissing(datasetVersion);
                swordService.setDatasetLicenseAndTermsOfUse(datasetVersion, deposit.getSwordEntry());
                try {
                    engineSvc.submit(updateDatasetCommand);
                } catch (CommandException ex) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "problem updating dataset: " + ex);
                }
                ReceiptGenerator receiptGenerator = new ReceiptGenerator();
                String baseUrl = urlManager.getHostnamePlusBaseUrlPath(uri);
                DepositReceipt depositReceipt = receiptGenerator.createDatasetReceipt(baseUrl, dataset);
                return depositReceipt;
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataset based on global id (" + globalId + ") in URL: " + uri);
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unknown target type specified on which to replace metadata: " + uri);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "No target specified on which to replace metadata: " + uri);
    }
}
Also used : SwordError(org.swordapp.server.SwordError) Dataset(edu.harvard.iq.dataverse.Dataset) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) SwordAuthException(org.swordapp.server.SwordAuthException) SwordServerException(org.swordapp.server.SwordServerException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) EJBException(javax.ejb.EJBException) CommandExecutionException(edu.harvard.iq.dataverse.engine.command.exception.CommandExecutionException) ParseException(org.apache.abdera.parser.ParseException) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DepositReceipt(org.swordapp.server.DepositReceipt) SwordEntry(org.swordapp.server.SwordEntry) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(org.apache.abdera.parser.ParseException) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)

Example 17 with DatasetVersion

use of edu.harvard.iq.dataverse.DatasetVersion 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 18 with DatasetVersion

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

the class JsonParserTest method testparseFiles.

@Test
public void testparseFiles() throws JsonParseException {
    JsonArrayBuilder metadatasJsonBuilder = Json.createArrayBuilder();
    JsonObjectBuilder fileMetadataGood = Json.createObjectBuilder();
    fileMetadataGood.add("label", "myLabel");
    JsonObjectBuilder fileGood = Json.createObjectBuilder();
    fileMetadataGood.add("dataFile", fileGood);
    fileMetadataGood.add("categories", Json.createArrayBuilder().add("Documentation"));
    JsonObjectBuilder fileMetadataBad = Json.createObjectBuilder();
    fileMetadataBad.add("label", "bad");
    JsonObjectBuilder fileBad = Json.createObjectBuilder();
    fileMetadataBad.add("dataFile", fileBad);
    fileMetadataBad.add("categories", Json.createArrayBuilder().add(BigDecimal.ONE));
    metadatasJsonBuilder.add(fileMetadataGood);
    metadatasJsonBuilder.add(fileMetadataBad);
    JsonArray metadatasJson = metadatasJsonBuilder.build();
    DatasetVersion dsv = new DatasetVersion();
    Dataset dataset = new Dataset();
    dsv.setDataset(dataset);
    List<FileMetadata> fileMetadatas = new JsonParser().parseFiles(metadatasJson, dsv);
    System.out.println("fileMetadatas: " + fileMetadatas);
    assertEquals("myLabel", fileMetadatas.get(0).getLabel());
    assertEquals("Documentation", fileMetadatas.get(0).getCategories().get(0).getName());
    assertEquals(null, fileMetadatas.get(1).getCategories());
    List<FileMetadata> codeCoverage = new JsonParser().parseFiles(Json.createArrayBuilder().add(Json.createObjectBuilder().add("label", "myLabel").add("dataFile", Json.createObjectBuilder().add("categories", JsonValue.NULL))).build(), dsv);
    assertEquals(null, codeCoverage.get(0).getCategories());
}
Also used : JsonArray(javax.json.JsonArray) Dataset(edu.harvard.iq.dataverse.Dataset) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Test(org.junit.Test)

Example 19 with DatasetVersion

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

the class PrivateUrlUtilTest method testGetDraftDatasetVersionFromRoleAssignmentNullDataset.

@Test
public void testGetDraftDatasetVersionFromRoleAssignmentNullDataset() {
    DataverseRole aRole = null;
    PrivateUrlUser privateUrlUser = new PrivateUrlUser(42);
    RoleAssignee anAssignee = privateUrlUser;
    DvObject dataset = null;
    String privateUrlToken = null;
    RoleAssignment ra = new RoleAssignment(aRole, anAssignee, dataset, privateUrlToken);
    DatasetVersion datasetVersion = PrivateUrlUtil.getDraftDatasetVersionFromRoleAssignment(ra);
    assertNull(datasetVersion);
}
Also used : DvObject(edu.harvard.iq.dataverse.DvObject) PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) RoleAssignee(edu.harvard.iq.dataverse.authorization.RoleAssignee) Test(org.junit.Test)

Example 20 with DatasetVersion

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

the class PrivateUrlUtilTest method testGetDraftUrlSuccess.

@Test
public void testGetDraftUrlSuccess() throws Exception {
    DatasetVersion draft = new DatasetVersion();
    Dataset dataset = new Dataset();
    dataset.setProtocol("doi");
    dataset.setAuthority("10.5072/FK2");
    dataset.setIdentifier("3L33T");
    draft.setDataset(dataset);
    assertEquals("/dataset.xhtml?persistentId=doi:10.5072/FK2/3L33T&version=DRAFT", PrivateUrlUtil.getDraftUrl(draft));
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Test(org.junit.Test)

Aggregations

DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)85 Dataset (edu.harvard.iq.dataverse.Dataset)43 Test (org.junit.Test)36 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)25 ArrayList (java.util.ArrayList)24 DataFile (edu.harvard.iq.dataverse.DataFile)19 JsonObject (javax.json.JsonObject)15 Date (java.util.Date)13 StringReader (java.io.StringReader)11 IOException (java.io.IOException)10 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)9 Timestamp (java.sql.Timestamp)9 EJBException (javax.ejb.EJBException)9 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)8 JsonObjectBuilder (javax.json.JsonObjectBuilder)8 JsonReader (javax.json.JsonReader)8 DatasetField (edu.harvard.iq.dataverse.DatasetField)7 Dataverse (edu.harvard.iq.dataverse.Dataverse)7 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)7 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)7