/
HibernateReportDAO.java
275 lines (241 loc) · 9.62 KB
/
HibernateReportDAO.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
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.reporting.report.service.db;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.openmrs.api.db.DAOException;
import org.openmrs.api.db.hibernate.DbSessionFactory;
import org.openmrs.module.reporting.report.ReportDesign;
import org.openmrs.module.reporting.report.ReportProcessorConfiguration;
import org.openmrs.module.reporting.report.ReportRequest;
import org.openmrs.module.reporting.report.ReportRequest.Status;
import org.openmrs.module.reporting.report.definition.ReportDefinition;
import org.openmrs.module.reporting.report.renderer.ReportRenderer;
import java.util.Date;
import java.util.List;
/**
* ReportService Database Access Interface
*/
public class HibernateReportDAO implements ReportDAO {
protected final Log log = LogFactory.getLog(getClass());
//***** PROPERTIES *****
private DbSessionFactory sessionFactory;
//***** INSTANCE METHODS *****
//****** REPORT DESIGNS *****
/**
* @param uuid
* @return the ReportDesign with the given uuid
*/
public ReportDesign getReportDesignByUuid(String uuid) throws DAOException {
Query q = sessionFactory.getCurrentSession().createQuery("from ReportDesign r where r.uuid = :uuid");
return (ReportDesign) q.setString("uuid", uuid).uniqueResult();
}
/**
* Get the {@link ReportDesign} with the given id
* @param id The Integer ReportDesign id
* @return the matching {@link ReportDesign} object
* @throws DAOException
*/
public ReportDesign getReportDesign(Integer id) throws DAOException {
return (ReportDesign) sessionFactory.getCurrentSession().get(ReportDesign.class, id);
}
/**
* Return a list of {@link ReportDesign}s for the passed {@link ReportDefinition} and {@link ReportRenderer} class,
* optionally including those that are retired
* @param includeRetired if true, indicates that retired {@link ReportDesign}s should also be included
* @return a List<ReportDesign> object containing all of the {@link ReportDesign}s
* @throws DAOException
*/
@SuppressWarnings("unchecked")
public List<ReportDesign> getReportDesigns(ReportDefinition reportDefinition, Class<? extends ReportRenderer> rendererType,
boolean includeRetired) throws DAOException {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(ReportDesign.class);
if (reportDefinition != null) {
crit.add(Expression.eq("reportDefinition", reportDefinition));
}
if (rendererType != null) {
crit.add(Expression.eq("rendererType", rendererType));
}
if (includeRetired == false) {
crit.add(Expression.eq("retired", false));
}
return crit.list();
}
/**
* Save or update the given <code>ReportDesign</code> in the database. If this is a new
* ReportDesign, the returned ReportDesign will have a new
* {@link ReportDesign#getId()} inserted into it that was generated by the database
*
* @param reportDesign The <code>ReportDesign</code> to save or update
* @throws DAOException
*/
public ReportDesign saveReportDesign(ReportDesign reportDesign) throws DAOException {
sessionFactory.getCurrentSession().saveOrUpdate(reportDesign);
return reportDesign;
}
/**
* Purges a <code>ReportDesign</code> from the database.
* @param reportDesign The <code>ReportDesign</code> to remove from the system
* @throws DAOException
*/
public void purgeReportDesign(ReportDesign reportDesign) {
sessionFactory.getCurrentSession().delete(reportDesign);
}
//****** REPORT PROCESSOR CONFIGURATIONS *****
/**
* Saves a {@link ReportProcessorConfiguration} to the database and returns it
*/
public ReportProcessorConfiguration saveReportProcessorConfiguration(ReportProcessorConfiguration processorConfiguration) {
sessionFactory.getCurrentSession().saveOrUpdate(processorConfiguration);
return processorConfiguration;
}
/**
* @return the {@link ReportProcessorConfiguration} with the passed id
*/
public ReportProcessorConfiguration getReportProcessorConfiguration(Integer id) {
return (ReportProcessorConfiguration) sessionFactory.getCurrentSession().get(ReportProcessorConfiguration.class, id);
}
/**
* @return the {@link ReportProcessorConfiguration} with the passed uuid
*/
public ReportProcessorConfiguration getReportProcessorConfigurationByUuid(String uuid) {
Query q = sessionFactory.getCurrentSession().createQuery("from ReportProcessorConfiguration r where r.uuid = :uuid");
return (ReportProcessorConfiguration) q.setString("uuid", uuid).uniqueResult();
}
/**
* @return all the {@link ReportProcessorConfiguration}s
*/
@SuppressWarnings("unchecked")
public List<ReportProcessorConfiguration> getAllReportProcessorConfigurations(boolean includeRetired) {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(ReportProcessorConfiguration.class);
if (includeRetired == false) {
crit.add(Expression.eq("retired", false));
}
return crit.list();
}
/**
* @return all the {@link ReportProcessorConfiguration}s that are meant to be applied globally, i.e., their reportDesign property is null
*/
@SuppressWarnings("unchecked")
public List<ReportProcessorConfiguration> getGlobalReportProcessorConfigurations() {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(ReportProcessorConfiguration.class);
crit.add(Expression.eq("retired", false));
crit.add(Expression.isNull("reportDesign"));
return crit.list();
}
/**
* Deletes the passed {@link ReportProcessorConfiguration}
*/
public void purgeReportProcessorConfiguration(ReportProcessorConfiguration processorConfiguration) {
sessionFactory.getCurrentSession().delete(processorConfiguration);
}
//****** REPORT REQUESTS *****
/**
* @see ReportDAO#saveReportRequest(ReportRequest)
*/
public ReportRequest saveReportRequest(ReportRequest request) {
sessionFactory.getCurrentSession().saveOrUpdate(request);
return request;
}
/**
* @see ReportDAO#getReportRequest(java.lang.Integer)
*/
public ReportRequest getReportRequest(Integer id) {
return (ReportRequest) sessionFactory.getCurrentSession().get(ReportRequest.class, id);
}
/**
* @see ReportDAO#getReportRequestByUuid(java.lang.String)
*/
public ReportRequest getReportRequestByUuid(String uuid) {
Query q = sessionFactory.getCurrentSession().createQuery("from ReportRequest r where r.uuid = :uuid");
return (ReportRequest) q.setString("uuid", uuid).uniqueResult();
}
/**
* @see ReportDAO#getReportRequests(ReportDefinition, Date, Date, Integer, Status...)
*/
@SuppressWarnings("unchecked")
public List<ReportRequest> getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses) {
Criteria c = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
if (reportDefinition != null) {
c.add(Restrictions.eq("reportDefinition.definition", reportDefinition.getUuid()));
}
if (requestOnOrAfter != null) {
c.add(Restrictions.ge("requestDate", requestOnOrAfter));
}
if (requestOnOrBefore != null) {
c.add(Restrictions.le("requestDate", requestOnOrBefore));
}
if (statuses != null && statuses.length > 0) {
c.add(Restrictions.in("status", statuses));
}
c.addOrder(Order.desc("evaluateCompleteDatetime"));
c.addOrder(Order.desc("evaluateStartDatetime"));
c.addOrder(Order.desc("priority"));
c.addOrder(Order.desc("requestDate"));
if (mostRecentNum != null) {
c.setMaxResults(mostRecentNum);
}
return c.list();
}
/**
* @see ReportDAO#purgeReportRequest(ReportRequest)
*/
public void purgeReportRequest(ReportRequest request) {
sessionFactory.getCurrentSession().delete(request);
}
/**
* @see ReportDAO#purgeReportRequestsForReportDefinition(String)
*/
@Override
public void purgeReportRequestsForReportDefinition(String reportDefinitionUuid) {
String hql = "delete from ReportRequest r where r.reportDefinition.definition=:uuid";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString("uuid", reportDefinitionUuid);
query.executeUpdate();
}
/**
* @see ReportDAO#purgeReportDesignsForReportDefinition(String)
*/
@Override
public void purgeReportDesignsForReportDefinition(String reportDefinitionUuid) {
String hql = "delete from ReportDesign r where r.reportDefinition=:uuid";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString("uuid", reportDefinitionUuid);
query.executeUpdate();
}
/**
* @see ReportDAO#getReportRequestUuids(String)
*/
@Override
public List<String> getReportRequestUuids(String reportDefinitionUuid) {
String hql = "select uuid from ReportRequest r where r.reportDefinition.definition=:uuid";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString("uuid", reportDefinitionUuid);
return query.list();
}
//***** PROPERTY ACCESS *****
/**
* @return the sessionFactory
*/
public DbSessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory the sessionFactory to set
*/
public void setSessionFactory(DbSessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}