forked from Raxa/raxacore
-
Notifications
You must be signed in to change notification settings - Fork 227
/
ObsDaoImpl.java
345 lines (305 loc) · 16.2 KB
/
ObsDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package org.bahmni.module.bahmnicore.dao.impl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.bahmni.module.bahmnicore.dao.ObsDao;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.openmrs.Concept;
import org.openmrs.ConceptDatatype;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Order;
import org.openmrs.Person;
import org.openmrs.api.ConceptNameType;
import org.openmrs.api.context.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ObsDaoImpl implements ObsDao {
public static final String COMMA = ",";
@Autowired
private SessionFactory sessionFactory;
public enum OrderBy {ASC, DESC}
@Override
public List<Obs> getNumericObsByPerson(String personUUID) {
Query query = sessionFactory.getCurrentSession().createQuery(
"select obs from Obs as obs inner join fetch " +
"obs.concept as concept inner join fetch " +
"concept.datatype as datatype inner join " +
"obs.person as person " +
"where datatype.hl7Abbreviation = :hl7abrv " +
"and person.uuid= :personUUID " +
"and obs.voided = false ");
query.setString("hl7abrv", ConceptDatatype.NUMERIC);
query.setString("personUUID", personUUID);
return query.list();
}
@Override
public List<Concept> getNumericConceptsForPerson(String personUUID) {
Query query = sessionFactory.getCurrentSession().createQuery(
"select concept " +
"from Obs as obs " +
"inner join obs.concept as concept " +
"inner join concept.datatype as datatype " +
"inner join obs.person as person " +
"where datatype.hl7Abbreviation = :hl7abrv " +
"and person.uuid = :personUUID " +
"and obs.voided = false ");
query.setString("hl7abrv", ConceptDatatype.NUMERIC);
query.setString("personUUID", personUUID);
return query.list();
}
public List<Obs> getObsByPatientAndVisit(String patientUuid, List<String> conceptNames, List<Integer> listOfVisitIds,
Integer limit, OrderBy sortOrder, List<String> obsIgnoreList, Boolean filterOutOrderObs, Order order, Date startDate, Date endDate) {
StringBuilder query = new StringBuilder("select obs from Obs as obs, ConceptName as cn " +
" where obs.person.uuid = :patientUuid " +
" and cn.concept = obs.concept.conceptId " +
" and cn.name in (:conceptNames) " +
" and cn.locale = :locale " +
" and cn.conceptNameType = :conceptNameType " +
" and cn.voided = false and obs.voided = false ");
if (CollectionUtils.isNotEmpty(listOfVisitIds)) {
query.append(" and obs.encounter.visit.visitId in (:listOfVisitIds) ");
}
if (startDate != null) {
query.append(" and obs.obsDatetime >= :startDate ");
}
if (endDate != null) {
query.append(" and obs.obsDatetime <= :endDate ");
}
if (CollectionUtils.isNotEmpty(obsIgnoreList)) {
query.append(" and cn.name not in (:obsIgnoreList) ");
}
if (filterOutOrderObs) {
query.append(" and obs.order.orderId is null ");
}
if (null != order) {
query.append(" and obs.order = (:order) ");
}
if (sortOrder == OrderBy.ASC) {
query.append(" order by obs.obsDatetime asc ");
} else {
query.append(" order by obs.obsDatetime desc ");
}
Query queryToGetObservations = sessionFactory.getCurrentSession().createQuery(query.toString());
queryToGetObservations.setMaxResults(limit);
queryToGetObservations.setString("patientUuid", patientUuid);
queryToGetObservations.setParameterList("conceptNames", conceptNames);
queryToGetObservations.setParameter("conceptNameType", ConceptNameType.FULLY_SPECIFIED);
queryToGetObservations.setString("locale", Context.getLocale().getLanguage());
if (null != obsIgnoreList && obsIgnoreList.size() > 0) {
queryToGetObservations.setParameterList("obsIgnoreList", obsIgnoreList);
}
if (null != listOfVisitIds && listOfVisitIds.size() > 0) {
queryToGetObservations.setParameterList("listOfVisitIds", listOfVisitIds);
}
if (null != order) {
queryToGetObservations.setParameter("order", order);
}
if (startDate != null) {
queryToGetObservations.setParameter("startDate", startDate);
}
if (endDate != null) {
queryToGetObservations.setParameter("endDate", endDate);
}
return queryToGetObservations.list();
}
@Override
public List<Obs> getLatestObsFor(String patientUuid, String conceptName, Integer limit) {
Query queryToGetObservations = sessionFactory.getCurrentSession().createQuery(
"select obs " +
" from Obs as obs, ConceptName as cn " +
" where obs.person.uuid = :patientUuid " +
" and cn.concept = obs.concept.conceptId " +
" and cn.name = (:conceptName) " +
" and cn.locale = :locale " +
" and cn.conceptNameType = :conceptNameType " +
" and cn.voided = false " +
" and obs.voided = false" +
" order by obs.obsDatetime desc ");
queryToGetObservations.setMaxResults(limit);
queryToGetObservations.setString("patientUuid", patientUuid);
queryToGetObservations.setParameter("conceptName", conceptName);
queryToGetObservations.setParameter("conceptNameType", ConceptNameType.FULLY_SPECIFIED);
queryToGetObservations.setString("locale", Context.getLocale().getLanguage());
return queryToGetObservations.list();
}
@Override
public List<Obs> getLatestObsForConceptSetByVisit(String patientUuid, String conceptName, Integer visitId) {
if (visitId == null) return new ArrayList<>();
String queryString =
"select obs\n" +
"from Obs obs join obs.encounter enc join enc.visit v \n" +
"where obs.voided = false and obs.concept.conceptId in " +
" ( select cs.concept.conceptId\n" +
" from ConceptName cn, ConceptSet cs\n" +
" where cs.conceptSet.conceptId = cn.concept.conceptId and cn.conceptNameType='FULLY_SPECIFIED' " +
" and cn.locale = :locale and cn.name=:conceptName)\n" +
" and obs.person.uuid=:patientUuid and v.visitId =:visitId order by enc.encounterId desc";
Query queryToGetObs = sessionFactory.getCurrentSession().createQuery(queryString);
queryToGetObs.setString("conceptName", conceptName);
queryToGetObs.setString("patientUuid", patientUuid);
queryToGetObs.setInteger("visitId", visitId);
queryToGetObs.setString("locale", Context.getLocale().getLanguage());
return queryToGetObs.list();
}
@Override
public List<Obs> getObsForConceptsByEncounter(String encounterUuid, List<String> conceptNames) {
if (encounterUuid == null) return new ArrayList<>();
String queryString =
"select obs\n" +
"from Obs obs, ConceptName cn \n" +
"where obs.voided = false and obs.encounter.uuid =:encounterUuid " +
"and obs.concept.conceptId = cn.concept.conceptId " +
"and cn.name in (:conceptNames) " +
"and cn.locale = :locale " +
"and cn.conceptNameType='FULLY_SPECIFIED'";
Query queryToGetObs = sessionFactory.getCurrentSession().createQuery(queryString);
queryToGetObs.setParameterList("conceptNames", conceptNames);
queryToGetObs.setString("encounterUuid", encounterUuid);
queryToGetObs.setString("locale", Context.getLocale().getLanguage());
return queryToGetObs.list();
}
@Override
public List<Obs> getObsForOrder(String orderUuid) {
String queryString = "from Obs obs where obs.voided = false and obs.order.uuid = :orderUuid order by obs.obsDatetime desc";
Query queryToGetObs = sessionFactory.getCurrentSession().createQuery(queryString);
queryToGetObs.setString("orderUuid", orderUuid);
return queryToGetObs.list();
}
@Override
public List<Obs> getObsForVisits(List<Person> persons, ArrayList<Encounter> encounters, List<Concept> conceptsForNames, Collection<Concept> obsIgnoreList, Boolean filterOutOrders, Order order) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class, "obs");
if (CollectionUtils.isEmpty(encounters)) {
return new ArrayList<>();
} else {
criteria.add(Restrictions.in("encounter", encounters));
}
if (CollectionUtils.isNotEmpty(persons)) {
criteria.add(Restrictions.in("person", persons));
}
if (CollectionUtils.isNotEmpty(conceptsForNames)) {
criteria.add(Restrictions.in("concept", conceptsForNames));
}
if (CollectionUtils.isNotEmpty(obsIgnoreList)) {
criteria.add(Restrictions.not(Restrictions.in("concept", obsIgnoreList)));
}
if (filterOutOrders) {
criteria.add(Restrictions.isNull("order"));
}
if (order != null) {
criteria.add(Restrictions.eq("order", order));
}
criteria.add(Restrictions.eq("voided", Boolean.FALSE));
criteria.addOrder(org.hibernate.criterion.Order.desc("obsDatetime"));
return criteria.list();
}
@Override
public List<Obs> getObsFor(String patientUuid, Concept rootConcept, Concept childConcept, List<Integer> listOfVisitIds, Collection<Encounter> encounters, Date startDate, Date endDate) {
if (listOfVisitIds == null || listOfVisitIds.isEmpty())
return new ArrayList<>();
String encounterFilter = "";
if (encounters != null && encounters.size() > 0) {
encounterFilter = "AND encounter.encounter_id in (" + commaSeparatedEncounterIds(encounters) + ")";
}
StringBuilder queryString = new StringBuilder("SELECT rootObs.* " +
"FROM obs rootObs " +
"JOIN concept_name rootConceptName " +
"ON rootObs.concept_id = rootConceptName.concept_id AND rootConceptName.name = :rootConceptName AND " +
"rootConceptName.concept_name_type = 'FULLY_SPECIFIED' " +
"JOIN person ON person.person_id = rootObs.person_id AND person.uuid = :patientUuid AND " +
"rootObs.voided = 0 AND person.voided = 0 " +
"JOIN encounter ON encounter.encounter_id = rootObs.encounter_id AND encounter.voided = 0 " +
encounterFilter +
"JOIN visit ON visit.visit_id = encounter.visit_id AND visit.visit_id IN :visitIds " +
"JOIN obs groupByObs ON groupByObs.obs_group_id = rootObs.obs_id AND groupByObs.voided = 0 " +
"JOIN concept_name groupByConceptName " +
"ON groupByConceptName.concept_id = groupByObs.concept_id AND groupByConceptName.name = :childConceptName AND " +
"groupByConceptName.concept_name_type = 'FULLY_SPECIFIED' ");
if (startDate != null) queryString.append("where groupByObs.obs_datetime >= :startDate ");
if (startDate != null && endDate != null) queryString.append("and groupByObs.obs_datetime <= :endDate ");
queryString.append("group by groupByObs.obs_group_id order by obs_datetime asc ");
Query queryToGetObs = sessionFactory.getCurrentSession()
.createSQLQuery(queryString.toString()).addEntity(Obs.class);
queryToGetObs.setParameter("rootConceptName", rootConcept.getName().getName());
queryToGetObs.setParameter("patientUuid", patientUuid);
queryToGetObs.setParameterList("visitIds", listOfVisitIds);
queryToGetObs.setParameter("childConceptName", childConcept.getName().getName());
if (startDate != null)
queryToGetObs.setParameter("startDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startDate));
if (endDate != null)
queryToGetObs.setParameter("endDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endDate));
return queryToGetObs.list();
}
private String commaSeparatedEncounterIds(Collection<Encounter> encounters) {
ArrayList<String> encounterIds = new ArrayList<>();
for (Encounter encounter : encounters) {
encounterIds.add(encounter.getEncounterId().toString());
}
return StringUtils.join(encounterIds, COMMA);
}
@Override
public Obs getChildObsFromParent(String parentObsUuid, Concept childConcept) {
String queryString = "from Obs obs where obs.obsGroup.uuid = :parentObsUuid and obs.concept = :concept and obs.voided = false order by obs.obsDatetime desc";
Query queryToGetObs = sessionFactory.getCurrentSession().createQuery(queryString);
queryToGetObs.setParameter("parentObsUuid", parentObsUuid);
queryToGetObs.setParameter("concept", childConcept);
List<Obs> obsList = queryToGetObs.list();
if (obsList.size() > 0) {
return (Obs) queryToGetObs.list().get(0);
}
return null;
}
@Override
public List<Obs> getObsByPatientProgramUuidAndConceptNames(String patientProgramUuid, List<String> conceptNames, Integer limit, OrderBy sortOrder, Date startDate, Date endDate) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
StringBuilder queryString = new StringBuilder("SELECT o.* " +
"FROM patient_program pp " +
"INNER JOIN episode_patient_program epp " +
"ON pp.patient_program_id = epp.patient_program_id\n " +
"INNER JOIN episode_encounter ee " +
"ON epp.episode_id = ee.episode_id\n " +
"INNER JOIN obs o " +
"ON o.encounter_id = ee.encounter_id\n " +
"INNER JOIN concept_name cn on o.concept_id = cn.concept_id\n " +
"WHERE pp.uuid = (:patientProgramUuid) " +
"AND o.voided = false " +
"AND cn.concept_name_type='FULLY_SPECIFIED' " +
"AND cn.name IN (:conceptNames) " +
"AND cn.locale = :locale");
if(null != startDate) {
queryString.append(" AND o.obs_datetime >= STR_TO_DATE(:startDate, '%Y-%m-%d')");
}
if(null != endDate) {
queryString.append(" AND o.obs_datetime <= STR_TO_DATE(:endDate, '%Y-%m-%d')");
}
if (sortOrder == OrderBy.ASC) {
queryString.append(" ORDER by o.obs_datetime asc");
} else {
queryString.append(" ORDER by o.obs_datetime desc");
}
if (limit != null) {
queryString.append(" limit " + limit);
}
Query queryToGetObs = sessionFactory.getCurrentSession().createSQLQuery(queryString.toString()).addEntity(Obs.class);
queryToGetObs.setParameterList("conceptNames", conceptNames);
queryToGetObs.setString("patientProgramUuid", patientProgramUuid);
queryToGetObs.setString("locale", Context.getLocale().getLanguage());
if(null != startDate) {
queryToGetObs.setString("startDate", dateFormat.format(startDate));
}
if(null != endDate) {
queryToGetObs.setString("endDate", dateFormat.format(endDate));
}
return queryToGetObs.list();
}
}