Skip to content

Commit

Permalink
Workaround to fix validation error waiting for OH2-182 (informatici#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwithi authored Dec 21, 2023
1 parent 9e58a55 commit ac3e7b2
Showing 1 changed file with 60 additions and 51 deletions.
111 changes: 60 additions & 51 deletions src/main/java/org/isf/lab/rest/LaboratoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@
public class LaboratoryController {

private static final Logger LOGGER = LoggerFactory.getLogger(LaboratoryController.class);

// TODO: to centralize
protected static final String DEFAULT_PAGE_SIZE = "80";

private static final String DRAFT = LaboratoryStatus.draft.toString();

private static final String OPEN = LaboratoryStatus.open.toString();

private static final String DELETED = LaboratoryStatus.deleted.toString();

private static final String INVALID = LaboratoryStatus.invalid.toString();

private static final String DONE = LaboratoryStatus.done.toString();

@Autowired
Expand All @@ -105,8 +105,8 @@ public class LaboratoryController {
private LaboratoryForPrintMapper laboratoryForPrintMapper;

public LaboratoryController(LabManager laboratoryManager, PatientBrowserManager patientBrowserManager,
ExamBrowsingManager examManager, LaboratoryMapper laboratoryMapper, LaboratoryRowMapper laboratoryRowMapper,
LaboratoryForPrintMapper laboratoryForPrintMapper) {
ExamBrowsingManager examManager, LaboratoryMapper laboratoryMapper, LaboratoryRowMapper laboratoryRowMapper,
LaboratoryForPrintMapper laboratoryForPrintMapper) {
this.laboratoryManager = laboratoryManager;
this.patientBrowserManager = patientBrowserManager;
this.examManager = examManager;
Expand Down Expand Up @@ -134,7 +134,7 @@ public ResponseEntity<Boolean> newLaboratory(@RequestBody LabWithRowsDTO labWith
}

Exam exam = examManager.getExams().stream().filter(e -> e.getCode().equals(laboratoryDTO.getExam().getCode()))
.findFirst().orElse(null);
.findFirst().orElse(null);
if (exam == null) {
throw new OHAPIException(new OHExceptionMessage("Exam not found."));
}
Expand All @@ -155,7 +155,7 @@ public ResponseEntity<Boolean> newLaboratory(@RequestBody LabWithRowsDTO labWith
}
return ResponseEntity.status(HttpStatus.CREATED).body(true);
}

/**
* Create a new {@link LaboratoryDTO}.
*
Expand All @@ -174,7 +174,7 @@ public ResponseEntity<Boolean> newExamRequest(@RequestBody LaboratoryDTO laborat
}

Exam exam = examManager.getExams().stream().filter(e -> e.getCode().equals(laboratoryDTO.getExam().getCode()))
.findFirst().orElse(null);
.findFirst().orElse(null);
if (exam == null) {
throw new OHAPIException(new OHExceptionMessage("Exam not found."));
}
Expand All @@ -187,7 +187,7 @@ public ResponseEntity<Boolean> newExamRequest(@RequestBody LaboratoryDTO laborat
labToInsert.setResult("");
labToInsert.setInOutPatient(laboratoryDTO.getInOutPatient().toString());
List<Laboratory> labList = laboratoryManager.getLaboratory(patient).stream()
.filter(e -> e.getStatus().equals(DRAFT)).collect(Collectors.toList());
.filter(e -> e.getStatus().equals(DRAFT)).collect(Collectors.toList());

if (!(labList == null || labList.isEmpty())) {
for (Laboratory lab : labList) {
Expand All @@ -214,7 +214,7 @@ public ResponseEntity<Boolean> newExamRequest(@RequestBody LaboratoryDTO laborat
*/
@PostMapping(value = "/laboratories/insertList", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> newLaboratory2(@RequestBody List<LabWithRowsDTO> labsWithRows)
throws OHServiceException {
throws OHServiceException {
LOGGER.info("store List of Exam with result");
List<Laboratory> labsToInsert = new ArrayList<>();
List<List<LaboratoryRow>> labsRowsToInsert = new ArrayList<>();
Expand All @@ -227,7 +227,7 @@ public ResponseEntity<Boolean> newLaboratory2(@RequestBody List<LabWithRowsDTO>
}

Exam exam = examManager.getExams().stream()
.filter(e -> e.getCode().equals(laboratoryDTO.getExam().getCode())).findFirst().orElse(null);
.filter(e -> e.getCode().equals(laboratoryDTO.getExam().getCode())).findFirst().orElse(null);
if (exam == null) {
throw new OHAPIException(new OHExceptionMessage("Exam not found."));
}
Expand All @@ -242,7 +242,7 @@ public ResponseEntity<Boolean> newLaboratory2(@RequestBody List<LabWithRowsDTO>
List<LaboratoryRow> labRowToInsert = new ArrayList<>();
for (String rowDescription : labWithRowsDTO.getLaboratoryRowList()) {
labRowToInsert
.add(laboratoryRowMapper.map2Model(new LaboratoryRowDTO(rowDescription, laboratoryDTO)));
.add(laboratoryRowMapper.map2Model(new LaboratoryRowDTO(rowDescription, laboratoryDTO)));
}
if (!labRowToInsert.isEmpty()) {
labsRowsToInsert.add(labRowToInsert);
Expand All @@ -257,7 +257,7 @@ public ResponseEntity<Boolean> newLaboratory2(@RequestBody List<LabWithRowsDTO>
}
return ResponseEntity.status(HttpStatus.CREATED).body(true);
}

/**
* Updates the specified {@link LaboratoryRowDTO} object.
*
Expand All @@ -268,7 +268,7 @@ public ResponseEntity<Boolean> newLaboratory2(@RequestBody List<LabWithRowsDTO>
*/
@PutMapping(value = "/laboratories/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> updateLaboratory(@PathVariable Integer code,
@RequestBody LabWithRowsDTO labWithRowsDTO) throws OHServiceException {
@RequestBody LabWithRowsDTO labWithRowsDTO) throws OHServiceException {
LOGGER.info("Update labWithRows code: {}", code);
LaboratoryDTO laboratoryDTO = labWithRowsDTO.getLaboratoryDTO();
List<String> labRow = labWithRowsDTO.getLaboratoryRowList();
Expand All @@ -291,7 +291,7 @@ public ResponseEntity<Boolean> updateLaboratory(@PathVariable Integer code,
}

Exam exam = examManager.getExams().stream().filter(e -> e.getCode().equals(laboratoryDTO.getExam().getCode()))
.findFirst().orElse(null);
.findFirst().orElse(null);
if (exam == null) {
throw new OHAPIException(new OHExceptionMessage("Exam not found."));
}
Expand All @@ -307,10 +307,18 @@ public ResponseEntity<Boolean> updateLaboratory(@PathVariable Integer code,
if (!laboratoryDTO.getResult().isEmpty()) {
labToInsert.setStatus(DONE);
}
laboratoryManager.updateLaboratory(labToInsert, labRows);
try {
laboratoryManager.updateLaboratory(labToInsert, labRows);
} catch (OHServiceException e) {
// TODO: workaround waiting OH2-182
if (e.getMessages().get(0).getMessage().equals("angal.labnew.someexamswithoutresultpleasecheck.msg")) {
throw new OHAPIException(new OHExceptionMessage("errors.lab.someexamswithoutresultpleasecheck"));
}
throw new OHAPIException(e.getMessages().get(0));
}
return ResponseEntity.ok(true);
}

/**
* Updates the specified {@link LaboratoryDTO} object.
*
Expand All @@ -322,7 +330,7 @@ public ResponseEntity<Boolean> updateLaboratory(@PathVariable Integer code,
*/
@PutMapping(value = "/laboratories/examRequest/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> updateExamRequest(@PathVariable Integer code, @RequestParam String status)
throws OHServiceException {
throws OHServiceException {
LOGGER.info("Update exam request code: {}", code);
LaboratoryStatus stat = LaboratoryStatus.valueOf(status);
if (stat != null) {
Expand All @@ -335,9 +343,9 @@ public ResponseEntity<Boolean> updateExamRequest(@PathVariable Integer code, @Re
} else {
throw new OHAPIException(new OHExceptionMessage("This status doesn't exist."));
}

}

/**
* Set an {@link LaboratoryDTO} record to be deleted.
*
Expand Down Expand Up @@ -375,7 +383,8 @@ public ResponseEntity<Boolean> deleteExam(@PathVariable Integer code) throws OHS
* @throws OHServiceException
*/
@GetMapping(value = "/laboratories", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Page<LabWithRowsDTO>> getLaboratory(@RequestParam boolean oneWeek, @RequestParam int page, @RequestParam int size) throws OHServiceException {
public ResponseEntity<Page<LabWithRowsDTO>> getLaboratory(@RequestParam boolean oneWeek, @RequestParam int page, @RequestParam int size)
throws OHServiceException {
LOGGER.info("Get all LabWithRows");
PagedResponse<Laboratory> labListPageable = laboratoryManager.getLaboratoryPageable(oneWeek, page, size);
if (labListPageable == null || labListPageable.getData().isEmpty()) {
Expand Down Expand Up @@ -412,7 +421,7 @@ public ResponseEntity<Page<LabWithRowsDTO>> getLaboratory(@RequestParam boolean
labWithRowsDtoPageable.setData(labWithRowsDto);
return ResponseEntity.ok(labWithRowsDtoPageable);
}

/**
* Get all {@link LaboratoryRowDTO}s for the specified id.
*
Expand All @@ -429,7 +438,7 @@ public ResponseEntity<List<LabWithRowsDTO>> getLaboratory(@PathVariable Integer
}

List<Laboratory> labList = laboratoryManager.getLaboratory(patient).stream()
.filter(e -> !e.getStatus().equalsIgnoreCase(DRAFT) && !e.getStatus().equalsIgnoreCase(OPEN)).collect(Collectors.toList());
.filter(e -> !e.getStatus().equalsIgnoreCase(DRAFT) && !e.getStatus().equalsIgnoreCase(OPEN)).collect(Collectors.toList());
if (labList == null || labList.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}
Expand Down Expand Up @@ -460,7 +469,7 @@ public ResponseEntity<List<LabWithRowsDTO>> getLaboratory(@PathVariable Integer
return labDTO;
}).collect(Collectors.toList()));
}

/**
* Get all {@link LaboratoryDTO}s for the specified id.
*
Expand All @@ -471,15 +480,15 @@ public ResponseEntity<List<LabWithRowsDTO>> getLaboratory(@PathVariable Integer
*/
@GetMapping(value = "/laboratories/examRequest/patient/{patId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<LaboratoryDTO>> getLaboratoryExamRequest(@PathVariable Integer patId)
throws OHServiceException {
throws OHServiceException {
LOGGER.info("Get Exam requested by patient Id: {}", patId);
Patient patient = patientBrowserManager.getPatientById(patId);
if (patient == null) {
throw new OHAPIException(new OHExceptionMessage("Patient not found."));
}

List<Laboratory> labList = laboratoryManager.getLaboratory(patient).stream()
.filter(e -> e.getStatus().equalsIgnoreCase(DRAFT) || e.getStatus().equalsIgnoreCase(OPEN)).collect(Collectors.toList());
.filter(e -> e.getStatus().equalsIgnoreCase(DRAFT) || e.getStatus().equalsIgnoreCase(OPEN)).collect(Collectors.toList());
if (labList == null || labList.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}
Expand All @@ -492,7 +501,7 @@ public ResponseEntity<List<LaboratoryDTO>> getLaboratoryExamRequest(@PathVariabl
return laboratoryDTO;
}).collect(Collectors.toList()));
}

/**
* Get all {@link LaboratoryDTO}s.
*
Expand All @@ -504,7 +513,7 @@ public ResponseEntity<List<LaboratoryDTO>> getLaboratoryExamRequest(@PathVariabl
public ResponseEntity<List<LaboratoryDTO>> getLaboratoryExamRequest() throws OHServiceException {
LOGGER.info("Get all Exam Requested");
List<Laboratory> labList = laboratoryManager.getLaboratory().stream()
.filter(e -> e.getStatus().equalsIgnoreCase(DRAFT) || e.getStatus().equalsIgnoreCase(OPEN)).collect(Collectors.toList());
.filter(e -> e.getStatus().equalsIgnoreCase(DRAFT) || e.getStatus().equalsIgnoreCase(OPEN)).collect(Collectors.toList());
if (labList == null || labList.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}
Expand All @@ -517,7 +526,7 @@ public ResponseEntity<List<LaboratoryDTO>> getLaboratoryExamRequest() throws OHS
return laboratoryDTO;
}).collect(Collectors.toList()));
}

/**
* Get all {@link String}s.
*
Expand Down Expand Up @@ -545,17 +554,17 @@ public ResponseEntity<List<String>> getMaterials() throws OHServiceException {
* @return the {@link List} of found {@link LabWithRowsDTO} or NO_CONTENT otherwise.
* @throws OHServiceException
*/

@GetMapping(value = "/laboratories/exams", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Page<LabWithRowsDTO>> getLaboratoryForPrint(
@RequestParam(required = false, defaultValue = "") String examName,
@RequestParam(value = "dateFrom") String dateFrom, @RequestParam(value = "dateTo") String dateTo,
@RequestParam(value = "patientCode", required = false, defaultValue = "0") int patientCode,
@RequestParam(value = "status", required = false, defaultValue = "") String status,
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
@RequestParam(value = "size", required = false, defaultValue = DEFAULT_PAGE_SIZE) int size,
@RequestParam(value = "paged", required = false, defaultValue = "false") boolean paged)
throws OHServiceException {
@RequestParam(required = false, defaultValue = "") String examName,
@RequestParam(value = "dateFrom") String dateFrom, @RequestParam(value = "dateTo") String dateTo,
@RequestParam(value = "patientCode", required = false, defaultValue = "0") int patientCode,
@RequestParam(value = "status", required = false, defaultValue = "") String status,
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
@RequestParam(value = "size", required = false, defaultValue = DEFAULT_PAGE_SIZE) int size,
@RequestParam(value = "paged", required = false, defaultValue = "false") boolean paged)
throws OHServiceException {
LOGGER.info("Get labWithRow within specified date");
LOGGER.debug("examName: {}", examName);
LOGGER.debug("dateFrom: {}", dateFrom);
Expand All @@ -570,48 +579,48 @@ public ResponseEntity<Page<LabWithRowsDTO>> getLaboratoryForPrint(
LocalDateTime dateT = LocalDateTime.parse(dateTo, formatter);
LocalDateTime dateF = LocalDateTime.parse(dateFrom, formatter);
Page<LabWithRowsDTO> result = new Page<>();

if (patientCode != 0) {
patient = patientBrowserManager.getPatientById(patientCode);
if (patient == null || laboratoryManager.getLaboratory(patient) == null) {
throw new OHAPIException(new OHExceptionMessage("Patient not found."),
HttpStatus.INTERNAL_SERVER_ERROR);
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
PagedResponse<Laboratory> laboratoryPageable;
List<Laboratory> labList;
if (paged) {
if (!status.isEmpty()) {
if (!examName.isEmpty()) {
Exam exam = examManager.getExams(examName).get(0);
Exam exam = examManager.getExams(examName).get(0);
laboratoryPageable = laboratoryManager.getLaboratoryPageable(exam, dateF, dateT, patient, page, size);
} else {
laboratoryPageable = laboratoryManager.getLaboratoryPageable(null, dateF, dateT, patient, page, size);
}
labList = laboratoryPageable.getData()
.stream().filter(lab -> lab.getStatus().equalsIgnoreCase(status)).collect(Collectors.toList());
.stream().filter(lab -> lab.getStatus().equalsIgnoreCase(status)).collect(Collectors.toList());
} else {
if (!examName.isEmpty()) {
Exam exam = examManager.getExams(examName).get(0);
Exam exam = examManager.getExams(examName).get(0);
laboratoryPageable = laboratoryManager.getLaboratoryPageable(exam, dateF, dateT, patient, page, size);
} else {
laboratoryPageable = laboratoryManager.getLaboratoryPageable(null, dateF, dateT, patient, page, size);
}
labList = laboratoryPageable.getData();
}
result.setPageInfo(laboratoryMapper.setParameterPageInfo(laboratoryPageable.getPageInfo()));

} else {
if (!status.isEmpty()) {
labList = laboratoryManager.getLaboratory(examName, dateF, dateT, patient)
.stream().filter(lab -> lab.getStatus().equalsIgnoreCase(status)).collect(Collectors.toList());
.stream().filter(lab -> lab.getStatus().equalsIgnoreCase(status)).collect(Collectors.toList());

} else {
labList = laboratoryManager.getLaboratory(examName, dateF, dateT, patient);

}
}

if (labList == null || labList.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
} else {
Expand Down Expand Up @@ -645,7 +654,7 @@ public ResponseEntity<Page<LabWithRowsDTO>> getLaboratoryForPrint(
}
}

/**
/**
* Get all the {@link LaboratoryDTO}s for the specified id.
*
* @param code
Expand Down Expand Up @@ -706,7 +715,7 @@ public ResponseEntity<LabWithRowsDTO> getExamWithRowsById(@PathVariable Integer
lab.setLaboratoryRowList(labDescription);
return ResponseEntity.ok(lab);
}

/**
* Set an {@link Laboratory} record to be deleted.
*
Expand Down

0 comments on commit ac3e7b2

Please sign in to comment.