Search in sources :

Example 1 with AuthenticatedUser

use of edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser in project dataverse by IQSS.

the class DatasetPage method downloadRsyncScript.

/*
    public String getThumbnail() {
        DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
        if (datasetThumbnail != null) {
            return datasetThumbnail.getBase64image();
        } else {
            return null;
        }
    }*/
public void downloadRsyncScript() {
    String bibFormatDowload = new BibtexCitation(workingVersion).toString();
    FacesContext ctx = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
    response.setContentType("application/download");
    String contentDispositionString;
    contentDispositionString = "attachment;filename=" + rsyncScriptFilename;
    response.setHeader("Content-Disposition", contentDispositionString);
    try {
        ServletOutputStream out = response.getOutputStream();
        out.write(getRsyncScript().getBytes());
        out.flush();
        ctx.responseComplete();
    } catch (IOException e) {
        String error = "Problem getting bytes from rsync script: " + e;
        logger.warning(error);
        return;
    }
    // If the script has been successfully downloaded, lock the dataset:
    String lockInfoMessage = "script downloaded";
    DatasetLock lock = datasetService.addDatasetLock(dataset.getId(), DatasetLock.Reason.DcmUpload, session.getUser() != null ? ((AuthenticatedUser) session.getUser()).getId() : null, lockInfoMessage);
    if (lock != null) {
        dataset.addLock(lock);
    } else {
        logger.log(Level.WARNING, "Failed to lock the dataset (dataset id={0})", dataset.getId());
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)

Example 2 with AuthenticatedUser

use of edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser in project dataverse by IQSS.

the class DatasetPage method save.

public String save() {
    // Validate
    Set<ConstraintViolation> constraintViolations = workingVersion.validate();
    if (!constraintViolations.isEmpty()) {
        // JsfHelper.addFlashMessage(JH.localize("dataset.message.validationError"));
        JH.addMessage(FacesMessage.SEVERITY_ERROR, JH.localize("dataset.message.validationError"));
        // FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error", "See below for details."));
        return "";
    }
    // Use the API to save the dataset:
    Command<Dataset> cmd;
    try {
        if (editMode == EditMode.CREATE) {
            if (selectedTemplate != null) {
                if (isSessionUserAuthenticated()) {
                    cmd = new CreateDatasetCommand(dataset, dvRequestService.getDataverseRequest(), false, null, selectedTemplate);
                } else {
                    JH.addMessage(FacesMessage.SEVERITY_FATAL, JH.localize("dataset.create.authenticatedUsersOnly"));
                    return null;
                }
            } else {
                cmd = new CreateDatasetCommand(dataset, dvRequestService.getDataverseRequest());
            }
        } else {
            cmd = new UpdateDatasetCommand(dataset, dvRequestService.getDataverseRequest(), filesToBeDeleted);
            ((UpdateDatasetCommand) cmd).setValidateLenient(true);
        }
        dataset = commandEngine.submit(cmd);
        if (editMode == EditMode.CREATE) {
            if (session.getUser() instanceof AuthenticatedUser) {
                userNotificationService.sendNotification((AuthenticatedUser) session.getUser(), dataset.getCreateDate(), UserNotification.Type.CREATEDS, dataset.getLatestVersion().getId());
            }
        }
        logger.fine("Successfully executed SaveDatasetCommand.");
    } catch (EJBException ex) {
        StringBuilder error = new StringBuilder();
        error.append(ex).append(" ");
        error.append(ex.getMessage()).append(" ");
        Throwable cause = ex;
        while (cause.getCause() != null) {
            cause = cause.getCause();
            error.append(cause).append(" ");
            error.append(cause.getMessage()).append(" ");
        }
        logger.log(Level.FINE, "Couldn''t save dataset: {0}", error.toString());
        populateDatasetUpdateFailureMessage();
        return returnToDraftVersion();
    } catch (CommandException ex) {
        // FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Dataset Save Failed", " - " + ex.toString()));
        logger.severe("CommandException, when attempting to update the dataset: " + ex.getMessage());
        populateDatasetUpdateFailureMessage();
        return returnToDraftVersion();
    }
    newFiles.clear();
    if (editMode != null) {
        if (editMode.equals(EditMode.CREATE)) {
            JsfHelper.addSuccessMessage(JH.localize("dataset.message.createSuccess"));
        }
        if (editMode.equals(EditMode.METADATA)) {
            JsfHelper.addSuccessMessage(JH.localize("dataset.message.metadataSuccess"));
        }
        if (editMode.equals(EditMode.LICENSE)) {
            JsfHelper.addSuccessMessage(JH.localize("dataset.message.termsSuccess"));
        }
        if (editMode.equals(EditMode.FILE)) {
            JsfHelper.addSuccessMessage(JH.localize("dataset.message.filesSuccess"));
        }
    } else {
        // must have been a bulk file update or delete:
        if (bulkFileDeleteInProgress) {
            JsfHelper.addSuccessMessage(JH.localize("dataset.message.bulkFileDeleteSuccess"));
        } else {
            JsfHelper.addSuccessMessage(JH.localize("dataset.message.bulkFileUpdateSuccess"));
        }
    }
    editMode = null;
    bulkFileDeleteInProgress = false;
    // Call Ingest Service one more time, to
    // queue the data ingest jobs for asynchronous execution:
    ingestService.startIngestJobs(dataset, (AuthenticatedUser) session.getUser());
    logger.fine("Redirecting to the Dataset page.");
    return returnToDraftVersion();
}
Also used : CreateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetCommand) ConstraintViolation(javax.validation.ConstraintViolation) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) EJBException(javax.ejb.EJBException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)

