@@ -108,8 +108,7 @@ public MedicalInventoryManager(MedicalInventoryIoOperation medicalInventoryIoOpe
108
108
@ Transactional
109
109
public MedicalInventory newMedicalInventory (MedicalInventory medicalInventory , List <MedicalInventoryRow > newMedicalInventoryRows )
110
110
throws OHServiceException {
111
- validateMedicalInventory (medicalInventory );
112
- checkReference (medicalInventory );
111
+ validateMedicalInventory (medicalInventory , true );
113
112
MedicalInventory inventory = ioOperations .newMedicalInventory (medicalInventory );
114
113
for (MedicalInventoryRow inventoryRow : newMedicalInventoryRows ) {
115
114
medicalInventoryRowManager .newMedicalInventoryRow (inventoryRow );
@@ -125,10 +124,7 @@ public MedicalInventory newMedicalInventory(MedicalInventory medicalInventory, L
125
124
* @throws OHServiceException
126
125
*/
127
126
public MedicalInventory updateMedicalInventory (MedicalInventory medicalInventory , boolean checkReference ) throws OHServiceException {
128
- validateMedicalInventory (medicalInventory );
129
- if (checkReference ) {
130
- checkReference (medicalInventory );
131
- }
127
+ validateMedicalInventory (medicalInventory , checkReference );
132
128
return ioOperations .updateMedicalInventory (medicalInventory );
133
129
}
134
130
@@ -239,9 +235,10 @@ public MedicalInventory getInventoryByReference(String reference) throws OHServi
239
235
* Verify if the object is valid for CRUD and return a list of errors, if any.
240
236
*
241
237
* @param medInventory
238
+ * @param checkReference
242
239
* @throws OHDataValidationException
243
240
*/
244
- private void validateMedicalInventory (MedicalInventory medInventory ) throws OHDataValidationException {
241
+ private void validateMedicalInventory (MedicalInventory medInventory , boolean checkReference ) throws OHDataValidationException {
245
242
List <OHExceptionMessage > errors = new ArrayList <>();
246
243
LocalDateTime tomorrow = LocalDateTime .now ().plusDays (1 );
247
244
String reference = medInventory .getInventoryReference ();
@@ -254,6 +251,13 @@ private void validateMedicalInventory(MedicalInventory medInventory) throws OHDa
254
251
if (reference == null || reference .equals ("" )) {
255
252
errors .add (new OHExceptionMessage (MessageBundle .getMessage ("angal.inventory.mustenterareference.msg" )));
256
253
}
254
+ if (checkReference ) {
255
+ try {
256
+ checkReference (medInventory );
257
+ } catch (OHServiceException e ) {
258
+ throw new OHDataValidationException (e .getMessages ());
259
+ }
260
+ }
257
261
if (!errors .isEmpty ()) {
258
262
throw new OHDataValidationException (errors );
259
263
}
@@ -579,7 +583,12 @@ public boolean confirmMedicalWardInventoryRow(MedicalInventory inventory, List<M
579
583
return true ;
580
584
}
581
585
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
+ */
583
592
private void checkReference (MedicalInventory medicalInventory ) throws OHServiceException {
584
593
List <OHExceptionMessage > errors = new ArrayList <>();
585
594
String reference = medicalInventory .getInventoryReference ();
@@ -592,7 +601,7 @@ private void checkReference(MedicalInventory medicalInventory) throws OHServiceE
592
601
errors .add (new OHExceptionMessage (MessageBundle .getMessage ("angal.inventory.referencealreadyused.msg" )));
593
602
}
594
603
if (!errors .isEmpty ()) {
595
- throw new OHServiceException (errors );
604
+ throw new OHDataValidationException (errors );
596
605
}
597
606
}
598
607
0 commit comments