Search in sources :

Example 11 with Allergy

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;
}
Also used : Allergy(org.openmrs.Allergy) Allergies(org.openmrs.Allergies) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with Allergy

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);
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) APIException(org.openmrs.api.APIException) DuplicateIdentifierException(org.openmrs.api.DuplicateIdentifierException) BlankIdentifierException(org.openmrs.api.BlankIdentifierException) PatientIdentifierTypeLockedException(org.openmrs.api.PatientIdentifierTypeLockedException) InsufficientIdentifiersException(org.openmrs.api.InsufficientIdentifiersException) APIException(org.openmrs.api.APIException) MissingRequiredIdentifierException(org.openmrs.api.MissingRequiredIdentifierException) SerializationException(org.openmrs.serialization.SerializationException) PatientIdentifierException(org.openmrs.api.PatientIdentifierException)

Example 13 with Allergy

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"));
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) Errors(org.springframework.validation.Errors) MessageSourceService(org.openmrs.messagesource.MessageSourceService) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) Allergies(org.openmrs.Allergies) Allergen(org.openmrs.Allergen) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with Allergy

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"));
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) Errors(org.springframework.validation.Errors) MessageSourceService(org.openmrs.messagesource.MessageSourceService) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) Allergies(org.openmrs.Allergies) Allergen(org.openmrs.Allergen) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with Allergy

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));
}
Also used : Allergy(org.openmrs.Allergy) Patient(org.openmrs.Patient) AllergyReaction(org.openmrs.AllergyReaction) Allergies(org.openmrs.Allergies) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Allergy (org.openmrs.Allergy)18 Allergies (org.openmrs.Allergies)15 Test (org.junit.Test)14 Patient (org.openmrs.Patient)13 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)11 Concept (org.openmrs.Concept)9 Allergen (org.openmrs.Allergen)6 AllergyReaction (org.openmrs.AllergyReaction)4 BindException (org.springframework.validation.BindException)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Errors (org.springframework.validation.Errors)3 MessageSourceService (org.openmrs.messagesource.MessageSourceService)2 Before (org.junit.Before)1 APIException (org.openmrs.api.APIException)1 BlankIdentifierException (org.openmrs.api.BlankIdentifierException)1 DuplicateIdentifierException (org.openmrs.api.DuplicateIdentifierException)1 InsufficientIdentifiersException (org.openmrs.api.InsufficientIdentifiersException)1 MissingRequiredIdentifierException (org.openmrs.api.MissingRequiredIdentifierException)1 PatientIdentifierException (org.openmrs.api.PatientIdentifierException)1 PatientIdentifierTypeLockedException (org.openmrs.api.PatientIdentifierTypeLockedException)1