Search in sources :

Example 16 with Patient

use of org.openmrs.Patient in project openmrs-module-pihcore by PIH.

the class PihCloseStaleVisitsTaskTest method test_shouldNotKeepVisitOpenIfItHasEDTriageAndEncounterWithinFortyEightHoursButAlsoDischargeEncounter.

@Test
public void test_shouldNotKeepVisitOpenIfItHasEDTriageAndEncounterWithinFortyEightHoursButAlsoDischargeEncounter() throws Exception {
    ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
    ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties);
    ContextSensitiveMetadataTestUtils.setupSupportsVisitLocationTag(locationService);
    // patient already has one visit in test dataset
    Patient patient = patientService.getPatient(7);
    // need to tag the unknown location so we don't run into an error when testing against the existing visits in the test dataset
    Location unknownLocation = locationService.getLocation(1);
    unknownLocation.addTag(emrApiProperties.getSupportsVisitsLocationTag());
    locationService.saveLocation(unknownLocation);
    Location location = locationService.getLocation(2);
    location.addTag(emrApiProperties.getSupportsVisitsLocationTag());
    locationService.saveLocation(location);
    Visit visit = new Visit();
    visit.setStartDatetime(DateUtils.addHours(new Date(), -60));
    visit.setPatient(patient);
    visit.setLocation(location);
    visit.setVisitType(emrApiProperties.getAtFacilityVisitType());
    // create ED Triage encounter
    Encounter edTriage = new Encounter();
    edTriage.setPatient(patient);
    edTriage.setEncounterType(encounterService.getEncounterTypeByUuid(ED_TRIAGE_ENCOUNTER_TYPE_UUID));
    edTriage.setEncounterDatetime(visit.getStartDatetime());
    encounterService.saveEncounter(edTriage);
    // create a another encounter within 48 hours
    Encounter consult = new Encounter();
    consult.setPatient(patient);
    consult.setEncounterType(emrApiProperties.getVisitNoteEncounterType());
    consult.setEncounterDatetime(DateUtils.addHours(new Date(), -47));
    encounterService.saveEncounter(consult);
    // create an encounter with a disposition obs of "discharge
    Encounter encounterWithDisposition = new Encounter();
    encounterWithDisposition.setPatient(patient);
    encounterWithDisposition.setEncounterType(encounterService.getEncounterType(1));
    encounterWithDisposition.setEncounterDatetime(visit.getStartDatetime());
    Obs dispositionObsGroup = new Obs();
    dispositionObsGroup.setConcept(dispositionService.getDispositionDescriptor().getDispositionSetConcept());
    Obs dispositionObs = new Obs();
    dispositionObs.setConcept(dispositionService.getDispositionDescriptor().getDispositionConcept());
    // this fake code is set in ContextSensitiveMetadataTestUtils
    dispositionObs.setValueCoded(emrConceptService.getConcept(EmrApiConstants.EMR_CONCEPT_SOURCE_NAME + ":Discharged"));
    dispositionObsGroup.addGroupMember(dispositionObs);
    encounterWithDisposition.addObs(dispositionObsGroup);
    encounterService.saveEncounter(encounterWithDisposition);
    visit.addEncounter(edTriage);
    visit.addEncounter(consult);
    visit.addEncounter(encounterWithDisposition);
    visitService.saveVisit(visit);
    VisitDomainWrapper activeVisit = adtService.getActiveVisit(patient, location);
    // sanity check
    assertNotNull(activeVisit);
    new PihCloseStaleVisitsTask().execute();
    activeVisit = adtService.getActiveVisit(patient, location);
    assertNull(activeVisit);
}
Also used : Obs(org.openmrs.Obs) Visit(org.openmrs.Visit) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Date(java.util.Date) Location(org.openmrs.Location) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 17 with Patient

use of org.openmrs.Patient in project openmrs-module-pihcore by PIH.

the class PihCloseStaleVisitsTaskTest method test_shouldNotCloseVisitIfHasEncounterWithinExpirationRange.

