Search in sources :

Example 51 with DatasetVersion

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

the class FileUtilTest method testIsPubliclyDownloadable.

@Test
public void testIsPubliclyDownloadable() {
    assertEquals(false, FileUtil.isPubliclyDownloadable(null));
    FileMetadata restrictedFileMetadata = new FileMetadata();
    restrictedFileMetadata.setRestricted(true);
    assertEquals(false, FileUtil.isPubliclyDownloadable(restrictedFileMetadata));
    FileMetadata nonRestrictedFileMetadata = new FileMetadata();
    DatasetVersion dsv = new DatasetVersion();
    dsv.setVersionState(DatasetVersion.VersionState.RELEASED);
    nonRestrictedFileMetadata.setDatasetVersion(dsv);
    Dataset dataset = new Dataset();
    dsv.setDataset(dataset);
    nonRestrictedFileMetadata.setRestricted(false);
    assertEquals(true, FileUtil.isPubliclyDownloadable(nonRestrictedFileMetadata));
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Test(org.junit.Test)

Example 52 with DatasetVersion

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

the class Dataverses method createDataset.

@POST
@Path("{identifier}/datasets")
public Response createDataset(String jsonBody, @PathParam("identifier") String parentIdtf) {
    try {
        User u = findUserOrDie();
        Dataverse owner = findDataverseOrDie(parentIdtf);
        JsonObject json;
        try (StringReader rdr = new StringReader(jsonBody)) {
            json = Json.createReader(rdr).readObject();
        } catch (JsonParsingException jpe) {
            LOGGER.log(Level.SEVERE, "Json: {0}", jsonBody);
            return error(Status.BAD_REQUEST, "Error parsing Json: " + jpe.getMessage());
        }
        Dataset ds = new Dataset();
        ds.setOwner(owner);
        JsonObject jsonVersion = json.getJsonObject("datasetVersion");
        if (jsonVersion == null) {
            return error(Status.BAD_REQUEST, "Json POST data are missing datasetVersion object.");
        }
        try {
            try {
                DatasetVersion version = new DatasetVersion();
                version.setDataset(ds);
                // Use the two argument version so that the version knows which dataset it's associated with.
                version = jsonParser().parseDatasetVersion(jsonVersion, version);
                // force "initial version" properties
                version.setMinorVersionNumber(null);
                version.setVersionNumber(null);
                version.setVersionState(DatasetVersion.VersionState.DRAFT);
                LinkedList<DatasetVersion> versions = new LinkedList<>();
                versions.add(version);
                version.setDataset(ds);
                ds.setVersions(versions);
            } catch (javax.ejb.TransactionRolledbackLocalException rbe) {
                throw rbe.getCausedByException();
            }
        } catch (JsonParseException ex) {
            LOGGER.log(Level.INFO, "Error parsing dataset version from Json", ex);
            return error(Status.BAD_REQUEST, "Error parsing datasetVersion: " + ex.getMessage());
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Error parsing dataset version from Json", e);
            return error(Status.INTERNAL_SERVER_ERROR, "Error parsing datasetVersion: " + e.getMessage());
        }
        Dataset managedDs = execCommand(new CreateDatasetCommand(ds, createDataverseRequest(u)));
        return created("/datasets/" + managedDs.getId(), Json.createObjectBuilder().add("id", managedDs.getId()).add("persistentId", managedDs.getGlobalId()));
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) Dataset(edu.harvard.iq.dataverse.Dataset) CreateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetCommand) JsonObject(javax.json.JsonObject) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) Dataverse(edu.harvard.iq.dataverse.Dataverse) LinkedList(java.util.LinkedList) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) EJBException(javax.ejb.EJBException) JsonParsingException(javax.json.stream.JsonParsingException) ConstraintViolationException(javax.validation.ConstraintViolationException) StringReader(java.io.StringReader) JsonParsingException(javax.json.stream.JsonParsingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 53 with DatasetVersion

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

the class Index method indexDatasetByPersistentId.

@GET
@Path("dataset")
public Response indexDatasetByPersistentId(@QueryParam("persistentId") String persistentId) {
    if (persistentId == null) {
        return error(Status.BAD_REQUEST, "No persistent id given.");
    }
    Dataset dataset = null;
    try {
        dataset = datasetService.findByGlobalId(persistentId);
    } catch (Exception ex) {
        return error(Status.BAD_REQUEST, "Problem looking up dataset with persistent id \"" + persistentId + "\". Error: " + ex.getMessage());
    }
    if (dataset != null) {
        boolean doNormalSolrDocCleanUp = true;
        Future<String> indexDatasetFuture = indexService.indexDataset(dataset, doNormalSolrDocCleanUp);
        JsonObjectBuilder data = Json.createObjectBuilder();
        data.add("message", "Reindexed dataset " + persistentId);
        data.add("id", dataset.getId());
        data.add("persistentId", dataset.getGlobalId());
        JsonArrayBuilder versions = Json.createArrayBuilder();
        for (DatasetVersion version : dataset.getVersions()) {
            JsonObjectBuilder versionObject = Json.createObjectBuilder();
            versionObject.add("semanticVersion", version.getSemanticVersion());
            versionObject.add("id", version.getId());
            versions.add(versionObject);
        }
        data.add("versions", versions);
        return ok(data);
    } else {
        return error(Status.BAD_REQUEST, "Could not find dataset with persistent id " + persistentId);
    }
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) SearchException(edu.harvard.iq.dataverse.search.SearchException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) EJBException(javax.ejb.EJBException) IOException(java.io.IOException) ConstraintViolationException(javax.validation.ConstraintViolationException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 54 with DatasetVersion

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

the class Access method datafileBundle.

// @EJB
// TODO:
// versions? -- L.A. 4.0 beta 10
@Path("datafile/bundle/{fileId}")
@GET
@Produces({ "application/zip" })
public BundleDownloadInstance datafileBundle(@PathParam("fileId") Long fileId, @QueryParam("gbrecs") Boolean gbrecs, @QueryParam("key") String apiToken, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    DataFile df = dataFileService.find(fileId);
    GuestbookResponse gbr = null;
    if (df == null) {
        logger.warning("Access: datafile service could not locate a DataFile object for id " + fileId + "!");
        throw new NotFoundException();
    }
    if (apiToken == null || apiToken.equals("")) {
        apiToken = headers.getHeaderString(API_KEY_HEADER);
    }
    // This will throw a ForbiddenException if access isn't authorized:
    checkAuthorization(df, apiToken);
    if (gbrecs == null && df.isReleased()) {
        // Write Guestbook record if not done previously and file is released
        User apiTokenUser = findAPITokenUser(apiToken);
        gbr = guestbookResponseService.initAPIGuestbookResponse(df.getOwner(), df, session, apiTokenUser);
        guestbookResponseService.save(gbr);
    }
    DownloadInfo dInfo = new DownloadInfo(df);
    BundleDownloadInstance downloadInstance = new BundleDownloadInstance(dInfo);
    FileMetadata fileMetadata = df.getFileMetadata();
    DatasetVersion datasetVersion = df.getOwner().getLatestVersion();
    downloadInstance.setFileCitationEndNote(datasetService.createCitationXML(datasetVersion, fileMetadata));
    downloadInstance.setFileCitationRIS(datasetService.createCitationRIS(datasetVersion, fileMetadata));
    downloadInstance.setFileCitationBibtex(new BibtexCitation(datasetVersion).toString());
    ByteArrayOutputStream outStream = null;
    outStream = new ByteArrayOutputStream();
    try {
        ddiExportService.exportDataFile(fileId, outStream, null, null);
        downloadInstance.setFileDDIXML(outStream.toString());
    } catch (Exception ex) {
    // if we can't generate the DDI, it's ok;
    // we'll just generate the bundle without it.
    }
    return downloadInstance;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) GuestbookResponse(edu.harvard.iq.dataverse.GuestbookResponse) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) NotFoundException(javax.ws.rs.NotFoundException) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) BibtexCitation(edu.harvard.iq.dataverse.BibtexCitation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) WebApplicationException(javax.ws.rs.WebApplicationException) ForbiddenException(javax.ws.rs.ForbiddenException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 55 with DatasetVersion

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

the class Access method dsCardImage.

// Note:
// the Dataverse page is no longer using this method.
@Path("dsCardImage/{versionId}")
@GET
@Produces({ "image/png" })
public InputStream dsCardImage(@PathParam("versionId") Long versionId, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    DatasetVersion datasetVersion = versionService.find(versionId);
    if (datasetVersion == null) {
        logger.warning("Preview: Version service could not locate a DatasetVersion object for id " + versionId + "!");
        return null;
    }
    // String imageThumbFileName = null;
    StorageIO thumbnailDataAccess = null;
    if (datasetVersion.getDataset() != null) {
        DataFile logoDataFile = datasetVersion.getDataset().getThumbnailFile();
        if (logoDataFile != null) {
            try {
                StorageIO<DataFile> dataAccess = logoDataFile.getStorageIO();
                if (dataAccess != null) {
                    // && dataAccess.isLocalFile()) {
                    dataAccess.open();
                    thumbnailDataAccess = ImageThumbConverter.getImageThumbnailAsInputStream(dataAccess, 48);
                }
            } catch (IOException ioEx) {
                thumbnailDataAccess = null;
            }
        }
        if (thumbnailDataAccess != null && thumbnailDataAccess.getInputStream() != null) {
            return thumbnailDataAccess.getInputStream();
        }
    }
    return null;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) StorageIO(edu.harvard.iq.dataverse.dataaccess.StorageIO) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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