List of usage examples for org.hibernate Criteria setCacheMode
public Criteria setCacheMode(CacheMode cacheMode);
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
@SuppressWarnings("unchecked") public Map<Integer, List<List<Object>>> getObservationsValues(Cohort patients, Concept c, List<String> attributes, Integer limit, boolean showMostRecentFirst) { Map<Integer, List<List<Object>>> ret = new HashMap<Integer, List<List<Object>>>(); List<String> aliases = new Vector<String>(); Boolean conditional = false;/*from w w w. java2s . co m*/ Criteria criteria = sessionFactory.getCurrentSession().createCriteria("org.openmrs.Obs", "obs"); criteria.setCacheMode(CacheMode.IGNORE); List<String> columns = new Vector<String>(); for (String attribute : attributes) { List<String> classNames = new Vector<String>(); if (attribute == null) { columns = findObsValueColumnName(c); if (columns.size() > 1) conditional = true; continue; //log.debug("c: " + c.getConceptId() + " attribute: " + attribute); } else if (attribute.equals("valueDatetime")) { // pass -- same column name } else if (attribute.equals("obsDatetime")) { // pass -- same column name } else if (attribute.equals("location")) { // pass -- same column name classNames.add("obs.location"); attribute = "location.name"; } else if (attribute.equals("comment")) { // pass -- same column name } else if (attribute.equals("encounterType")) { classNames.add("obs.encounter"); classNames.add("encounter.encounterType"); attribute = "encounterType.name"; } else if (attribute.equals("provider")) { classNames.add("obs.encounter"); attribute = "encounter.provider"; } else { throw new DAOException("Attribute: " + attribute + " is not recognized. Please add reference in " + this.getClass()); } for (String className : classNames) { // if aliasing is necessary if (!aliases.contains(className)) { // if we haven't aliased this already criteria.createAlias(className, className.split("\\.")[1]); aliases.add(className); } } columns.add(attribute); } String aliasName = "obs"; // set up the query ProjectionList projections = Projections.projectionList(); projections.add(Projections.property("obs.personId")); for (String col : columns) { if (col.contains(".")) projections.add(Projections.property(col)); else projections.add(Projections.property(aliasName + "." + col)); } criteria.setProjection(projections); // only restrict on patient ids if some were passed in if (patients != null) criteria.add(Restrictions.in("obs.personId", patients.getMemberIds())); criteria.add(Expression.eq("obs.concept", c)); criteria.add(Expression.eq("obs.voided", false)); if (showMostRecentFirst) criteria.addOrder(org.hibernate.criterion.Order.desc("obs.obsDatetime")); else criteria.addOrder(org.hibernate.criterion.Order.asc("obs.obsDatetime")); long start = System.currentTimeMillis(); List<Object[]> rows = criteria.list(); log.debug("Took: " + (System.currentTimeMillis() - start) + " ms to run the patient/obs query"); // set up the return map for (Object[] rowArray : rows) { //log.debug("row[0]: " + row[0] + " row[1]: " + row[1] + (row.length > 2 ? " row[2]: " + row[2] : "")); Integer ptId = (Integer) rowArray[0]; List<List<Object>> oldArr = ret.get(ptId); // if we have already fetched all of the results the user wants if (limit != null && limit > 0 && oldArr != null && oldArr.size() >= limit) { // the user provided a limit value and this patient already has more than // that number of values. // do nothing with this row } else { Boolean tmpConditional = conditional.booleanValue(); // get all columns int index = 1; List<Object> row = new Vector<Object>(); while (index < rowArray.length) { Object value = rowArray[index++]; if (tmpConditional) { if (index == 2 && value != null) // skip null first value if we must row.add(value); else row.add(rowArray[index]); tmpConditional = false; index++; // increment counter for next column. (Skips over value_concept) } else row.add(value == null ? "" : value); } // if we haven't seen a different row for this patient already: if (oldArr == null) { List<List<Object>> arr = new Vector<List<Object>>(); arr.add(row); ret.put(ptId, arr); } // if we have seen a row for this patient already else { oldArr.add(row); ret.put(ptId, oldArr); } } } return ret; }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
@SuppressWarnings("unchecked") public Map<Integer, Encounter> getEncountersByType(Cohort patients, List<EncounterType> encTypes) { 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 necessary if patients were passed in if (patients != null && patients.size() > 0) criteria.add(Restrictions.in("patient.personId", patients.getMemberIds())); criteria.add(Restrictions.eq("voided", false)); if (encTypes != null && encTypes.size() > 0) criteria.add(Restrictions.in("encounterType", encTypes)); criteria.addOrder(org.hibernate.criterion.Order.desc("patient.personId")); criteria.addOrder(org.hibernate.criterion.Order.desc("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);//from w w w . j a v a2 s. c om } return ret; }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
/** * Gets a list of encounters associated with the given form, filtered by the given patient set. * //from w ww . java 2s. c o m * @param patients the patients to filter by (null will return all encounters for all patients) * @param forms the forms to filter by */ @SuppressWarnings("unchecked") public List<Encounter> getEncountersByForm(Cohort patients, List<Form> forms) { // default query Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Encounter.class); criteria.setCacheMode(CacheMode.IGNORE); // this "where clause" is only necessary if patients were passed in if (patients != null && patients.size() > 0) criteria.add(Restrictions.in("patient.personId", patients.getMemberIds())); criteria.add(Restrictions.eq("voided", false)); if (forms != null && forms.size() > 0) criteria.add(Restrictions.in("form", forms)); criteria.addOrder(org.hibernate.criterion.Order.desc("patient.personId")); criteria.addOrder(org.hibernate.criterion.Order.desc("encounterDatetime")); return criteria.list(); }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
@SuppressWarnings("unchecked") public Map<Integer, Object> getEncounterAttrsByType(Cohort patients, List<EncounterType> encTypes, String attr, Boolean earliestFirst) {//from w w w . j a va 2s . c om Map<Integer, Object> ret = new HashMap<Integer, Object>(); // default query Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Encounter.class); criteria.setCacheMode(CacheMode.IGNORE); // this "where clause" is only necessary if patients were specified if (patients != null) criteria.add(Restrictions.in("patient.personId", patients.getMemberIds())); criteria.add(Restrictions.eq("voided", false)); if (encTypes != null && encTypes.size() > 0) criteria.add(Restrictions.in("encounterType", encTypes)); criteria.setProjection(Projections.projectionList().add(Projections.property("patient.personId")) .add(Projections.property(attr))); criteria.addOrder(org.hibernate.criterion.Order.desc("patient.personId")); if (earliestFirst) criteria.addOrder(org.hibernate.criterion.Order.asc("encounterDatetime")); else criteria.addOrder(org.hibernate.criterion.Order.desc("encounterDatetime")); List<Object[]> attrs = criteria.list(); // set up the return map for (Object[] row : attrs) { Integer ptId = (Integer) row[0]; if (!ret.containsKey(ptId)) ret.put(ptId, row[1]); } return ret; }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
@SuppressWarnings("unchecked") public Map<Integer, Encounter> getEncounters(Cohort patients) { Map<Integer, Encounter> ret = new HashMap<Integer, Encounter>(); // default query Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Encounter.class); 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())); criteria.add(Restrictions.eq("voided", false)); criteria.addOrder(org.hibernate.criterion.Order.desc("patient.personId")); criteria.addOrder(org.hibernate.criterion.Order.desc("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);/*from w w w. j a v a 2 s . co m*/ } return ret; }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source 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())); 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);//from ww w.j av a 2 s . c om } return ret; }
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; // 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// ww w . jav a 2s. c om 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
@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 w w.j a va 2s. 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 a2s . 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 www . jav a 2 s . c om 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; }