Search in sources :

Example 6 with FileMetadata

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

the class StatementManagerImpl method getStatement.

@Override
public Statement getStatement(String editUri, Map<String, String> map, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordServerException, SwordError, SwordAuthException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
    urlManager.processUrl(editUri);
    String globalId = urlManager.getTargetIdentifier();
    if (urlManager.getTargetType().equals("study") && globalId != null) {
        logger.fine("request for sword statement by user " + user.getDisplayInfo().getTitle());
        Dataset dataset = datasetService.findByGlobalId(globalId);
        if (dataset == null) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "couldn't find dataset with global ID of " + globalId);
        }
        Dataverse dvThatOwnsDataset = dataset.getOwner();
        if (!permissionService.isUserAllowedOn(user, new GetDraftDatasetVersionCommand(dvReq, dataset), dataset)) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to view dataset with global ID " + globalId);
        }
        String feedUri = urlManager.getHostnamePlusBaseUrlPath(editUri) + "/edit/study/" + dataset.getGlobalId();
        String author = dataset.getLatestVersion().getAuthorsStr();
        String title = dataset.getLatestVersion().getTitle();
        // in the statement, the element is called "updated"
        Date lastUpdatedFinal = new Date();
        Date lastUpdateTime = dataset.getLatestVersion().getLastUpdateTime();
        if (lastUpdateTime != null) {
            lastUpdatedFinal = lastUpdateTime;
        } else {
            logger.info("lastUpdateTime was null, trying createtime");
            Date createtime = dataset.getLatestVersion().getCreateTime();
            if (createtime != null) {
                lastUpdatedFinal = createtime;
            } else {
                logger.info("creatime was null, using \"now\"");
                lastUpdatedFinal = new Date();
            }
        }
        AtomDate atomDate = new AtomDate(lastUpdatedFinal);
        String datedUpdated = atomDate.toString();
        Statement statement = new AtomStatement(feedUri, author, title, datedUpdated);
        Map<String, String> states = new HashMap<>();
        states.put("latestVersionState", dataset.getLatestVersion().getVersionState().toString());
        Boolean isMinorUpdate = dataset.getLatestVersion().isMinorUpdate();
        states.put("isMinorUpdate", isMinorUpdate.toString());
        if (dataset.isLocked()) {
            states.put("locked", "true");
            states.put("lockedDetail", dataset.getLocks().stream().map(l -> l.getInfo()).collect(joining(",")));
            Optional<DatasetLock> earliestLock = dataset.getLocks().stream().min((l1, l2) -> (int) Math.signum(l1.getStartTime().getTime() - l2.getStartTime().getTime()));
            states.put("lockedStartTime", earliestLock.get().getStartTime().toString());
        } else {
            states.put("locked", "false");
        }
        statement.setStates(states);
        List<FileMetadata> fileMetadatas = dataset.getLatestVersion().getFileMetadatas();
        for (FileMetadata fileMetadata : fileMetadatas) {
            DataFile dataFile = fileMetadata.getDataFile();
            // We are exposing the filename for informational purposes. The file id is what you
            // actually operate on to delete a file, etc.
            // 
            // Replace spaces to avoid IRISyntaxException
            String fileNameFinal = fileMetadata.getLabel().replace(' ', '_');
            String fileUrlString = urlManager.getHostnamePlusBaseUrlPath(editUri) + "/edit-media/file/" + dataFile.getId() + "/" + fileNameFinal;
            IRI fileUrl;
            try {
                fileUrl = new IRI(fileUrlString);
            } catch (IRISyntaxException ex) {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Invalid URL for file ( " + fileUrlString + " ) resulted in " + ex.getMessage());
            }
            ResourcePart resourcePart = new ResourcePart(fileUrl.toString());
            // default to something that doesn't throw a org.apache.abdera.util.MimeTypeParseException
            String finalFileFormat = "application/octet-stream";
            String contentType = dataFile.getContentType();
            if (contentType != null) {
                finalFileFormat = contentType;
            }
            resourcePart.setMediaType(finalFileFormat);
            /**
             * @todo: Why are properties set on a ResourcePart not exposed
             * when you GET a Statement? Asked about this at
             * http://www.mail-archive.com/sword-app-tech@lists.sourceforge.net/msg00394.html
             */
            // Map<String, String> properties = new HashMap<String, String>();
            // properties.put("filename", studyFile.getFileName());
            // properties.put("category", studyFile.getLatestCategory());
            // properties.put("originalFileType", studyFile.getOriginalFileType());
            // properties.put("id", studyFile.getId().toString());
            // properties.put("UNF", studyFile.getUnf());
            // resourcePart.setProperties(properties);
            statement.addResource(resourcePart);
        /**
         * @todo it's been noted at
         * https://github.com/IQSS/dataverse/issues/892#issuecomment-54159284
         * that at the file level the "updated" date is always "now",
         * which seems to be set here:
         * https://github.com/swordapp/JavaServer2.0/blob/sword2-server-1.0/src/main/java/org/swordapp/server/AtomStatement.java#L70
         */
        }
        return statement;
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not determine target type or identifier from URL: " + editUri);
    }
}
Also used : IRI(org.apache.abdera.i18n.iri.IRI) ResourcePart(org.swordapp.server.ResourcePart) SwordError(org.swordapp.server.SwordError) AtomStatement(org.swordapp.server.AtomStatement) HashMap(java.util.HashMap) Dataset(edu.harvard.iq.dataverse.Dataset) AtomStatement(org.swordapp.server.AtomStatement) Statement(org.swordapp.server.Statement) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) Date(java.util.Date) AtomDate(org.apache.abdera.model.AtomDate) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DataFile(edu.harvard.iq.dataverse.DataFile) AtomDate(org.apache.abdera.model.AtomDate) GetDraftDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.GetDraftDatasetVersionCommand) DatasetLock(edu.harvard.iq.dataverse.DatasetLock) IRISyntaxException(org.apache.abdera.i18n.iri.IRISyntaxException)

