KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > config > ResultSetVector


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/ResultSetVector.java,v 1.21 2005/02/19 21:26:28 hkollmann Exp $
3  * $Revision: 1.21 $
4  * $Date: 2005/02/19 21:26:28 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.config;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.util.IEscaper;
30 import org.dbforms.util.Util;
31
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.ResultSetMetaData JavaDoc;
34 import java.sql.SQLException JavaDoc;
35
36 import java.util.HashMap JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 /**
42  * <p>
43  * In version 0.5, this class held the actual data of a ResultSet (SELECT from a
44  * table). The main weakness of this class was that it used too much memory and
45  * processor time:
46  *
47  * <ul>
48  * <li>1. every piece of data gots stored in an array (having a table with a
49  * million datasets will mean running into trouble because all the memory gets
50  * allocated at one time.)</li>
51  * <li>2. every piece of data gots converted into a String and trim()ed</li>
52  * </ul>
53  * </p>
54  *
55  * <p>
56  * since version 0.7 DbForms queries only those record from the database the
57  * user really wants to see. this way you can query from a table with millions
58  * of records and you will still have no memory problems, [exception: you choose
59  * count="" in DbForms-tag :=) -> see org.dbforms.taglib.DbFormTag]
60  * </p>
61  *
62  * @author Joe Peer
63  */

64 public class ResultSetVector implements java.io.Serializable JavaDoc {
65     private static Log logCat = LogFactory.getLog(ResultSetVector.class
66             .getName());
67
68     private Hashtable JavaDoc selectFieldsHashtable;
69
70     private Map JavaDoc attributes = new HashMap JavaDoc();
71
72     private Table table = null;
73
74     private Vector JavaDoc objectVector = new Vector JavaDoc();
75
76     private Vector JavaDoc selectFields = new Vector JavaDoc();
77
78     private boolean firstPage = false;
79
80     private boolean lastPage = false;
81
82     // logging category for this class
83
private int pointer = 0;
84
85     /**
86      * Creates a new, empty ResultSetVector object.
87      */

88     public ResultSetVector() {
89     }
90
91     /**
92      * Creates a new ResultSetVector object with the given FieldList
93      *
94      * @param table
95      * The FieldList to use
96      */

97     public ResultSetVector(Table table) {
98         this.table = table;
99
100         if (table != null) {
101             setupSelectFieldsHashtable(table.getFields());
102             setupSelectFieldsHashtable(table.getCalcFields());
103         }
104     }
105
106     /**
107      * Creates a new ResultSetVector object.
108      *
109      * @param table
110      * DOCUMENT ME!
111      * @param selectedFields
112      * DOCUMENT ME!
113      */

114     public ResultSetVector(Table table, Vector JavaDoc selectedFields) {
115         this.table = table;
116         setupSelectFieldsHashtable(selectedFields);
117     }
118
119     /**
120      * Checks if the given ResultSetVector is null
121      *
122      * @param rsv
123      * ResultSetVector to check
124      *
125      * @return true if ResultSetVector is null
126      */

127     public static final boolean isNull(ResultSetVector rsv) {
128         return ((rsv == null) || (rsv.size() == 0));
129     }
130
131     /**
132      * Stores a value in the attributes list
133      *
134      * @param key
135      * key for the value
136      * @param value
137      * value to store
138      */

139     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
140         attributes.put(key, value);
141     }
142
143     /**
144      * reads a value from the attributes list
145      *
146      * @param key
147      * key to read
148      *
149      * @return The value. If not found null
150      */

151     public Object JavaDoc getAttribute(String JavaDoc key) {
152         return attributes.get(key);
153     }
154
155     /**
156      * DOCUMENT ME!
157      *
158      * @return Returns the attributes.
159      */

160     public Map JavaDoc getAttributes() {
161         return attributes;
162     }
163
164     public String JavaDoc getTableName() {
165         String JavaDoc result = null;
166         if (table != null)
167            result = table.getName();
168         return result;
169     }
170     
171     /**
172      * DOCUMENT ME!
173      *
174      * @return DOCUMENT ME!
175      */

