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;
}
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;
}
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());
}
}
Aggregations