1
package org.hisp.dhis.datavalue.hibernate;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.Collections;
34
import org.hibernate.Criteria;
35
import org.hibernate.Query;
36
import org.hibernate.Session;
37
import org.hibernate.criterion.Order;
38
import org.hibernate.criterion.Restrictions;
39
import org.hisp.dhis.dataelement.DataElement;
40
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
41
import org.hisp.dhis.datavalue.DataValue;
42
import org.hisp.dhis.datavalue.DataValueStore;
43
import org.hisp.dhis.hibernate.HibernateSessionManager;
44
import org.hisp.dhis.period.Period;
45
import org.hisp.dhis.period.PeriodStore;
46
import org.hisp.dhis.source.Source;
49
* @author Torgeir Lorange Ostby
50
* @version $Id: HibernateDataValueStore.java 5715 2008-09-17 14:05:28Z larshelg $
52
public class HibernateDataValueStore
53
implements DataValueStore
55
// -------------------------------------------------------------------------
57
// -------------------------------------------------------------------------
59
private HibernateSessionManager sessionManager;
61
public void setSessionManager( HibernateSessionManager sessionManager )
63
this.sessionManager = sessionManager;
66
private PeriodStore periodStore;
68
public void setPeriodStore( PeriodStore periodStore )
70
this.periodStore = periodStore;
73
// -------------------------------------------------------------------------
74
// Support methods for reloading periods
75
// -------------------------------------------------------------------------
77
private final Period reloadPeriod( Period period )
79
Session session = sessionManager.getCurrentSession();
81
if ( session.contains( period ) )
83
return period; // Already in session, no reload needed
86
return periodStore.getPeriod( period.getStartDate(), period.getEndDate(), period.getPeriodType() );
89
private final Period reloadPeriodForceAdd( Period period )
91
Period storedPeriod = reloadPeriod( period );
93
if ( storedPeriod == null )
95
periodStore.addPeriod( period );
103
// -------------------------------------------------------------------------
105
// -------------------------------------------------------------------------
107
public void addDataValue( DataValue dataValue )
109
dataValue.setPeriod( reloadPeriodForceAdd( dataValue.getPeriod() ) );
111
Session session = sessionManager.getCurrentSession();
113
session.save( dataValue );
116
public void updateDataValue( DataValue dataValue )
118
dataValue.setPeriod( reloadPeriodForceAdd( dataValue.getPeriod() ) );
120
Session session = sessionManager.getCurrentSession();
122
session.update( dataValue );
125
public void deleteDataValue( DataValue dataValue )
127
Session session = sessionManager.getCurrentSession();
129
session.delete( dataValue );
132
public int deleteDataValuesBySource( Source source )
134
Session session = sessionManager.getCurrentSession();
136
Query query = session.createQuery( "delete DataValue where source = :source" );
137
query.setEntity( "source", source );
139
return query.executeUpdate();
142
public int deleteDataValuesByDataElement( DataElement dataElement )
144
Session session = sessionManager.getCurrentSession();
146
Query query = session.createQuery( "delete DataValue where dataElement = :dataElement" );
147
query.setEntity( "dataElement", dataElement );
149
return query.executeUpdate();
152
public DataValue getDataValue( Source source, DataElement dataElement, Period period )
154
Session session = sessionManager.getCurrentSession();
156
Period storedPeriod = reloadPeriod( period );
158
if ( storedPeriod == null )
163
Criteria criteria = session.createCriteria( DataValue.class );
164
criteria.add( Restrictions.eq( "source", source ) );
165
criteria.add( Restrictions.eq( "dataElement", dataElement ) );
166
criteria.add( Restrictions.eq( "period", storedPeriod ) );
168
return (DataValue) criteria.uniqueResult();
171
public DataValue getDataValue( Source source, DataElement dataElement, Period period, DataElementCategoryOptionCombo optionCombo )
173
Session session = sessionManager.getCurrentSession();
175
Period storedPeriod = reloadPeriod( period );
177
if ( storedPeriod == null )
182
Criteria criteria = session.createCriteria( DataValue.class );
183
criteria.add( Restrictions.eq( "source", source ) );
184
criteria.add( Restrictions.eq( "dataElement", dataElement ) );
185
criteria.add( Restrictions.eq( "period", storedPeriod ) );
186
criteria.add( Restrictions.eq( "optionCombo", optionCombo ) );
188
return (DataValue) criteria.uniqueResult();
191
// -------------------------------------------------------------------------
192
// Collections of DataValues
193
// -------------------------------------------------------------------------
195
@SuppressWarnings( "unchecked" )
196
public Collection<DataValue> getAllDataValues()
198
Session session = sessionManager.getCurrentSession();
200
Criteria criteria = session.createCriteria( DataValue.class );
202
return criteria.list();
205
@SuppressWarnings( "unchecked" )
206
public Collection<DataValue> getDataValues( Source source, Period period )
208
Period storedPeriod = reloadPeriod( period );
210
if ( storedPeriod == null )
212
return Collections.emptySet();
215
Session session = sessionManager.getCurrentSession();
217
Criteria criteria = session.createCriteria( DataValue.class );
218
criteria.add( Restrictions.eq( "source", source ) );
219
criteria.add( Restrictions.eq( "period", storedPeriod ) );
221
return criteria.list();
224
@SuppressWarnings( "unchecked" )
225
public Collection<DataValue> getDataValues( Source source, DataElement dataElement )
227
Session session = sessionManager.getCurrentSession();
229
Criteria criteria = session.createCriteria( DataValue.class );
230
criteria.add( Restrictions.eq( "source", source ) );
231
criteria.add( Restrictions.eq( "dataElement", dataElement ) );
233
return criteria.list();
236
@SuppressWarnings( "unchecked" )
237
public Collection<DataValue> getDataValues( Collection<? extends Source> sources, DataElement dataElement )
239
Session session = sessionManager.getCurrentSession();
241
Criteria criteria = session.createCriteria( DataValue.class );
242
criteria.add( Restrictions.in( "source", sources ) );
243
criteria.add( Restrictions.eq( "dataElement", dataElement ) );
245
return criteria.list();
248
@SuppressWarnings( "unchecked" )
249
public Collection<DataValue> getDataValues( Source source, Period period, Collection<DataElement> dataElements )
251
Period storedPeriod = reloadPeriod( period );
253
if ( storedPeriod == null )
255
return Collections.emptySet();
258
Session session = sessionManager.getCurrentSession();
260
Criteria criteria = session.createCriteria( DataValue.class );
261
criteria.add( Restrictions.eq( "source", source ) );
262
criteria.add( Restrictions.eq( "period", storedPeriod ) );
263
criteria.add( Restrictions.in( "dataElement", dataElements ) );
265
return criteria.list();
268
@SuppressWarnings( "unchecked" )
269
public Collection<DataValue> getDataValues( Source source, Period period, Collection<DataElement> dataElements, Collection<DataElementCategoryOptionCombo> optionCombos )
271
Period storedPeriod = reloadPeriod( period );
273
if ( storedPeriod == null )
275
return Collections.emptySet();
278
Session session = sessionManager.getCurrentSession();
280
Criteria criteria = session.createCriteria( DataValue.class );
281
criteria.add( Restrictions.eq( "source", source ) );
282
criteria.add( Restrictions.eq( "period", storedPeriod ) );
283
criteria.add( Restrictions.in( "dataElement", dataElements ) );
284
criteria.add( Restrictions.in( "optionCombo", optionCombos ) );
286
return criteria.list();
289
@SuppressWarnings( "unchecked" )
290
public Collection<DataValue> getDataValues( DataElement dataElement, Collection<Period> periods,
291
Collection<? extends Source> sources )
293
Collection<Period> storedPeriods = new ArrayList<Period>();
295
for ( Period period : periods )
297
Period storedPeriod = reloadPeriod( period );
299
if ( storedPeriod != null )
301
storedPeriods.add( storedPeriod );
305
Session session = sessionManager.getCurrentSession();
307
Criteria criteria = session.createCriteria( DataValue.class );
308
criteria.add( Restrictions.eq( "dataElement", dataElement ) );
309
criteria.add( Restrictions.in( "period", storedPeriods ) );
310
criteria.add( Restrictions.in( "source", sources ) );
312
return criteria.list();
315
@SuppressWarnings( "unchecked" )
316
public Collection<DataValue> getDataValues( DataElement dataElement, DataElementCategoryOptionCombo optionCombo,
317
Collection<Period> periods, Collection<? extends Source> sources )
319
Collection<Period> storedPeriods = new ArrayList<Period>();
321
for ( Period period : periods )
323
Period storedPeriod = reloadPeriod( period );
325
if ( storedPeriod != null )
327
storedPeriods.add( storedPeriod );
331
Session session = sessionManager.getCurrentSession();
333
Criteria criteria = session.createCriteria( DataValue.class );
334
criteria.add( Restrictions.eq( "dataElement", dataElement ) );
335
criteria.add( Restrictions.eq( "optionCombo", optionCombo ) );
336
criteria.add( Restrictions.in( "period", storedPeriods ) );
337
criteria.add( Restrictions.in( "source", sources ) );
339
return criteria.list();
342
@SuppressWarnings( "unchecked" )
343
public Collection<DataValue> getDataValues( Collection<DataElement> dataElements, Collection<Period> periods,
344
Collection<? extends Source> sources, int firstResult, int maxResults )
346
Collection<Period> storedPeriods = new ArrayList<Period>();
348
for ( Period period : periods )
350
Period storedPeriod = reloadPeriod( period );
352
if ( storedPeriod != null )
354
storedPeriods.add( storedPeriod );
358
if ( storedPeriods.size() == 0 )
360
return Collections.emptySet();
363
Session session = sessionManager.getCurrentSession();
365
Criteria criteria = session.createCriteria( DataValue.class );
367
criteria.add( Restrictions.in( "dataElement", dataElements ) );
368
criteria.add( Restrictions.in( "period", storedPeriods ) );
369
criteria.add( Restrictions.in( "source", sources ) );
371
if ( maxResults != 0 )
373
criteria.addOrder( Order.asc( "dataElement" ) );
374
criteria.addOrder( Order.asc( "period" ) );
375
criteria.addOrder( Order.asc( "source" ) );
377
criteria.setFirstResult( firstResult );
378
criteria.setMaxResults( maxResults );
381
return criteria.list();
384
@SuppressWarnings( "unchecked" )
385
public Collection<DataValue> getDataValues( Collection<DataElementCategoryOptionCombo> optionCombos )
387
Session session = sessionManager.getCurrentSession();
389
Criteria criteria = session.createCriteria( DataValue.class );
390
criteria.add( Restrictions.in( "optionCombo", optionCombos ) );
392
return criteria.list();
395
@SuppressWarnings( "unchecked" )
396
public Collection<DataValue> getDataValues( DataElement dataElement )
398
Session session = sessionManager.getCurrentSession();
400
Criteria criteria = session.createCriteria( DataValue.class );
401
criteria.add( Restrictions.eq( "dataElement", dataElement ) );
403
return criteria.list();