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