~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/datavalue/hibernate/HibernateDataValueStore.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.datavalue.hibernate;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import java.util.ArrayList;
 
31
import java.util.Collection;
 
32
import java.util.Collections;
 
33
 
 
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;
 
47
 
 
48
/**
 
49
 * @author Torgeir Lorange Ostby
 
50
 * @version $Id: HibernateDataValueStore.java 5715 2008-09-17 14:05:28Z larshelg $
 
51
 */
 
52
public class HibernateDataValueStore
 
53
    implements DataValueStore
 
54
{
 
55
    // -------------------------------------------------------------------------
 
56
    // Dependencies
 
57
    // -------------------------------------------------------------------------
 
58
 
 
59
    private HibernateSessionManager sessionManager;
 
60
 
 
61
    public void setSessionManager( HibernateSessionManager sessionManager )
 
62
    {
 
63
        this.sessionManager = sessionManager;
 
64
    }
 
65
 
 
66
    private PeriodStore periodStore;
 
67
 
 
68
    public void setPeriodStore( PeriodStore periodStore )
 
69
    {
 
70
        this.periodStore = periodStore;
 
71
    }
 
72
 
 
73
    // -------------------------------------------------------------------------
 
74
    // Support methods for reloading periods
 
75
    // -------------------------------------------------------------------------
 
76
 
 
77
    private final Period reloadPeriod( Period period )
 
78
    {
 
79
        Session session = sessionManager.getCurrentSession();
 
80
 
 
81
        if ( session.contains( period ) )
 
82
        {
 
83
            return period; // Already in session, no reload needed
 
84
        }
 
85
 
 
86
        return periodStore.getPeriod( period.getStartDate(), period.getEndDate(), period.getPeriodType() );
 
87
    }
 
88
 
 
89
    private final Period reloadPeriodForceAdd( Period period )
 
90
    {
 
91
        Period storedPeriod = reloadPeriod( period );
 
92
 
 
93
        if ( storedPeriod == null )
 
94
        {
 
95
            periodStore.addPeriod( period );
 
96
 
 
97
            return period;
 
98
        }
 
99
 
 
100
        return storedPeriod;
 
101
    }
 
102
 
 
103
    // -------------------------------------------------------------------------
 
104
    // Basic DataValue
 
105
    // -------------------------------------------------------------------------
 
106
 
 
107
    public void addDataValue( DataValue dataValue )
 
108
    {
 
109
        dataValue.setPeriod( reloadPeriodForceAdd( dataValue.getPeriod() ) );
 
110
 
 
111
        Session session = sessionManager.getCurrentSession();
 
112
 
 
113
        session.save( dataValue );
 
114
    }
 
115
 
 
116
    public void updateDataValue( DataValue dataValue )
 
117
    {
 
118
        dataValue.setPeriod( reloadPeriodForceAdd( dataValue.getPeriod() ) );
 
119
 
 
120
        Session session = sessionManager.getCurrentSession();
 
121
 
 
122
        session.update( dataValue );
 
123
    }
 
124
 
 
125
    public void deleteDataValue( DataValue dataValue )
 
126
    {
 
127
        Session session = sessionManager.getCurrentSession();
 
128
 
 
129
        session.delete( dataValue );
 
130
    }
 
131
 
 
132
    public int deleteDataValuesBySource( Source source )
 
133
    {
 
134
        Session session = sessionManager.getCurrentSession();
 
135
 
 
136
        Query query = session.createQuery( "delete DataValue where source = :source" );
 
137
        query.setEntity( "source", source );
 
138
 
 
139
        return query.executeUpdate();
 
140
    }
 
141
    
 
142
    public int deleteDataValuesByDataElement( DataElement dataElement )
 
143
    {
 
144
        Session session = sessionManager.getCurrentSession();
 
145
 
 
146
        Query query = session.createQuery( "delete DataValue where dataElement = :dataElement" );
 
147
        query.setEntity( "dataElement", dataElement );
 
148
 
 
149
        return query.executeUpdate();    
 
150
    }
 
151
    
 
152
    public DataValue getDataValue( Source source, DataElement dataElement, Period period )
 
153
    {
 
154
        Session session = sessionManager.getCurrentSession();
 
155
 
 
156
        Period storedPeriod = reloadPeriod( period );
 
157
 
 
158
        if ( storedPeriod == null )
 
159
        {
 
160
            return null;
 
161
        }
 
162
 
 
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 ) );
 
