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