Source code: org/acs/damsel/srvr/db/Table.java
1 package org.acs.damsel.srvr.db;
2
3 import java.util.*;
4
5 /**
6 * <p>Title: Abstracts SQL query result tables</p>
7 * <p>Description: This class is used to handle the results of SQL queries
8 * made on the database. It can be used to process data in from the results
9 * of a table or out into methods using the results data. The entries in
10 * the table can also be sorted and accessed by rows, columns, or individual
11 * cells.</p>
12 * @version 1.0
13 */
14 public class Table {
15 Vector results;
16 Vector metaData;
17
18 public Table() {}
19
20 /**
21 * Returns a specific element from the result set given row and column indices.
22 * @param row int specifying horizontal location of element
23 * @param column int specifying vertical location of element
24 * @return String, the value of the specified element. null if either of the
25 * indices are out of bounds.
26 */
27 public String getResultsElement(int row, int column) {
28 String str;
29 try {
30 str = (String) ( (Vector) results.get(row)).get(column);
31 }
32 catch (Exception ex) {
33 return null;
34 }
35 return str;
36 }
37
38 /**
39 * Returns the specified element from the metaData list (i.e., the list of
40 * column names).
41 * @param index of the metadata element to be retrieved
42 * @return String, the specified column name. null if the index is out of
43 * bounds.
44 */
45 public String getMetaDataElement(int index) {
46 String str;
47 try {
48 str = (String) metaData.get(index);
49 }
50 catch (Exception ex) {
51 return null;
52 }
53 return str;
54 }
55
56 /**
57 * Method returns the number of rows in the table.
58 * @return int, number of rows in table
59 */
60 public int getRowCount() {
61 return results.size();
62 }
63
64 /**
65 * Method returns the number of column in the table.
66 * @return int, number of columns in table
67 */
68 public int getColCount() {
69 int result = -1;
70 try {
71 result = ((Vector) results.firstElement()).size();
72 }
73 catch (Exception ex) {
74 return -1;
75 }
76 return result;
77 }
78
79 /**
80 * The method isInResults takes in the name of a tag and an item to
81 * search for and returns true if the item is under that tag
82 * or false otherwise. For example, if you added an asset to the
83 * database with the title of "tester", you could call
84 * isInResults("Title","tester") and it would return true.
85 * @param tag String containing the field to be search
86 * @param searchItem String containing the value to be searched for
87 * @return boolean
88 */
89 public boolean isInResults(String tag, String searchItem) {
90 if (!isEmpty()) {
91 int colNum = 0;
92 boolean checkString = false;
93 for (int k = 0; k < getSizeMetaData(); k++) {
94 if (metaData.elementAt(k).equals(tag)) {
95 colNum = k;
96 }
97 }
98 for (int j = 0; j < getRowCount(); j++) {
99 if (getResultsElement(j,colNum).equals(searchItem)) {
100 checkString = true;
101 }
102 }
103 return checkString;
104 }
105 else {
106 return false;
107 }
108 }
109
110 /**
111 * Method returns the size of the vector containing the metadata elements
112 * stored in the table.
113 * @return int, size of metaData vector
114 */
115 public int getSizeMetaData() {
116 return metaData.size();
117 }
118
119 /**
120 * Method returns true if the table does not contain any elements, false
121 * otherwise.
122 * @return boolean
123 */
124 public boolean isEmpty() {
125 return results.size() == 0;
126 }
127
128 /**
129 * Method returns a vector of the specified row number
130 * @param index row number of the row to be retrieved
131 * @return Vector representing the contents of the specified row or null if
132 * the index is greater than the number of rows
133 * michelle S. and christy
134 */
135 public Vector getRow(int index) {
136 if(index >= results.size())
137 return null;
138 return (Vector) results.get(index);
139 }
140
141 /**
142 * Method returns vector of the specified column number
143 * @param index column number of the column to be retrieved
144 * @return Vector representing contents of specified column or null if index
145 * is greater than the number of columns
146 * michelle s. and christy
147 */
148 public Vector getCol(int index) {
149 Vector data = new Vector();
150 Vector temp = new Vector();
151 if (index >= metaData.size()) {
152 return null;
153 }
154 for (int i = 0; i < results.size(); i++) {
155 temp = (Vector) results.elementAt(i);
156 data.add(temp.elementAt(index));
157 }
158 return data;
159 }
160
161 /**
162 * Method returns vector of specified column name.
163 * @param colName String containing the name of the column
164 * @return Vector representing the contents of specified column, or null if
165 * colName doesn't exist.
166 * michelle s. and christy
167 */
168 public Vector getCol(String colName) {
169 Vector data = new Vector();
170 Vector temp = new Vector();
171 int index = 0;
172 boolean found = false;
173 while ( (!found) && (index < metaData.size())) {
174 if ((metaData.elementAt(index)).equals(colName)) {
175 found = true;
176 }
177 else {
178 index++;
179 }
180 }
181 if (!found) {
182 return null;
183 }
184 for (int i = 0; i < results.size(); i++) {
185 temp = (Vector) results.elementAt(i);
186 data.add(temp.elementAt(index));
187 }
188 return data;
189 }
190
191 /**
192 * Orders this table by the specified tagName
193 * @param tagName the tagname to order by
194 * @throws TagNameNotFoundException if the specified tagName is not in this table's
195 * metadata
196 * @todo use faster sorting algorithm
197 */
198 public void orderBy(String tagName) throws TagNameNotFoundException{
199 /*Find the index to the specified tagName in the first row (metadata tags) */
200 int colNum = this.getMetaData().indexOf(tagName);
201
202 /*Throw a TagNameNotFoundException if the tag name does not exist in this table */
203 if (colNum == -1) {
204 throw new TagNameNotFoundException("Tag Name does not exist in this Table.");
205 }
206 /*Uses the select sort method for sorting... more efficient sort methods could be used
207 in the future*/
208 this.selectSort(colNum);
209 }
210
211 /* getters and setters */
212 public Vector getMetaData() {
213 return metaData;
214 }
215 public Vector getResults() {
216 return results;
217 }
218 public void setMetaData(Vector metaData) {
219 this.metaData = metaData;
220 }
221 public void setResults(Vector results) {
222 this.results = results;
223 }
224
225 /*Sorts using the select sort method */
226 private void selectSort(int colNum) {
227 if (this != null) {
228 Vector temp;
229 String stringToCompare;
230 for (int j = 0; j < this.getRowCount() - 1; j++) {
231 stringToCompare = this.getResultsElement(j, colNum);
232 for (int i = j + 1; i < this.getRowCount(); i++) {
233 if ( (this.getResultsElement(i, colNum) != null) &&
234 (this.getResultsElement(i,
235 colNum).toUpperCase().compareTo(stringToCompare.toUpperCase()) <
236 0)) {
237 temp = (Vector) results.elementAt(j);
238 results.setElementAt(results.elementAt(i), j);
239 results.setElementAt(temp, i);
240 stringToCompare = this.getResultsElement(j, colNum);
241 } // end of if statement
242 } // end of inner for loop
243 } // end of outer for loop
244 } // end of if not null statement
245 } // end of method select sort
246 } // end of Table Class