Example usage for org.hibernate Criteria setCacheMode

List of usage examples for org.hibernate Criteria setCacheMode

Introduction

In this page you can find the example usage for org.hibernate Criteria setCacheMode.

Prototype

public Criteria setCacheMode(CacheMode cacheMode);

Source Link

Document

Override the cache mode for this particular query.

Usage

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public Map<Integer, Encounter> getFirstEncountersByType(Cohort patients, List<EncounterType> types) {
    Map<Integer, Encounter> ret = new HashMap<Integer, Encounter>();

    // default query
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Encounter.class);
    criteria.setCacheMode(CacheMode.IGNORE);

    // this "where clause" is only needed if patients were specified
    if (patients != null) {
        criteria.add(Restrictions.in("patient.personId", patients.getMemberIds()));
    }/* ww  w. j a v a2  s .  com*/

    criteria.add(Restrictions.eq("voided", false));

    if (types != null && types.size() > 0) {
        criteria.add(Restrictions.in("encounterType", types));
    }

    criteria.addOrder(org.hibernate.criterion.Order.desc("patient.personId"));
    criteria.addOrder(org.hibernate.criterion.Order.asc("encounterDatetime"));

    List<Encounter> encounters = criteria.list();

    // set up the return map
    for (Encounter enc : encounters) {
        Integer ptId = enc.getPatientId();
        if (!ret.containsKey(ptId)) {
            ret.put(ptId, enc);
        }
    }

    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public Map<Integer, Object> getPatientAttributes(Cohort patients, String className, String property,
        boolean returnAll) throws DAOException {
    Map<Integer, Object> ret = new HashMap<Integer, Object>();

    className = "org.openmrs." + className;

    // default query
    Criteria criteria = null;

    // make 'patient.**' reference 'patient' like alias instead of object
    if ("org.openmrs.Patient".equals(className)) {
        criteria = sessionFactory.getCurrentSession().createCriteria("org.openmrs.Patient", "patient");
    } else if ("org.openmrs.Person".equals(className)) {
        criteria = sessionFactory.getCurrentSession().createCriteria("org.openmrs.Person", "person");
    } else {/*from   w  ww.j  a  v  a  2 s  .co m*/
        criteria = sessionFactory.getCurrentSession().createCriteria(className);
    }

    criteria.setCacheMode(CacheMode.IGNORE);

    // set up the query
    ProjectionList projectionList = Projections.projectionList();

    // if Person, PersonName, or PersonAddress
    if (className.contains("Person")) {
        projectionList.add(Projections.property("person.personId"));
        projectionList.add(Projections.property(property));

        if (patients != null) {
            criteria.add(Restrictions.in("person.personId", patients.getMemberIds()));
        }

        // do not include voided person rows
        if ("org.openmrs.Person".equals(className)) {
            // the voided column on the person table is mapped to the person object
            // through the getPersonVoided() to distinguish it from patient/user.voided
            criteria.add(Restrictions.eq("personVoided", false));
        } else {
            // this is here to support PersonName and PersonAddress
            criteria.add(Restrictions.eq("voided", false));
        }
    }
    // if one of the Patient tables
    else {
        projectionList.add(Projections.property("patient.personId"));
        projectionList.add(Projections.property(property));

        if (patients != null) {
            criteria.add(Restrictions.in("patient.personId", patients.getMemberIds()));
        }

        // do not include voided patients
        criteria.add(Restrictions.eq("voided", false));
    }
    criteria.setProjection(projectionList);

    // add 'preferred' sort order if necessary
    try {
        boolean hasPreferred = false;
        for (Field f : Class.forName(className).getDeclaredFields()) {
            if ("preferred".equals(f.getName())) {
                hasPreferred = true;
            }
        }

        if (hasPreferred) {
            criteria.addOrder(org.hibernate.criterion.Order.desc("preferred"));
        }
    } catch (ClassNotFoundException e) {
        log.warn("Class not found: " + className);
    }

    criteria.addOrder(org.hibernate.criterion.Order.desc("dateCreated"));
    List<Object[]> rows = criteria.list();

    // set up the return map
    if (returnAll) {
        for (Object[] row : rows) {
            Integer ptId = (Integer) row[0];
            Object columnValue = row[1];
            if (!ret.containsKey(ptId)) {
                Object[] arr = { columnValue };
                ret.put(ptId, arr);
            } else {
                Object[] oldArr = (Object[]) ret.get(ptId);
                Object[] newArr = new Object[oldArr.length + 1];
                System.arraycopy(oldArr, 0, newArr, 0, oldArr.length);
                newArr[oldArr.length] = columnValue;
                ret.put(ptId, newArr);
            }
        }
    } else {
        for (Object[] row : rows) {
            Integer ptId = (Integer) row[0];
            Object columnValue = row[1];
            if (!ret.containsKey(ptId)) {
                ret.put(ptId, columnValue);
            }
        }
    }

    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public List<Patient> getPatients(Collection<Integer> patientIds) throws DAOException {
    List<Patient> ret = new ArrayList<Patient>();

    if (!patientIds.isEmpty()) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class);
        criteria.setCacheMode(CacheMode.IGNORE);
        criteria.add(Restrictions.in("patientId", patientIds));
        criteria.add(Restrictions.eq("voided", false));
        log.debug("criteria: " + criteria);
        List<Patient> temp = criteria.list();
        for (Patient p : temp) {
            ret.add(p);/*from w w  w . j a v  a  2 s . co m*/
        }
    }

    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public Map<Integer, PatientState> getCurrentStates(Cohort ps, ProgramWorkflow wf) throws DAOException {
    Map<Integer, PatientState> ret = new HashMap<Integer, PatientState>();

    if (ps == null || ps.getMemberIds().size() > 0) {
        Date now = new Date();

        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientState.class);
        criteria.setFetchMode("patient", FetchMode.JOIN);
        criteria.setCacheMode(CacheMode.IGNORE);
        //criteria.add(Restrictions.in("patientProgram.patient.personId", ids));

        // only include this where clause if patients were passed in
        if (ps != null) {
            criteria.createCriteria("patientProgram")
                    .add(Restrictions.in("patient.personId", ps.getMemberIds()));
        }//from   www . ja  v a  2  s .  c  o  m

        //criteria.add(Restrictions.eq("state.programWorkflow", wf));
        criteria.createCriteria("state").add(Restrictions.eq("programWorkflow", wf));
        criteria.add(Restrictions.eq("voided", false));
        criteria.add(Restrictions.or(Restrictions.isNull("startDate"), Restrictions.le("startDate", now)));
        criteria.add(Restrictions.or(Restrictions.isNull("endDate"), Restrictions.ge("endDate", now)));
        log.debug("criteria: " + criteria);
        List<PatientState> temp = criteria.list();
        for (PatientState state : temp) {
            Integer ptId = state.getPatientProgram().getPatient().getPatientId();
            ret.put(ptId, state);
        }
    }
    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

