KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > rdbms > RdbmsCursor


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.repository.rdbms;
31
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.Stack JavaDoc;
35
36 import com.genimen.djeneric.repository.DjCursor;
37 import com.genimen.djeneric.repository.DjExtent;
38 import com.genimen.djeneric.repository.DjList;
39 import com.genimen.djeneric.repository.DjObject;
40 import com.genimen.djeneric.repository.DjObjectMatcher;
41 import com.genimen.djeneric.repository.DjSession;
42 import com.genimen.djeneric.repository.exceptions.DjenericException;
43 import com.genimen.djeneric.util.DjLogger;
44
45 /**
46  * Description of the Class
47  *
48  *@author Wido Riezebos
49  *@created 15 jan 2002
50  */

51 public class RdbmsCursor extends DjCursor
52 {
53   SqlStatement _stmt;
54   ResultSet JavaDoc _rs = null;
55   Stack JavaDoc _subCursors = new Stack JavaDoc();
56   DjObjectMatcher _matcher = null;
57
58   /**
59    * Constructor for the RdbmsCursor object
60    *
61    *@param session Description of the Parameter
62    *@param extent Description of the Parameter
63    *@param stmt Description of the Parameter
64    * @param qbe
65    *@param sessionObjects Description of the Parameter
66    *@param forceReloadFromDB Description of the Parameter
67    *@exception DjenericException Description of the Exception
68    */

69   protected RdbmsCursor(DjSession session, DjExtent extent, SqlStatement stmt, DjObjectMatcher qbe,
70                         DjList sessionObjects, boolean forceReloadFromDB) throws DjenericException
71   {
72     super(session, extent, sessionObjects, forceReloadFromDB);
73     _stmt = stmt;
74     _matcher = qbe;
75   }
76
77   /**
78    * Chains another cursor (in case of inheritance) to this cursor;
79    * Chaining is used to ensure that all specialisations of the extent
80    * are returned too
81    *
82    *@param the subcursor
83    *
84    */

85   public void chain(DjCursor subCursor)
86   {
87     _subCursors.push(subCursor);
88   }
89
90   /**
91    * Returns the next object to be returned by the query
92    *
93    *@return The next value
94    *@exception DjenericException Description of the Exception
95    */

96   public DjObject getNext() throws DjenericException
97   {
98     DjObject result = null;
99
100     while (result == null && !_subCursors.isEmpty())
101     {
102       RdbmsCursor firstTryThisOne = (RdbmsCursor) _subCursors.peek();
103       result = firstTryThisOne.getNext();
104       // If the cursor is done or empty then pop it and try the next
105
// cursor until not cursors are left
106
if (result == null) _subCursors.pop();
107     }
108     if (result != null) return result;
109
110     if (getSessionObjects().size() > 0)
111     {
112       result = getSessionObjects().getDjenericObjectAt(0);
113       getSessionObjects().remove(0);
114       return result;
115     }
116
117     try
118     {
119       if (_rs == null) _rs = _stmt.executeQuery();
120
121       // repeat the following until no object is found, or the object
122
// is found AND not marked for delete
123
// i.e. SKIP DELETED OBJECTS
124
while (true)
125       {
126         if (!_rs.next())
127         {
128           close();
129           return null;
130         }
131
132         RdbmsSession ses = (RdbmsSession) getSession();
133         // Check existence in the session; if it is there return that object (except when marked for delete)
134
RdbmsDjenericObject po = (RdbmsDjenericObject) ses.getFromSession(_rs.getLong(getIdColumnName()));
135         if (po == null)
136         {
137           po = (RdbmsDjenericObject) getSession().createObject(getExtent());
138           po.setFromRecord(_rs, true);
139           return po;
140         }
141         else
142         {
143           // It might be an updated object that does not match the criteria (anymore)
144
// so we need to match it here to make sure it does belong in the resultset
145
// (discrepancy between uncommitted records in the database and session)
146

147           boolean skip = po.isMarkedForDelete();
148           if (!skip && isForceReloadFromDB()) po.reload();
149           // Skip if it does not match the criteria anymore
150
if (!skip && _matcher != null) skip = !_matcher.match(po);
151           if (!skip) return po;
152         }
153       }
154     }
155     catch (SQLException JavaDoc x)
156     {
157       DjLogger.log(_stmt.getExternalSql());
158       DjLogger.log(_stmt.getInternalSql());
159       throw new DjenericException(x);
160     }
161   }
162
163   /**
164    * Closes the cursor, freeing the resources (result set and statement)
165    *
166    *@exception DjenericException Description of the Exception
167    */

168   public void close() throws DjenericException
169   {
170     try
171     {
172       if (_rs != null) _rs.close();
173       _rs = null;
174       _stmt.close();
175     }
176     catch (SQLException JavaDoc x)
177     {
178       throw new DjenericException(x);
179     }
180   }
181
182 }
Popular Tags