Search in sources :

Example 11 with EntityExistsException

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;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project)

Example 12 with EntityExistsException

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;
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) DTUserGroup(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUserGroup) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)12 Test (org.junit.Test)6 Project (ca.corefacility.bioinformatics.irida.model.project.Project)4 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)4 ConstraintViolationException (javax.validation.ConstraintViolationException)4 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)4 ConstraintViolation (javax.validation.ConstraintViolation)3 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)2 User (ca.corefacility.bioinformatics.irida.model.user.User)2 HashMap (java.util.HashMap)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2 LaunchesProjectEvent (ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent)1 PasswordReusedException (ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException)1 Announcement (ca.corefacility.bioinformatics.irida.model.announcements.Announcement)1 ProjectEvent (ca.corefacility.bioinformatics.irida.model.event.ProjectEvent)1 SampleAddedProjectEvent (ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent)1 UserRemovedProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent)1 UserRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent)1