Search in sources :

Example 26 with ProjectSampleJoin

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

the class UpdateSamplePermissionTest method testGrantPermission.

@Test
public void testGrantPermission() {
    final Project p1 = new Project();
    final Project p2 = new Project();
    final Sample s = new Sample();
    when(projectSampleJoinRepository.getProjectForSample(s)).thenReturn(ImmutableList.of(new ProjectSampleJoin(p1, s, true), new ProjectSampleJoin(p2, s, true)));
    when(projectOwnerPermission.isAllowed(auth, p1)).thenReturn(true);
    when(projectOwnerPermission.isAllowed(auth, p2)).thenReturn(true);
    assertTrue("Permission to update sample should be given.", updateSamplePermission.isAllowed(auth, s));
}
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) Test(org.junit.Test)

Example 27 with ProjectSampleJoin

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

the class UpdateSamplePermissionTest method testRejectPermissionWithNoProjects.

@Test
public void testRejectPermissionWithNoProjects() {
    final Project p1 = new Project();
    final Project p2 = new Project();
    final Sample s = new Sample();
    when(projectSampleJoinRepository.getProjectForSample(s)).thenReturn(ImmutableList.of(new ProjectSampleJoin(p1, s, true), new ProjectSampleJoin(p2, s, true)));
    when(projectOwnerPermission.isAllowed(auth, p1)).thenReturn(false);
    when(projectOwnerPermission.isAllowed(auth, p2)).thenReturn(false);
    assertFalse("Permission to update sample should not be given.", updateSamplePermission.isAllowed(auth, s));
}
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) Test(org.junit.Test)

Example 28 with ProjectSampleJoin

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

the class TestDataFactory method getPageOfProjectSampleJoin.

public static Page<ProjectSampleJoin> getPageOfProjectSampleJoin() {
    return new Page<ProjectSampleJoin>() {

        @Override
        public int getTotalPages() {
            return 1;
        }

        @Override
        public long getTotalElements() {
            return 1;
        }

        @Override
        public int getNumber() {
            return 1;
        }

        @Override
        public int getSize() {
            return 1;
        }

        @Override
        public int getNumberOfElements() {
            return 1;
        }

        @Override
        public List<ProjectSampleJoin> getContent() {
            Project project = new Project("Joined Project");
            project.setId(1L);
            Sample sample = new Sample("Joined Sample");
            sample.setId(23L);
            ProjectSampleJoin join = new ProjectSampleJoin(project, sample, true);
            return ImmutableList.of(join);
        }

        @Override
        public boolean hasContent() {
            return false;
        }

        @Override
        public Sort getSort() {
            return null;
        }

        @Override
        public boolean isFirst() {
            return false;
        }

        @Override
        public boolean isLast() {
            return false;
        }

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public boolean hasPrevious() {
            return false;
        }

        @Override
        public Pageable nextPageable() {
            return null;
        }

        @Override
        public Pageable previousPageable() {
            return null;
        }

        @Override
        public Iterator<ProjectSampleJoin> iterator() {
            return null;
        }
    };
}
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) Page(org.springframework.data.domain.Page)

Example 29 with ProjectSampleJoin

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

the class SampleServiceImpl method mergeSamples.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@PreAuthorize("hasPermission(#project, 'isProjectOwner') and hasPermission(#mergeInto, 'canUpdateSample') and hasPermission(#toMerge, 'canUpdateSample')")
public Sample mergeSamples(Project project, Sample mergeInto, Collection<Sample> toMerge) {
    // confirm that all samples are part of the same project:
    confirmProjectSampleJoin(project, mergeInto);
    logger.debug("Merging samples " + toMerge.stream().map(t -> t.getId()).collect(Collectors.toList()) + " into sample [" + mergeInto.getId() + "]");
    for (Sample s : toMerge) {
        confirmProjectSampleJoin(project, s);
        List<SampleSequencingObjectJoin> sequencesForSample = ssoRepository.getSequencesForSample(s);
        for (SampleSequencingObjectJoin join : sequencesForSample) {
            SequencingObject sequencingObject = join.getObject();
            ssoRepository.delete(join);
            addSequencingObjectToSample(mergeInto, sequencingObject);
        }
        Collection<SampleGenomeAssemblyJoin> genomeAssemblyJoins = getAssembliesForSample(s);
        for (SampleGenomeAssemblyJoin join : genomeAssemblyJoins) {
            GenomeAssembly genomeAssembly = join.getObject();
            logger.trace("Removing genome assembly [" + genomeAssembly.getId() + "] from sample [" + s.getId() + "]");
            sampleGenomeAssemblyJoinRepository.delete(join);
            logger.trace("Adding genome assembly [" + genomeAssembly.getId() + "] to sample [" + mergeInto.getId() + "]");
            SampleGenomeAssemblyJoin newJoin = new SampleGenomeAssemblyJoin(mergeInto, genomeAssembly);
            sampleGenomeAssemblyJoinRepository.save(newJoin);
        }
        // have to remove the sample to be deleted from its project:
        ProjectSampleJoin readSampleForProject = psjRepository.readSampleForProject(project, s);
        psjRepository.delete(readSampleForProject);
        sampleRepository.delete(s.getId());
    }
    return mergeInto;
}
Also used : SampleGenomeAssemblyJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.SampleGenomeAssemblyJoin) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) GenomeAssembly(ca.corefacility.bioinformatics.irida.model.assembly.GenomeAssembly) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 30 with ProjectSampleJoin

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

the class ProjectServiceImpl method addSampleToProject.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@LaunchesProjectEvent(SampleAddedProjectEvent.class)
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SEQUENCER') or (hasPermission(#project, 'isProjectOwner'))")
public ProjectSampleJoin addSampleToProject(Project project, Sample sample, boolean owner) {
    logger.trace("Adding sample to project.");
    // project already
    if (sampleRepository.getSampleBySampleName(project, sample.getSampleName()) != null) {
        throw new EntityExistsException("Sample with sequencer id '" + sample.getSampleName() + "' already exists in project " + project.getId());
    }
    // the relationshipRepository.
    if (sample.getId() == null) {
        logger.trace("Going to validate and persist sample prior to creating relationship.");
        // validate the sample, then persist it:
        Set<ConstraintViolation<Sample>> constraintViolations = validator.validate(sample);
        if (constraintViolations.isEmpty()) {
            sample = sampleRepository.save(sample);
        } else {
            throw new ConstraintViolationException(constraintViolations);
        }
    }
    ProjectSampleJoin join = new ProjectSampleJoin(project, sample, owner);
    try {
        return psjRepository.save(join);
    } catch (DataIntegrityViolationException e) {
        throw new EntityExistsException("Sample [" + sample.getId() + "] has already been added to project [" + project.getId() + "]");
    }
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) 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