/**
 * This method assumes the patient is not simultaneously enrolled in the program more than once.
 * if (includeVoided == true) then include voided programs if (includePast == true) then include
 * program which are already complete In all cases this only returns the latest program
 * enrollment for each patient./* w  w  w .j a  v a  2 s.c  o  m*/
 */
@SuppressWarnings("unchecked")
public Map<Integer, PatientProgram> getPatientPrograms(Cohort ps, Program program, boolean includeVoided,
        boolean includePast) throws DAOException {
    Map<Integer, PatientProgram> ret = new HashMap<Integer, PatientProgram>();

    Date now = new Date();

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientProgram.class);
    criteria.setFetchMode("patient", FetchMode.JOIN);
    criteria.setCacheMode(CacheMode.IGNORE);

    // this "where clause" is only necessary if patients were passed in
    if (ps != null) {
        criteria.add(Restrictions.in("patient.personId", ps.getMemberIds()));
    }

    criteria.add(Restrictions.eq("program", program));
    if (!includeVoided) {
        criteria.add(Restrictions.eq("voided", false));
    }
    criteria.add(Restrictions.or(Restrictions.isNull("dateEnrolled"), Restrictions.le("dateEnrolled", now)));
    if (!includePast) {
        criteria.add(
                Restrictions.or(Restrictions.isNull("dateCompleted"), Restrictions.ge("dateCompleted", now)));
    }
    log.debug("criteria: " + criteria);
    List<PatientProgram> temp = criteria.list();
    for (PatientProgram prog : temp) {
        Integer ptId = prog.getPatient().getPatientId();
        ret.put(ptId, prog);
    }

    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public Map<Integer, List<DrugOrder>> getCurrentDrugOrders(Cohort patients, List<Concept> drugConcepts)
        throws DAOException {
    Map<Integer, List<DrugOrder>> ret = new HashMap<Integer, List<DrugOrder>>();

    Date now = new Date();

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(DrugOrder.class);
    criteria.setFetchMode("patient", FetchMode.JOIN);
    criteria.setCacheMode(CacheMode.IGNORE);

    // this "where clause" is only necessary if patients were passed in
    if (patients != null) {
        criteria.add(Restrictions.in("patient.personId", patients.getMemberIds()));
    }/*from w w w.  java 2 s.  c  om*/

    //criteria.add(Restrictions.in("encounter.patient.personId", ids));
    //criteria.createCriteria("encounter").add(Restrictions.in("patient.personId", ids));
    if (drugConcepts != null) {
        criteria.add(Restrictions.in("concept", drugConcepts));
    }
    criteria.add(Restrictions.eq("voided", false));
    criteria.add(Restrictions.le("dateActivated", now));
    criteria.add(Restrictions.and(
            Restrictions.or(Restrictions.isNull("autoExpireDate"), Restrictions.gt("autoExpireDate", now)),
            Restrictions.or(Restrictions.isNull("dateStopped"), Restrictions.gt("dateStopped", now))));
    criteria.addOrder(org.hibernate.criterion.Order.asc("dateActivated"));
    log.debug("criteria: " + criteria);
    List<DrugOrder> temp = criteria.list();
    for (DrugOrder regimen : temp) {
        Integer ptId = regimen.getPatient().getPatientId();
        List<DrugOrder> list = ret.get(ptId);
        if (list == null) {
            list = new ArrayList<DrugOrder>();
            ret.put(ptId, list);
        }
        list.add(regimen);
    }
    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public Map<Integer, List<DrugOrder>> getDrugOrders(Cohort patients, List<Concept> drugConcepts)
        throws DAOException {
    Map<Integer, List<DrugOrder>> ret = new HashMap<Integer, List<DrugOrder>>();
    if (patients != null && patients.size() == 0) {
        return ret;
    }/*from ww w  . j  av  a  2s .  co  m*/

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(DrugOrder.class);
    criteria.setFetchMode("patient", FetchMode.JOIN);
    criteria.setCacheMode(CacheMode.IGNORE);

    // only include this where clause if patients were passed in
    if (patients != null) {
        criteria.add(Restrictions.in("patient.personId", patients.getMemberIds()));
    }

    if (drugConcepts != null) {
        criteria.add(Restrictions.in("concept", drugConcepts));
    }
    criteria.add(Restrictions.eq("voided", false));
    criteria.addOrder(org.hibernate.criterion.Order.asc("dateActivated"));
    log.debug("criteria: " + criteria);
    List<DrugOrder> temp = criteria.list();
    for (DrugOrder regimen : temp) {
        Integer ptId = regimen.getPatient().getPatientId();
        List<DrugOrder> list = ret.get(ptId);
        if (list == null) {
            list = new ArrayList<DrugOrder>();
            ret.put(ptId, list);
        }
        list.add(regimen);
    }
    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public Map<Integer, List<Relationship>> getRelationships(Cohort patients, RelationshipType relType) {
    Map<Integer, List<Relationship>> ret = new HashMap<Integer, List<Relationship>>();

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Relationship.class);
    criteria.setCacheMode(CacheMode.IGNORE);
    if (relType != null) {
        criteria.add(Restrictions.eq("relationshipType", relType));
    }//from   www.j  a  v  a 2  s .co  m

    // this "where clause" is only useful if patients were passed in
    if (patients != null) {
        criteria.createCriteria("personB").add(Restrictions.in("personId", patients.getMemberIds()));
    }

    criteria.add(Restrictions.eq("voided", false));
    log.debug("criteria: " + criteria);
    List<Relationship> temp = criteria.list();
    for (Relationship rel : temp) {
        Integer ptId = rel.getPersonB().getPersonId();
        List<Relationship> rels = ret.get(ptId);
        if (rels == null) {
            rels = new ArrayList<Relationship>();
            ret.put(ptId, rels);
        }
        rels.add(rel);
    }
    return ret;
}

From source file:org.openmrs.api.db.hibernate.HibernatePatientSetDAO.java

License:Mozilla Public License

/**
 * @param patients/*from w ww.  ja v  a 2 s .c  om*/
 * @param types List&lt;PatientIdentifierTypes&gt; of types to get
 * @return Map of {@link PatientIdentifier}s
 */
@SuppressWarnings("unchecked")
public Map<Integer, String> getPatientIdentifierByType(Cohort patients, List<PatientIdentifierType> types) {
    Map<Integer, String> patientIdentifiers = new HashMap<Integer, String>();

    // default query
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifier.class);

    // only get the "identifier" and "patientId" columns
    ProjectionList projections = Projections.projectionList();
    projections.add(Projections.property("identifier"));
    projections.add(Projections.property("patient.personId"));
    criteria.setProjection(projections);

    criteria.setCacheMode(CacheMode.IGNORE);

    // Add patient restriction if necessary
    if (patients != null) {
        criteria.add(Restrictions.in("patient.personId", patients.getMemberIds()));
    }

    // all identifiers must be non-voided
    criteria.add(Restrictions.eq("voided", false));

    // Add identifier type filter
    if (types != null && types.size() > 0) {
        criteria.add(Restrictions.in("identifierType", types));
    }

    // Order by ID
    criteria.addOrder(org.hibernate.criterion.Order.desc("patient.personId"));

    List<Object[]> rows = criteria.list();

    // set up the return map
    for (Object[] row : rows) {
        String identifier = (String) row[0];
        Integer patientId = (Integer) row[1];
        if (!patientIdentifiers.containsKey(patientId)) {
            patientIdentifiers.put(patientId, identifier);
        }
    }

    return patientIdentifiers;
}

