public void savePathology(Pathology pathology) throws InvalidModelException { // validate List<String> errors = new ArrayList<String>(); int total = pathology.getTotalNumber() != null ? pathology.getTotalNumber() : 0; int sum = 0; for (Integer value : Arrays.asList( pathology.getNumberSclerosed(), pathology.getNumberSegmentallySclerosed(), pathology.getNumberCellularCrescents(), pathology.getNumberFibrousCrescents(), pathology.getNumberEndocapillaryHypercelluarity(), pathology.getNumberFibrinoidNecrosis())) { sum += value != null ? value : 0; } if (sum > total) { errors.add(TOTAL_ERROR); } if (!errors.isEmpty()) { InvalidModelException exception = new InvalidModelException("Pathology model is not valid"); exception.setErrors(errors); throw exception; } pathologyDao.savePathology(pathology); }
public void saveTreatment(Treatment treatment) throws InvalidModelException { // validation List<String> errors = new ArrayList<String>(); List<Treatment> treatmentsList = treatmentDao.getTreatmentsByRadarNumber(treatment.getRadarNumber()); // Cannot start a new treatment after a previous treatment start date which has not been closed for (Treatment existingTreatment : treatmentsList) { if (existingTreatment.getId().equals(treatment.getId())) { continue; } if (existingTreatment.getEndDate() == null && treatment.getStartDate().compareTo(existingTreatment.getStartDate()) > 0) { if (treatment.getEndDate() != null) { if (treatment.getEndDate().compareTo(existingTreatment.getStartDate()) > 0) { errors.add(TreatmentManager.PREVIOUS_TREATMENT_NOT_CLOSED_ERROR); break; } } } } // dates must not overlap for (Treatment existingTreatment : treatmentsList) { if (existingTreatment.getId().equals(treatment.getId())) { continue; } if (RadarUtility.isEventsOverlapping( existingTreatment.getStartDate(), existingTreatment.getEndDate(), treatment.getStartDate(), treatment.getEndDate())) { errors.add(TreatmentManager.OVERLAPPING_ERROR); break; } } List<Date> datesToCheck = Arrays.asList(treatment.getStartDate(), treatment.getEndDate()); // cannot be before date of birth Patient patient = patientManager.getPatientByRadarNumber(treatment.getRadarNumber()); if (patient != null) { Date dob = patient.getDob(); if (dob != null) { for (Date date : datesToCheck) { if (date != null) { if (dob.compareTo(date) > 0) { errors.add(TreatmentManager.BEFORE_DOB_ERROR); break; } } } } } // cannot be after today Date today = new Date(); for (Date date : datesToCheck) { if (date != null) { if (today.compareTo(date) < 0) { errors.add(TreatmentManager.AFTER_TODAY_ERROR); break; } } } if (!errors.isEmpty()) { InvalidModelException exception = new InvalidModelException("treatment model is not valid"); exception.setErrors(errors); throw exception; } treatmentDao.saveTreatment(treatment); }