use of ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException in project irida by phac-nml.
the class ProjectSamplesController method shareSampleToProject.
/**
* Share or move samples from one project to another
*
* @param projectId The original project id
* @param sampleIds the sample identifiers to share
* @param newProjectId The new project id
* @param remove true/false whether to remove the samples from the original project
* @param giveOwner whether to give ownership of the sample to the new project
* @param locale the locale specified by the browser.
* @return A list of warnings
*/
@RequestMapping(value = "/projects/{projectId}/ajax/samples/copy", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> shareSampleToProject(@PathVariable Long projectId, @RequestParam(value = "sampleIds[]") List<Long> sampleIds, @RequestParam Long newProjectId, @RequestParam(required = false) boolean remove, @RequestParam(required = false, defaultValue = "false") boolean giveOwner, Locale locale) {
Project originalProject = projectService.read(projectId);
Project newProject = projectService.read(newProjectId);
Map<String, Object> response = new HashMap<>();
List<String> warnings = new ArrayList<>();
Iterable<Sample> samples = sampleService.readMultiple(sampleIds);
List<ProjectSampleJoin> successful = new ArrayList<>();
try {
if (remove) {
successful = projectService.moveSamples(originalProject, newProject, Lists.newArrayList(samples), giveOwner);
} else {
successful = projectService.shareSamples(originalProject, newProject, Lists.newArrayList(samples), giveOwner);
}
} catch (EntityExistsException ex) {
logger.warn("Attempt to add project to sample failed", ex);
warnings.add(ex.getLocalizedMessage());
} catch (AccessDeniedException ex) {
logger.warn("Access denied adding samples to project " + newProjectId, ex);
String msg = remove ? "project.samples.move.sample-denied" : "project.samples.copy.sample-denied";
warnings.add(messageSource.getMessage(msg, new Object[] { newProject.getName() }, locale));
}
if (!warnings.isEmpty() || successful.size() == 0) {
response.put("result", "warning");
response.put("warnings", warnings);
} else {
response.put("result", "success");
}
// 2. Only one sample moved
if (successful.size() == 1) {
if (remove) {
response.put("message", messageSource.getMessage("project.samples.move-single-success-message", new Object[] { successful.get(0).getObject().getSampleName(), newProject.getName() }, locale));
} else {
response.put("message", messageSource.getMessage("project.samples.copy-single-success-message", new Object[] { successful.get(0).getObject().getSampleName(), newProject.getName() }, locale));
}
} else // 4. Multiple samples moved
if (successful.size() > 1) {
if (remove) {
response.put("message", messageSource.getMessage("project.samples.move-multiple-success-message", new Object[] { successful.size(), newProject.getName() }, locale));
} else {
response.put("message", messageSource.getMessage("project.samples.copy-multiple-success-message", new Object[] { successful.size(), newProject.getName() }, locale));
}
}
response.put("successful", successful.stream().map(ProjectSampleJoin::getId).collect(Collectors.toList()));
return response;
}
use of ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException in project irida by phac-nml.
the class GroupsController method editGroup.
/**
* Submit changes to the {@link UserGroup}.
*
* @param userGroupId
* the group ID to edit.
* @param name
* the new name of the group.
* @param description
* the new description of the group.
* @param principal
* the currently logged in user.
* @param model
* the model to add attributes to.
* @param locale
* the locale of the browser.
* @return the route to the editing page on validation failure, or the
* details page on success.
*/
@RequestMapping(path = "/{userGroupId}/edit", method = RequestMethod.POST)
public String editGroup(@PathVariable final Long userGroupId, @RequestParam final String name, @RequestParam final String description, final Principal principal, final Model model, final Locale locale) {
logger.debug("Editing group: [" + userGroupId + "]");
final Map<String, String> errors = new HashMap<>();
UserGroup group = userGroupService.read(userGroupId);
try {
group.setName(name);
group.setDescription(description);
userGroupService.update(group);
return getDetailsPage(userGroupId, principal, model);
} catch (final ConstraintViolationException e) {
for (final ConstraintViolation<?> v : e.getConstraintViolations()) {
errors.put(v.getPropertyPath().toString(), v.getMessage());
}
} catch (final EntityExistsException | DataIntegrityViolationException e) {
errors.put("name", messageSource.getMessage("group.name.exists", null, locale));
}
model.addAttribute("errors", errors);
model.addAttribute("group", userGroupService.read(userGroupId));
model.addAttribute("given_name", name);
model.addAttribute("given_description", description);
return GROUPS_EDIT;
}
Aggregations