Example usage for org.hibernate CacheMode IGNORE

List of usage examples for org.hibernate CacheMode IGNORE

Introduction

In this page you can find the example usage for org.hibernate CacheMode IGNORE.

Prototype

CacheMode IGNORE

To view the source code for org.hibernate CacheMode IGNORE.

Click Source Link

Document

The session will never interact with the cache, except to invalidate cache items when updates occur.

Usage

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;/*  w ww . j ava 2s.  c  om*/

    // 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 {
        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

public Cohort getPatientsHavingTextObs(Integer conceptId, String value, TimeModifier timeModifier)
        throws DAOException {
    Query query;/*from  w  w w  .  j a va2  s  .c o m*/
    StringBuffer sb = new StringBuffer();
    sb.append("select o.person_id from obs o ");

    if (timeModifier != null) {
        if (timeModifier.equals(TimeModifier.LAST)) {
            log.debug("timeModifier is NOT NULL, and appears to be LAST, so we'll try to add a subquery");
            sb.append("inner join (select person_id, max(obs_datetime) as obs_datetime from obs where ");
            sb.append(
                    "concept_id = :concept_id group by person_id) sub on o.person_id = sub.person_id and o.obs_datetime = sub.obs_datetime ");
        } else {
            log.debug("timeModifier is NOT NULL, and appears to not be LAST, so we won't do anything");
        }
    } else {
        log.debug("timeModifier is NULL, skipping to full query");
    }

    sb.append("where o.concept_id = :concept_id ");
    boolean useVal = false;
    if (value != null) {
        sb.append("and o.value_text = :value ");
        useVal = true;
    } else {
        sb.append("and o.value_text is not null ");
    }
    sb.append("group by o.person_id ");

    query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
    query.setCacheMode(CacheMode.IGNORE);
    query.setInteger("concept_id", conceptId);
    if (useVal) {
        query.setString("value", value);
    }

    return new Cohort(query.list());
}

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

License:Mozilla Public License

public Cohort convertPatientIdentifier(List<String> identifiers) throws DAOException {

    StringBuffer sb = new StringBuffer();
    sb.append("select distinct(patient_id) from patient_identifier p ");
    sb.append("where identifier in (:identifiers)");
    Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
    query.setCacheMode(CacheMode.IGNORE);
    query.setParameterList("identifiers", identifiers, new StringType());
    return new Cohort(query.list());
}

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 ww w  . ja  v a 2 s .  c  o m*/
        }
    }

    return ret;
}

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

License:Mozilla Public License

/**
 * Returns a Map from patientId to a Collection of drugIds for drugs active for the patients on
 * that date If patientIds is null then do this for all patients Does not return anything for
 * voided patients/*from  w  w  w  .ja v a2  s  .  c om*/
 * 
 * @throws DAOException
 */
@SuppressWarnings("unchecked")
public Map<Integer, Collection<Integer>> getActiveDrugIds(Collection<Integer> patientIds, Date fromDate,
        Date toDate) throws DAOException {
    Set<Integer> idsLookup = patientIds == null ? null
            : (patientIds instanceof HashSet ? (HashSet<Integer>) patientIds
                    : new HashSet<Integer>(patientIds));

    Map<Integer, Collection<Integer>> ret = new HashMap<Integer, Collection<Integer>>();

    List<String> whereClauses = new ArrayList<String>();
    whereClauses.add("o.voided = false");
    if (toDate != null) {
        whereClauses.add("o.date_activated <= :toDate");
    }
    if (fromDate != null) {
        whereClauses.add("(o.auto_expire_date is null or o.auto_expire_date > :fromDate)");
        whereClauses.add("(o.date_stopped is null or o.date_stopped > :fromDate)");
    }

    StringBuilder sql = new StringBuilder("select o.patient_id, d.drug_inventory_id " + "from orders o "
            + "    inner join patient p on o.patient_id = p.patient_id and p.voided = false "
            + "    inner join drug_order d on o.order_id = d.order_id ");
    for (ListIterator<String> i = whereClauses.listIterator(); i.hasNext();) {
        sql.append((i.nextIndex() == 0 ? " where " : " and ")).append(i.next());
    }

    log.debug("sql= " + sql);

    Query query = sessionFactory.getCurrentSession().createSQLQuery(sql.toString());
    query.setCacheMode(CacheMode.IGNORE);

    if (toDate != null) {
        query.setDate("toDate", toDate);
    }
    if (fromDate != null) {
        query.setDate("fromDate", fromDate);
    }

    List<Object[]> results = (List<Object[]>) query.list();
    for (Object[] row : results) {
        Integer patientId = (Integer) row[0];
        if (idsLookup == null || idsLookup.contains(patientId)) {
            Integer drugId = (Integer) row[1];
            Collection<Integer> drugIds = ret.get(patientId);
            if (drugIds == null) {
                drugIds = new HashSet<Integer>();
                ret.put(patientId, drugIds);
            }
            drugIds.add(drugId);
        }
    }
    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()));
        }/*w w w  . jav a  2  s.co  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  ww.j  a va 2s.co 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 ww .j av a  2s .  c o m

    //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  a v 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));
    }/* ww  w.  ja v  a 2 s  .c  o  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;
}