View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Claudio Fratarcangeli
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   */
23  
24  package com.pow2.dao.datalist;
25  
26  import java.sql.SQLException;
27  
28  import org.apache.log4j.*;
29  
30  
31  
32  /***
33   *  ResultSetDataListChunk class.
34   *
35   * @author  Claudio Fratarcangeli
36   * @created  5 dicembre 2002
37   */
38  public class ResultSetDataListChunk implements ResultSetDataList
39  {
40    /*** Log4J category. */
41    protected static Category cat = Category.getInstance(ResultSetDataListChunk.class);
42  
43    private ResultSetDataList rsDataList;
44    private int firstElementIndex;
45    private int countOfElements;
46    private int currIndex = -1;
47  
48  
49    /***
50     *  Constructor
51     *
52     * @param  rsDataList the resultSetDataList object
53     * @param  firstElementIndex the index of the first element of the chunk
54     * @param  countOfElements the number of the chunk elements
55     */
56    public ResultSetDataListChunk(ResultSetDataList rsDataList,
57                                  int firstElementIndex,
58                                  int countOfElements)
59    {
60      this.rsDataList = rsDataList;
61      this.firstElementIndex = firstElementIndex;
62      this.countOfElements = countOfElements;
63    }
64  
65  
66   /***
67     *  Get the DataListIterator object.
68     *
69     * @return  the DataListIterator object
70     * @exception Exception if any error occurs
71     */
72    public DataListIterator iterator() throws Exception
73    {
74      return new ResultSetDataListIterator(this);
75    }
76  
77  
78    /***
79     *  Close the data list.
80     *  <br>
81     *  Implementation note: do not close this object !!
82     *
83     * @exception Exception if any error occurs
84     */
85    public void close() throws Exception
86    {
87       throw new Exception("ResultSetDataListChunkImpl: Invalid call to clear");
88    }
89  
90  
91    /***
92     *  Put object state in item and return it.
93     *  <br>
94     *  Use rowMapper to map fields of ResultSet to instance variables of item.
95     *  ResultSet index ranges from 1, while ResultSetDataList index
96     *  ranges from 0.
97     *
98     * @param  index  the index value
99     * @param  obj   the item object to fill
100    * @return  the item object filled with values retrieved from the resultSet,
101    *          or null if the input index is out of the bondaries of the chunk
102    * @exception  Exception if any error occurs
103    */
104   public Object get(int index, Object obj) throws Exception
105   {
106     if (index >= countOfElements)
107     {
108       obj = null;
109     }
110     else
111     {
112       int chunkIndex = (firstElementIndex + index);
113       cat.info("::get - getting element from index [" + index + " (" + chunkIndex + ")]");
114       obj = rsDataList.get(chunkIndex, obj);
115       currIndex = index;
116     }
117 
118     return obj;
119   }
120 
121 
122   /***
123    *  Check if the resultSet data list has got another element.
124    *
125    * @return  true  if the resultSet data list has got another record,
126    *          false otherwise
127    * @exception  Exception if any error occurs
128    */
129   public boolean hasNext() throws Exception
130   {
131     return ((currIndex + 1) < countOfElements) && rsDataList.hasNext();
132   }
133 
134 
135   /***
136    *  Move the resultSetDataListChunk cursor before the first row of the list.
137    *
138    * @exception Exception if any error occurs
139    */
140   public void beforeFirst() throws Exception
141   {
142     if (firstElementIndex == 0)
143       rsDataList.beforeFirst();
144     else
145       rsDataList.absolute(firstElementIndex - 1);
146   }
147 
148 
149   /***
150    *  Move the resultSetDataListChunk cursor to a specific row number.
151    *  Rows are numbered starting from 0.
152    *
153    * @param  index the cursor index value
154    * @exception Exception if any error occurs
155    */
156   public boolean absolute(int index) throws Exception
157   {
158     if (index >= countOfElements)
159       return false;
160 
161     boolean ret  = rsDataList.absolute(firstElementIndex + index);
162 
163     if (ret)
164       currIndex = index;
165 
166     return ret;
167   }
168 
169 
170   /***
171    *  Check if any element exist at the <code>index</code> poition.
172    *
173    * @param   index the index value
174    * @return  true if any element exist at the <code>index</code> poition,
175    *          false otherwise
176    * @exception Exception if any error occurs
177    */
178   public boolean elementExists(int index) throws Exception
179   {
180     if (index >= countOfElements)
181       return false;
182 
183     return rsDataList.elementExists(firstElementIndex + index);
184   }
185 
186 
187   /***
188    *  Check if the resultSetDataListChunk is empty.
189    *
190    * @return  true if the resultSetDataListChunk is empty; false otherwise
191    * @exception Exception if any error occurs
192    */
193   public boolean isEmpty() throws Exception
194   {
195     boolean isFull  = false;
196 
197     try
198     {
199       isFull = rsDataList.absolute(firstElementIndex);
200 
201       if (isFull && (currIndex != -1) && (currIndex != firstElementIndex))
202         rsDataList.absolute(currIndex);
203     }
204     catch (SQLException e)
205     {
206       // log exception here;
207       throw e;
208     }
209 
210     return !isFull;
211   }
212 }