Search in sources :

Example 51 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class RESTProjectSamplesControllerTest method testCopySampleToProject.

@Test
public void testCopySampleToProject() {
    final Project p = TestDataFactory.constructProject();
    final Sample s = TestDataFactory.constructSample();
    final ProjectSampleJoin r = new ProjectSampleJoin(p, s, true);
    MockHttpServletResponse response = new MockHttpServletResponse();
    when(projectService.read(p.getId())).thenReturn(p);
    when(sampleService.read(s.getId())).thenReturn(s);
    when(projectService.addSampleToProject(p, s, false)).thenReturn(r);
    ModelMap modelMap = controller.copySampleToProject(p.getId(), Lists.newArrayList(s.getId()), response);
    verify(projectService).addSampleToProject(p, s, false);
    assertEquals("response should have CREATED status", HttpStatus.CREATED.value(), response.getStatus());
    final String location = response.getHeader(HttpHeaders.LOCATION);
    assertEquals("location should include sample and project IDs", "http://localhost/api/projects/" + p.getId() + "/samples/" + s.getId(), location);
    // test that the modelMap contains a correct resource collection.
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue("Object should be an instance of ResourceCollection", o instanceof ResourceCollection);
    @SuppressWarnings("unchecked") ResourceCollection<LabelledRelationshipResource<Project, Sample>> labeledRRs = (ResourceCollection<LabelledRelationshipResource<Project, Sample>>) o;
    assertEquals("There should be one item in the resource collection", 1, labeledRRs.size());
    List<Link> resourceLinks = labeledRRs.getLinks();
    assertEquals("There should be one link", 1, resourceLinks.size());
    Link self = resourceLinks.iterator().next();
    assertEquals("Self link should be correct", "self", self.getRel());
    assertEquals("http://localhost/api/projects/" + p.getId() + "/samples", self.getHref());
    LabelledRelationshipResource<Project, Sample> resource = labeledRRs.iterator().next();
    Object o2 = resource.getResource();
    assertTrue("Object should be an instance of ProjectSampleJoin", o2 instanceof ProjectSampleJoin);
    ProjectSampleJoin join = (ProjectSampleJoin) o2;
    Object o3 = join.getObject();
    assertTrue("Object should be an instance of Sample", o3 instanceof Sample);
    Sample sample = (Sample) o3;
    assertEquals("Sample name should be correct", s.getSampleName(), sample.getSampleName());
    List<Link> links = resource.getLinks();
    Set<String> rels = Sets.newHashSet(Link.REL_SELF, RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES, RESTProjectSamplesController.REL_PROJECT, RESTProjectSamplesController.REL_PROJECT_SAMPLE);
    for (Link link : links) {
        assertTrue("Rels should contain link [" + link + "]", rels.contains(link.getRel()));
        assertNotNull("Rels should remove link [" + link + "]", rels.remove(link.getRel()));
    }
    assertTrue("Rels should be empty after removing expected links", rels.isEmpty());
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Link(org.springframework.hateoas.Link) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) Test(org.junit.Test)

Example 52 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class ProjectServiceImpl method removeSampleFromProject.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#project, 'canManageLocalProjectSettings')")
@LaunchesProjectEvent(SampleRemovedProjectEvent.class)
public void removeSampleFromProject(Project project, Sample sample) {
    ProjectSampleJoin readSampleForProject = psjRepository.readSampleForProject(project, sample);
    psjRepository.delete(readSampleForProject);
    // if the sample doesn't refer to any other projects, delete it
    if (psjRepository.getProjectForSample(sample).isEmpty()) {
        sampleRepository.delete(sample);
    }
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 53 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class ProjectServiceImpl method shareSamples.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@LaunchesProjectEvent(SampleAddedProjectEvent.class)
@PreAuthorize("hasPermission(#source, 'canManageLocalProjectSettings')" + " and hasPermission(#destination, 'isProjectOwner')" + " and hasPermission(#samples, 'canReadSample')" + " and ((not #giveOwner) or hasPermission(#samples, 'canUpdateSample'))")
public List<ProjectSampleJoin> shareSamples(Project source, Project destination, Collection<Sample> samples, boolean giveOwner) {
    List<ProjectSampleJoin> newJoins = new ArrayList<>();
    for (Sample sample : samples) {
        ProjectSampleJoin newJoin = addSampleToProject(destination, sample, giveOwner);
        logger.trace("Shared sample " + sample.getId() + " to project " + destination.getId());
        newJoins.add(newJoin);
    }
    return newJoins;
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ArrayList(java.util.ArrayList) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 54 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class ProjectServiceImpl method moveSampleBetweenProjects.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@LaunchesProjectEvent(SampleAddedProjectEvent.class)
@PreAuthorize("hasRole('ROLE_ADMIN') or ( hasPermission(#source, 'isProjectOwner') and hasPermission(#destination, 'isProjectOwner'))")
public ProjectSampleJoin moveSampleBetweenProjects(Project source, Project destination, Sample sample, boolean owner) {
    ProjectSampleJoin join = addSampleToProject(destination, sample, owner);
    removeSampleFromProject(source, sample);
    return join;
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 55 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class SampleServiceImpl method getSampleForProject.

/**
 * {@inheritDoc}
 */
@Override
@Transactional(readOnly = true)
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_SEQUENCER') or (hasPermission(#project, 'canReadProject') and hasPermission(#sampleId, 'canReadSample'))")
public ProjectSampleJoin getSampleForProject(Project project, Long sampleId) {
    Sample sample = read(sampleId);
    ProjectSampleJoin join = psjRepository.readSampleForProject(project, sample);
    if (join == null) {
        throw new EntityNotFoundException("Join between the project and this identifier doesn't exist");
    }
    return join;
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)59 Project (ca.corefacility.bioinformatics.irida.model.project.Project)51 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)51 Test (org.junit.Test)43 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)15 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)15 ArrayList (java.util.ArrayList)11 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)8 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)8 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 User (ca.corefacility.bioinformatics.irida.model.user.User)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Authentication (org.springframework.security.core.Authentication)7 Transactional (org.springframework.transaction.annotation.Transactional)7 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)6 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)6 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)6 LaunchesProjectEvent (ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent)5 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)5 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)5