176     public String JavaDoc[] getCurrentRow() {
177         Object JavaDoc[] obj = getCurrentRowAsObjects();
178         String JavaDoc[] res = null;
179
180         if (obj != null) {
181             res = new String JavaDoc[obj.length];
182
183             for (int i = 0; i < obj.length; i++) {
184                 res[i] = getField(i);
185             }
186         }
187
188         return res;
189     }
190
191     /**
192      * DOCUMENT ME!
193      *
194      * @return DOCUMENT ME!
195      *
196      * @throws IllegalArgumentException
197      * DOCUMENT ME!
198      */

199     public FieldValues getCurrentRowAsFieldValues() {
200         if (selectFields == null) {
201             throw new IllegalArgumentException JavaDoc(
202                     "no field vector was provided to this result");
203         }
204
205         String JavaDoc[] rowData = getCurrentRow();
206
207         if (rowData == null) {
208             return null;
209         }
210
211         FieldValues fvHT = new FieldValues();
212
213         for (int i = 0; i < selectFields.size(); i++) {
214             Field f = (Field) selectFields.elementAt(i);
215             FieldValue fv = new FieldValue(f, rowData[i]);
216             fvHT.put(fv);
217         }
218
219         return fvHT;
220     }
221
222     /**
223      * DOCUMENT ME!
224      *
225      * @return DOCUMENT ME!
226      *
227      * @throws IllegalArgumentException
228      * DOCUMENT ME!
229      */

230     public Map JavaDoc getCurrentRowAsMap() {
231         if (selectFields == null) {
232             throw new IllegalArgumentException JavaDoc(
233                     "no field vector was provided to this result");
234         }
235
236         String JavaDoc[] rowData = getCurrentRow();
237
238         if (rowData == null) {
239             return null;
240         }
241
242         Hashtable JavaDoc ht = new Hashtable JavaDoc();
243
244         for (int i = 0; i < selectFields.size(); i++) {
245             Field f = (Field) selectFields.elementAt(i);
246             ht.put(f.getName(), rowData[i]);
247         }
248
249         return ht;
250     }
251
252     /**
253      * DOCUMENT ME!
254      *
255      * @return DOCUMENT ME!
256      */

257     public Object JavaDoc[] getCurrentRowAsObjects() {
258         Object JavaDoc[] res = null;
259         if (isPointerLegal(pointer)) {
260             res = (Object JavaDoc[]) objectVector.elementAt(pointer);
261         }
262         return res;
263     }
264
265     /**
266      * returns the fieldValues String representation given by index i
267      *
268      * @param i
269      * Index into the objectArray
270      *
271      * @return the object
272      */

273     public String JavaDoc getField(int i) {
274         Object JavaDoc obj = getFieldAsObject(i);
275
276         return (obj != null) ? obj.toString() : "";
277     }
278
279     /**
280      * returns the fieldValues string representation given by it's name
281      *
282      * @param fieldName
283      * name of the field
284      *
285      * @return the object
286      */

287     public String JavaDoc getField(String JavaDoc fieldName) {
288         int fieldIndex = getFieldIndex(fieldName);
289
290         if (fieldIndex < 0) {
291             return null;
292         }
293
294         return getField(fieldIndex);
295     }
296
297     /**
298      * returns the fieldValues Object given by index i
299      *
300      * @param i
301      * Index into the objectArray
302      *
303      * @return the object
304      */

305     public Object JavaDoc getFieldAsObject(int i) {
306         return getFieldAsObject(pointer, i);
307     }
308
309     /**
310      * returns the fieldValues Object given by row row and index i
311      *
312      * @param row
313      * row in the rsv
314      * @param i
315      * Index into the objectArray
316      *
317      * @return the object
318      */

319     public Object JavaDoc getFieldAsObject(int row, int i) {
320         Object JavaDoc res = null;
321         if (isPointerLegal(row)) {
322             try {
323                 res = ((Object JavaDoc[]) objectVector.elementAt(row))[i];
324             } catch (Exception JavaDoc e) {
325                 logCat.error("getFieldAsObject", e);
326             }
327         }
328         return res;
329     }
330
331     /**
332      * sets the fieldValue Object given by row row and index i
333      *
334      * @param row
335      * row in the rsv
336      * @param i
337      * Index into the objectArray
338      *
339      * @return the object
340      */

