Search in sources :

Example 1 with CreateDataverseCommand

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

the class DataversePage method save.

public String save() {
    List<DataverseFieldTypeInputLevel> listDFTIL = new ArrayList<>();
    if (editMode != null && editMode.equals(EditMode.INFO)) {
        List<MetadataBlock> selectedBlocks = new ArrayList<>();
        if (dataverse.isMetadataBlockRoot()) {
            dataverse.getMetadataBlocks().clear();
        }
        for (MetadataBlock mdb : this.allMetadataBlocks) {
            if (dataverse.isMetadataBlockRoot() && (mdb.isSelected() || mdb.isRequired())) {
                selectedBlocks.add(mdb);
                for (DatasetFieldType dsft : mdb.getDatasetFieldTypes()) {
                    if (dsft.isRequiredDV() && !dsft.isRequired() && ((!dsft.isHasParent() && dsft.isInclude()) || (dsft.isHasParent() && dsft.getParentDatasetFieldType().isInclude()))) {
                        DataverseFieldTypeInputLevel dftil = new DataverseFieldTypeInputLevel();
                        dftil.setDatasetFieldType(dsft);
                        dftil.setDataverse(dataverse);
                        dftil.setRequired(true);
                        dftil.setInclude(true);
                        listDFTIL.add(dftil);
                    }
                    if ((!dsft.isHasParent() && !dsft.isInclude()) || (dsft.isHasParent() && !dsft.getParentDatasetFieldType().isInclude())) {
                        DataverseFieldTypeInputLevel dftil = new DataverseFieldTypeInputLevel();
                        dftil.setDatasetFieldType(dsft);
                        dftil.setDataverse(dataverse);
                        dftil.setRequired(false);
                        dftil.setInclude(false);
                        listDFTIL.add(dftil);
                    }
                }
            }
        }
        if (!selectedBlocks.isEmpty()) {
            dataverse.setMetadataBlocks(selectedBlocks);
        }
        if (!dataverse.isFacetRoot()) {
            facets.getTarget().clear();
        }
    }
    Command<Dataverse> cmd = null;
    // TODO change to Create - for now the page is expecting INFO instead.
    Boolean create;
    if (dataverse.getId() == null) {
        if (session.getUser().isAuthenticated()) {
            dataverse.setOwner(ownerId != null ? dataverseService.find(ownerId) : null);
            create = Boolean.TRUE;
            cmd = new CreateDataverseCommand(dataverse, dvRequestService.getDataverseRequest(), facets.getTarget(), listDFTIL);
        } else {
            JH.addMessage(FacesMessage.SEVERITY_FATAL, BundleUtil.getStringFromBundle("dataverse.create.authenticatedUsersOnly"));
            return null;
        }
    } else {
        create = Boolean.FALSE;
        if (editMode != null && editMode.equals(EditMode.FEATURED)) {
            cmd = new UpdateDataverseCommand(dataverse, null, featuredDataverses.getTarget(), dvRequestService.getDataverseRequest(), null);
        } else {
            cmd = new UpdateDataverseCommand(dataverse, facets.getTarget(), null, dvRequestService.getDataverseRequest(), listDFTIL);
        }
    }
    try {
        dataverse = commandEngine.submit(cmd);
        if (session.getUser() instanceof AuthenticatedUser) {
            if (create) {
                userNotificationService.sendNotification((AuthenticatedUser) session.getUser(), dataverse.getCreateDate(), Type.CREATEDV, dataverse.getId());
            }
        }
        String message;
        if (editMode != null && editMode.equals(EditMode.FEATURED)) {
            message = "The featured dataverses for this dataverse have been updated.";
        } else {
            message = (create) ? BundleUtil.getStringFromBundle("dataverse.create.success", Arrays.asList(settingsWrapper.getGuidesBaseUrl(), systemConfig.getGuidesVersion())) : BundleUtil.getStringFromBundle("dataverse.update.success");
        }
        JsfHelper.addSuccessMessage(message);
        editMode = null;
        return returnRedirect();
    } catch (CommandException ex) {
        logger.log(Level.SEVERE, "Unexpected Exception calling dataverse command", ex);
        String errMsg = create ? BundleUtil.getStringFromBundle("dataverse.create.failure") : BundleUtil.getStringFromBundle("dataverse.update.failure");
        JH.addMessage(FacesMessage.SEVERITY_FATAL, errMsg);
        return null;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Unexpected Exception calling dataverse command", e);
        String errMsg = create ? BundleUtil.getStringFromBundle("dataverse.create.failure") : BundleUtil.getStringFromBundle("dataverse.update.failure");
        JH.addMessage(FacesMessage.SEVERITY_FATAL, errMsg);
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) UpdateDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) CreateDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDataverseCommand) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) SearchException(edu.harvard.iq.dataverse.search.SearchException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) EJBException(javax.ejb.EJBException)

Example 2 with CreateDataverseCommand

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

the class ImportServiceBean method createDataverse.

/**
 * This is just a convenience method, for testing migration.  It creates
 * a dummy dataverse with the directory name as dataverse name & alias.
 * @param dvName
 * @param dataverseRequest
 * @return
 * @throws ImportException
 */
