From fede72f1e4016e130eddd653dadfee43d35c3c02 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Thu, 8 Aug 2024 17:41:21 +0200 Subject: [PATCH] [kbss-cvut/record-manager-ui#203] Add new findAllAvailableRecordsPhases service method for retrieving available Records Phases --- .../persistence/dao/PatientRecordDao.java | 21 +++++++++++-------- .../study/rest/PatientRecordController.java | 5 +---- .../study/service/PatientRecordService.java | 16 ++++++++++++++ .../RepositoryPatientRecordService.java | 11 ++++++---- .../rest/PatientRecordControllerTest.java | 8 ++----- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDao.java b/src/main/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDao.java index d7015067..d7f4e445 100644 --- a/src/main/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDao.java +++ b/src/main/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDao.java @@ -11,10 +11,7 @@ import cz.cvut.kbss.study.dto.PatientRecordDto; import cz.cvut.kbss.study.exception.PersistenceException; import cz.cvut.kbss.study.exception.ValidationException; -import cz.cvut.kbss.study.model.Institution; -import cz.cvut.kbss.study.model.PatientRecord; -import cz.cvut.kbss.study.model.User; -import cz.cvut.kbss.study.model.Vocabulary; +import cz.cvut.kbss.study.model.*; import cz.cvut.kbss.study.model.export.RawRecord; import cz.cvut.kbss.study.persistence.dao.util.QuestionSaver; import cz.cvut.kbss.study.persistence.dao.util.RecordFilterParams; @@ -31,11 +28,7 @@ import java.math.BigInteger; import java.net.URI; import java.time.ZoneOffset; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; @Repository @@ -243,6 +236,16 @@ private Page findRecords(RecordFilterParams filters, Pageable pageSpec, C return new PageImpl<>(records, pageSpec, totalCount); } + public Set findAllAvailableRecordsPhases(){ + return em.createNativeQuery("SELECT ?o WHERE { ?s ?p ?o } ", String.class) + .setParameter("p", URI.create(Vocabulary.s_p_has_phase)) + .getResultList() + .stream() + .map(RecordPhase::fromIri) + .collect(Collectors.toSet()); + + } + public Page findAllRecordsRaw(RecordFilterParams filters, Pageable pageSpec){ final Map queryParams = new HashMap<>(); final String whereClause = constructWhereClauseWithGraphs(filters, queryParams); diff --git a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java index d675128a..cb64f01a 100644 --- a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java +++ b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java @@ -87,10 +87,7 @@ public List getRecords( @PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)") @GetMapping(value="availablePhases", produces = MediaType.APPLICATION_JSON_VALUE) public Set getAvailableRecordPhases(@RequestParam(value = "institution", required = false) String institutionKey){ - List records = recordService.findAll(); - return records.stream() - .map(PatientRecord::getPhase) - .collect(Collectors.toSet()); + return recordService.findAllAvailableRecordsPhases(); } diff --git a/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java b/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java index 08c7a98f..d942101a 100644 --- a/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java +++ b/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java @@ -10,6 +10,7 @@ import org.springframework.data.domain.Pageable; import java.util.List; +import java.util.Set; public interface PatientRecordService extends BaseService { @@ -87,4 +88,19 @@ public interface PatientRecordService extends BaseService { * @return List of matching records */ Page exportRecords(RecordFilterParams filters, Pageable pageSpec); + + + + /** + * Retrieves a set of all distinct phases that records can be in. + *

+ * This method provides a way to get a comprehensive list of the various phases + * associated with records. Phases typically represent different stages or statuses + * that a record might go through in its lifecycle. + * + * @return Set of all available record phases + */ + Set findAllAvailableRecordsPhases(); + + } diff --git a/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java b/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java index 3dc56c30..5d6f2529 100644 --- a/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java +++ b/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java @@ -21,10 +21,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Date; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; @Service public class RepositoryPatientRecordService extends KeySupportingRepositoryService @@ -134,4 +131,10 @@ public RecordImportResult importRecords(List records, RecordPhase public Page exportRecords(RecordFilterParams filters, Pageable pageSpec){ return patientRecordDao.findAllRecordsRaw(filters, pageSpec); } + + @Transactional(readOnly = true) + @Override + public Set findAllAvailableRecordsPhases() { + return recordDao.findAllAvailableRecordsPhases(); + } } diff --git a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java index cc2ea06b..15a640f1 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java @@ -97,14 +97,10 @@ public void getRecordThrowsNotFoundWhenReportIsNotFound() throws Exception { @Test public void testGetAvailableRecordPhases() throws Exception{ + Set phases = new HashSet<>(Arrays.asList(RecordPhase.completed, RecordPhase.valid)); - PatientRecord record1 = Generator.generatePatientRecord(user); - record1.setPhase(RecordPhase.completed); - PatientRecord record2 = Generator.generatePatientRecord(user); - record2.setPhase(RecordPhase.valid); - List records = Arrays.asList(record1, record2); - when(patientRecordServiceMock.findAll()).thenReturn(records); + when(patientRecordServiceMock.findAllAvailableRecordsPhases()).thenReturn(phases); final MvcResult result = mockMvc.perform(get("/records/availablePhases")) .andReturn();