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

« back to all changes in this revision

Viewing changes to local/in/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/jdbc/JdbcAggregationStore.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.aggregation.jdbc;
 
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.sql.ResultSet;
 
31
import java.sql.SQLException;
 
32
import java.util.ArrayList;
 
33
import java.util.Collection;
 
34
import java.util.HashSet;
 
35
 
 
36
import org.hisp.dhis.aggregation.AggregationStore;
 
37
import org.hisp.dhis.jdbc.StatementHolder;
 
38
import org.hisp.dhis.jdbc.StatementManager;
 
39
import org.hisp.dhis.datavalue.DataValue;
 
40
import org.hisp.dhis.period.Period;
 
41
 
 
42
/**
 
43
 * @author Lars Helge Overland
 
44
 * @version $Id: JdbcAggregationStore.java 5942 2008-10-16 15:44:57Z larshelg $
 
45
 */
 
46
public class JdbcAggregationStore
 
47
    implements AggregationStore
 
48
{
 
49
    // ----------------------------------------------------------------------
 
50
    // Dependencies
 
51
    // ----------------------------------------------------------------------
 
52
 
 
53
    private StatementManager statementManager;
 
54
 
 
55
    public void setStatementManager( StatementManager statementManager )
 
56
    {
 
57
        this.statementManager = statementManager;
 
58
    }
 
59
    
 
60
    // ----------------------------------------------------------------------
 
61
    // DataValue
 
62
    // ----------------------------------------------------------------------
 
63
    
 
64
    public Collection<DataValue> getDataValues( Collection<Integer> sourceIds, int dataElementId, int optionComboId, Collection<Integer> periodIds )
 
65
    {
 
66
        if ( sourceIds != null && sourceIds.size() > 0 && periodIds != null && periodIds.size() > 0 )
 
67
        {
 
68
            StatementHolder holder = statementManager.getHolder();
 
69
                
 
70
            try
 
71
            {
 
72
                String sql = 
 
73
                    "SELECT periodid, value " +
 
74
                    "FROM datavalue " +
 
75
                    "WHERE dataelementid = " + dataElementId + " " +
 
76
                    "AND categoryoptioncomboid = " + optionComboId + " " +
 
77
                    "AND periodid IN ( " + getCommaDelimitedString( periodIds ) + " ) " +
 
78
                    "AND sourceid IN ( " + getCommaDelimitedString( sourceIds ) + " )";                 
 
79
                
 
80
                ResultSet resultSet = holder.getStatement().executeQuery( sql );
 
81
                
 
82
                return getDataValues( resultSet );             
 
83
            }
 
84
            catch ( Exception ex )
 
85
            {
 
86
                throw new RuntimeException( "Failed to get DataValues", ex );
 
87
            }
 
88
            finally
 
89
            {
 
90
                holder.close();                 
 
91
            }
 
92
        }
 
93
        
 
94
        return new ArrayList<DataValue>();
 
95
    }
 
96
    
 
97
    public Collection<DataValue> getDataValues( int sourceId, int dataElementId, int optionComboId, Collection<Integer> periodIds )
 
98
    {
 
99
        if ( periodIds != null && periodIds.size() > 0 )
 
100
        {
 
101
            StatementHolder holder = statementManager.getHolder();
 
102
            
 
103
            try
 
104
            {
 
105
                String sql = 
 
106
                    "SELECT periodid, value " +
 
107
                    "FROM datavalue " +
 
108
                    "WHERE dataelementid = " + dataElementId + " " +
 
109
                    "AND categoryoptioncomboid = " + optionComboId + " " +
 
110
                    "AND periodid IN ( " + getCommaDelimitedString( periodIds ) + " ) " +
 
111
                    "AND sourceid = " + sourceId;
 
112
 
 
113
                ResultSet resultSet = holder.getStatement().executeQuery( sql );
 
114
                
 
115
                return getDataValues( resultSet );
 
116
            }
 
117
            catch ( SQLException ex )
 
118
            {
 
119
                throw new RuntimeException( "Failed to get DataValues", ex );
 
120
            }
 
121
            finally
 
122
            {
 
123
                holder.close();
 
124
            }
 
125
        }
 
126
        
 
127
        return new ArrayList<DataValue>();
 
128
    }
 
129
    
 
130
    public Collection<String> getDataValueIdentifiers()
 
131
    {
 
132
        int min = 0;
 
133
        
 
134
        final int limit = 10000;
 
135
        
 
136
        Collection<String> identifiers = new HashSet<String>();
 
137
        
 
138
        Collection<String> temp = null;
 
139
        
 
140
        while ( ( temp = getDataValueIdentifiers( min, limit ) ).size() > 0 )
 
141
        {
 
142
            identifiers.addAll( temp );
 
143
            
 
144
            min += limit;
 
145
        }
 
146
        
 
147
        return identifiers;
 
148
    }
 
149
    
 
150
    private Collection<String> getDataValueIdentifiers( int min, int limit )
 
151
    {
 
152
        StatementHolder holder = statementManager.getHolder();
 
153
        
 
154
        try
 
155
        {
 
156
            String sql = 
 
157
                "SELECT dataelementid, periodid, sourceid " +
 
158
                "FROM datavalue " +
 
159
                "ORDER BY dataelementid, periodid, sourceid " +
 
160
                "LIMIT " + min + ", " + limit;
 
161
            
 
162
            ResultSet resultSet = holder.getStatement().executeQuery( sql );
 
163
            
 
164
            Collection<String> identifiers = new HashSet<String>();
 
165
            
 
166
            while ( resultSet.next() )
 
167
            {
 
168
                identifiers.add( resultSet.getInt( 1 ) + "-" + resultSet.getInt( 2 ) + "-" + resultSet.getInt( 3 ) );
 
169
            }
 
170
            
 
171
            return identifiers;
 
172
        }
 
173
        catch ( SQLException ex )
 
174
        {
 
175
            throw new RuntimeException( "Failed to get DataValue identifiers", ex );
 
176
        }
 
177
        finally
 
178
        {
 
179
            holder.close();
 
180
        }
 
181
    }
 
182
    
 
183
    // ----------------------------------------------------------------------
 
184
    // Supportive methods
 
185
    // ----------------------------------------------------------------------
 
186
 
 
187
    private Collection<DataValue> getDataValues( ResultSet resultSet )
 
188
    {
 
189
        try
 
190
        {
 
191
            Collection<DataValue> list = new ArrayList<DataValue>();
 
192
            
 
193
            while ( resultSet.next() )
 
194
            {
 
195
                Period period = new Period();
 
196
                
 
197
                period.setId( Integer.parseInt( resultSet.getString( 1 ) ) );
 
198
                
 
199
                DataValue dataValue = new DataValue();
 
200
                
 
201
                dataValue.setPeriod( period );
 
202
                dataValue.setValue( resultSet.getString( 2 ) );
 
203
                
 
204
                list.add( dataValue );
 
205
            }
 
206
            
 
207
            return list;
 
208
        }
 
209
        catch ( SQLException ex )
 
210
        {
 
211
            throw new RuntimeException( "Failed to transform resultset into collection", ex );
 
212
        }
 
213
    }   
 
214
    
 
215
    private String getCommaDelimitedString( Collection<Integer> elements )
 
216
    {
 
217
        if ( elements != null && elements.size() > 0 )
 
218
        {
 
219
            StringBuffer sourceSqlBuffer = new StringBuffer();        
 
220
        
 
221
            for ( Integer element : elements )
 
222
            {
 
223
                sourceSqlBuffer.append( element.toString() + ", " );
 
224
            }
 
225
            
 
226
            return sourceSqlBuffer.substring( 0, sourceSqlBuffer.length() - ", ".length() );
 
227
        }
 
228
        
 
229
        return null;
 
230
    }
 
231
}