@TransactionAttribute(REQUIRES_NEW)
public Dataverse createDataverse(String dvName, DataverseRequest dataverseRequest) throws ImportException {
    Dataverse d = new Dataverse();
    Dataverse root = dataverseService.findByAlias("root");
    d.setOwner(root);
    d.setAlias(dvName);
    d.setName(dvName);
    d.setAffiliation("affiliation");
    d.setPermissionRoot(false);
    d.setDescription("description");
    d.setDataverseType(Dataverse.DataverseType.RESEARCHERS);
    DataverseContact dc = new DataverseContact();
    dc.setContactEmail("pete@mailinator.com");
    ArrayList<DataverseContact> dcList = new ArrayList<>();
    dcList.add(dc);
    d.setDataverseContacts(dcList);
    try {
        d = engineSvc.submit(new CreateDataverseCommand(d, dataverseRequest, null, null));
    } catch (EJBException ex) {
        Throwable cause = ex;
        StringBuilder sb = new StringBuilder();
        sb.append("Error creating dataverse.");
        while (cause.getCause() != null) {
            cause = cause.getCause();
            if (cause instanceof ConstraintViolationException) {
                ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                    sb.append(" Invalid value: <<<").append(violation.getInvalidValue()).append(">>> for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
                }
            }
        }
        logger.log(Level.SEVERE, sb.toString());
        System.out.println("Error creating dataverse: " + sb.toString());
        throw new ImportException(sb.toString());
    } catch (CommandException e) {
        throw new ImportException(e.getMessage());
    }
    return d;
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) ConstraintViolationException(javax.validation.ConstraintViolationException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) CreateDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDataverseCommand) EJBException(javax.ejb.EJBException) Dataverse(edu.harvard.iq.dataverse.Dataverse) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 3 with CreateDataverseCommand

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

the class Dataverses method addDataverse.

@POST
@Path("{identifier}")
public Response addDataverse(String body, @PathParam("identifier") String parentIdtf) {
    Dataverse d;
    JsonObject dvJson;
    try (StringReader rdr = new StringReader(body)) {
        dvJson = Json.createReader(rdr).readObject();
        d = jsonParser().parseDataverse(dvJson);
    } catch (JsonParsingException jpe) {
        LOGGER.log(Level.SEVERE, "Json: {0}", body);
        return error(Status.BAD_REQUEST, "Error parsing Json: " + jpe.getMessage());
    } catch (JsonParseException ex) {
        Logger.getLogger(Dataverses.class.getName()).log(Level.SEVERE, "Error parsing dataverse from json: " + ex.getMessage(), ex);
        return error(Response.Status.BAD_REQUEST, "Error parsing the POSTed json into a dataverse: " + ex.getMessage());
    }
    try {
        if (!parentIdtf.isEmpty()) {
            Dataverse owner = findDataverseOrDie(parentIdtf);
            d.setOwner(owner);
        }
        // set the dataverse - contact relationship in the contacts
        for (DataverseContact dc : d.getDataverseContacts()) {
            dc.setDataverse(d);
        }
        AuthenticatedUser u = findAuthenticatedUserOrDie();
        d = execCommand(new CreateDataverseCommand(d, createDataverseRequest(u), null, null));
        return created("/dataverses/" + d.getAlias(), json(d));
    } catch (WrappedResponse ww) {
        Throwable cause = ww.getCause();
        StringBuilder sb = new StringBuilder();
        if (cause == null) {
            return ww.refineResponse("cause was null!");
        }
        while (cause.getCause() != null) {
            cause = cause.getCause();
            if (cause instanceof ConstraintViolationException) {
                ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                    sb.append(" Invalid value: <<<").append(violation.getInvalidValue()).append(">>> for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
                }
            }
        }
        String error = sb.toString();
        if (!error.isEmpty()) {
            LOGGER.log(Level.INFO, error);
            return ww.refineResponse(error);
        }
        return ww.getResponse();
    } catch (EJBException ex) {
        Throwable cause = ex;
        StringBuilder sb = new StringBuilder();
        sb.append("Error creating dataverse.");
        while (cause.getCause() != null) {
            cause = cause.getCause();
            if (cause instanceof ConstraintViolationException) {
                ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                    sb.append(" Invalid value: <<<").append(violation.getInvalidValue()).append(">>> for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
                }
            }
        }
        LOGGER.log(Level.SEVERE, sb.toString());
        return error(Response.Status.INTERNAL_SERVER_ERROR, "Error creating dataverse: " + sb.toString());
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Error creating dataverse", ex);
        return error(Response.Status.INTERNAL_SERVER_ERROR, "Error creating dataverse: " + ex.getMessage());
    }
}
Also used : JsonObject(javax.json.JsonObject) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) JsonString(javax.json.JsonString) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) CreateDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDataverseCommand) Dataverse(edu.harvard.iq.dataverse.Dataverse) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) EJBException(javax.ejb.EJBException) JsonParsingException(javax.json.stream.JsonParsingException) ConstraintViolationException(javax.validation.ConstraintViolationException) ConstraintViolation(javax.validation.ConstraintViolation) StringReader(java.io.StringReader) ConstraintViolationException(javax.validation.ConstraintViolationException) EJBException(javax.ejb.EJBException) JsonParsingException(javax.json.stream.JsonParsingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

CreateDataverseCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateDataverseCommand)3 EJBException (javax.ejb.EJBException)3 Dataverse (edu.harvard.iq.dataverse.Dataverse)2 DataverseContact (edu.harvard.iq.dataverse.DataverseContact)2 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)2 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)2 ArrayList (java.util.ArrayList)2 ConstraintViolation (javax.validation.ConstraintViolation)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 UpdateDataverseCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand)1 SearchException (edu.harvard.iq.dataverse.search.SearchException)1 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)1 StringReader (java.io.StringReader)1 TransactionAttribute (javax.ejb.TransactionAttribute)1 JsonObject (javax.json.JsonObject)1 JsonString (javax.json.JsonString)1 JsonParsingException (javax.json.stream.JsonParsingException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1