From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
/**/*w  w  w . j av a  2s  .c  o m*/
 * fromDate and toDate are both inclusive
 * TODO: finish this. 
 */
public Map<Integer, List<Obs>> getObservations(Cohort patients, Concept concept, Date fromDate, Date toDate)
        throws DAOException {
    Map<Integer, List<Obs>> ret = new HashMap<Integer, List<Obs>>();

    /*
    Query query = sessionFactory.getCurrentSession().createQuery("select obs, obs.patientId " +
                        "from Obs obs where obs.conceptId = :conceptId " +
                        " and obs.patientId in :ids " +
                        "order by obs.obsDatetime asc");
    query.setInteger("conceptId", conceptId);
    query.set
            
    List<Object[]> temp = query.list();
    for (Object[] holder : temp) {
       Obs obs = (Obs) holder[0];
       Integer ptId = (Integer) holder[1];
       List<Obs> forPatient = ret.get(ptId);
       if (forPatient == null) {
    forPatient = new ArrayList<Obs>();
    ret.put(ptId, forPatient);
       }
       forPatient.add(obs);
    }
     */
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class);
    criteria.setCacheMode(CacheMode.IGNORE);

    criteria.add(Restrictions.eq("concept", concept));

    // only add this where clause if patients were passed in
    if (patients != null)
        criteria.add(Restrictions.in("person.personId", patients.getMemberIds()));

    criteria.add(Restrictions.eq("voided", false));
    criteria.addOrder(org.hibernate.criterion.Order.desc("obsDatetime"));
    log.debug("criteria: " + criteria);
    List<Obs> temp = criteria.list();
    for (Obs obs : temp) {
        Integer ptId = obs.getPersonId();
        List<Obs> forPatient = ret.get(ptId);
        if (forPatient == null) {
            forPatient = new ArrayList<Obs>();
            ret.put(ptId, forPatient);
        }
        forPatient.add(obs);
    }

    return ret;
}