diff --git a/src/main/java/ca/corefacility/bioinformatics/irida/repositories/sample/MetadataEntryRepository.java b/src/main/java/ca/corefacility/bioinformatics/irida/repositories/sample/MetadataEntryRepository.java index e8780558bbe..7acb5227890 100644 --- a/src/main/java/ca/corefacility/bioinformatics/irida/repositories/sample/MetadataEntryRepository.java +++ b/src/main/java/ca/corefacility/bioinformatics/irida/repositories/sample/MetadataEntryRepository.java @@ -1,10 +1,12 @@ package ca.corefacility.bioinformatics.irida.repositories.sample; +import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField; import ca.corefacility.bioinformatics.irida.model.sample.Sample; import ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry; import ca.corefacility.bioinformatics.irida.repositories.IridaJpaRepository; import org.springframework.data.jpa.repository.Query; +import java.util.List; import java.util.Set; /** @@ -12,12 +14,22 @@ */ public interface MetadataEntryRepository extends IridaJpaRepository { - /** - * Get all the {@link MetadataEntry} attached to the given {@link Sample} - * - * @param sample the sample to get metadata for - * @return a set of {@link MetadataEntry} - */ - @Query("FROM MetadataEntry m WHERE m.sample=?1") - Set getMetadataForSample(Sample sample); + /** + * Get all the {@link MetadataEntry} attached to the given {@link Sample} + * + * @param sample the sample to get metadata for + * @return a set of {@link MetadataEntry} + */ + @Query("FROM MetadataEntry m WHERE m.sample=?1") + Set getMetadataForSample(Sample sample); + + /** + * Get the {@link MetadataEntry} related to the {@link MetadataTemplateField} attached to the given {@link Sample} + * + * @param sample the sample to get metadata for + * @param fields the fields to get on the sample + * @return a set of {@link MetadataEntry} + */ + @Query("FROM MetadataEntry m WHERE m.sample=?1 AND m.field IN (?2)") + Set getMetadataForSampleAndField(Sample sample, List fields); } diff --git a/src/main/java/ca/corefacility/bioinformatics/irida/ria/web/linelist/LineListController.java b/src/main/java/ca/corefacility/bioinformatics/irida/ria/web/linelist/LineListController.java index 00d7388bea1..da03d725e77 100644 --- a/src/main/java/ca/corefacility/bioinformatics/irida/ria/web/linelist/LineListController.java +++ b/src/main/java/ca/corefacility/bioinformatics/irida/ria/web/linelist/LineListController.java @@ -69,20 +69,30 @@ public LineListController(ProjectService projectService, SampleService sampleSer * {@link Sample}s in a {@link Project} * * @param projectId {@link Long} identifier for a {@link Project} + * @param fieldIds List of metadata field IDs to return * @return {@link List} of {@link UISampleMetadata}s of all {@link Sample} metadata in a {@link Project} */ @RequestMapping(value = "/entries", method = RequestMethod.GET) @ResponseBody - public List getProjectSamplesMetadataEntries(@RequestParam long projectId) { + public List getProjectSamplesMetadataEntries(@RequestParam long projectId, @RequestParam(required = false, defaultValue = "") List fieldIds) { Authentication authentication = SecurityContextHolder.getContext() .getAuthentication(); Project project = projectService.read(projectId); + //if we haven't requested anything, get all fields + List metadataTemplateFields; + if (fieldIds.isEmpty()) { + metadataTemplateFields = metadataTemplateService.getMetadataFieldsForProject(project); + } else { + metadataTemplateFields = fieldIds.stream().map(i -> metadataTemplateService.readMetadataField(i)).collect(Collectors.toList()); + } + + List> projectSamples = sampleService.getSamplesForProject(project); return projectSamples.stream() .map(join -> { - ProjectSampleJoin psj = (ProjectSampleJoin)join; - Set metadata = sampleService.getMetadataForSample(psj.getObject()); + ProjectSampleJoin psj = (ProjectSampleJoin) join; + Set metadata = sampleService.getMetadataForSample(psj.getObject(), metadataTemplateFields); return new UISampleMetadata(psj, updateSamplePermission.isAllowed(authentication, psj.getObject()), metadata); }) .collect(Collectors.toList()); diff --git a/src/main/java/ca/corefacility/bioinformatics/irida/service/impl/sample/SampleServiceImpl.java b/src/main/java/ca/corefacility/bioinformatics/irida/service/impl/sample/SampleServiceImpl.java index c98e14cec6c..33f2d9bc6fd 100644 --- a/src/main/java/ca/corefacility/bioinformatics/irida/service/impl/sample/SampleServiceImpl.java +++ b/src/main/java/ca/corefacility/bioinformatics/irida/service/impl/sample/SampleServiceImpl.java @@ -197,6 +197,15 @@ public Set getMetadataForSample(Sample sample) { return metadataEntryRepository.getMetadataForSample(sample); } + /** + * {@inheritDoc} + */ + @PreAuthorize("hasPermission(#sample, 'canReadSample')") + @Override + public Set getMetadataForSample(Sample sample, List fields) { + return metadataEntryRepository.getMetadataForSampleAndField(sample, fields); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/ca/corefacility/bioinformatics/irida/service/sample/SampleService.java b/src/main/java/ca/corefacility/bioinformatics/irida/service/sample/SampleService.java index ffcdbd7f9b1..97f187b90d4 100644 --- a/src/main/java/ca/corefacility/bioinformatics/irida/service/sample/SampleService.java +++ b/src/main/java/ca/corefacility/bioinformatics/irida/service/sample/SampleService.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Set; +import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField; import ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort; @@ -76,6 +77,15 @@ public interface SampleService extends CRUDService { * @return the metadata associated with the given sample */ public Set getMetadataForSample(Sample sample); + + /** + * Get the {@link MetadataEntry} related to the {@link MetadataTemplateField} attached to the given {@link Sample} + * + * @param sample the sample to get metadata for + * @param fields the fields to get on the sample + * @return a set of {@link MetadataEntry} + */ + public Set getMetadataForSample(Sample sample, List fields); /** * Find a {@link Sample} assocaited with a {@link SequencingObject} diff --git a/src/test/java/ca/corefacility/bioinformatics/irida/ria/unit/web/LineListControllerTest.java b/src/test/java/ca/corefacility/bioinformatics/irida/ria/unit/web/LineListControllerTest.java index 7d66cf070db..7b70b39d6d6 100644 --- a/src/test/java/ca/corefacility/bioinformatics/irida/ria/unit/web/LineListControllerTest.java +++ b/src/test/java/ca/corefacility/bioinformatics/irida/ria/unit/web/LineListControllerTest.java @@ -2,6 +2,7 @@ import static org.mockito.Mockito.*; +import java.util.ArrayList; import java.util.Locale; import org.junit.Before; @@ -51,7 +52,7 @@ public void testGetProjectMetadataTemplateFields() { @Test public void testGetAllProjectMetadataEntries() { long projectId = 1L; - lineListController.getProjectSamplesMetadataEntries(projectId); + lineListController.getProjectSamplesMetadataEntries(projectId, new ArrayList<>()); verify(sampleService, times(1)).getSamplesForProject(any(Project.class)); } }