Example 7 with FileMetadata

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

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

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

the class IngestUtilTest method testDirectoryLabels.

@Test
public void testDirectoryLabels() {
    DatasetVersion datasetVersion = new DatasetVersion();
    FileMetadata fileMetadata = new FileMetadata();
    fileMetadata.setLabel("foo.png");
    fileMetadata.setDirectoryLabel("/has/leading/slash");
    datasetVersion.getFileMetadatas().add(fileMetadata);
    Set<ConstraintViolation> violations1 = datasetVersion.validate();
    assertEquals(1, violations1.size());
    ConstraintViolation violation1 = violations1.iterator().next();
    assertEquals("Directory Name cannot contain leading or trailing file separators.", violation1.getMessage());
    // reset
    datasetVersion.setFileMetadatas(new ArrayList<>());
    Set<ConstraintViolation> violations2 = datasetVersion.validate();
    assertEquals(0, violations2.size());
    fileMetadata.setDirectoryLabel("has/trailing/slash/");
    datasetVersion.getFileMetadatas().add(fileMetadata);
    Set<ConstraintViolation> violations3 = datasetVersion.validate();
    assertEquals(1, violations3.size());
    assertEquals("Directory Name cannot contain leading or trailing file separators.", violations3.iterator().next().getMessage());
    // reset
    datasetVersion.setFileMetadatas(new ArrayList<>());
    Set<ConstraintViolation> violations4 = datasetVersion.validate();
    assertEquals(0, violations4.size());
    fileMetadata.setDirectoryLabel("just/right");
    datasetVersion.getFileMetadatas().add(fileMetadata);
    Set<ConstraintViolation> violations5 = datasetVersion.validate();
    assertEquals(0, violations5.size());
    // reset
    datasetVersion.setFileMetadatas(new ArrayList<>());
    Set<ConstraintViolation> violations6 = datasetVersion.validate();
    assertEquals(0, violations6.size());
    fileMetadata.setDirectoryLabel("");
    datasetVersion.getFileMetadatas().add(fileMetadata);
    Set<ConstraintViolation> violations7 = datasetVersion.validate();
    assertEquals(0, violations7.size());
    // reset
    datasetVersion.setFileMetadatas(new ArrayList<>());
    Set<ConstraintViolation> violations8 = datasetVersion.validate();
    assertEquals(0, violations8.size());
    fileMetadata.setDirectoryLabel(null);
    datasetVersion.getFileMetadatas().add(fileMetadata);
    Set<ConstraintViolation> violations9 = datasetVersion.validate();
    assertEquals(0, violations9.size());
    // reset
    datasetVersion.setFileMetadatas(new ArrayList<>());
    Set<ConstraintViolation> violations10 = datasetVersion.validate();
    assertEquals(0, violations10.size());
    String singleCharacter = "a";
    fileMetadata.setDirectoryLabel(singleCharacter);
    datasetVersion.getFileMetadatas().add(fileMetadata);
    Set<ConstraintViolation> violations11 = datasetVersion.validate();
    assertEquals(0, violations11.size());
    // reset
    datasetVersion.setFileMetadatas(new ArrayList<>());
    Set<ConstraintViolation> violations12 = datasetVersion.validate();
    assertEquals(0, violations12.size());
    fileMetadata.setDirectoryLabel("/leadingAndTrailing/");
    datasetVersion.getFileMetadatas().add(fileMetadata);
    Set<ConstraintViolation> violations13 = datasetVersion.validate();
    assertEquals(1, violations13.size());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Test(org.junit.Test)

Example 10 with FileMetadata

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

the class IngestUtilTest method testCheckForDuplicateFileNamesTabular.

@Test
public /**
 * Test tabular files (e.g., .dta) are changed when .tab files with the same
 * name exist.
 */
void testCheckForDuplicateFileNamesTabular() throws Exception {
    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd");
    // create dataset
    Dataset dataset = makeDataset();
    // create dataset version
    DatasetVersion datasetVersion = dataset.getEditVersion();
    datasetVersion.setCreateTime(dateFmt.parse("20001012"));
    datasetVersion.setLastUpdateTime(datasetVersion.getLastUpdateTime());
    datasetVersion.setId(MocksFactory.nextId());
    datasetVersion.setReleaseTime(dateFmt.parse("20010101"));
    datasetVersion.setVersionState(DatasetVersion.VersionState.RELEASED);
    datasetVersion.setMinorVersionNumber(0L);
    datasetVersion.setVersionNumber(1L);
    datasetVersion.setFileMetadatas(new ArrayList<>());
    // create datafiles
    List<DataFile> dataFileList = new ArrayList<>();
    DataFile datafile1 = new DataFile("application/x-strata");
    datafile1.setStorageIdentifier("foobar.dta");
    datafile1.setFilesize(200);
    datafile1.setModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setCreateDate(new Timestamp(new Date().getTime()));
    datafile1.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile1.setOwner(dataset);
    datafile1.setIngestDone();
    datafile1.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile1.setChecksumValue("Unknown");
    DataTable dt1 = new DataTable();
    dt1.setOriginalFileFormat("application/x-stata");
    datafile1.setDataTable(dt1);
    // set metadata and add version
    FileMetadata fmd1 = new FileMetadata();
    fmd1.setId(1L);
    fmd1.setLabel("foobar.tab");
    fmd1.setDataFile(datafile1);
    datafile1.getFileMetadatas().add(fmd1);
    datasetVersion.getFileMetadatas().add(fmd1);
    fmd1.setDatasetVersion(datasetVersion);
    DataFile datafile2 = new DataFile("application/x-strata");
    datafile2.setStorageIdentifier("foobar.dta");
    datafile2.setFilesize(200);
    datafile2.setModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setCreateDate(new Timestamp(new Date().getTime()));
    datafile2.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile2.setOwner(dataset);
    datafile2.setIngestDone();
    datafile2.setChecksumType(DataFile.ChecksumType.SHA1);
    datafile2.setChecksumValue("Unknown");
    DataTable dt2 = new DataTable();
    dt2.setOriginalFileFormat("application/x-stata");
    datafile2.setDataTable(dt2);
    // set metadata and add version
    FileMetadata fmd2 = new FileMetadata();
    fmd2.setId(2L);
    fmd2.setLabel("foobar.dta");
    fmd2.setDataFile(datafile2);
    datafile2.getFileMetadatas().add(fmd2);
    dataFileList.add(datafile2);
    IngestUtil.checkForDuplicateFileNamesFinal(datasetVersion, dataFileList);
    boolean file2NameAltered = false;
    for (DataFile df : dataFileList) {
        if (df.getFileMetadata().getLabel().equals("foobar-1.dta")) {
            file2NameAltered = true;
        }
    }
    // check filename is altered since tabular and will change to .tab after ingest
    assertEquals(file2NameAltered, true);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataTable(edu.harvard.iq.dataverse.DataTable) Dataset(edu.harvard.iq.dataverse.Dataset) MocksFactory.makeDataset(edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset) ArrayList(java.util.ArrayList) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.Test)

Aggregations

FileMetadata (edu.harvard.iq.dataverse.FileMetadata)54 DataFile (edu.harvard.iq.dataverse.DataFile)30 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)26 ArrayList (java.util.ArrayList)23 Dataset (edu.harvard.iq.dataverse.Dataset)18 Test (org.junit.Test)13 Date (java.util.Date)12 IOException (java.io.IOException)10 Timestamp (java.sql.Timestamp)10 DataTable (edu.harvard.iq.dataverse.DataTable)5 DatasetField (edu.harvard.iq.dataverse.DatasetField)5 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)5 MocksFactory.makeDataset (edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset)5 SimpleDateFormat (java.text.SimpleDateFormat)5 HashMap (java.util.HashMap)5 Dataverse (edu.harvard.iq.dataverse.Dataverse)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 JsonObjectBuilder (javax.json.JsonObjectBuilder)4 DataFileTag (edu.harvard.iq.dataverse.DataFileTag)3