List of usage examples for org.hibernate CacheMode IGNORE
CacheMode IGNORE
To view the source code for org.hibernate CacheMode IGNORE.
Click Source Link
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
/** * TODO: don't return voided patients Gets all patients with an obs's value_date column value * within <code>startTime</code> and <code>endTime</code> * //from www . ja v a 2s.c o m * @param conceptId * @param startTime * @param endTime * @return PatientSet */ public Cohort getPatientsHavingDateObs(Integer conceptId, Date startTime, Date endTime) { StringBuffer sb = new StringBuffer(); sb.append("select o.person_id from obs o " + "where concept_id = :concept_id "); sb.append(" and o.value_datetime between :startValue and :endValue"); sb.append(" and o.voided = 0"); Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString()); query.setCacheMode(CacheMode.IGNORE); query.setInteger("concept_id", conceptId); query.setDate("startValue", startTime); query.setDate("endValue", endTime); return new Cohort(query.list()); }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
public Cohort getPatientsHavingNumericObs(Integer conceptId, PatientSetService.TimeModifier timeModifier, PatientSetService.Modifier modifier, Number value, Date fromDate, Date toDate) { Concept concept = Context.getConceptService().getConcept(conceptId); if (!concept.isNumeric()) { // throw new IllegalArgumentException(concept + " is not numeric"); }/* w ww. jav a 2 s . com*/ StringBuffer sb = new StringBuffer(); boolean useValue = modifier != null && value != null; boolean doSqlAggregation = timeModifier == TimeModifier.MIN || timeModifier == TimeModifier.MAX || timeModifier == TimeModifier.AVG; String valueSql = "o.value_numeric"; boolean doInvert = false; String dateSql = ""; if (fromDate != null) dateSql += " and o.obs_datetime >= :fromDate "; if (toDate != null) dateSql += " and o.obs_datetime <= :toDate "; if (timeModifier == TimeModifier.ANY || timeModifier == TimeModifier.NO) { if (timeModifier == TimeModifier.NO) doInvert = true; sb.append("select o.person_id from obs o " + "where voided = false and concept_id = :concept_id "); sb.append(dateSql); } else if (timeModifier == TimeModifier.FIRST || timeModifier == TimeModifier.LAST) { boolean isFirst = timeModifier == PatientSetService.TimeModifier.FIRST; sb.append("select o.person_id " + "from obs o inner join (" + " select person_id, " + (isFirst ? "min" : "max") + "(obs_datetime) as obs_datetime" + " from obs" + " where voided = false and concept_id = :concept_id " + dateSql + " group by person_id" + ") subq on o.person_id = subq.person_id and o.obs_datetime = subq.obs_datetime " + "where o.voided = false and o.concept_id = :concept_id "); } else if (doSqlAggregation) { String sqlAggregator = timeModifier.toString(); valueSql = sqlAggregator + "(o.value_numeric)"; sb.append("select o.person_id " + "from obs o where o.voided = false and concept_id = :concept_id " + dateSql + "group by o.person_id "); } else { throw new IllegalArgumentException("TimeModifier '" + timeModifier + "' not recognized"); } if (useValue) { sb.append(doSqlAggregation ? "having " : " and "); sb.append(valueSql + " "); sb.append(modifier.getSqlRepresentation() + " :value"); } if (!doSqlAggregation) sb.append(" group by o.person_id "); log.debug("query: " + sb); Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString()); query.setCacheMode(CacheMode.IGNORE); query.setInteger("concept_id", conceptId); if (useValue) { query.setDouble("value", value.doubleValue()); } if (fromDate != null) query.setDate("fromDate", fromDate); if (toDate != null) query.setDate("toDate", fromDate); Cohort ret; if (doInvert) { ret = getAllPatients(); ret.getMemberIds().removeAll(query.list()); } else { ret = new Cohort(query.list()); } return ret; }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
public Cohort getPatientsByCharacteristics(String gender, Date minBirthdate, Date maxBirthdate, Integer minAge, Integer maxAge, Boolean aliveOnly, Boolean deadOnly, Date effectiveDate) throws DAOException { if (effectiveDate == null) { effectiveDate = new Date(); }/*from ww w . ja va 2 s .co m*/ StringBuffer queryString = new StringBuffer("select patientId from Patient patient"); List<String> clauses = new ArrayList<String>(); clauses.add("patient.voided = false"); if (gender != null) { gender = gender.toUpperCase(); clauses.add("patient.gender = :gender"); } if (minBirthdate != null) { clauses.add("patient.birthdate >= :minBirthdate"); } if (maxBirthdate != null) { clauses.add("patient.birthdate <= :maxBirthdate"); } if (aliveOnly != null && aliveOnly) { clauses.add("patient.dead = false"); // TODO: Should this use effectiveDate? What if deathDate is null? } if (deadOnly != null && deadOnly) { clauses.add("patient.dead = true"); // TODO: Should this use effectiveDate? What if deathDate is null? } Date maxBirthFromAge = null; if (minAge != null) { Calendar cal = Calendar.getInstance(); cal.setTime(effectiveDate); cal.add(Calendar.YEAR, -minAge); maxBirthFromAge = cal.getTime(); clauses.add("patient.birthdate <= :maxBirthFromAge"); } Date minBirthFromAge = null; if (maxAge != null) { Calendar cal = Calendar.getInstance(); cal.setTime(effectiveDate); cal.add(Calendar.YEAR, -(maxAge + 1)); minBirthFromAge = cal.getTime(); clauses.add("patient.birthdate > :minBirthFromAge"); } clauses.add("(patient.birthdate is null or patient.birthdate <= :effectiveDate)"); boolean first = true; for (String clause : clauses) { if (first) { queryString.append(" where ").append(clause); first = false; } else { queryString.append(" and ").append(clause); } } Query query = sessionFactory.getCurrentSession().createQuery(queryString.toString()); query.setCacheMode(CacheMode.IGNORE); if (gender != null) { query.setString("gender", gender); } if (minBirthdate != null) { query.setDate("minBirthdate", minBirthdate); } if (maxBirthdate != null) { query.setDate("maxBirthdate", maxBirthdate); } if (minAge != null) { query.setDate("maxBirthFromAge", maxBirthFromAge); } if (maxAge != null) { query.setDate("minBirthFromAge", minBirthFromAge); } query.setDate("effectiveDate", effectiveDate); return new Cohort(query.list()); }
From source file:org.openmrs.module.reportingcompatibility.service.db.HibernateReportingCompatibilityDAO.java
License:Open Source License
@SuppressWarnings("unchecked") /**//from w w w . j a v a2 s . 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; }
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. ja v a 2 s . c o 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);//w w w.j av a2s . c o m } 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. * //w ww .j a v a 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) {/* w w w. j av a2s. co m*/ 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);/* w w w .ja v a2s . 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 w ww .j a va 2 s . co m } return ret; }