167
 
 
168
        return (DataValue) criteria.uniqueResult();
 
169
    }
 
170
    
 
171
    public DataValue getDataValue( Source source, DataElement dataElement, Period period, DataElementCategoryOptionCombo optionCombo )
 
172
    {
 
173
        Session session = sessionManager.getCurrentSession();
 
174
 
 
175
        Period storedPeriod = reloadPeriod( period );
 
176
 
 
177
        if ( storedPeriod == null )
 
178
        {
 
179
            return null;
 
180
        }
 
181
 
 
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 ) );
 
187
 
 
188
        return (DataValue) criteria.uniqueResult();
 
189
    }
 
190
 
 
191
    // -------------------------------------------------------------------------
 
192
    // Collections of DataValues
 
193
    // -------------------------------------------------------------------------
 
194
 
 
195
    @SuppressWarnings( "unchecked" )
 
196
    public Collection<DataValue> getAllDataValues()
 
197
    {
 
198
        Session session = sessionManager.getCurrentSession();
 
199
 
 
200
        Criteria criteria = session.createCriteria( DataValue.class );
 
201
 
 
202
        return criteria.list();
 
203
    }
 
204
 
 
205
    @SuppressWarnings( "unchecked" )
 
206
    public Collection<DataValue> getDataValues( Source source, Period period )
 
207
    {
 
208
        Period storedPeriod = reloadPeriod( period );
 
209
 
 
210
        if ( storedPeriod == null )
 
211
        {
 
212
            return Collections.emptySet();
 
213
        }
 
214
 
 
215
        Session session = sessionManager.getCurrentSession();
 
216
 
 
217
        Criteria criteria = session.createCriteria( DataValue.class );
 
218
        criteria.add( Restrictions.eq( "source", source ) );
 
219
        criteria.add( Restrictions.eq( "period", storedPeriod ) );
 
220
 
 
221
        return criteria.list();
 
222
    }
 
223
 
 
224
    @SuppressWarnings( "unchecked" )
 
225
    public Collection<DataValue> getDataValues( Source source, DataElement dataElement )
 
226
    {
 
227
        Session session = sessionManager.getCurrentSession();
 
228
 
 
229
        Criteria criteria = session.createCriteria( DataValue.class );
 
230
        criteria.add( Restrictions.eq( "source", source ) );
 
231
        criteria.add( Restrictions.eq( "dataElement", dataElement ) );
 
232
 
 
233
        return criteria.list();
 
234
    }
 
235
 
 
236
    @SuppressWarnings( "unchecked" )
 
237
    public Collection<DataValue> getDataValues( Collection<? extends Source> sources, DataElement dataElement )
 
238
    {
 
239
        Session session = sessionManager.getCurrentSession();
 
240
 
 
241
        Criteria criteria = session.createCriteria( DataValue.class );
 
242
        criteria.add( Restrictions.in( "source", sources ) );
 
243
        criteria.add( Restrictions.eq( "dataElement", dataElement ) );
 
244
 
 
245
        return criteria.list();
 
246
    }
 
247
 
 
248
    @SuppressWarnings( "unchecked" )
 
249
    public Collection<DataValue> getDataValues( Source source, Period period, Collection<DataElement> dataElements )
 
250
    {
 
251
        Period storedPeriod = reloadPeriod( period );
 
252
 
 
253
        if ( storedPeriod == null )
 
254
        {
 
255
            return Collections.emptySet();
 
256
        }
 
257
 
 
258
        Session session = sessionManager.getCurrentSession();
 
259
 
 
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 ) );
 
264
 
 
265
        return criteria.list();
 
266
    }
 
267
    
 
268
    @SuppressWarnings( "unchecked" )
 
269
    public Collection<DataValue> getDataValues( Source source, Period period, Collection<DataElement> dataElements, Collection<DataElementCategoryOptionCombo> optionCombos  )
 
270
    {
 
271
        Period storedPeriod = reloadPeriod( period );
 
272
 
 
273
        if ( storedPeriod == null )
 
274
        {
 
275
            return Collections.emptySet();
 
276
        }
 
277
 
 
278
        Session session = sessionManager.getCurrentSession();
 
279
 
 
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 ) );
 
285
 
 
286
        return criteria.list();
 
