Search in sources :

Example 1 with DataverseContact

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

the class JsonParser method parseDataverse.

public Dataverse parseDataverse(JsonObject jobj) throws JsonParseException {
    Dataverse dv = new Dataverse();
    /**
     * @todo Instead of this getMandatoryString method we should run the
     * String through ConstraintValidator. See EMailValidatorTest and
     * EMailValidator for examples. That way we can check not only if it's
     * required or not but other bean validation rules such as "must match
     * this regex".
     */
    dv.setAlias(getMandatoryString(jobj, "alias"));
    dv.setName(getMandatoryString(jobj, "name"));
    dv.setDescription(jobj.getString("description", null));
    dv.setPermissionRoot(jobj.getBoolean("permissionRoot", false));
    dv.setFacetRoot(jobj.getBoolean("facetRoot", false));
    dv.setAffiliation(jobj.getString("affiliation", null));
    if (jobj.containsKey("dataverseContacts")) {
        JsonArray dvContacts = jobj.getJsonArray("dataverseContacts");
        int i = 0;
        List<DataverseContact> dvContactList = new LinkedList<>();
        for (JsonValue jsv : dvContacts) {
            DataverseContact dvc = new DataverseContact(dv);
            dvc.setContactEmail(getMandatoryString((JsonObject) jsv, "contactEmail"));
            dvc.setDisplayOrder(i++);
            dvContactList.add(dvc);
        }
        dv.setDataverseContacts(dvContactList);
    }
    if (jobj.containsKey("theme")) {
        DataverseTheme theme = parseDataverseTheme(jobj.getJsonObject("theme"));
        dv.setDataverseTheme(theme);
        theme.setDataverse(dv);
    }
    // default
    dv.setDataverseType(Dataverse.DataverseType.UNCATEGORIZED);
    if (jobj.containsKey("dataverseType")) {
        for (Dataverse.DataverseType dvtype : Dataverse.DataverseType.values()) {
            if (dvtype.name().equals(jobj.getString("dataverseType"))) {
                dv.setDataverseType(dvtype);
            }
        }
    }
    return dv;
}
Also used : JsonArray(javax.json.JsonArray) DataverseTheme(edu.harvard.iq.dataverse.DataverseTheme) JsonValue(javax.json.JsonValue) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) JsonObject(javax.json.JsonObject) Dataverse(edu.harvard.iq.dataverse.Dataverse) LinkedList(java.util.LinkedList)

Example 2 with DataverseContact

use of edu.harvard.iq.dataverse.DataverseContact 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 DataverseContact

use of edu.harvard.iq.dataverse.DataverseContact 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

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