Search in sources :

Example 81 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class RESTProjectSamplesController method removeSampleFromProject.

/**
 * Remove a specific {@link Sample} from the collection of {@link Sample}s
 * associated with a {@link Project}.
 *
 * @param projectId
 *            the {@link Project} identifier.
 * @param sampleId
 *            the {@link Sample} identifier.
 * @return a response including links back to the specific {@link Project}
 *         and collection of {@link Sample}.
 */
@RequestMapping(value = "/api/projects/{projectId}/samples/{sampleId}", method = RequestMethod.DELETE)
public ModelMap removeSampleFromProject(@PathVariable Long projectId, @PathVariable Long sampleId) {
    ModelMap modelMap = new ModelMap();
    // load the sample and project
    Project p = projectService.read(projectId);
    Sample s = sampleService.read(sampleId);
    // remove the relationship.
    projectService.removeSampleFromProject(p, s);
    // respond to the client.
    RootResource resource = new RootResource();
    // add links back to the collection of samples and to the project
    // itself.
    resource.add(linkTo(methodOn(RESTProjectSamplesController.class).getProjectSamples(projectId)).withRel(REL_PROJECT_SAMPLES));
    resource.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(RESTProjectsController.REL_PROJECT));
    // add the links to the response.
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, resource);
    return modelMap;
}
Also used : RootResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 82 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class RESTProjectUsersController method addUserToProject.

/**
 * Add a relationship between a {@link Project} and a {@link User}.
 *
 * @param projectId
 *            the project ID to add the user to.
 * @param representation
 *            the JSON key-value pair that contains the identifier for the
 *            project and the identifier for the user.
 * @param response
 *            a reference to the servlet response.
 * @return a response indicating that the collection was modified.
 * @throws ProjectWithoutOwnerException
 *             this cannot actually be thrown, it's an artifact of using
 *             spring HATEOAS {@code linkTo} and {@code methodOn}.
 */
@RequestMapping(value = "/api/projects/{projectId}/users", method = RequestMethod.POST)
public ModelMap addUserToProject(@PathVariable Long projectId, @RequestBody Map<String, String> representation, HttpServletResponse response) throws ProjectWithoutOwnerException {
    // first, get the project
    Project p = projectService.read(projectId);
    String username = representation.get(USER_ID_KEY);
    // then, get the user
    User u = userService.getUserByUsername(username);
    ProjectRole r = ProjectRole.PROJECT_USER;
    // then add the user to the project with the specified role.
    Join<Project, User> joined = projectService.addUserToProject(p, u, r);
    LabelledRelationshipResource<Project, User> lrr = new LabelledRelationshipResource<Project, User>(joined.getLabel(), joined);
    // prepare a link to the user
    lrr.add(linkTo(RESTUsersController.class).slash(u.getUsername()).withSelfRel());
    // prepare a link to the user as added to the project
    lrr.add(linkTo(methodOn(RESTProjectUsersController.class).removeUserFromProject(projectId, u.getUsername())).withRel(RESTGenericController.REL_RELATIONSHIP));
    // prepare a link back to the user collection of the project
    lrr.add(linkTo(methodOn(RESTProjectUsersController.class).getUsersForProject(projectId)).withRel(REL_PROJECT_USERS));
    // prepare a link back to the project
    lrr.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(RESTProjectsController.REL_PROJECT));
    String location = linkTo(RESTProjectsController.class).slash(projectId).slash("users").slash(username).withSelfRel().getHref();
    // add a location header and set the response status.
    response.addHeader(HttpHeaders.LOCATION, location);
    response.setStatus(HttpStatus.CREATED.value());
    // prepare the response for the client
    ModelMap modelMap = new ModelMap();
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, lrr);
    return modelMap;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) RESTUsersController(ca.corefacility.bioinformatics.irida.web.controller.api.RESTUsersController) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 83 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class RESTProjectUsersController method removeUserFromProject.

/**
 * Remove the relationship between a {@link Project} and a {@link User}.
 *
 * @param projectId the {@link Project} identifier to remove the {@link User} from.
 * @param userId    the {@link User} identifier to remove from the {@link Project}.
 * @return a response including links back to the {@link Project} and the {@link User} collection for
 *         the {@link Project}.
 * @throws ProjectWithoutOwnerException if removing this user will leave the project without an owner
 */
