1
package org.hisp.dhis.databrowser;
4
* Copyright (c) 2004-${year}, 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.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.util.List;
33
import java.util.Vector;
36
* @author Joakim Bj�rnstad
39
public class DataBrowserTable
42
* A List of List with integers to simulate a 2D array.
44
private List<List<Integer>> counts = new Vector<List<Integer>>();
47
* A List of the MetaValues for columns.
49
private List<MetaValue> columnMeta = new Vector<MetaValue>();
52
* A List of the MetaValues for rows.
54
private List<MetaValue> rowMeta = new Vector<MetaValue>();
57
* Metadata - this value will hold how long the query took in the Store
60
private long queryTime = 0;
63
* Metadata - this value will hold number of queries this DataBrowserTable
66
private int queryCount = 0;
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
74
* index 1 = id index 2 = name
76
* @param resultSet the SQL ResultSet
78
public void createStructure( ResultSet resultSet )
82
while ( resultSet.next() )
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 );
91
catch ( SQLException e )
99
* Adds one Column to the table structure.
101
* ResultSet contents: index 1: Id index 2: Name index 3: Count
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)
108
* @param resultSet the SQL ResultSet
109
* @return 0 if ResultSet was empty else number of rows inserted with
112
public Integer addColumnToAllRows( ResultSet resultSet )
114
Integer countRows = 0;
117
if ( resultSet.first() != true )
121
resultSet.beforeFirst();
122
for ( List<Integer> rowItem : counts )
127
while ( resultSet.next() )
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 );
138
catch ( SQLException e )
147
* Adds a name to a column
149
* @param name The name of the column
151
public void addColumnName( String name )
153
columnMeta.add( new MetaValue( name ) );
157
* Adds a name and metadata to column
159
* @param name The name of the column
160
* @param metaValue Metadata of the column
162
public void addColumnName( String name, String metaValue )
164
columnMeta.add( new MetaValue( name, metaValue ) );
168
* Adds a name to a row
170
* @param name The name of the row
172
public void addRowName( String name )
174
MetaValue mv = new MetaValue();
180
* Adds id and name to column metadata
182
* @param id The id of the row
183
* @param name The name of the row
185
public void addRowNameAndId( Integer id, String name )
187
rowMeta.add( new MetaValue( id, name ) );
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.
194
* @param rowName the rowName to check
195
* @return index in rowMeta
197
public List<Integer> getRowBasedOnRowName( String rowName )
199
int rowIndex = rowMeta.indexOf( new MetaValue( rowName ) );
200
return counts.get( rowIndex );
205
* Finds the count value of x,y count.
213
public Integer getCountFromRowAndColumnIndex( int x, int y )
215
return counts.get( x ).get( y );
218
public long getQueryTime()
223
public void setQueryTime( long queryTime )
225
this.queryTime = queryTime;
228
public void addQueryTime(long queryTime)
230
this.queryTime += queryTime;
233
public List<List<Integer>> getCounts()
238
public void setCounts( List<List<Integer>> counts )
240
this.counts = counts;
243
public List<MetaValue> getColumns()
248
public void setColumnNames( List<MetaValue> columnMeta )
250
this.columnMeta = columnMeta;
253
public List<MetaValue> getRows()
258
public void setRowNames( List<MetaValue> rowMeta )
260
this.rowMeta = rowMeta;
263
public int getQueryCount()
269
* Helper method to increase queryCount by one.
271
public void incrementQueryCount()