-
Notifications
You must be signed in to change notification settings - Fork 42
/
HibernateDHIS2ReportingDAO.java
387 lines (329 loc) · 12.3 KB
/
HibernateDHIS2ReportingDAO.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Copyright 2012 Society for Health Information Systems Programmes, India (HISP India)
*
* This file is part of DHIS2 Reporting module.
*
* DHIS2 Reporting module is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* DHIS2 Reporting module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DHIS2 Reporting module. If not, see <http://www.gnu.org/licenses/>.
*
**/
package org.openmrs.module.dhisreport.api.db.hibernate;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.openmrs.Location;
import org.openmrs.module.dhisreport.api.DHIS2ReportingException;
import org.openmrs.module.dhisreport.api.db.DHIS2ReportingDAO;
import org.openmrs.module.dhisreport.api.model.*;
import org.openmrs.module.dhisreport.api.utils.Period;
import org.springframework.transaction.annotation.Transactional;
/**
* It is a default implementation of {@link DHIS2ReportingDAO}.
*/
public class HibernateDHIS2ReportingDAO
implements DHIS2ReportingDAO
{
// query parameters
private static final String LOCATION = "locationId";
private static final String START = "startDate";
private static final String END = "endDate";
protected final Log log = LogFactory.getLog( this.getClass() );
private SessionFactory sessionFactory;
/**
* @param sessionFactory the sessionFactory to set
*/
public void setSessionFactory( SessionFactory sessionFactory )
{
this.sessionFactory = sessionFactory;
}
/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory()
{
return sessionFactory;
}
@Override
public DataElement getDataElement( Integer id )
{
return (DataElement) getCurrentSession().get( DataElement.class, id );
}
@Override
public DataElement saveDataElement( DataElement de )
{
return (DataElement) saveObject( de );
}
@Override
public void deleteDataElement( DataElement de )
{
getCurrentSession().delete( de );
}
@Override
public Disaggregation getDisaggregation( Integer id )
{
return (Disaggregation) getCurrentSession().get( Disaggregation.class, id );
}
@Override
public Disaggregation saveDisaggregation( Disaggregation disagg )
{
return (Disaggregation) saveObject( disagg );
}
@Override
public ReportDefinition getReportDefinition( Integer id )
{
return (ReportDefinition) getCurrentSession().get( ReportDefinition.class, id );
}
@Override
public ReportDefinition saveReportDefinition( ReportDefinition rd )
{
// System.out.println( "sending the report definition for being saved which has period :------"
// + rd.getPeriodType() );
return (ReportDefinition) saveObject( rd );
}
@Override
public Collection<DataElement> getAllDataElements()
{
Query query = getCurrentSession().createQuery( "from DataElement order by name asc" );
return (List<DataElement>) query.list();
}
@Override
public Collection<Disaggregation> getAllDisaggregations()
{
Query query = getCurrentSession().createQuery( "from Disaggregation" );
return (List<Disaggregation>) query.list();
}
@Override
public void deleteDisaggregation( Disaggregation disagg )
{
getCurrentSession().delete( disagg );
}
@Override
public Collection<ReportDefinition> getAllReportDefinitions()
{
Query query = getCurrentSession().createQuery( "from ReportDefinition order by name asc" );
return (List<ReportDefinition>) query.list();
}
@Override
public void deleteReportDefinition( ReportDefinition rd )
{
getCurrentSession().delete( rd );
}
/*
* Should evaluateDataValueTemplates
* return String
* */
@Override
public String evaluateDataValueTemplate( DataValueTemplate dvt, Period period, Location location )
throws DHIS2ReportingException
{
String queryString = dvt.getQuery();
queryString = queryString.replaceAll( "\t", " " );
queryString = queryString.replaceAll( "\n", " " );
queryString = queryString.trim();
if ( queryString == null || queryString.isEmpty() )
{
log.debug( "Empty query for " + dvt.getDataelement().getName() + " : " + dvt.getDisaggregation().getName() );
return null;
}
if ( dvt.potentialUpdateDelete() )
{
throw new DHIS2ReportingException( "Attempt to execute potential update/delete query for "
+ dvt.getDataelement().getName() + " : " + dvt.getDisaggregation().getName() );
}
Query query = getCurrentSession().createSQLQuery( queryString );
List<String> parameters = new ArrayList<String>( Arrays.asList( query.getNamedParameters() ) );
// loactionId is optional
if ( parameters.contains( "locationId" ) )
{
query.setParameter( "locationId", location.getId().toString() );
}
if ( parameters.contains( "startOfPeriod" ) )
{
query.setParameter( "startOfPeriod", period.getStartDate() );
}
if ( parameters.contains( "endOfPeriod" ) )
{
query.setParameter( "endOfPeriod", period.getEndDate() );
}
if ( !parameters.contains( "startOfPeriod" ) && !parameters.contains( "endOfPeriod" ) )
{
return "NoParam";
}
return query.uniqueResult().toString();
}
// --------------------------------------------------------------------------------------------------------------
// Generic methods for DHIS2 identifiable objects
// --------------------------------------------------------------------------------------------------------------
public Identifiable getObjectByUid( String uid, Class<?> clazz )
{
Criteria criteria = getCurrentSession().createCriteria( clazz );
criteria.add( Restrictions.eq( "uid", uid ) );
return (Identifiable) criteria.uniqueResult();
}
@Transactional
public Identifiable saveReportDefinitionObject( ReportDefinition object )
{
Session session = getCurrentSession();
// force merge if uid already exists
Criteria criteria = session.createCriteria( object.getClass() );
criteria.add( Restrictions.eq( "uid", object.getUid() ) );
ReportDefinition existingObject = (ReportDefinition) criteria.uniqueResult();
if ( existingObject != null )
{
session.evict( existingObject );
existingObject.setPeriodType( object.getPeriodType() );
existingObject.setName( object.getName() );
existingObject.setCode( object.getCode() );
existingObject.setDataValueTemplates( object.getDataValueTemplates() );
session.update( existingObject );
return existingObject;
}
session.saveOrUpdate( object );
return object;
}
@Transactional
public Identifiable saveDataElementObject( DataElement object )
{
Session session = getCurrentSession();
// force merge if uid already exists
DataElement existingObject = (DataElement) getObjectByUid( object.getUid(), object.getClass() );
if ( existingObject != null )
{
existingObject.setName( object.getName() );
existingObject.setCode( object.getCode() );
session.update( existingObject );
return existingObject;
}
// getCurrentSession().saveOrUpdate( object );
session.save( object );
return object;
}
@Transactional
public Identifiable saveObject( Identifiable object )
{
Session session = getCurrentSession();
// force merge if uid already exists
Identifiable existingObject = getObjectByUid( object.getUid(), object.getClass() );
if ( existingObject != null )
{
session.evict( existingObject );
object.setId( existingObject.getId() );
session.load( object, object.getId() );
}
getCurrentSession().saveOrUpdate( object );
return object;
}
@Override
public DataElement getDataElementByUid( String uid )
{
return (DataElement) getObjectByUid( uid, DataElement.class );
}
@Override
public Disaggregation getDisaggregationByUid( String uid )
{
return (Disaggregation) getObjectByUid( uid, Disaggregation.class );
}
@Override
public ReportDefinition getReportDefinitionByUid( String uid )
{
return (ReportDefinition) getObjectByUid( uid, ReportDefinition.class );
}
@Override
public ReportDefinition getReportDefinitionByCode( String code )
{
Criteria criteria = getCurrentSession().createCriteria( ReportDefinition.class );
criteria.add( Restrictions.eq( "code", code ) );
return (ReportDefinition) criteria.uniqueResult();
}
@Override
public DataValueTemplate getDataValueTemplate( Integer id )
{
return (DataValueTemplate) getCurrentSession().get( DataValueTemplate.class, id );
}
@Override
public DataValueTemplate saveDataValueTemplate( DataValueTemplate dvt )
{
getCurrentSession().saveOrUpdate( dvt );
return dvt;
}
@Override
public DataValueTemplate saveDataValueTemplateTest( DataValueTemplate dvt )
{
ReportDefinition rd = getReportDefinitionByUid( dvt.getReportDefinition().getUid() );
DataElement de = getDataElementByUid( dvt.getDataelement().getUid() );
Disaggregation dis = getDisaggregationByUid( dvt.getDisaggregation().getUid() );
dvt.setDataelement( de );
dvt.setDisaggregation( dis );
dvt.setReportDefinition( rd );
Criteria criteria = getCurrentSession().createCriteria( DataValueTemplate.class );
criteria.add( Restrictions.eq( "reportDefinition", rd ) ).add( Restrictions.eq( "dataelement", de ) ).add(
Restrictions.eq( "disaggregation", dis ) );
DataValueTemplate dvt_db = (DataValueTemplate) criteria.uniqueResult();
if ( dvt_db == null )
{
getCurrentSession().save( dvt );
return dvt;
}
else
{
getCurrentSession().saveOrUpdate( dvt );
return dvt_db;
}
}
@Override
public Location getLocationByOU_Code( String OU_Code )
{
Criteria criteria = getCurrentSession().createCriteria( Location.class );
criteria.add( Restrictions.like( "name", OU_Code ) );
return (Location) criteria.uniqueResult();
}
@Override
public DataElement getDataElementByCode( String code )
{
Criteria criteria = getCurrentSession().createCriteria( DataElement.class );
criteria.add( Restrictions.eq( "code", code ) );
return (DataElement) criteria.uniqueResult();
}
/**
* Gets the current hibernate session while taking care of the hibernate 3 and 4 differences.
*
* @return the current hibernate session.
*/
private org.hibernate.Session getCurrentSession()
{
try
{
return sessionFactory.getCurrentSession();
}
catch ( NoSuchMethodError ex )
{
try
{
Method method = sessionFactory.getClass().getMethod( "getCurrentSession", null );
return (org.hibernate.Session) method.invoke( sessionFactory, null );
}
catch ( Exception e )
{
throw new RuntimeException( "Failed to get the current hibernate session", e );
}
}
}
}