@Test
public void test_shouldNotCloseVisitIfHasEncounterWithinExpirationRange() throws Exception {
    ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
    ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties);
    ContextSensitiveMetadataTestUtils.setupSupportsVisitLocationTag(locationService);
    // patient already has one visit in test dataset
    Patient patient = patientService.getPatient(7);
    // need o tag the unknown location so we don't run into an error when testing against the existing visits in the test dataset
    Location unknownLocation = locationService.getLocation(1);
    unknownLocation.addTag(emrApiProperties.getSupportsVisitsLocationTag());
    locationService.saveLocation(unknownLocation);
    Location location = locationService.getLocation(2);
    location.addTag(emrApiProperties.getSupportsVisitsLocationTag());
    locationService.saveLocation(location);
    Visit visit = new Visit();
    visit.setStartDatetime(DateUtils.addHours(new Date(), -14));
    visit.setPatient(patient);
    visit.setLocation(location);
    visit.setVisitType(emrApiProperties.getAtFacilityVisitType());
    // create an encounter within the expiration range
    Encounter encounter = new Encounter();
    encounter.setPatient(patient);
    encounter.setEncounterType(encounterService.getEncounterType(1));
    // expire hours - 1
    encounter.setEncounterDatetime(DateUtils.addHours(new Date(), -(emrApiProperties.getVisitExpireHours() - 1)));
    encounterService.saveEncounter(encounter);
    visit.addEncounter(encounter);
    visitService.saveVisit(visit);
    VisitDomainWrapper activeVisit = adtService.getActiveVisit(patient, location);
    // sanity check
    assertNotNull(activeVisit);
    new PihCloseStaleVisitsTask().execute();
    activeVisit = adtService.getActiveVisit(patient, location);
    assertNotNull(activeVisit);
}
Also used : Visit(org.openmrs.Visit) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Date(java.util.Date) Location(org.openmrs.Location) BaseModuleContextSensitiveTest(org.openmrs.test.BaseModuleContextSensitiveTest) Test(org.junit.Test)

Example 18 with Patient

use of org.openmrs.Patient in project openmrs-core by openmrs.

the class HibernatePatientDAO method getDuplicatePatientsSQLString.

private String getDuplicatePatientsSQLString(List<String> attributes) {
    StringBuilder outerSelect = new StringBuilder("select distinct t1.patient_id from patient t1 ");
    final String t5 = " = t5.";
    Set<String> patientFieldNames = OpenmrsUtil.getDeclaredFields(Patient.class);
    Set<String> personFieldNames = OpenmrsUtil.getDeclaredFields(Person.class);
    Set<String> personNameFieldNames = OpenmrsUtil.getDeclaredFields(PersonName.class);
    Set<String> identifierFieldNames = OpenmrsUtil.getDeclaredFields(PatientIdentifier.class);
    List<String> whereConditions = new ArrayList<>();
    List<String> innerFields = new ArrayList<>();
    StringBuilder innerSelect = new StringBuilder(" from patient p1 ");
    for (String attribute : attributes) {
        if (attribute != null) {
            attribute = attribute.trim();
        }
        if (patientFieldNames.contains(attribute)) {
            AbstractEntityPersister aep = (AbstractEntityPersister) sessionFactory.getClassMetadata(Patient.class);
            String[] properties = aep.getPropertyColumnNames(attribute);
            if (properties.length >= 1) {
                attribute = properties[0];
            }
            whereConditions.add(" t1." + attribute + t5 + attribute);
            innerFields.add("p1." + attribute);
        } else if (personFieldNames.contains(attribute)) {
            if (!outerSelect.toString().contains("person")) {
                outerSelect.append("inner join person t2 on t1.patient_id = t2.person_id ");
                innerSelect.append("inner join person person1 on p1.patient_id = person1.person_id ");
            }
            AbstractEntityPersister aep = (AbstractEntityPersister) sessionFactory.getClassMetadata(Person.class);
            if (aep != null) {
                String[] properties = aep.getPropertyColumnNames(attribute);
                if (properties != null && properties.length >= 1) {
                    attribute = properties[0];
                }
            }
            whereConditions.add(" t2." + attribute + t5 + attribute);
            innerFields.add("person1." + attribute);
        } else if (personNameFieldNames.contains(attribute)) {
            if (!outerSelect.toString().contains("person_name")) {
                outerSelect.append("inner join person_name t3 on t2.person_id = t3.person_id ");
                innerSelect.append("inner join person_name pn1 on person1.person_id = pn1.person_id ");
            }
            // Since we are firing a native query get the actual table column name from the field name of the entity
            AbstractEntityPersister aep = (AbstractEntityPersister) sessionFactory.getClassMetadata(PersonName.class);
            if (aep != null) {
                String[] properties = aep.getPropertyColumnNames(attribute);
                if (properties != null && properties.length >= 1) {
                    attribute = properties[0];
                }
            }
            whereConditions.add(" t3." + attribute + t5 + attribute);
            innerFields.add("pn1." + attribute);
        } else if (identifierFieldNames.contains(attribute)) {
            if (!outerSelect.toString().contains("patient_identifier")) {
                outerSelect.append("inner join patient_identifier t4 on t1.patient_id = t4.patient_id ");
                innerSelect.append("inner join patient_identifier pi1 on p1.patient_id = pi1.patient_id ");
            }
            AbstractEntityPersister aep = (AbstractEntityPersister) sessionFactory.getClassMetadata(PatientIdentifier.class);
            if (aep != null) {
                String[] properties = aep.getPropertyColumnNames(attribute);
                if (properties != null && properties.length >= 1) {
                    attribute = properties[0];
                }
            }
            whereConditions.add(" t4." + attribute + t5 + attribute);
            innerFields.add("pi1." + attribute);
        } else {
            log.warn("Unidentified attribute: " + attribute);
        }
    }
    if (CollectionUtils.isNotEmpty(innerFields) || CollectionUtils.isNotEmpty(whereConditions)) {
        String innerFieldsJoined = StringUtils.join(innerFields, ", ");
        String whereFieldsJoined = StringUtils.join(whereConditions, " and ");
        String innerWhereCondition = "";
        if (!attributes.contains("includeVoided")) {
            innerWhereCondition = " where p1.voided = false ";
        }
        String innerQuery = "(Select " + innerFieldsJoined + innerSelect + innerWhereCondition + " group by " + innerFieldsJoined + " having count(*) > 1" + " order by " + innerFieldsJoined + ") t5";
        return outerSelect + ", " + innerQuery + " where " + whereFieldsJoined + ";";
    }
    return null;
}
Also used : PersonName(org.openmrs.PersonName) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister)