287
    }
 
288
 
 
289
    @SuppressWarnings( "unchecked" )
 
290
    public Collection<DataValue> getDataValues( DataElement dataElement, Collection<Period> periods,
 
291
        Collection<? extends Source> sources )
 
292
    {
 
293
        Collection<Period> storedPeriods = new ArrayList<Period>();
 
294
 
 
295
        for ( Period period : periods )
 
296
        {
 
297
            Period storedPeriod = reloadPeriod( period );
 
298
 
 
299
            if ( storedPeriod != null )
 
300
            {
 
301
                storedPeriods.add( storedPeriod );
 
302
            }
 
303
        }
 
304
 
 
305
        Session session = sessionManager.getCurrentSession();
 
306
 
 
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 ) );
 
311
 
 
312
        return criteria.list();
 
313
    }
 
314
 
 
315
    @SuppressWarnings( "unchecked" )
 
316
    public Collection<DataValue> getDataValues( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, 
 
317
        Collection<Period> periods, Collection<? extends Source> sources )
 
318
    {
 
319
        Collection<Period> storedPeriods = new ArrayList<Period>();
 
320
 
 
321
        for ( Period period : periods )
 
322
        {
 
323
            Period storedPeriod = reloadPeriod( period );
 
324
 
 
325
            if ( storedPeriod != null )
 
326
            {
 
327
                storedPeriods.add( storedPeriod );
 
328
            }
 
329
        }
 
330
 
 
331
        Session session = sessionManager.getCurrentSession();
 
332
 
 
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 ) );
 
338
 
 
339
        return criteria.list();
 
340
    }
 
341
    
 
342
    @SuppressWarnings( "unchecked" )
 
343
    public Collection<DataValue> getDataValues( Collection<DataElement> dataElements, Collection<Period> periods,
 
344
        Collection<? extends Source> sources, int firstResult, int maxResults )
 
345
    {
 
346
        Collection<Period> storedPeriods = new ArrayList<Period>();
 
347
 
 
348
        for ( Period period : periods )
 
349
        {
 
350
            Period storedPeriod = reloadPeriod( period );
 
351
 
 
352
            if ( storedPeriod != null )
 
353
            {
 
354
                storedPeriods.add( storedPeriod );
 
355
            }
 
356
        }
 
357
 
 
358
        if ( storedPeriods.size() == 0 )
 
359
        {
 
360
            return Collections.emptySet();
 
361
        }
 
362
 
 
363
        Session session = sessionManager.getCurrentSession();
 
364
 
 
365
        Criteria criteria = session.createCriteria( DataValue.class );
 
366
 
 
367
        criteria.add( Restrictions.in( "dataElement", dataElements ) );
 
368
        criteria.add( Restrictions.in( "period", storedPeriods ) );
 
369
        criteria.add( Restrictions.in( "source", sources ) );
 
370
 
 
371
        if ( maxResults != 0 )
 
372
        {
 
373
            criteria.addOrder( Order.asc( "dataElement" ) );
 
374
            criteria.addOrder( Order.asc( "period" ) );
 
375
            criteria.addOrder( Order.asc( "source" ) );
 
376
    
 
377
            criteria.setFirstResult( firstResult );
 
378
            criteria.setMaxResults( maxResults );
 
379
        }
 
380
 
 
381
        return criteria.list();
 
382
    }
 
383
    
 
384
    @SuppressWarnings( "unchecked" )
 
385
    public Collection<DataValue> getDataValues( Collection<DataElementCategoryOptionCombo> optionCombos )
 
386
    {
 
387
        Session session = sessionManager.getCurrentSession();
 
388
 
 
389
        Criteria criteria = session.createCriteria( DataValue.class );        
 
390
        criteria.add( Restrictions.in( "optionCombo", optionCombos ) );
 
391
 
 
392
        return criteria.list();
 
393
    }
 
394
    
 
395
    @SuppressWarnings( "unchecked" )
 
396
    public Collection<DataValue> getDataValues( DataElement dataElement )
 
397
    {
 
398
        Session session = sessionManager.getCurrentSession();
 
399
 
 
400
        Criteria criteria = session.createCriteria( DataValue.class );        
 
401
        criteria.add( Restrictions.eq( "dataElement", dataElement ) );
 
402
 
 
403
        return criteria.list();
 
404
    }
 
405
}