Search in sources :

Example 6 with Allergies

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

Example 7 with Allergies

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

Example 8 with Allergies

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

Example 9 with Allergies

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());
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) Errors(org.springframework.validation.Errors) 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 10 with Allergies

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

Aggregations

Allergies (org.openmrs.Allergies)20 Test (org.junit.Test)18 Patient (org.openmrs.Patient)18 Allergy (org.openmrs.Allergy)15 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)15 Concept (org.openmrs.Concept)9 Allergen (org.openmrs.Allergen)6 AllergyReaction (org.openmrs.AllergyReaction)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 BindException (org.springframework.validation.BindException)3 Errors (org.springframework.validation.Errors)3 MessageSourceService (org.openmrs.messagesource.MessageSourceService)2 Transactional (org.springframework.transaction.annotation.Transactional)1