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.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
// TODO: this method seems to be missing a check for voided==false.
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;/*  ww  w.j  a  va  2s .  c  o m*/

    // make 'patient.**' reference 'patient' like alias instead of object
    if (className.equals("org.openmrs.Patient"))
        criteria = sessionFactory.getCurrentSession().createCriteria("org.openmrs.Patient", "patient");
    else if (className.equals("org.openmrs.Person"))
        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 (className.equals("org.openmrs.Person"))
            // 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(Expression.eq("personVoided", false));
        else
            // this is here to support PersonName and PersonAddress
            criteria.add(Expression.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(Expression.eq("voided", false));
    }
    criteria.setProjection(projectionList);

    // add 'preferred' sort order if necessary
    try {
        boolean hasPreferred = false;
        for (Field f : Class.forName(className).getDeclaredFields()) {
            if (f.getName().equals("preferred"))
                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.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java

License:Open Source License

/**
 * Returns a Map from patientId to a Collection of drugIds for drugs active for the patients on
 * that date//from   w  w w .ja v  a 2 s. co m
 * If patientIds is null then do this for all patients
 * Does not return anything for voided patients
 * 
 * @throws DAOException
 */
@SuppressWarnings("unchecked")
public Map<Integer, Collection<Integer>> getActiveDrugIds(Collection<Integer> patientIds, Date fromDate,
        Date toDate) throws DAOException {
    HashSet<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.start_date <= :toDate");
    if (fromDate != null) {
        whereClauses.add("(o.auto_expire_date is null or o.auto_expire_date > :fromDate)");
        whereClauses.add("(o.discontinued_date is null or o.discontinued_date > :fromDate)");
    }

    String sql = "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 += (i.nextIndex() == 0 ? " where " : " and ");
        sql += i.next();
    }

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

    Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);
    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.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java

License:Open Source License

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

    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()));

    //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);//  w  ww . ja va2 s .  c  o  m
    }

    return ret;
}

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

License:Open Source 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  2s  . c  om
 */
@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();

    // First get Map of patientId to patientProgramId for efficiency
    Map<Integer, Integer> programIdToPatient = new HashMap<Integer, Integer>();
    String sql = "select patient_program_id, patient_id from patient_program";
    Query q = sessionFactory.getCurrentSession().createSQLQuery(sql);
    q.setCacheMode(CacheMode.IGNORE);
    List<Object[]> l = q.list();
    for (Object[] row : l) {
        programIdToPatient.put((Integer) row[0], (Integer) row[1]);
    }

    // Next get all PatientPrograms from the DB
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientProgram.class);
    criteria.setCacheMode(CacheMode.IGNORE);
    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();

    // Finally, construct return list based on input Cohort      
    for (PatientProgram prog : temp) {
        Integer patientProgramId = prog.getPatientProgramId();
        Integer patientId = programIdToPatient.get(patientProgramId);
        if (ps == null || ps.contains(patientId)) {
            ret.put(patientId, prog);
        }
    }
    return ret;
}

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

License:Open Source 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()));

    //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("startDate", now));
    criteria.add(Restrictions.or(//from   w w w .  j  av a 2 s .  co m
            Restrictions.and(Restrictions.eq("discontinued", false),
                    Restrictions.or(Restrictions.isNull("autoExpireDate"),
                            Restrictions.gt("autoExpireDate", now))),
            Restrictions.and(Restrictions.eq("discontinued", true), Restrictions.gt("discontinuedDate", now))));
    criteria.addOrder(org.hibernate.criterion.Order.asc("startDate"));
    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.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java

License:Open Source 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;
    }/*  w w w  .j  a v  a  2 s  .co m*/

    // First get Map of patientId to orderId for efficiency
    Map<Integer, Integer> orderIdToPatient = new HashMap<Integer, Integer>();
    String sql = "select o.order_id, o.patient_id from orders o, drug_order d where o.order_id = d.order_id";
    Query q = sessionFactory.getCurrentSession().createSQLQuery(sql);
    q.setCacheMode(CacheMode.IGNORE);
    List<Object[]> l = q.list();
    for (Object[] row : l) {
        orderIdToPatient.put((Integer) row[0], (Integer) row[1]);
    }

    // Next get all DrugOrders from the DB
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(DrugOrder.class);
    criteria.setCacheMode(CacheMode.IGNORE);
    if (drugConcepts != null) {
        criteria.add(Restrictions.in("concept", drugConcepts));
    }
    criteria.add(Restrictions.eq("voided", false));
    criteria.addOrder(org.hibernate.criterion.Order.asc("startDate"));
    log.debug("criteria: " + criteria);
    List<DrugOrder> temp = criteria.list();

    // Finally, construct return list based on input Cohort      
    for (DrugOrder regimen : temp) {
        Integer orderId = regimen.getOrderId();
        Integer patientId = orderIdToPatient.get(orderId);
        if (patients == null || patients.contains(patientId)) {
            List<DrugOrder> list = ret.get(patientId);
            if (list == null) {
                list = new ArrayList<DrugOrder>();
                ret.put(patientId, list);
            }
            list.add(regimen);
        }
    }
    return ret;
}

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

License:Open Source 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));

    // 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);/* w  w  w.  ja v a 2  s. c  om*/
        }
        rels.add(rel);
    }
    return ret;
}

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

License:Open Source License

/**
 * @param patients//  w  w w  . j  a va  2 s . c o m
 * @param types List<PatientIdentifierTypes> 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.openspaces.persistency.hibernate.iterator.DefaultCriteriaByExampleDataIterator.java

License:Open Source License

protected Iterator createIterator() {
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    Example example = Example.create(template);
    Criteria criteria = session.createCriteria(template.getClass()).add(example);
    criteria.setCacheMode(CacheMode.IGNORE);
    criteria.setCacheable(false);//from  w ww . ja va2s .com
    criteria.setFlushMode(FlushMode.NEVER);
    return criteria.list().iterator();
}

From source file:org.openspaces.persistency.hibernate.iterator.DefaultListQueryDataIterator.java

License:Open Source License

protected Iterator createIterator() {
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    if (entityName != null) {
        Criteria criteria = session.createCriteria(entityName);
        criteria.setCacheMode(CacheMode.IGNORE);
        criteria.setCacheable(false);//w w w . j  a  va 2s .  c o  m
        criteria.setFlushMode(FlushMode.NEVER);
        if (from >= 0) {
            criteria.setFirstResult(from);
            criteria.setMaxResults(size);
        }
        return criteria.list().iterator();
    } else if (sqlQuery != null) {
        Query query = HibernateIteratorUtils.createQueryFromSQLQuery(sqlQuery, session);
        if (from >= 0) {
            query.setFirstResult(from);
            query.setMaxResults(size);
        }
        return query.list().iterator();
    } else if (dataSourceSQLQuery != null) {
        Query query = HibernateIteratorUtils.createQueryFromDataSourceSQLQuery(dataSourceSQLQuery, session);
        if (from >= 0) {
            query.setFirstResult(from);
            query.setMaxResults(size);
        }
        return query.list().iterator();
    } else {
        throw new IllegalStateException("Either SQLQuery or entity must be provided");
    }
}