Search in sources :

Example 1 with PublishDatasetCommand

use of edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand in project dataverse by IQSS.

the class Datasets method publishDataset.

@POST
@Path("{id}/actions/:publish")
public Response publishDataset(@PathParam("id") String id, @QueryParam("type") String type) {
    try {
        if (type == null) {
            return error(Response.Status.BAD_REQUEST, "Missing 'type' parameter (either 'major' or 'minor').");
        }
        type = type.toLowerCase();
        boolean isMinor;
        switch(type) {
            case "minor":
                isMinor = true;
                break;
            case "major":
                isMinor = false;
                break;
            default:
                return error(Response.Status.BAD_REQUEST, "Illegal 'type' parameter value '" + type + "'. It needs to be either 'major' or 'minor'.");
        }
        Dataset ds = findDatasetOrDie(id);
        PublishDatasetResult res = execCommand(new PublishDatasetCommand(ds, createDataverseRequest(findAuthenticatedUserOrDie()), isMinor));
        return res.isCompleted() ? ok(json(res.getDataset())) : accepted(json(res.getDataset()));
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) PublishDatasetResult(edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetResult) PublishDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with PublishDatasetCommand

use of edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand in project dataverse by IQSS.

the class ContainerManagerImpl method useHeaders.

@Override
public DepositReceipt useHeaders(String uri, Deposit deposit, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
    logger.fine("uri was " + uri);
    logger.fine("isInProgress:" + deposit.isInProgress());
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvRequest = new DataverseRequest(user, httpRequest);
    urlManager.processUrl(uri);
    String targetType = urlManager.getTargetType();
    if (!targetType.isEmpty()) {
        logger.fine("operating on target type: " + urlManager.getTargetType());
        if ("study".equals(targetType)) {
            String globalId = urlManager.getTargetIdentifier();
            if (globalId != null) {
                Dataset dataset = null;
                try {
                    dataset = datasetService.findByGlobalId(globalId);
                } catch (EJBException ex) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataset based on global id (" + globalId + ") in URL: " + uri);
                }
                if (dataset != null) {
                    Dataverse dvThatOwnsDataset = dataset.getOwner();
                    boolean doMinorVersionBump = false;
                    // if dataset is unreleased, major version; if released, then check if can be minor
                    if (dataset.isReleased() && dataset.getLatestVersion().isMinorUpdate()) {
                        doMinorVersionBump = true;
                    }
                    PublishDatasetCommand publishDatasetCommand = new PublishDatasetCommand(dataset, dvRequest, doMinorVersionBump);
                    if (!permissionService.isUserAllowedOn(user, publishDatasetCommand, dataset)) {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "User " + user.getDisplayInfo().getTitle() + " is not authorized to modify dataverse " + dvThatOwnsDataset.getAlias());
                    }
                    if (!deposit.isInProgress()) {
                        /**
                         * We are considering a draft version of a study to
                         * be incomplete and are saying that sending
                         * isInProgress=false means the study version is
                         * complete and can be released.
                         *
                         * 9.2. Deposit Incomplete
                         *
                         * "If In-Progress is true, the server SHOULD expect
                         * the client to provide further updates to the item
                         * some undetermined time in the future. Details of
                         * how this is implemented is dependent on the
                         * server's purpose. For example, a repository
                         * system may hold items which are marked
                         * In-Progress in a workspace until such time as a
                         * client request indicates that the deposit is
                         * complete." --
                         * http://swordapp.github.io/SWORDv2-Profile/SWORDProfile.html#continueddeposit_incomplete
                         */
                        if (!dataset.getLatestVersion().getVersionState().equals(DatasetVersion.VersionState.RELEASED)) {
                            try {
                                dataset = engineSvc.submit(publishDatasetCommand).getDataset();
                            } catch (CommandException ex) {
                                String msg = "Unable to publish dataset: " + ex;
                                logger.severe(msg + ": " + ex.getMessage());
                                throw SwordUtil.throwRegularSwordErrorWithoutStackTrace(msg);
                            }
                            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, "Latest version of dataset " + globalId + " has already been published.");
                        }
                    } else {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Pass 'In-Progress: false' header to publish a dataset.");
                    }
                } else {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataset using globalId " + globalId);
                }
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to find globalId for dataset in URL:" + uri);
            }
        } else if ("dataverse".equals(targetType)) {
            String dvAlias = urlManager.getTargetIdentifier();
            if (dvAlias != null) {
                Dataverse dvToRelease = dataverseService.findByAlias(dvAlias);
                if (dvToRelease != null) {
                    PublishDataverseCommand publishDataverseCommand = new PublishDataverseCommand(dvRequest, dvToRelease);
                    if (!permissionService.isUserAllowedOn(user, publishDataverseCommand, dvToRelease)) {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "User " + user.getDisplayInfo().getTitle() + " is not authorized to modify dataverse " + dvAlias);
                    }
                    if (deposit.isInProgress()) {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unpublishing a dataverse is not supported.");
                    }
                    try {
                        engineSvc.submit(publishDataverseCommand);
                        ReceiptGenerator receiptGenerator = new ReceiptGenerator();
                        String baseUrl = urlManager.getHostnamePlusBaseUrlPath(uri);
                        DepositReceipt depositReceipt = receiptGenerator.createDataverseReceipt(baseUrl, dvToRelease);
                        return depositReceipt;
                    } catch (CommandException ex) {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't publish dataverse " + dvAlias + ": " + ex);
                    }
                } else {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataverse based on alias in URL: " + uri);
                }
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to find dataverse alias in URL: " + uri);
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "unsupported target type (" + targetType + ") in URL:" + uri);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Target type missing from URL: " + uri);
    }
}
Also used : SwordError(org.swordapp.server.SwordError) PublishDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.PublishDataverseCommand) Dataset(edu.harvard.iq.dataverse.Dataset) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DepositReceipt(org.swordapp.server.DepositReceipt) PublishDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand) EJBException(javax.ejb.EJBException)

Aggregations

Dataset (edu.harvard.iq.dataverse.Dataset)2 PublishDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand)2 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)1 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)1 PublishDatasetResult (edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetResult)1 PublishDataverseCommand (edu.harvard.iq.dataverse.engine.command.impl.PublishDataverseCommand)1 EJBException (javax.ejb.EJBException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 DepositReceipt (org.swordapp.server.DepositReceipt)1 SwordError (org.swordapp.server.SwordError)1