use of org.openmrs.Allergy 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;
}
use of org.openmrs.Allergy in project openmrs-core by openmrs.
the class PatientServiceImpl method setAllergies.
/**
* @see org.openmrs.api.PatientService#setAllergies(org.openmrs.Patient,
* org.openmrs.Allergies)
*/
@Override
public Allergies setAllergies(Patient patient, Allergies allergies) {
// NOTE We neither delete nor edit allergies. We instead void them.
// Because we shield the API users from this business logic,
// we end up with the complicated code below. :)
// get the current allergies as stored in the database
List<Allergy> dbAllergyList = getAllergies(patient);
for (Allergy originalAllergy : dbAllergyList) {
// check if we still have each allergy, else it has just been deleted
if (allergies.contains(originalAllergy)) {
// we still have this allergy, check if it has been edited/changed
Allergy potentiallyEditedAllergy = allergies.getAllergy(originalAllergy.getAllergyId());
if (!potentiallyEditedAllergy.hasSameValues(originalAllergy)) {
// allergy has been edited, so void it and create a new one with the current values
Allergy newAllergy = new Allergy();
try {
// remove the edited allergy from our current list, and void id
allergies.remove(potentiallyEditedAllergy);
// copy values from edited allergy, and add it to the current list
newAllergy.copy(potentiallyEditedAllergy);
allergies.add(newAllergy);
// we void its original values, as came from the database,
// instead the current ones which have just been copied
// into the new allergy we have just created above
voidAllergy(originalAllergy);
} catch (Exception ex) {
throw new APIException("Failed to copy edited values", ex);
}
}
continue;
}
// void the allergy that has been deleted
voidAllergy(originalAllergy);
}
for (Allergy allergy : allergies) {
if (allergy.getAllergyId() == null && allergy.getAllergen().getCodedAllergen() == null && StringUtils.isNotBlank(allergy.getAllergen().getNonCodedAllergen())) {
Concept otherNonCoded = Context.getConceptService().getConceptByUuid(Allergen.getOtherNonCodedConceptUuid());
if (otherNonCoded == null) {
throw new APIException("Can't find concept with uuid:" + Allergen.getOtherNonCodedConceptUuid());
}
allergy.getAllergen().setCodedAllergen(otherNonCoded);
}
}
return dao.saveAllergies(patient, allergies);
}
use of org.openmrs.Allergy in project openmrs-core by openmrs.
the class AllergyValidatorTest method validate_shouldRejectADuplicateNonCodedAllergen.
/**
* @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldRejectADuplicateNonCodedAllergen() {
PowerMockito.mockStatic(Context.class);
MessageSourceService ms = mock(MessageSourceService.class);
when(Context.getMessageSourceService()).thenReturn(ms);
Allergies allergies = new Allergies();
Concept nonCodedConcept = createMockConcept(getOtherNonCodedConceptUuid());
final String freeText = "some text";
Allergen allergen1 = new Allergen(AllergenType.DRUG, nonCodedConcept, freeText);
allergies.add(new Allergy(null, allergen1, null, null, null));
when(ps.getAllergies(any(Patient.class))).thenReturn(allergies);
Allergen duplicateAllergen = new Allergen(AllergenType.FOOD, nonCodedConcept, freeText);
Allergy allergy = new Allergy(mock(Patient.class), duplicateAllergen, null, null, null);
Errors errors = new BindException(allergy, "allergy");
validator.validate(allergy, errors);
assertTrue(errors.hasFieldErrors("allergen"));
assertThat(errors.getFieldError("allergen").getCode(), is("allergyapi.message.duplicateAllergen"));
}
use of org.openmrs.Allergy in project openmrs-core by openmrs.
the class AllergyValidatorTest method validate_shouldRejectADuplicateAllergen.
/**
* @see AllergyValidator#validate(Object, org.springframework.validation.Errors)
*/
@Test
public void validate_shouldRejectADuplicateAllergen() {
PowerMockito.mockStatic(Context.class);
MessageSourceService ms = mock(MessageSourceService.class);
when(Context.getMessageSourceService()).thenReturn(ms);
Allergies allergies = new Allergies();
Concept aspirin = createMockConcept();
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 duplicateAllergen = new Allergen(AllergenType.FOOD, aspirin, null);
Allergy allergy = new Allergy(mock(Patient.class), duplicateAllergen, null, null, null);
Errors errors = new BindException(allergy, "allergy");
validator.validate(allergy, errors);
assertTrue(errors.hasFieldErrors("allergen"));
assertThat(errors.getFieldError("allergen").getCode(), is("allergyapi.message.duplicateAllergen"));
}
use of org.openmrs.Allergy in project openmrs-core by openmrs.
the class PatientServiceAllergyTest method setAllergies_shouldVoidAllergiesWithEditedReactionNonCoded.
/**
* @see PatientService#setAllergies(Patient,Allergies)
*/
@Test
public void setAllergies_shouldVoidAllergiesWithEditedReactionNonCoded() {
// get a patient with some allergies
Patient patient = allergyService.getPatient(2);
Allergies allergies = allergyService.getAllergies(patient);
Assert.assertEquals(Allergies.SEE_LIST, allergies.getAllergyStatus());
Assert.assertEquals(4, allergies.size());
Allergy editedAllergy = allergies.get(0);
// clear any cache for this object such that the next calls fetch it from the database
Context.evictFromSession(editedAllergy);
// edit a reaction
AllergyReaction reaction = editedAllergy.getReactions().get(0);
reaction.setReactionNonCoded("some non coded text");
Assert.assertTrue(allergies.contains(editedAllergy));
allergyService.setAllergies(patient, allergies);
// should remain with four unvoided allergies and status maintained as see list
allergies = allergyService.getAllergies(patient);
Assert.assertEquals(Allergies.SEE_LIST, allergies.getAllergyStatus());
Assert.assertEquals(4, allergies.size());
// the edited allergy should have been voided
Assert.assertFalse(allergies.contains(editedAllergy));
}
Aggregations