Skip to content

Commit

Permalink
add validation method for inventory and inventory row
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudFonzam committed Jul 2, 2024
1 parent 4bf18de commit 838227a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

import org.isf.generaldata.MessageBundle;
import org.isf.medicalinventory.model.MedicalInventory;
import org.isf.medicalinventory.model.MedicalInventoryRow;
import org.isf.medicalinventory.service.MedicalInventoryIoOperation;
import org.isf.utils.exception.OHDataValidationException;
import org.isf.utils.exception.OHServiceException;
import org.isf.utils.exception.model.OHExceptionMessage;
import org.isf.ward.model.Ward;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
Expand All @@ -54,6 +58,7 @@ public MedicalInventoryManager(MedicalInventoryIoOperation medicalInventoryIoOpe
* @throws OHServiceException
*/
public MedicalInventory newMedicalInventory(MedicalInventory medicalInventory) throws OHServiceException {
validationMedicalInventory(medicalInventory);
return ioOperations.newMedicalInventory(medicalInventory);
}

Expand All @@ -65,6 +70,7 @@ public MedicalInventory newMedicalInventory(MedicalInventory medicalInventory) t
* @throws OHServiceException
*/
public MedicalInventory updateMedicalInventory(MedicalInventory medicalInventory) throws OHServiceException {
validationMedicalInventory(medicalInventory);
return ioOperations.updateMedicalInventory(medicalInventory);
}

Expand Down Expand Up @@ -159,4 +165,28 @@ public Page<MedicalInventory> getMedicalInventoryByParamsPageable(LocalDateTime
dateTo = LocalDateTime.of(dateTo.toLocalDate(), LocalTime.MAX);
return ioOperations.getMedicalInventoryByParamsPageable(dateFrom, dateTo, status, type, page, size);
}

/**
* Verify if the object is valid for CRUD and return a list of errors, if any.
*
* @param medInventory
* @throws OHDataValidationException
*/
private void validationMedicalInventory(MedicalInventory medInventory) throws OHDataValidationException {
List<OHExceptionMessage> errors = new ArrayList<>();
LocalDateTime tomorrow = LocalDateTime.now().plusDays(1);
if (medInventory.getInventoryDate() == null) {
errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.inventory.pleaseinsertavalidinventorydate.msg")));
}
if (medInventory.getInventoryDate() != null && medInventory.getInventoryDate().isAfter(tomorrow)) {
errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.inventory.notdateinfuture.msg")));
}
if (medInventory.getInventoryReference() == null || medInventory.getInventoryReference().equals("")) {
errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.inventory.mustenterareference.msg")));
}

if (!errors.isEmpty()) {
throw new OHDataValidationException(errors);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package org.isf.medicalinventory.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -54,6 +55,7 @@ public MedicalInventoryRowManager(MedicalInventoryRowIoOperation medicalInventor
* @throws OHServiceException
*/
public MedicalInventoryRow newMedicalInventoryRow(MedicalInventoryRow medicalInventoryRow) throws OHServiceException {
validationMedicalInventoryRow(medicalInventoryRow);
return ioOperation.newMedicalInventoryRow(medicalInventoryRow);
}

Expand All @@ -65,17 +67,8 @@ public MedicalInventoryRow newMedicalInventoryRow(MedicalInventoryRow medicalInv
* @throws OHServiceException
*/
public MedicalInventoryRow updateMedicalInventoryRow(MedicalInventoryRow medicalInventoryRow) throws OHServiceException {
Optional<MedicalInventoryRow> medInvRow = ioOperation.getMedicalInventoryRowById(medicalInventoryRow.getId());
if (medInvRow.isPresent()) {
MedicalInventoryRow medInvR = medInvRow.get();
medInvR.setLot(medicalInventoryRow.getLot());
medInvR.setRealqty(medicalInventoryRow.getRealQty());
if (medicalInventoryRow.isNewLot()) {
medInvR.setNewLot(true);
}
return ioOperation.updateMedicalInventoryRow(medInvR);
}
throw new OHDataValidationException(new OHExceptionMessage(MessageBundle.getMessage("angal.inventoryrow.notfound.msg")));
validationMedicalInventoryRow(medicalInventoryRow);
return ioOperation.updateMedicalInventoryRow(medicalInventoryRow);
}

/**
Expand Down Expand Up @@ -137,4 +130,25 @@ public MedicalInventoryRow getMedicalInventoryRowById(Integer invRowId) {
}
return null;
}

/**
* Verify if the object is valid for CRUD and return a list of errors, if any.
*
* @param medInventoryRow
* @throws OHDataValidationException
*/
private void validationMedicalInventoryRow(MedicalInventoryRow medicalInventoryRow) throws OHDataValidationException {
List<OHExceptionMessage> errors = new ArrayList<>();

if (medicalInventoryRow.getInventory() == null) {
errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.inventory.pleaseinsertinventory.msg")));
}
if (medicalInventoryRow.getMedical() == null) {
errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.inventory.pleaseinsertmedical.msg")));
}

if (!errors.isEmpty()) {
throw new OHDataValidationException(errors);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ public class MedicalInventoryRow extends Auditable<String> {
@Column(name = "MINVTR_ID")
private Integer id;

@NotNull
@Column(name = "MINVTR_THEORETIC_QTY")
private double theoreticQty;

@NotNull
@Column(name = "MINVTR_REAL_QTY")
private double realQty;

Expand Down

0 comments on commit 838227a

Please sign in to comment.