Skip to content

Commit 561ba57

Browse files
committed
Move check reference into general validation method
1 parent 90377fb commit 561ba57

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ public MedicalInventoryManager(MedicalInventoryIoOperation medicalInventoryIoOpe
108108
@Transactional
109109
public MedicalInventory newMedicalInventory(MedicalInventory medicalInventory, List<MedicalInventoryRow> newMedicalInventoryRows)
110110
throws OHServiceException {
111-
validateMedicalInventory(medicalInventory);
112-
checkReference(medicalInventory);
111+
validateMedicalInventory(medicalInventory, true);
113112
MedicalInventory inventory = ioOperations.newMedicalInventory(medicalInventory);
114113
for (MedicalInventoryRow inventoryRow : newMedicalInventoryRows) {
115114
medicalInventoryRowManager.newMedicalInventoryRow(inventoryRow);
@@ -125,10 +124,7 @@ public MedicalInventory newMedicalInventory(MedicalInventory medicalInventory, L
125124
* @throws OHServiceException
126125
*/
127126
public MedicalInventory updateMedicalInventory(MedicalInventory medicalInventory, boolean checkReference) throws OHServiceException {
128-
validateMedicalInventory(medicalInventory);
129-
if (checkReference) {
130-
checkReference(medicalInventory);
131-
}
127+
validateMedicalInventory(medicalInventory, checkReference);
132128
return ioOperations.updateMedicalInventory(medicalInventory);
133129
}
134130

@@ -239,9 +235,10 @@ public MedicalInventory getInventoryByReference(String reference) throws OHServi
239235
* Verify if the object is valid for CRUD and return a list of errors, if any.
240236
*
241237
* @param medInventory
238+
* @param checkReference
242239
* @throws OHDataValidationException
243240
*/
244-
private void validateMedicalInventory(MedicalInventory medInventory) throws OHDataValidationException {
241+
private void validateMedicalInventory(MedicalInventory medInventory, boolean checkReference) throws OHDataValidationException {
245242
List<OHExceptionMessage> errors = new ArrayList<>();
246243
LocalDateTime tomorrow = LocalDateTime.now().plusDays(1);
247244
String reference = medInventory.getInventoryReference();
@@ -254,6 +251,13 @@ private void validateMedicalInventory(MedicalInventory medInventory) throws OHDa
254251
if (reference == null || reference.equals("")) {
255252
errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.inventory.mustenterareference.msg")));
256253
}
254+
if (checkReference) {
255+
try {
256+
checkReference(medInventory);
257+
} catch (OHServiceException e) {
258+
throw new OHDataValidationException(e.getMessages());
259+
}
260+
}
257261
if (!errors.isEmpty()) {
258262
throw new OHDataValidationException(errors);
259263
}
@@ -579,7 +583,12 @@ public boolean confirmMedicalWardInventoryRow(MedicalInventory inventory, List<M
579583
return true;
580584
}
581585

582-
// TODO: this should be inside the validation method?
586+
/**
587+
* Check if the reference is present with suffixes also once the {@link MedicalInventory} is confirmed.
588+
*
589+
* @param medicalInventory
590+
* @throws OHServiceException
591+
*/
583592
private void checkReference(MedicalInventory medicalInventory) throws OHServiceException {
584593
List<OHExceptionMessage> errors = new ArrayList<>();
585594
String reference = medicalInventory.getInventoryReference();
@@ -592,7 +601,7 @@ private void checkReference(MedicalInventory medicalInventory) throws OHServiceE
592601
errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.inventory.referencealreadyused.msg")));
593602
}
594603
if (!errors.isEmpty()) {
595-
throw new OHServiceException(errors);
604+
throw new OHDataValidationException(errors);
596605
}
597606
}
598607

0 commit comments

Comments
 (0)