341     public void setFieldAsObject(int row, int i, Object JavaDoc obj) {
342         if (isPointerLegal(row)) {
343             try {
344                 Object JavaDoc[] objRow = (Object JavaDoc[]) objectVector.elementAt(row);
345                 objRow[i] = obj;
346             } catch (Exception JavaDoc e) {
347             }
348         }
349     }
350
351     /**
352      * returns the fieldValues Object given by it's name
353      *
354      * @param fieldName
355      * name of the field
356      *
357      * @return the object
358      */

359     public Object JavaDoc getFieldAsObject(String JavaDoc fieldName) {
360         return getFieldAsObject(pointer, fieldName);
361     }
362
363     /**
364      * returns the fieldValues Object given by row row and fieldName
365      *
366      * @param row
367      * row in the rsv
368      *
369      * @return the object
370      */

371     public Object JavaDoc getFieldAsObject(int row, String JavaDoc fieldName) {
372         int fieldIndex = getFieldIndex(fieldName);
373
374         if (fieldIndex < 0) {
375             return null;
376         }
377
378         return getFieldAsObject(row, fieldIndex);
379     }
380
381     /**
382      * sets the fieldValue Object given by row row and fieldName
383      *
384      * @param row
385      * row in the rsv
386      * @param fieldName
387      * name of the field
388      *
389      * @return the object
390      */

391     public void setFieldAsObject(int row, String JavaDoc fieldName, Object JavaDoc obj) {
392         int fieldIndex = getFieldIndex(fieldName);
393         if (fieldIndex >= 0) {
394             setFieldAsObject(row, fieldIndex, obj);
395         }
396     }
397
398     /**
399      * gets the Field object to a given fieldName
400      *
401      * @param fieldName
402      * name of the field
403      *
404      * @return the Field object of the given name null if not found
405      */

406     public Field getFieldDescription(String JavaDoc fieldName) {
407         return (Field) selectFieldsHashtable.get(fieldName);
408     }
409
410     /**
411      * returns the index of a given fieldName
412      *
413      * @param fieldName
414      * name of the field
415      *
416      * @return the index of the field in the data arrays
417      */

418     public int getFieldIndex(String JavaDoc fieldName) {
419         int res = -1;
420
421         if (!Util.isNull(fieldName)) {
422             Field f = (Field) selectFieldsHashtable.get(fieldName);
423
424             if (f != null) {
425                 res = selectFields.indexOf(f);
426             }
427         }
428
429         return res;
430     }
431
432     /**
433      * Return true if the current record is the first record
434      *
435      * @return true if the first record is reached
436      */

437     public boolean isFirst() {
438         return (pointer == 0);
439     }
440
441     /**
442      * DOCUMENT ME!
443      *
444      * @param b
445      * value to set
446      */

447     public void setFirstPage(boolean b) {
448         firstPage = b;
449     }
450
451     /**
452      * DOCUMENT ME!
453      *
454      * @return DOCUMENT ME!
455      */

456     public boolean isFirstPage() {
457         return firstPage;
458     }
459
460     /**
461      * Return true if the current record is the last record
462      *
463      * @return true if the last record is reached
464      */

465     public boolean isLast() {
466         return (pointer == (size() - 1));
467     }
468
469     /**
470      * DOCUMENT ME!
471      *
472      * @param b
473      * value to set
474      */

475     public void setLastPage(boolean b) {
476         lastPage = b;
477     }
478
479     /**
480      * DOCUMENT ME!
481      *
482      * @return DOCUMENT ME!
483      */

484     public boolean isLastPage() {
485         return lastPage;
486     }
487
488     /**
489      * DOCUMENT ME!
490      *
491      * @param interceptorData
492      * DOCUMENT ME!
493      * @param rs
494      * DOCUMENT ME!
495      *
496      * @throws SQLException
497      * DOCUMENT ME!
498      */

