Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#203] Add new findAllAvailableRecordsPhas…
Browse files Browse the repository at this point in the history
…es service method for retrieving available Records Phases
  • Loading branch information
palagdan committed Aug 8, 2024
1 parent 1f4e2d8 commit fede72f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -243,6 +236,16 @@ private <T> Page<T> findRecords(RecordFilterParams filters, Pageable pageSpec, C
return new PageImpl<>(records, pageSpec, totalCount);
}

public Set<RecordPhase> 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<RawRecord> findAllRecordsRaw(RecordFilterParams filters, Pageable pageSpec){
final Map<String, Object> queryParams = new HashMap<>();
final String whereClause = constructWhereClauseWithGraphs(filters, queryParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ public List<PatientRecordDto> getRecords(
@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)")
@GetMapping(value="availablePhases", produces = MediaType.APPLICATION_JSON_VALUE)
public Set<RecordPhase> getAvailableRecordPhases(@RequestParam(value = "institution", required = false) String institutionKey){
List<PatientRecord> records = recordService.findAll();
return records.stream()
.map(PatientRecord::getPhase)
.collect(Collectors.toSet());
return recordService.findAllAvailableRecordsPhases();
}


Expand Down
16 changes: 16 additions & 0 deletions src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Set;

public interface PatientRecordService extends BaseService<PatientRecord> {

Expand Down Expand Up @@ -87,4 +88,19 @@ public interface PatientRecordService extends BaseService<PatientRecord> {
* @return List of matching records
*/
Page<RawRecord> exportRecords(RecordFilterParams filters, Pageable pageSpec);



/**
* Retrieves a set of all distinct phases that records can be in.
* <p>
* 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<RecordPhase> findAllAvailableRecordsPhases();


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PatientRecord>
Expand Down Expand Up @@ -134,4 +131,10 @@ public RecordImportResult importRecords(List<PatientRecord> records, RecordPhase
public Page<RawRecord> exportRecords(RecordFilterParams filters, Pageable pageSpec){
return patientRecordDao.findAllRecordsRaw(filters, pageSpec);
}

@Transactional(readOnly = true)
@Override
public Set<RecordPhase> findAllAvailableRecordsPhases() {
return recordDao.findAllAvailableRecordsPhases();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,10 @@ public void getRecordThrowsNotFoundWhenReportIsNotFound() throws Exception {

@Test
public void testGetAvailableRecordPhases() throws Exception{

Set<RecordPhase> 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<PatientRecord> records = Arrays.asList(record1, record2);

when(patientRecordServiceMock.findAll()).thenReturn(records);
when(patientRecordServiceMock.findAllAvailableRecordsPhases()).thenReturn(phases);

final MvcResult result = mockMvc.perform(get("/records/availablePhases"))
.andReturn();
Expand Down

0 comments on commit fede72f

Please sign in to comment.