Example 19 with Patient

use of org.openmrs.Patient in project openmrs-core by openmrs.

the class HibernatePatientDAO method getDuplicatePatientsByAttributes.

/**
 * @param attributes attributes on a Person or Patient object. similar to: [gender, givenName,
 *                   middleName, familyName]
 * @return           list of patients that match other patients
 * @see org.openmrs.api.db.PatientDAO#getDuplicatePatientsByAttributes(java.util.List)
 */
@SuppressWarnings("unchecked")
@Override
public List<Patient> getDuplicatePatientsByAttributes(List<String> attributes) {
    List<Patient> patients = new ArrayList<>();
    List<Integer> patientIds = new ArrayList<>();
    if (!attributes.isEmpty()) {
        String sqlString = getDuplicatePatientsSQLString(attributes);
        if (sqlString != null) {
            SQLQuery sqlquery = sessionFactory.getCurrentSession().createSQLQuery(sqlString);
            patientIds = sqlquery.list();
            if (!patientIds.isEmpty()) {
                Query query = sessionFactory.getCurrentSession().createQuery("from Patient p1 where p1.patientId in (:ids)");
                query.setParameterList("ids", patientIds);
                patients = query.list();
            }
        }
    }
    sortDuplicatePatients(patients, patientIds);
    return patients;
}
Also used : SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) LuceneQuery(org.openmrs.api.db.hibernate.search.LuceneQuery) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) SQLQuery(org.hibernate.SQLQuery)

Example 20 with Patient

use of org.openmrs.Patient in project openmrs-core by openmrs.

the class OrderServiceTest method getOrders_shouldIncludeVoidedMatchesIfIncludeVoidedIsSetToTrue.

/**
 * @see OrderService#getOrders(org.openmrs.Patient, org.openmrs.CareSetting,
 *      org.openmrs.OrderType, boolean)
 */
@Test
public void getOrders_shouldIncludeVoidedMatchesIfIncludeVoidedIsSetToTrue() {
    Patient patient = patientService.getPatient(2);
    CareSetting outPatient = orderService.getCareSetting(1);
    OrderType testOrderType = orderService.getOrderType(2);
    assertEquals(4, orderService.getOrders(patient, outPatient, testOrderType, true).size());
}
Also used : OrderType(org.openmrs.OrderType) Patient(org.openmrs.Patient) CareSetting(org.openmrs.CareSetting) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Aggregations

Patient (org.openmrs.Patient)389 Test (org.junit.Test)345 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)284 Date (java.util.Date)106 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)91 Encounter (org.openmrs.Encounter)76 PatientIdentifier (org.openmrs.PatientIdentifier)60 Location (org.openmrs.Location)57 OrderUtilTest (org.openmrs.order.OrderUtilTest)47 PersonName (org.openmrs.PersonName)43 DrugOrder (org.openmrs.DrugOrder)42 BindException (org.springframework.validation.BindException)38 Concept (org.openmrs.Concept)37 Order (org.openmrs.Order)36 PatientIdentifierType (org.openmrs.PatientIdentifierType)33 Errors (org.springframework.validation.Errors)33 Visit (org.openmrs.Visit)29 ArrayList (java.util.ArrayList)28 Obs (org.openmrs.Obs)28 TestOrder (org.openmrs.TestOrder)28