| | 195 | // Helper function, converts logic service's criteria into Hibernate's |
|---|
| | 196 | // criteria |
|---|
| | 197 | public Criteria logicToHibernate(LogicCriteria logicCriteria, |
|---|
| | 198 | boolean notOperator) { |
|---|
| | 199 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria( |
|---|
| | 200 | Obs.class); |
|---|
| | 201 | |
|---|
| | 202 | while (logicCriteria != null) { |
|---|
| | 203 | if (logicCriteria.getOperator() == null) { |
|---|
| | 204 | // there's a concept string inside the right operand |
|---|
| | 205 | Concept concept = Context.getConceptService() |
|---|
| | 206 | .getConceptByIdOrName(logicCriteria.getRootToken()); |
|---|
| | 207 | Criterion c = Restrictions.eq("concept", concept); |
|---|
| | 208 | |
|---|
| | 209 | if (notOperator) |
|---|
| | 210 | c = Restrictions.not(c); |
|---|
| | 211 | criteria.add(c); |
|---|
| | 212 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Before) { |
|---|
| | 213 | Criterion c = Restrictions.lt("obsDateTime", logicCriteria |
|---|
| | 214 | .getRightOperand()); |
|---|
| | 215 | |
|---|
| | 216 | if (notOperator) |
|---|
| | 217 | c = Restrictions.not(c); |
|---|
| | 218 | criteria.add(c); |
|---|
| | 219 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.After) { |
|---|
| | 220 | Criterion c = Restrictions.gt("obsDateTime", logicCriteria |
|---|
| | 221 | .getRightOperand()); |
|---|
| | 222 | |
|---|
| | 223 | if (notOperator) |
|---|
| | 224 | c = Restrictions.not(c); |
|---|
| | 225 | criteria.add(c); |
|---|
| | 226 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.And) { |
|---|
| | 227 | // TODO handle AND operator |
|---|
| | 228 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Or) { |
|---|
| | 229 | // TODO handle OR operator |
|---|
| | 230 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Not) { |
|---|
| | 231 | notOperator = !notOperator; |
|---|
| | 232 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Contains) { |
|---|
| | 233 | // TODO handle CONTAINS operator |
|---|
| | 234 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Equals) { |
|---|
| | 235 | Criterion c = null; |
|---|
| | 236 | if (logicCriteria.getRightOperand() instanceof Float |
|---|
| | 237 | || logicCriteria.getRightOperand() instanceof Integer) |
|---|
| | 238 | c = Restrictions.eq("valueNumeric", logicCriteria |
|---|
| | 239 | .getRightOperand()); |
|---|
| | 240 | else if (logicCriteria.getRightOperand() instanceof String) |
|---|
| | 241 | c = Restrictions.eq("valueText", logicCriteria |
|---|
| | 242 | .getRightOperand()); |
|---|
| | 243 | else if (logicCriteria.getRightOperand() instanceof Date) |
|---|
| | 244 | c = Restrictions.eq("obsDateTime", logicCriteria |
|---|
| | 245 | .getRightOperand()); |
|---|
| | 246 | else if (logicCriteria.getRightOperand() instanceof Concept) |
|---|
| | 247 | c = Restrictions.eq("concept", logicCriteria |
|---|
| | 248 | .getRightOperand()); |
|---|
| | 249 | else |
|---|
| | 250 | log.error("Invalid operand value for EQUALS operation"); |
|---|
| | 251 | |
|---|
| | 252 | if (notOperator) |
|---|
| | 253 | c = Restrictions.not(c); |
|---|
| | 254 | criteria.add(c); |
|---|
| | 255 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.LessThan) { |
|---|
| | 256 | Criterion c = null; |
|---|
| | 257 | if (logicCriteria.getRightOperand() instanceof Float |
|---|
| | 258 | || logicCriteria.getRightOperand() instanceof Integer) |
|---|
| | 259 | c = Restrictions.lt("valueNumeric", logicCriteria |
|---|
| | 260 | .getRightOperand()); |
|---|
| | 261 | else if (logicCriteria.getRightOperand() instanceof String) |
|---|
| | 262 | c = Restrictions.lt("valueText", logicCriteria |
|---|
| | 263 | .getRightOperand()); |
|---|
| | 264 | else if (logicCriteria.getRightOperand() instanceof Date) |
|---|
| | 265 | c = Restrictions.lt("obsDateTime", logicCriteria |
|---|
| | 266 | .getRightOperand()); |
|---|
| | 267 | else |
|---|
| | 268 | log.error("Invalid operand value for LESS THAN operation"); |
|---|
| | 269 | |
|---|
| | 270 | if (notOperator) |
|---|
| | 271 | c = Restrictions.not(c); |
|---|
| | 272 | criteria.add(c); |
|---|
| | 273 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.GreaterThan) { |
|---|
| | 274 | Criterion c = null; |
|---|
| | 275 | if (logicCriteria.getRightOperand() instanceof Float |
|---|
| | 276 | || logicCriteria.getRightOperand() instanceof Integer) |
|---|
| | 277 | c = Restrictions.gt("valueNumeric", logicCriteria |
|---|
| | 278 | .getRightOperand()); |
|---|
| | 279 | else if (logicCriteria.getRightOperand() instanceof String) |
|---|
| | 280 | c = Restrictions.gt("valueText", logicCriteria |
|---|
| | 281 | .getRightOperand()); |
|---|
| | 282 | else if (logicCriteria.getRightOperand() instanceof Date) |
|---|
| | 283 | c = Restrictions.gt("obsDateTime", logicCriteria |
|---|
| | 284 | .getRightOperand()); |
|---|
| | 285 | else |
|---|
| | 286 | log.error("Invalid operand value for LESS THAN operation"); |
|---|
| | 287 | |
|---|
| | 288 | if (notOperator) |
|---|
| | 289 | c = Restrictions.not(c); |
|---|
| | 290 | criteria.add(c); |
|---|
| | 291 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Last) { |
|---|
| | 292 | // TODO think of a solution for a NOT LAST case (is it needed at |
|---|
| | 293 | // all?) |
|---|
| | 294 | criteria.addOrder(Order.asc("obsDateTime")).setMaxResults(1); |
|---|
| | 295 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Exists) { |
|---|
| | 296 | // TODO handle EXISTS operator |
|---|
| | 297 | } else if (logicCriteria.getOperator() instanceof org.openmrs.logic.op.Within) { |
|---|
| | 298 | // TODO handle WITHIN operator |
|---|
| | 299 | } |
|---|
| | 300 | |
|---|
| | 301 | logicCriteria = (LogicCriteria) logicCriteria.getLeftOperand(); |
|---|
| | 302 | } |
|---|
| | 303 | |
|---|
| | 304 | return criteria; |
|---|
| | 305 | } |
|---|
| | 306 | |
|---|
| | 307 | /** |
|---|
| | 308 | * @see org.openmrs.api.db.ObsDAO#getObservations(org.openmrs.Person, |
|---|
| | 309 | * org.openmrs.logic.LogicCriteria) |
|---|
| | 310 | */ |
|---|
| | 311 | @SuppressWarnings("unchecked") |
|---|
| | 312 | public List<Obs> getObservations(PatientSet who, LogicCriteria logicCriteria) { |
|---|
| | 313 | Criteria criteria = logicToHibernate(logicCriteria, false); |
|---|
| | 314 | List<Obs> results = new ArrayList<Obs>(); |
|---|
| | 315 | |
|---|
| | 316 | for (Patient patient : who.getPatients()) { |
|---|
| | 317 | // TODO possibly won't work - multiple "person" clauses |
|---|
| | 318 | criteria.add(Restrictions.eq("person", patient)); |
|---|
| | 319 | results.addAll(criteria.list()); |
|---|
| | 320 | } |
|---|
| | 321 | |
|---|
| | 322 | return results; |
|---|
| | 323 | } |
|---|
| | 324 | |
|---|