Skip to content

Commit

Permalink
OH2-388 | Add methods to mutate exam with rows (#1435)
Browse files Browse the repository at this point in the history
* update: Add methods to find exam and exam type by codes

* chore: Fix doc

* chore: Fix java doc

* chore: Remove autor and revision date in ExamBrowsingManager

* fix: Delete exam rows when procedure switch to 3

* chore: Refactor exam update method

---------

Co-authored-by: SteveGT96 <[email protected]>
Co-authored-by: Alessandro Domanico <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent a7252ae commit 9889ca3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 64 deletions.
59 changes: 38 additions & 21 deletions src/main/java/org/isf/exa/manager/ExamBrowsingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,20 @@
import org.springframework.stereotype.Component;

/**
* Class that provides gui separation from database operations and gives some
* useful logic manipulations of the dinamic data (memory)
*
* @author bob
* 19-dec-2005
* 14-jan-2006
* Class that provides gui separation from database operations and gives some useful logic
* manipulations of the dynamic data (memory)
*/
@Component
public class ExamBrowsingManager {

private ExamIoOperations ioOperations;
private final ExamIoOperations ioOperations;

public ExamBrowsingManager(ExamIoOperations examIoOperations) {
this.ioOperations = examIoOperations;
}

/**
* Verify if the object is valid for CRUD and return a list of errors, if any
*
* @param exam
* @param insert {@code true} or updated {@code false}
* @throws OHServiceException
Expand All @@ -78,7 +73,6 @@ protected void validateExam(Exam exam, boolean insert) throws OHServiceException

/**
* Returns the list of {@link Exam}s
*
* @return the list of {@link Exam}s. It could be {@code null}
* @throws OHServiceException
*/
Expand All @@ -88,7 +82,6 @@ public List<Exam> getExams() throws OHServiceException {

/**
* Returns the list of {@link Exam}s that matches passed description
*
* @param description - the exam description
* @return the list of {@link Exam}s. It could be {@code null}
* @throws OHServiceException
Expand All @@ -99,7 +92,6 @@ public List<Exam> getExams(String description) throws OHServiceException {

/**
* Returns the list of {@link Exam}s by {@link ExamType} description
*
* @param description - the exam description
* @return the list of {@link Exam}s. It could be {@code null}
* @throws OHServiceException
Expand All @@ -110,7 +102,6 @@ public List<Exam> getExamsByTypeDescription(String description) throws OHService

/**
* Returns the list of {@link ExamType}s
*
* @return the list of {@link ExamType}s. It could be {@code null}
* @throws OHServiceException
*/
Expand All @@ -119,10 +110,7 @@ public List<ExamType> getExamType() throws OHServiceException {
}

/**
* This function controls the presence of a record with the same key as in
* the parameter; Returns false if the query finds no record, else returns
* true
*
* This function controls the presence of a record with the same key as in the parameter; Returns false if the query finds no record, else returns true
* @param exam the {@link Exam}
* @return {@code true} if the Exam code has already been used, {@code false} otherwise
* @throws OHServiceException
Expand All @@ -132,8 +120,29 @@ public boolean isKeyPresent(Exam exam) throws OHServiceException {
}

/**
* Insert a new {@link Exam} in the DB.
*
* Insert a new {@link Exam} with exam rows.
* @param payload - the {@link Exam} to insert
* @param rows - the {@link List<String>} to associate as exam rows
* @return the newly persisted {@link Exam}.
* @throws OHServiceException
*/
public Exam create(Exam payload, List<String> rows) throws OHServiceException {
return ioOperations.create(payload, rows);
}

/**
* Update an existing {@link Exam}with exam rows.
* @param payload - the {@link Exam} to insert
* @param rows - the {@link List<String>} to associate as exam rows
* @return the newly persisted {@link Exam}.
* @throws OHServiceException
*/
public Exam update(Exam payload, List<String> rows) throws OHServiceException {
return ioOperations.update(payload, rows);
}

/**
* Insert a new {@link Exam}.
* @param exam - the {@link Exam} to insert
* @return the newly persisted {@link Exam}.
* @throws OHServiceException
Expand All @@ -144,8 +153,7 @@ public Exam newExam(Exam exam) throws OHServiceException {
}

/**
* Updates an existing {@link Exam} in the db
*
* Updates an existing {@link Exam}.
* @param exam - the {@link Exam} to update
* @return {@code true} if the existing {@link Exam} has been updated, {@code false} otherwise
* @throws OHServiceException
Expand All @@ -157,11 +165,20 @@ public Exam updateExam(Exam exam) throws OHServiceException {

/**
* Delete an {@link Exam}
*
* @param exam - the {@link Exam} to delete
* @throws OHServiceException
*/
public void deleteExam(Exam exam) throws OHServiceException {
ioOperations.deleteExam(exam);
}

/**
* Find exam by code
* @param code - the code
* @return The exam if found, {@code null} otherwise.
* @throws OHServiceException
*/
public Exam findByCode(String code) throws OHServiceException {
return ioOperations.findByCode(code);
}
}
103 changes: 77 additions & 26 deletions src/main/java/org/isf/exa/service/ExamIoOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.isf.exa.service;

import java.util.List;
import java.util.Objects;

import org.isf.exa.model.Exam;
import org.isf.exa.model.ExamRow;
Expand All @@ -33,18 +34,18 @@
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor=OHServiceException.class)
@Transactional(rollbackFor = OHServiceException.class)
@TranslateOHServiceException
public class ExamIoOperations {

private ExamIoOperationRepository repository;
private final ExamIoOperationRepository repository;

private ExamRowIoOperationRepository rowRepository;
private final ExamRowIoOperationRepository rowRepository;

private ExamTypeIoOperationRepository typeRepository;
private final ExamTypeIoOperationRepository typeRepository;

public ExamIoOperations(ExamIoOperationRepository examIoOperationRepository, ExamRowIoOperationRepository examRowIoOperationRepository,
ExamTypeIoOperationRepository examTypeIoOperationRepository) {
ExamTypeIoOperationRepository examTypeIoOperationRepository) {
this.repository = examIoOperationRepository;
this.rowRepository = examRowIoOperationRepository;
this.typeRepository = examTypeIoOperationRepository;
Expand All @@ -58,7 +59,7 @@ public ExamIoOperations(ExamIoOperationRepository examIoOperationRepository, Exa
public List<Exam> getExams() throws OHServiceException {
return getExamsByDesc(null);
}

/**
* Returns the list of {@link Exam}s that matches passed description
* @param description - the exam description
Expand All @@ -67,9 +68,9 @@ public List<Exam> getExams() throws OHServiceException {
*/
public List<Exam> getExamsByDesc(String description) throws OHServiceException {
return description != null ? repository.findByDescriptionContainingOrderByExamtypeDescriptionAscDescriptionAsc(description) :
repository.findByOrderByDescriptionAscDescriptionAsc();
repository.findByOrderByDescriptionAscDescriptionAsc();
}

/**
* Returns the list of {@link Exam}s by {@link ExamType} description
* @param description - the exam description
Expand All @@ -78,7 +79,7 @@ public List<Exam> getExamsByDesc(String description) throws OHServiceException {
*/
public List<Exam> getExamsByExamTypeDesc(String description) throws OHServiceException {
return description != null ? repository.findByExamtype_DescriptionContainingOrderByExamtypeDescriptionAscDescriptionAsc(description) :
repository.findByOrderByDescriptionAscDescriptionAsc();
repository.findByOrderByDescriptionAscDescriptionAsc();
}

/**
Expand All @@ -91,19 +92,65 @@ public List<ExamType> getExamType() throws OHServiceException {
}

/**
* Insert a new {@link Exam} in the DB.
*
* Insert a new {@link Exam} with exam rows.
* @param payload - the {@link Exam} to insert
* @param rows - the {@link List<String>} to associate as exam rows
* @return the newly persisted {@link Exam}.
* @throws OHServiceException
*/
@Transactional
public Exam create(Exam payload, List<String> rows) throws OHServiceException {
Exam exam = repository.save(payload);
if (exam.getProcedure() == 3) {
return exam;
}
if (rows != null && !rows.isEmpty()) {
rowRepository.saveAll(rows.stream().map(description -> new ExamRow(exam, description)).toList());
}
return exam;
}

/**
* Update an existing {@link Exam} with exam rows.
* @param payload - the {@link Exam} to insert
* @param rows - the {@link List<String>} to associate as exam rows
* @return the newly persisted {@link Exam}.
* @throws OHServiceException
*/
@Transactional
public Exam update(Exam payload, List<String> rows) throws OHServiceException {
Exam oldExam = findByCode(payload.getCode());
Exam exam = repository.save(payload);
List<ExamRow> examRows = rowRepository.findAllByExam_CodeOrderByDescription(exam.getCode());
if (exam.getProcedure() == 3 && oldExam.getProcedure() != 3) {
rowRepository.deleteAll(examRows);
} else {
List<ExamRow> rowsToRemove = examRows.stream().filter(examRow -> !rows.contains(examRow.getDescription())).toList();
List<ExamRow> rowsToAdd = rows.stream().filter(row -> examRows.stream().noneMatch(examRow -> Objects.equals(row, examRow.getDescription())))
.map(description -> new ExamRow(exam, description)).toList();

if (!rowsToRemove.isEmpty()) {
rowRepository.deleteAll(rowsToRemove);
}
if (!rowsToAdd.isEmpty()) {
rowRepository.saveAll(rowsToAdd);
}
}
return exam;
}

/**
* Insert a new {@link Exam}.
* @param exam - the {@link Exam} to insert
* @return the newly persisted {@link Exam}.
* @throws OHServiceException
* @throws OHServiceException
*/
public Exam newExam(Exam exam) throws OHServiceException {
return repository.save(exam);
}

/**
* Insert a new {@link ExamRow} in the DB.
*
* Insert a new {@link ExamRow}.
* @param examRow - the {@link ExamRow} to insert
* @return the newly persisted {@link ExamRow}.
* @throws OHServiceException
Expand Down Expand Up @@ -142,21 +189,17 @@ public void deleteExamRow(ExamRow examRow) throws OHServiceException {
}

/**
* This function controls the presence of a record with the same key as in
* the parameter; Returns false if the query finds no record, else returns
* true
*
* This function controls the presence of a record with the same key as in the parameter; Returns false if the query finds no record, else returns true
* @param exam the {@link Exam}
* @return {@code true} if the Exam code has already been used, {@code false} otherwise
* @throws OHServiceException
* @throws OHServiceException
*/
public boolean isKeyPresent(Exam exam) throws OHServiceException {
return repository.findById(exam.getCode()).orElse(null) != null;
}

/**
* Sanitize the given {@link String} value.
* This method is maintained only for backward compatibility.
* Sanitize the given {@link String} value. This method is maintained only for backward compatibility.
* @param value the value to sanitize.
* @return the sanitized value or {@code null} if the passed value is {@code null}.
*/
Expand All @@ -169,23 +212,31 @@ protected String sanitize(String value) {

/**
* Checks if the code is already in use
*
* @param code - the exam code
* @return {@code true} if the code is already in use, {@code false} otherwise
* @throws OHServiceException
* @throws OHServiceException
*/
public boolean isCodePresent(String code) throws OHServiceException {
return repository.existsById(code);
}

/**
* Checks if the code is already in use
*
* @param code - the exam row code
* @return {@code true} if the code is already in use, {@code false} otherwise
* @throws OHServiceException
* @throws OHServiceException
*/
public boolean isRowPresent(Integer code) throws OHServiceException {
return rowRepository.existsById(code);
}

/**
* Find exam by code
* @param code - the code
* @return The exam if found, {@code null} otherwise.
* @throws OHServiceException
*/
public Exam findByCode(String code) throws OHServiceException {
return repository.findById(code).orElse(null);
}
}
Loading

0 comments on commit 9889ca3

Please sign in to comment.