@RequestMapping(value = "/api/projects/{projectId}/users/{userId}", method = RequestMethod.DELETE)
public ModelMap removeUserFromProject(@PathVariable Long projectId, @PathVariable String userId) throws ProjectWithoutOwnerException {
    // Read the project and user from the database
    Project p = projectService.read(projectId);
    User u = userService.getUserByUsername(userId);
    // ask the project service to remove the user from the project
    projectService.removeUserFromProject(p, u);
    // prepare a link back to the user collection of the project
    RootResource response = new RootResource();
    response.add(linkTo(methodOn(RESTProjectUsersController.class).getUsersForProject(projectId)).withRel(REL_PROJECT_USERS));
    // prepare a link back to the project
    response.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(RESTProjectsController.REL_PROJECT));
    // respond to the client
    ModelMap modelMap = new ModelMap();
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, response);
    return modelMap;
}
Also used : RootResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource) Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 84 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class ProjectEventHandlerTest method testHandleSequenceFileAddedEventMultipleReturn.

@SuppressWarnings("unchecked")
@Test
public void testHandleSequenceFileAddedEventMultipleReturn() {
    Class<? extends ProjectEvent> clazz = DataAddedToSampleProjectEvent.class;
    Project project = new Project();
    Sample sample = new Sample();
    SequenceFile file = new SequenceFile();
    SingleEndSequenceFile seqObj1 = new SingleEndSequenceFile(file);
    SingleEndSequenceFile seqObj2 = new SingleEndSequenceFile(file);
    SampleSequencingObjectJoin join1 = new SampleSequencingObjectJoin(sample, seqObj1);
    SampleSequencingObjectJoin join2 = new SampleSequencingObjectJoin(sample, seqObj2);
    when(psjRepository.getProjectForSample(sample)).thenReturn(Lists.newArrayList(new ProjectSampleJoin(project, sample, true)));
    when(eventRepository.save(any(ProjectEvent.class))).thenReturn(new DataAddedToSampleProjectEvent(project, sample));
    Object[] args = {};
    MethodEvent methodEvent = new MethodEvent(clazz, Lists.newArrayList(join1, join2), args);
    handler.delegate(methodEvent);
    ArgumentCaptor<ProjectEvent> captor = ArgumentCaptor.forClass(ProjectEvent.class);
    verify(eventRepository).save(captor.capture());
    ProjectEvent event = captor.getValue();
    assertTrue(event instanceof DataAddedToSampleProjectEvent);
    verify(projectRepository).save(any(Project.class));
    verify(sampleRepository).save(any(Sample.class));
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) Test(org.junit.Test)

Example 85 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class ProjectEventHandlerTest method testDelegateSampleAdded.

@Test
public void testDelegateSampleAdded() {
    Class<? extends ProjectEvent> clazz = SampleAddedProjectEvent.class;
    Project project = new Project();
    Sample sample = new Sample();
    ProjectSampleJoin returnValue = new ProjectSampleJoin(project, sample, true);
    Object[] args = { project, sample };
    MethodEvent methodEvent = new MethodEvent(clazz, returnValue, args);
    when(eventRepository.save(any(ProjectEvent.class))).thenReturn(new SampleAddedProjectEvent(returnValue));
    handler.delegate(methodEvent);
    ArgumentCaptor<ProjectEvent> captor = ArgumentCaptor.forClass(ProjectEvent.class);
    verify(eventRepository).save(captor.capture());
    ProjectEvent event = captor.getValue();
    assertTrue(event instanceof SampleAddedProjectEvent);
    verify(projectRepository).save(any(Project.class));
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) Test(org.junit.Test)

Aggregations

Project (ca.corefacility.bioinformatics.irida.model.project.Project)331 Test (org.junit.Test)190 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)120 User (ca.corefacility.bioinformatics.irida.model.user.User)88 WithMockUser (org.springframework.security.test.context.support.WithMockUser)80 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)71 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)62 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)51 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)37 ArrayList (java.util.ArrayList)34 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)30 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)30 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)27 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)25 ReferenceFile (ca.corefacility.bioinformatics.irida.model.project.ReferenceFile)23 ProjectEvent (ca.corefacility.bioinformatics.irida.model.event.ProjectEvent)22 ProjectAnalysisSubmissionJoin (ca.corefacility.bioinformatics.irida.model.workflow.submission.ProjectAnalysisSubmissionJoin)22 List (java.util.List)22 UserRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent)21 ImmutableMap (com.google.common.collect.ImmutableMap)21