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

« back to all changes in this revision

Viewing changes to local/in/dhis-web-integration/src/main/java/org/hisp/dhis/integration/rims/util/RIMSTableAdapter.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
/*
 
2
 * Copyright (c) 2004-2008, University of Oslo
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 * * Redistributions of source code must retain the above copyright notice, this
 
8
 *   list of conditions and the following disclaimer.
 
9
 * * Redistributions in binary form must reproduce the above copyright notice,
 
10
 *   this list of conditions and the following disclaimer in the documentation
 
11
 *   and/or other materials provided with the distribution.
 
12
 * * Neither the name of the HISP project nor the names of its contributors may
 
13
 *   be used to endorse or promote products derived from this software without
 
14
 *   specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
23
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
package org.hisp.dhis.integration.rims.util;
 
28
 
 
29
import java.sql.Connection;
 
30
import java.sql.PreparedStatement;
 
31
import java.sql.ResultSet;
 
32
import java.sql.ResultSetMetaData;
 
33
import java.sql.SQLException;
 
34
import java.sql.Statement;
 
35
import java.util.HashMap;
 
36
import java.util.Map;
 
37
 
 
38
import org.hisp.dhis.integration.rims.api.RIMSTable;
 
39
import org.hisp.dhis.integration.rims.api.RIMS_Mapping_DataElement;
 
40
import org.hisp.dhis.integration.rims.api.tables.RIMSDistrict;
 
41
import org.hisp.dhis.integration.rims.api.tables.RIMSOrgUnit;
 
42
import org.hisp.dhis.integration.rims.api.tables.RIMS_PHC;
 
43
 
 
44
/**
 
45
 * Imported entries are cached, so take care with reusing instances of this class.
 
46
 * @author Leif Arne Storset
 
47
 * @version $Id$
 
48
 */
 
49
public abstract class RIMSTableAdapter
 
50
    implements RIMSTable
 