499     public void addResultSet(DbEventInterceptorData interceptorData,
500             ResultSet JavaDoc rs) throws SQLException JavaDoc {
501         ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
502         int columns = rsmd.getColumnCount();
503         IEscaper escaper = null;
504
505         try { // #JP Jun 27, 2001
506

507             while (rs.next()) {
508                 Object JavaDoc[] objectRow = new Object JavaDoc[columns];
509
510                 for (int i = 0; i < columns; i++) {
511                     if ((selectFields != null) && (i < selectFields.size())) {
512                         Field curField = (Field) selectFields.elementAt(i);
513
514                         if (curField != null) {
515                             escaper = curField.getEscaper();
516                         }
517                     }
518
519                     if (table != null) {
520                         escaper = (escaper == null) ? escaper : table
521                                 .getEscaper();
522                     }
523
524                     if (escaper == null) {
525                         try {
526                             escaper = DbFormsConfigRegistry.instance().lookup()
527                                     .getEscaper();
528                         } catch (Exception JavaDoc e) {
529                             logCat.error(
530                                     "cannot create the new default escaper", e);
531                         }
532                     }
533
534                     objectRow[i] = JDBCDataHelper.getData(rs, escaper, i + 1);
535                 }
536
537                 addRow(interceptorData, objectRow);
538             }
539         } finally {
540             rs.close();
541         }
542     }
543
544     /**
545      * adds a row to the ResultSetVector
546      *
547      * @param interceptorData
548      * DOCUMENT ME!
549      * @param objectRow
550      * row to add
551      */

552     public void addRow(DbEventInterceptorData interceptorData,
553             Object JavaDoc[] objectRow) {
554         if (objectRow != null) {
555             boolean doit = true;
556             int size = (objectRow.length > selectFields.size()) ? objectRow.length
557                     : selectFields.size();
558
559             Object JavaDoc[] newRow = new Object JavaDoc[size];
560
561             for (int i = 0; i < objectRow.length; i++) {
562                 newRow[i] = objectRow[i];
563             }
564
565             if ((interceptorData != null)
566                     && (interceptorData.getTable() != null)) {
567                 interceptorData.setAttribute(DbEventInterceptorData.RESULTSET,
568                         this);
569                 interceptorData.setAttribute(DbEventInterceptorData.OBJECTROW,
570                         newRow);
571
572                 int res = DbEventInterceptor.GRANT_OPERATION;
573
574                 try {
575                     res = interceptorData.getTable().processInterceptors(
576                             DbEventInterceptor.PRE_ADDROW, interceptorData);
577                 } catch (SQLException JavaDoc e) {
578                     ;
579                 }
580
581                 doit = (res == DbEventInterceptor.GRANT_OPERATION);
582             }
583
584             if (doit) {
585                 objectVector.addElement(newRow);
586
587                 if ((interceptorData != null)
588                         && (interceptorData.getTable() != null)) {
589                     try {
590                         interceptorData.getTable()
591                                 .processInterceptors(
592                                         DbEventInterceptor.POST_ADDROW,
593                                         interceptorData);
594                     } catch (SQLException JavaDoc e) {
595                         ;
596                     }
597                 }
598             }
599         }
600     }
601
602     /**
603      * DOCUMENT ME!
604      */

605     public void flip() {
606         int vSize = this.size();
607
608         if (vSize > 1) {
609             logCat.info("flipping " + vSize + " elements!");
610
611             for (int i = 1; i < vSize; i++) {
612                 Object JavaDoc o = objectVector.elementAt(i);
613                 objectVector.remove(i);
614                 objectVector.insertElementAt(o, 0);
615             }
616         }
617     }
618
619     /**
620      * moves to the first record
621      */

622     public void moveFirst() {
623         this.pointer = 0;
624     }
625
626     /**
627      * moves to the last record
628      */

629     public void moveLast() {
630         this.pointer = size() - 1;
631     }
632
633     /**
634      * moves to the next record
635      *
636      * @return true if end is reached
637      */

638     public boolean moveNext() {
639         pointer++;
640
641         return (pointer >= size());
642     }
643
644     /**
645      * moves to the previous record
646      *
647      * @return true if beginning is reached
648      */

649     public boolean movePrevious() {
650         pointer--;
651
652         return (pointer < 0);
653     }
654
655     /**
656      * implements size()
657      *
658      * @return the sizeof the vector
659      */

660     public int size() {
661         return objectVector.size();
662     }
663
664     private boolean isPointerLegal(int p) {
665         return ((p >= 0) && (p < size()));
666     }
667
668     private void setupSelectFieldsHashtable(Vector JavaDoc paramSelectFields) {
669         this.selectFields.addAll(paramSelectFields);
670         selectFieldsHashtable = new Hashtable JavaDoc();
671
672         for (int i = 0; i < selectFields.size(); i++) {
673             Field f = (Field) selectFields.elementAt(i);
674             selectFieldsHashtable.put(f.getName(), f);
675         }
676     }
677 }
678
Popular Tags