Example 3 with AuthenticatedUser

use of edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser in project dataverse by IQSS.

the class DatasetServiceBean method getDatasetVersionUser.

public DatasetVersionUser getDatasetVersionUser(DatasetVersion version, User user) {
    DatasetVersionUser ddu = null;
    Query query = em.createQuery("select object(o) from DatasetVersionUser as o " + "where o.datasetVersion.id =:versionId and o.authenticatedUser.id =:userId");
    query.setParameter("versionId", version.getId());
    String identifier = user.getIdentifier();
    identifier = identifier.startsWith("@") ? identifier.substring(1) : identifier;
    AuthenticatedUser au = authentication.getAuthenticatedUser(identifier);
    query.setParameter("userId", au.getId());
    try {
        ddu = (DatasetVersionUser) query.getSingleResult();
    } catch (javax.persistence.NoResultException e) {
    // DO nothing, just return null.
    }
    return ddu;
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)

Example 4 with AuthenticatedUser

use of edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser in project dataverse by IQSS.

the class DatasetServiceBean method addDatasetLock.

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public /*?*/
DatasetLock addDatasetLock(Long datasetId, DatasetLock.Reason reason, Long userId, String info) {
    Dataset dataset = em.find(Dataset.class, datasetId);
    AuthenticatedUser user = null;
    if (userId != null) {
        user = em.find(AuthenticatedUser.class, userId);
    }
    DatasetLock lock = new DatasetLock(reason, user);
    lock.setDataset(dataset);
    lock.setInfo(info);
    lock.setStartTime(new Date());
    if (userId != null) {
        lock.setUser(user);
        if (user.getDatasetLocks() == null) {
            user.setDatasetLocks(new ArrayList<>());
        }
        user.getDatasetLocks().add(lock);
    }
    return addDatasetLock(dataset, lock);
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Date(java.util.Date) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 5 with AuthenticatedUser

use of edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser in project dataverse by IQSS.

the class DatasetServiceBean method removeDatasetLocks.

/**
 * Removes all {@link DatasetLock}s for the dataset whose id is passed and reason
 * is {@code aReason}.
 * @param datasetId Id of the dataset whose locks will b removed.
 * @param aReason The reason of the locks that will be removed.
 */
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void removeDatasetLocks(Long datasetId, DatasetLock.Reason aReason) {
    Dataset dataset = em.find(Dataset.class, datasetId);
    new HashSet<>(dataset.getLocks()).stream().filter(l -> l.getReason() == aReason).forEach(lock -> {
        dataset.removeLock(lock);
        AuthenticatedUser user = lock.getUser();
        user.getDatasetLocks().remove(lock);
        em.remove(lock);
    });
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) WorkflowComment(edu.harvard.iq.dataverse.workflows.WorkflowComment) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) User(edu.harvard.iq.dataverse.authorization.users.User) FileHandler(java.util.logging.FileHandler) TransactionAttributeType(javax.ejb.TransactionAttributeType) Permission(edu.harvard.iq.dataverse.authorization.Permission) DatasetUtil(edu.harvard.iq.dataverse.dataset.DatasetUtil) Asynchronous(javax.ejb.Asynchronous) TransactionAttribute(javax.ejb.TransactionAttribute) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Named(javax.inject.Named) EJB(javax.ejb.EJB) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) OutputStream(java.io.OutputStream) Stateless(javax.ejb.Stateless) IndexServiceBean(edu.harvard.iq.dataverse.search.IndexServiceBean) Set(java.util.Set) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) AuthenticationServiceBean(edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean) Logger(java.util.logging.Logger) File(java.io.File) EJBException(javax.ejb.EJBException) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) ImageThumbConverter(edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter) List(java.util.List) Query(javax.persistence.Query) Strings(org.ocpsoft.common.util.Strings) OAIRecordServiceBean(edu.harvard.iq.dataverse.harvest.server.OAIRecordServiceBean) InputStream(java.io.InputStream) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) TransactionAttribute(javax.ejb.TransactionAttribute)

Aggregations

AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)125 Dataverse (edu.harvard.iq.dataverse.Dataverse)24 Timestamp (java.sql.Timestamp)24 Date (java.util.Date)24 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)23 Dataset (edu.harvard.iq.dataverse.Dataset)22 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)21 Path (javax.ws.rs.Path)19 EJBException (javax.ejb.EJBException)16 ArrayList (java.util.ArrayList)14 User (edu.harvard.iq.dataverse.authorization.users.User)13 DataFile (edu.harvard.iq.dataverse.DataFile)11 IOException (java.io.IOException)11 JsonObjectBuilder (javax.json.JsonObjectBuilder)11 POST (javax.ws.rs.POST)11 Test (org.junit.Test)11 BuiltinUser (edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser)10 SwordError (org.swordapp.server.SwordError)10 DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)8 PermissionException (edu.harvard.iq.dataverse.engine.command.exception.PermissionException)8