use of org.openmrs.Allergies in project openmrs-core by openmrs.
the class PatientServiceAllergyTest method setAllergies_shouldSetTheNonCodedConceptForNonCodedAllergenIfNotSpecified.
@Test
public void setAllergies_shouldSetTheNonCodedConceptForNonCodedAllergenIfNotSpecified() {
Patient patient = allergyService.getPatient(2);
Allergen allergen = new Allergen(AllergenType.DRUG, null, "Some allergy name");
Allergy allergy = new Allergy(patient, allergen, null, null, null);
Allergies allergies = allergyService.getAllergies(patient);
allergies.add(allergy);
allergyService.setAllergies(patient, allergies);
Assert.assertFalse(allergy.getAllergen().isCoded());
}
use of org.openmrs.Allergies in project openmrs-core by openmrs.
the class PatientServiceAllergyTest method setAllergies_shouldSaveTheAllergyListAndStatus.
/**
* @see PatientService#setAllergies(Patient,Allergies)
*/
@Test
public void setAllergies_shouldSaveTheAllergyListAndStatus() {
// get a patient without any allergies
Patient patient = allergyService.getPatient(7);
Allergies allergies = allergyService.getAllergies(patient);
Assert.assertEquals(Allergies.UNKNOWN, allergies.getAllergyStatus());
Assert.assertEquals(0, allergies.size());
// save some allergies for this patient
Allergen allergen = new Allergen(AllergenType.DRUG, new Concept(3), null);
Concept severity = new Concept(4);
Allergy allergy = new Allergy(patient, allergen, severity, "some comment", new ArrayList<>());
AllergyReaction reaction = new AllergyReaction(allergy, new Concept(21), null);
allergy.addReaction(reaction);
allergies = new Allergies();
allergies.add(allergy);
allergyService.setAllergies(patient, allergies);
// now the patient should have allergies and the correct allergy status
allergies = allergyService.getAllergies(patient);
Assert.assertEquals(Allergies.SEE_LIST, allergies.getAllergyStatus());
Assert.assertEquals(1, allergies.size());
Assert.assertEquals(1, allergies.get(0).getReactions().size());
}
use of org.openmrs.Allergies in project openmrs-core by openmrs.
the class AllergyValidator method validate.
/**
* @see Validator#validate(Object, org.springframework.validation.Errors)
* @param target
* @param errors
* @should fail for a null value
* @should fail if patient is null
* @should fail id allergenType is null
* @should fail if allergen is null
* @should fail if codedAllergen is null
* @should fail if nonCodedAllergen is null and allergen is set to other non coded
* @should reject a duplicate allergen
* @should reject a duplicate non coded allergen
* @should pass for a valid allergy
*/
@Override
public void validate(Object target, Errors errors) {
if (target == null) {
throw new IllegalArgumentException("Allergy should not be null");
}
ValidationUtils.rejectIfEmpty(errors, "patient", "allergyapi.patient.required");
Allergy allergy = (Allergy) target;
if (allergy.getAllergen() == null) {
errors.rejectValue("allergen", "allergyapi.allergen.required");
} else {
Allergen allergen = allergy.getAllergen();
if (allergen.getAllergenType() == null) {
errors.rejectValue("allergen", "allergyapi.allergenType.required");
}
if (allergen.getCodedAllergen() == null && StringUtils.isBlank(allergen.getNonCodedAllergen())) {
errors.rejectValue("allergen", "allergyapi.allergen.codedOrNonCodedAllergen.required");
} else if (!allergen.isCoded() && StringUtils.isBlank(allergen.getNonCodedAllergen())) {
errors.rejectValue("allergen", "allergyapi.allergen.nonCodedAllergen.required");
}
if (allergy.getAllergyId() == null && allergy.getPatient() != null) {
Allergies existingAllergies = patientService.getAllergies(allergy.getPatient());
if (existingAllergies.containsAllergen(allergy)) {
String key = "ui.i18n.Concept.name." + allergen.getCodedAllergen().getUuid();
String name = Context.getMessageSourceService().getMessage(key);
if (key.equals(name)) {
name = allergen.toString();
}
errors.rejectValue("allergen", "allergyapi.message.duplicateAllergen", new Object[] { name }, null);
}
}
}
}
use of org.openmrs.Allergies in project openmrs-core by openmrs.
the class AllergyValidatorTest method validate_shouldPassForAValidAllergy.
/**
* @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldPassForAValidAllergy() {
Allergies allergies = new Allergies();
Concept aspirin = new Concept();
Allergen allergen1 = new Allergen(AllergenType.DRUG, aspirin, null);
allergies.add(new Allergy(null, allergen1, null, null, null));
when(ps.getAllergies(any(Patient.class))).thenReturn(allergies);
Allergen anotherAllergen = new Allergen(AllergenType.DRUG, new Concept(), null);
Allergy allergy = new Allergy(mock(Patient.class), anotherAllergen, null, null, null);
Errors errors = new BindException(allergy, "allergy");
validator.validate(allergy, errors);
assertFalse(errors.hasErrors());
}
use of org.openmrs.Allergies in project openmrs-core by openmrs.
the class PatientServiceImpl method getAllergies.
/**
* @see org.openmrs.api.PatientService#getAllergies(org.openmrs.Patient)
*/
@Override
@Transactional(readOnly = true)
public Allergies getAllergies(Patient patient) {
if (patient == null) {
throw new IllegalArgumentException("An existing (NOT NULL) patient is required to get allergies");
}
Allergies allergies = new Allergies();
List<Allergy> allergyList = dao.getAllergies(patient);
if (!allergyList.isEmpty()) {
allergies.addAll(allergyList);
} else {
String status = dao.getAllergyStatus(patient);
if (Allergies.NO_KNOWN_ALLERGIES.equals(status)) {
allergies.confirmNoKnownAllergies();
}
}
return allergies;
}
Aggregations