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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserTable.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.databrowser;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-${year}, 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.List;
 
33
import java.util.Vector;
 
34
 
 
35
/**
 
36
 * @author Joakim Bj�rnstad
 
37
 * @version $Id$
 
38
 */
 
39
public class DataBrowserTable
 
40
{
 
41
    /**
 
42
     * A List of List with integers to simulate a 2D array.
 
43
     */
 
44
    private List<List<Integer>> counts = new Vector<List<Integer>>();
 
45
 
 
46
    /**
 
47
     * A List of the MetaValues for columns.
 
48
     */
 
49
    private List<MetaValue> columnMeta = new Vector<MetaValue>();
 
50
 
 
51
    /**
 
52
     * A List of the MetaValues for rows.
 
53
     */
 
54
    private List<MetaValue> rowMeta = new Vector<MetaValue>();
 
55
 
 
56
    /**
 
57
     * Metadata - this value will hold how long the query took in the Store
 
58
     * layer.
 
59
     */
 
60
    private long queryTime = 0;
 
61
 
 
62
    /**
 
63
     * Metadata - this value will hold number of queries this DataBrowserTable
 
64
     * has results from.
 
65
     */
 
66
    private int queryCount = 0;
 
67
 
 
68
    /**
 
69
     * 
 
70
     * Takes a sql ResultSet and creates the structure for the DataBrowserTable.
 
71
     * Creates a new List for every Row in the ResultSet and a new MetaValue
 
72
     * row.
 
73
     * 
 
74
     * index 1 = id index 2 = name
 
75
     * 
 
76
     * @param resultSet the SQL ResultSet
 
77
     */
 
78
    public void createStructure( ResultSet resultSet )
 
79
    {
 
80
        try
 
81
        {
 
82
            while ( resultSet.next() )
 
83
            {
 
84
                Integer rowId = resultSet.getInt( 1 );
 
85
                String rowName = resultSet.getString( 2 );
 
86
                List<Integer> rowItem = new Vector<Integer>();
 
87
                counts.add( rowItem );
 
88
                addRowNameAndId( rowId, rowName );
 
89
            }
 
90
        }
 
91
        catch ( SQLException e )
 
92
        {
 
93
            e.printStackTrace();
 
94
        }
 
95
    }
 
96
 
 
97
    /**
 
98
     * 
 
99
     * Adds one Column to the table structure.
 
100
     * 
 
101
     * ResultSet contents: index 1: Id index 2: Name index 3: Count
 
102
     * 
 
103
     * Initially adds 0 to each row in the column. Looks up in RowMeta and finds
 
104
     * index based on Name. Inserts into counts based on that. If the ResultSet
 
105
     * is empty, nothing is inserted into the list. (the Period has no
 
106
     * DataValues referenced)
 
107
     * 
 
108
     * @param resultSet the SQL ResultSet
 
109
     * @return 0 if ResultSet was empty else number of rows inserted with
 
110
     *         column.
 
111
     */
 
112
    public Integer addColumnToAllRows( ResultSet resultSet )
 
113
    {
 
114
        Integer countRows = 0;
 
115
        try
 
116
        {
 
117
            if ( resultSet.first() != true )
 
118
            {
 
119
                return countRows;
 
120
            }
 
121
            resultSet.beforeFirst();
 
122
            for ( List<Integer> rowItem : counts )
 
123
            {
 
124
                rowItem.add( 0 );
 
125
            }
 
126
 
 
127
            while ( resultSet.next() )
 
128
            {
 
129
                String name = resultSet.getString( 2 );
 
130
                int value = resultSet.getInt( 3 );
 
131
                List<Integer> rowItem = getRowBasedOnRowName( name );
 
132
                rowItem.remove( rowItem.size() - 1 );
 
133
                rowItem.add( value );
 
134
                countRows++;
 
135
            }
 
136
 
 
137
        }
 
138
        catch ( SQLException e )
 
139
        {
 
140
            e.printStackTrace();
 
141
        }
 
142
 
 
143
        return countRows;
 
144
    }
 
145
 
 
146
    /**
 
147
     * Adds a name to a column
 
148
     * 
 
149
     * @param name The name of the column
 
150
     */
 
151
    public void addColumnName( String name )
 
152
    {
 
153
        columnMeta.add( new MetaValue( name ) );
 
154
    }
 
155
 
 
156
    /**
 
157
     * Adds a name and metadata to column
 
158
     * 
 
159
     * @param name The name of the column
 
160
     * @param metaValue Metadata of the column
 
161
     */
 
162
    public void addColumnName( String name, String metaValue )
 
163
    {
 
164
        columnMeta.add( new MetaValue( name, metaValue ) );
 
165
    }
 
166
 
 
167
    /**
 
168
     * Adds a name to a row
 
169
     * 
 
170
     * @param name The name of the row
 
171
     */
 
172
    public void addRowName( String name )
 
173
    {
 
174
        MetaValue mv = new MetaValue();
 
175
        mv.setName( name );
 
176
        rowMeta.add( mv );
 
177
    }
 
178
 
 
179
    /**
 
180
     * Adds id and name to column metadata
 
181
     * 
 
182
     * @param id The id of the row
 
183
     * @param name The name of the row
 
184
     */
 
185
    public void addRowNameAndId( Integer id, String name )
 
186
    {
 
187
        rowMeta.add( new MetaValue( id, name ) );
 
188
    }
 
189
 
 
190
    /**
 
191
     * Finds the index of a Row based on the name of the row, this is used to
 
192
     * insert the correct data into the correct List of Lists for count values.
 
193
     * 
 
194
     * @param rowName the rowName to check
 
195
     * @return index in rowMeta
 
196
     */
 
197
    public List<Integer> getRowBasedOnRowName( String rowName )
 
198
    {
 
199
        int rowIndex = rowMeta.indexOf( new MetaValue( rowName ) );
 
200
        return counts.get( rowIndex );
 
201
    }
 
202
 
 
203
    /**
 
204
     * 
 
205
     * Finds the count value of x,y count.
 
206
     * 
 
207
     * No error handling.
 
208
     * 
 
209
     * @param x
 
210
     * @param y
 
211
     * @return
 
212
     */
 
213
    public Integer getCountFromRowAndColumnIndex( int x, int y )
 
214
    {
 
215
        return counts.get( x ).get( y );
 
216
    }
 
217
 
 
218
    public long getQueryTime()
 
219
    {
 
220
        return queryTime;
 
221
    }
 
222
 
 
223
    public void setQueryTime( long queryTime )
 
224
    {
 
225
        this.queryTime = queryTime;
 
226
    }
 
227
    
 
228
    public void addQueryTime(long queryTime)
 
229
    {
 
230
        this.queryTime += queryTime;
 
231
    }
 
232
 
 
233
    public List<List<Integer>> getCounts()
 
234
    {
 
235
        return counts;
 
236
    }
 
237
 
 
238
    public void setCounts( List<List<Integer>> counts )
 
239
    {
 
240
        this.counts = counts;
 
241
    }
 
242
 
 
243
    public List<MetaValue> getColumns()
 
244
    {
 
245
        return columnMeta;
 
246
    }
 
247
 
 
248
    public void setColumnNames( List<MetaValue> columnMeta )
 
249
    {
 
250
        this.columnMeta = columnMeta;
 
251
    }
 
252
 
 
253
    public List<MetaValue> getRows()
 
254
    {
 
255
        return rowMeta;
 
256
    }
 
257
 
 
258
    public void setRowNames( List<MetaValue> rowMeta )
 
259
    {
 
260
        this.rowMeta = rowMeta;
 
261
    }
 
262
 
 
263
    public int getQueryCount()
 
264
    {
 
265
        return queryCount;
 
266
    }
 
267
 
 
268
    /**
 
269
     * Helper method to increase queryCount by one.
 
270
     */
 
271
    public void incrementQueryCount()
 
272
    {
 
273
        queryCount++;
 
274
    }
 
275
 
 
276
}