51
{
 
52
    private Configuration config;
 
53
    private Connection conn;
 
54
 
 
55
    /**
 
56
     * Get the current connection instance, creating one if necessary.
 
57
     * 
 
58
     * @return
 
59
     * @throws SQLException if the connection could not be created.
 
60
     */
 
61
    private Connection getConnection() throws SQLException
 
62
    {
 
63
        if ( conn == null )
 
64
        {
 
65
            conn = config.getConnection();
 
66
            // Hopefully this will prevent SQLExceptions such as 
 
67
            // "Could not update; currently locked by user 'admin'"
 
68
            conn.setTransactionIsolation( Connection.TRANSACTION_NONE );
 
69
        }
 
70
        return conn;
 
71
    }
 
72
    
 
73
    public void setConnection( Connection conn )
 
74
    {
 
75
        this.conn = conn;
 
76
    }
 
77
    
 
78
    public boolean isData( RIMSOrgUnit orgUnit, int month, int year,
 
79
        RIMS_Mapping_DataElement mappingDataElement )
 
80
        throws SQLException
 
81
    {
 
82
        if ( orgUnit instanceof RIMS_PHC )
 
83
        {
 
84
            return isData( (RIMS_PHC) orgUnit, month, year, mappingDataElement );
 
85
        }
 
86
        else if ( orgUnit instanceof RIMSDistrict )
 
87
        {
 
88
            return isData( (RIMSDistrict) orgUnit, month, year, mappingDataElement );
 
89
        }
 
90
        else
 
91
        {
 
92
            throw new IllegalArgumentException ( "orgUnit must be one of" +
 
93
                        " RIMS_PHC and RIMSDistrict" );
 
94
        }
 
95
    }
 
96
    
 
97
    public abstract boolean isData( RIMS_PHC phc, int month, int year,
 
98
        RIMS_Mapping_DataElement mappingDataElement ) throws SQLException;
 
99
    
 
100
    public abstract boolean isData( RIMSDistrict district, int month, int year,
 
101
        RIMS_Mapping_DataElement mappingDataElement ) throws SQLException;
 
102
 
 
103
    public int insertData( RIMSOrgUnit orgUnit, int month, int year,
 
104
        RIMS_Mapping_DataElement mappingDataElement, String value )
 
105
        throws SQLException
 
106
    {
 
107
        if ( orgUnit instanceof RIMS_PHC )
 
108
        {
 
109
            return insertData( (RIMS_PHC) orgUnit, month, year, mappingDataElement, value );
 
110
        }
 
111
        else if ( orgUnit instanceof RIMSDistrict )
 
112
        {
 
113
            return insertData( (RIMSDistrict) orgUnit, month, year, mappingDataElement, value );
 
114
        }
 
115
        else
 
116
        {
 
117
            throw new IllegalArgumentException ( "orgUnit must be one of" +
 
118
                        " RIMS_PHC and RIMSDistrict" );
 
119
        }
 
120
    }
 
121
    
 
122
    public abstract int insertData( RIMS_PHC phc, int month, int year,
 
123
        RIMS_Mapping_DataElement mappingDataElement, String value ) throws SQLException;
 
124
    
 
125
    public abstract int insertData( RIMSDistrict district, int month, int year,
 
126
        RIMS_Mapping_DataElement mappingDataElement, String value ) throws SQLException;
 
127
    
 
128
    public int updateData( RIMSOrgUnit orgUnit, int month, int year,
 
129
        RIMS_Mapping_DataElement mappingDataElement, String value )
 
130
        throws SQLException
 
131
    {
 
132
        if ( orgUnit instanceof RIMS_PHC )
 
133
        {
 
134
            return updateData( (RIMS_PHC) orgUnit, month, year, mappingDataElement, value );
 
135
        }
 
136
        else if ( orgUnit instanceof RIMSDistrict )
 
137
        {
 
138
            return updateData( (RIMSDistrict) orgUnit, month, year, mappingDataElement, value );
 
139
        }
 
140
        else
 
141
        {
 
142
            throw new IllegalArgumentException ( "orgUnit must be one of" +
 
143
                        " RIMS_PHC and RIMSDistrict" );
 
144
        }
 
145
    }
 
146
    
 
147
    public abstract int updateData( RIMS_PHC phc, int month, int year,
 
148
        RIMS_Mapping_DataElement mappingDataElement, String value ) throws SQLException;
 
149
    
 
150
    public abstract int updateData( RIMSDistrict district, int month, int year,
 
151
        RIMS_Mapping_DataElement mappingDataElement, String value ) throws SQLException;
 
152
    
 
153
    
 
154
    protected int executeUpdate( String query ) throws SQLException
 
155
    {        
 
156
        return executeUpdate( query, null );
 
157
    }
 
158
    
 
159
    public String getDataValue( RIMS_Mapping_DataElement mappingDataElement, RIMSOrgUnit orgUnit, int month, int year ) throws SQLException
 
160
    {
 
161
        if ( orgUnit instanceof RIMS_PHC )
 
162
        {
 
163
            return getDataValue( mappingDataElement, (RIMS_PHC) orgUnit, month, year );
 
164
        }
 
165
        else if ( orgUnit instanceof RIMSDistrict )
 
166
        {
 
167
            return getDataValue( mappingDataElement, (RIMSDistrict) orgUnit, month, year );
 
168
        }
 
169
        else
 
170
        {
 
171
            throw new IllegalArgumentException ( "orgUnit must be one of" +
 
172
                        " RIMS_PHC and RIMSDistrict" );
 
173
        }
 
174
    }
 
175
    
 
176
    public abstract String getDataValue( RIMS_Mapping_DataElement mappingDataElement, RIMS_PHC orgUnit, int month, int year ) throws SQLException;
 
177
    public abstract String getDataValue( RIMS_Mapping_DataElement mappingDataElement, RIMSDistrict orgUnit, int month, int year ) throws SQLException;
 
178
    
 
179
 
 
180
    /**
 
181
     * Checks if there is existing data using the provided query.
 
182
     * 
 
183
     * @param query
 
184
     * @return
 
185
     * @throws SQLException 
 
186
     */
 
187
    protected boolean existingData( String query ) throws SQLException
 
188
    {
 
189
        return existingData( query, null );
 
190
    }
 
191
 
 
192
    /**
 
193
     * Checks if there is existing data using the provided query.
 
194
     * 
 
195
     * @param query
 
196
     * @param params
 
197
     * @return
 
198
     * @throws SQLException 
 
199
     */
 
200
    protected boolean existingData( String query, Object[] params ) throws SQLException
 
201
    {
 
202
        PreparedStatement st = null;
 
203
        ResultSet rs = null;
 
204
 
 
205
        boolean res = false;
 
206
        try
 
207
        {
 
208
            st = getConnection().prepareStatement( query );
 
209
            
 
210
            if ( params != null )
 
211
            {
 
212
                for ( int i = 0; i < params.length; i++ )
 
213
                {
 
214
                    st.setObject( i + 1, params[i] );
 
215
                }
 
216
            }
 
217
            
 
218
            rs = st.executeQuery();
 
219
            if ( rs.next() )
 
220
            {
 
221
                res = true;
 
222
            }
 
223
        }
 
224
        finally
 
225
        {
 
226
            if ( rs != null )
 
227
                rs.close();
 
228
            if ( st != null )
 
229
                st.close();
 
230
        }
 
231
        return res;
 
232
    }
 
233
    
 
234
    public void finalize()
 
235
    {
 
236
 
 
237
        try
 
238
        {
 
239
            if ( conn != null && !conn.isClosed() )
 
240
            {
 
241
                conn.close();
 
242
            }
 
243
        }
 
244
        catch ( SQLException e )
 
245
        {
 
246
            // No sense in rethrowing since it is ignored anyhow
 
247
            e.printStackTrace();
 
248
        }
 
249
    }
 
250
 
 
251
    @SuppressWarnings("unused")
 
252
    private Cache cache = new Cache();
 
253
 
 
254
    private class Cache {
 
255
        /**
 
256
         *  {@code timeout} seconds after the last cache put, getting the cache
 
257
         *  will automatically clear it. 
 
258
         */ 
 
259
        private int timeout = 60;
 
260
        private long lastPut;
 
261
        
 
262
        public Cache()
 
263
        {
 
264
            init();
 
265
        }
 
266
        
 
267
        private void init()
 
268
        {
 
269
            cache = new HashMap<String, Object[]>();
 
270
            columns = null;
 
271
        }
 
272
        
 
273
        @SuppressWarnings("hiding")
 
274
        private HashMap<String, Object[]> cache;
 
275
        private String[] columns;
 
276
        
 
277
        private String cacheKey( String query, Object[] params )
 
278
        {
 
279
            StringBuffer sb = new StringBuffer();
 
280
            sb.append( query ).append( '\n' );
 
281
            for( Object param: params )
 
282
            {
 
283
                sb.append( param.toString() ).append( '\n' );
 
284
            }
 
285
            return sb.toString();
 
286
        }
 
287
        
 
288
        /**
 
289
         * Inserts into the cache. Returns a {@link Map} as if 
 
290
         * {@link #getFromCache(String, Object[])} had been called.
 
291
         * @param query
 
292
         * @param params
 
293
         * @param resultSet
 
294
         * @return
 
295
         */
 
296
        @SuppressWarnings("unused")
 
297
        public Map<String, String> putInCache( String query, Object[] params, ResultSet resultSet )
 
298
        {
 
299
            lastPut = System.currentTimeMillis();
 
300
            Object[] row = null;
 
301
            try
 
302
            {
 
303
                if ( columns == null )
 
304
                {
 
305
                    ResultSetMetaData meta = resultSet.getMetaData();
 
306
                    columns = new String[ meta.getColumnCount() ];
 
307
                    for( int i = 0; i < columns.length; i++ )
 
308
                    {
 
309
                        columns[i] = meta.getColumnName( i + 1 );
 
310
                    }
 
311
                }
 
312
                
 
313
                row = new Object[ columns.length ];
 
314
                for( int i = 0; i < row.length; i++ )
 
315
                {
 
316
                    row[i] = resultSet.getObject( i + 1 );  
 
317
                    if ( resultSet.wasNull() )
 
318
                    {
 
319
                        row[i] = null;
 
320
                    }
 
321
                }
 
322
 
 
323
                cache.put( cacheKey( query, params ), row );
 
324
            }
 
325
            catch ( SQLException e )
 
326
            {
 
327
                // TODO Auto-generated catch block
 
328
                e.printStackTrace();
 
329
            }
 
330
 
 
331
            return createMap( row );
 
332
        }
 
333
        
 
334
        @SuppressWarnings("unused")
 
335
        public Map<String, String> getFromCache( String query, Object[] params )
 
336
        {
 
337
            // Clear the cache if we've gone past timeout.
 
338
            if ( System.currentTimeMillis() - lastPut > timeout * 1000 )
 
339
            {
 
340
                init();
 
341
            }
 
342
            
 
343
            // Get the cache key
 
344
            Object[] row = cache.get( cacheKey( query, params ) );
 
345
            if ( row == null )
 
346
            {
 
347
                return null;
 
348
            }
 
349
            return createMap( row );
 
350
        }
 
351
 
 
352
        private Map<String, String> createMap( Object[] row )
 
353
        {
 
354
            Map<String, String> ret = new HashMap<String, String>( row.length );
 
355
            for ( int i = 0; i < row.length; i++ )
 
356
            {
 
357
                ret.put( columns[i], row[i] + "" );
 
358
            }
 
359
            return ret;
 
360
        }
 
361
 
 
362
        public int getTimeout()
 
363
        {
 
364
            return timeout;
 
365
        }
 
366
 
 
367
        public void setTimeout( int timeout )
 
368
        {
 
369
            this.timeout = timeout;
 
370
        }
 
371
    }
 
372
    
 
373
    /**
 
374
     * Executes the given query with the given parameters.
 
375
     * 
 
376
     * @param query the query to execute.
 
377
     * @param getColumn the column to read.
 
378
     * @param params the parameters to replace with.
 
379
     * @return the value of the given column, or null if it is NULL or no rows
 
380
     *         returned.
 
381
     * @throws SQLException
 
382
     */
 
383
    protected String executeQuery( String query, String getColumn, Object[] params ) throws SQLException
 
384
    {
 
385
        PreparedStatement st = null;
 
386
        ResultSet resultSet = null;
 
387
        Object rimsValue = null;
 
388
        
 
389
        /*
 
390
        Map<String, String> cachehit = cache.getFromCache( query, params );
 
391
        if ( cachehit != null )
 
392
        {
 
393
            return cachehit.get(  getColumn ) + "";
 
394
        }
 
395
        */
 
396
    
 
397
        try
 
398
        {
 
399
            st = getConnection().prepareStatement( query );
 
400
            
 
401
            for( int i = 0; i < params.length; i++ )
 
402
            {
 
403
                st.setObject( i + 1, params[i] );
 
404
            }
 
405
            
 
406
            resultSet = st.executeQuery();
 
407
            if( !resultSet.next() )
 
408
            {
 
409
                return null;
 
410
            }
 
411
            rimsValue = resultSet.getObject( getColumn );
 
412
            if (resultSet.wasNull())
 
413
            {
 
414
                return null;
 
415
            }
 
416
            
 
417
            //cache.putInCache( query, params, resultSet );
 
418
            // Must cache first since the ResultSet only supports getting data once.
 
419
            //rimsValue = cache.putInCache( query, params, resultSet ).get( "getColumn" );
 
420
        }
 
421
        finally
 
422
        {
 
423
            if ( resultSet != null )
 
424
                resultSet.close();
 
425
            if ( st != null )
 
426
                st.close();
 
427
        }
 
428
    
 
429
        return rimsValue +"";
 
430
    }
 
431
    
 
432
    /**
 
433
     * Executes the given update with the given parameters.
 
434
     * 
 
435
     * @param update the update to execute.
 
436
     * @param params the parameters to replace with.
 
437
     * @return the value of the given column, or null if it is NULL.
 
438
     * @throws SQLException 
 
439
     */
 
440
    protected int executeUpdate( String update, Object[] params ) throws SQLException
 
441
    {
 
442
        PreparedStatement st = null;
 
443
        int recordCount = -1;
 
444
    
 
445
        try
 
446
        {
 
447
            st = getConnection().prepareStatement( update );
 
448
            
 
449
            if ( params != null )
 
450
            {
 
451
                for ( int i = 0; i < params.length; i++ )
 
452
                {
 
453
                    st.setObject( i + 1, params[i] );
 
454
                }
 
455
            }
 
456
            
 
457
            recordCount = st.executeUpdate();
 
458
        }
 
459
 
 
460
        finally
 
461
        {
 
462
            if ( st != null )
 
463
                st.close();
 
464
        }
 
465
    
 
466
        return recordCount;
 
467
    }
 
468
    
 
469
    public int truncate() throws SQLException
 
470
    {
 
471
        @SuppressWarnings("hiding")
 
472
        Connection conn = getConnection();
 
473
        Statement st = conn.createStatement();
 
474
        int rowCount;
 
475
        try {
 
476
            rowCount = st.executeUpdate( "DELETE FROM "+ getTableName() );
 
477
            conn.commit();
 
478
            }
 
479
        finally
 
480
        {
 
481
            if ( st != null )
 
482
            {
 
483
                st.close();
 
484
            }
 
485
        }
 
486
        return rowCount;
 
487
    }
 
488
    
 
489
    public void commit() throws SQLException
 
490
    {
 
491
        if ( conn != null )
 
492
        {
 
493
            conn.commit();
 
494
            conn.setAutoCommit( true );
 
495
        }
 
496
    }
 
497
    
 
498
    public void beginTransaction() throws SQLException
 
499
    {
 
500
        if ( conn != null )
 
501
        {
 
502
            conn.setAutoCommit( false );
 
503
        }
 
504
    }
 
505
 
 
506
    public void setConfiguration( Configuration config )
 
507
    {
 
508
        this.config = config;
 
509
    }
 
510
}