KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > model > query > PageIteratorSolver


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7  http://www.apache.org/licenses/LICENSE-2.0
8
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15
16 package com.jdon.model.query;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
23
24 import org.apache.log4j.Logger;
25
26 import com.jdon.controller.cache.CacheManager;
27 import com.jdon.controller.cache.LRUCache;
28 import com.jdon.controller.model.PageIterator;
29 import com.jdon.model.query.block.Block;
30 import com.jdon.model.query.block.BlockQueryJDBC;
31 import com.jdon.model.query.block.BlockQueryJDBCTemp;
32 import com.jdon.model.query.block.BlockStrategy;
33 import com.jdon.model.query.cache.BlockCacheManager;
34 import com.jdon.model.query.cache.QueryConditonDatakey;
35 import com.jdon.util.UtilValidate;
36
37 /**
38  * this class will supply a API that create PagfeIterator object this class is a
39  * cache proxy for JDBCTemp
40  *
41  * this class is a standard POJO, it can be called by Session Bean or directly
42  * by other pojos.
43  *
44  * default this class is not configured in container, because jdon container do
45  * not exist in EJB container. currently only exists in Web container. if you
46  * only use web, you can configure it in pojoService way in jdonframework.xml
47  *
48  * directly create the PageIteratorSolver object PageIteratorSolver
49  * pageIteratorSolver = new PageIteratorSolver(datasource);
50  *
51  * get the PageIteratorSolver object from container: <pojoService
52  * name="pageIteratorSolver" class="com.jdon.model.query.PageIteratorSolver"/>
53  *
54  *
55  *
56  *
57  * @author banq
58  * @see
59  */

60 public class PageIteratorSolver {
61
62     private final static Logger logger = Logger.getLogger(PageIteratorSolver.class);
63
64     private BlockQueryJDBC blockQueryJDBC;
65
66     private JdbcTemp jdbcTemp;
67
68     private BlockCacheManager blockCacheManager;
69     
70     private BlockStrategy blockStrategy;
71
72     /**
73      * active cache default is yes
74      */

75     private boolean cacheEnable = true;
76
77     /**
78      * default construtor, this construtor is avaliable when you using Jdon's
79      * cache system (use jdon framework completely) but at first you must get
80      * CacheManager from the jdon container. by coding, you must get a
81      * CacheManager from the container, and create PageIteratorSolver object.
82      *
83      * if you don't know how to get a CacheManager object, you can use next
84      * construtor;
85      *
86      * @param dataSource
87      * @param pageIteratorJDBC
88      * your type:String or Integer PageIteratorString or
89      * PageIteratorInteger
90      */

91     public PageIteratorSolver(DataSource JavaDoc dataSource, CacheManager cacheManager) {
92         this.blockQueryJDBC = new BlockQueryJDBCTemp(dataSource);
93         this.jdbcTemp = new JdbcTemp(dataSource);
94         this.blockCacheManager = new BlockCacheManager(cacheManager);
95         this.blockStrategy = new BlockStrategy(blockQueryJDBC, jdbcTemp, blockCacheManager);
96     }
97     
98     /**
99      * we can change the block Strategy.
100      */

101     public PageIteratorSolver(DataSource JavaDoc dataSource, CacheManager cacheManager, BlockStrategy blockStrategy) {
102         this(dataSource, cacheManager);
103         this.blockStrategy = blockStrategy;
104     }
105
106     /**
107      * construtor without supplying a CacheManager, but you must put the
108      * cache.xml into the classpath, this construtor will auto find it. when you
109      * call PageIteratorSolver in Dao of a EJB, this construtor is avaliable
110      *
111      * if you use this construtor in Jdon container, this object will create
112      * another cache system that not relation with the container, this will
113      * waste memory. you must hold the PageIteratorSolver object by yourself,
114      * otherelse, the cache will disable.
115      *
116      * @param dataSource
117      * DataBase datasource that can be obtained by JNDI
118      *
119      */

120     public PageIteratorSolver(DataSource JavaDoc dataSource) {
121         this.blockQueryJDBC = new BlockQueryJDBCTemp(dataSource);
122         this.jdbcTemp = new JdbcTemp(dataSource);
123         CacheManager cacheManager = new CacheManager(new LRUCache("cache.xml"));
124         this.blockCacheManager = new BlockCacheManager(cacheManager);
125         this.blockStrategy = new BlockStrategy(blockQueryJDBC, jdbcTemp, blockCacheManager);
126     }
127
128     /**
129      * query one object from database delgate to JdbCQueryTemp's
130      * querySingleObject method
131      *
132      * @param queryParams
133      * query sql parameter (?)
134      * @param sqlquery
135      * query sql
136      * @return Object
137      * @throws Exception
138      */

139     public Object JavaDoc querySingleObject(Collection JavaDoc queryParams, String JavaDoc sqlquery) throws Exception JavaDoc {
140         return jdbcTemp.querySingleObject(queryParams, sqlquery);
141     }
142
143     /**
144      * query multi object from database delgate to JdbCQueryTemp's
145      * queryMultiObject method
146      *
147      * @param queryParams
148      * @param sqlquery
149      * @return
150      * @throws Exception
151      */

152     public List JavaDoc queryMultiObject(Collection JavaDoc queryParams, String JavaDoc sqlquery) throws Exception JavaDoc {
153         return jdbcTemp.queryMultiObject(queryParams, sqlquery);
154     }
155
156     /**
157      * create a PageIterator instance
158      *
159      *
160      * @param queryParam
161      * the value of sqlquery's "?"
162      * @param sqlqueryAllCount
163      * the sql for query all count that fit for condition
164      * @param sqlquery
165      * the sql for query that fit for condtion, return id collection
166      * @param start
167      * @param count
168      * @return PageIterator
169      * @throws java.lang.Exception
170      */

171     public PageIterator getDatas(String JavaDoc queryParam, String JavaDoc sqlqueryAllCount, String JavaDoc sqlquery, int start, int count) {
172         if (UtilValidate.isEmail(sqlqueryAllCount)){
173             logger.error(" the parameter sqlqueryAllCount is null");
174             return new PageIterator();
175         }
176         if (UtilValidate.isEmail(sqlquery)){
177             logger.error(" the parameter sqlquery is null");
178             return new PageIterator();
179         }
180         
181         Collection JavaDoc queryParams = new ArrayList JavaDoc();
182         if (!UtilValidate.isEmpty(queryParam))
183            queryParams.add(queryParam);
184         return getPageIterator(sqlqueryAllCount, sqlquery, queryParams, start, count);
185
186     }
187
188     /**
189      * same as getDatas
190      * the parameters sort is different from the getDatas method
191      *
192      * @param sqlqueryAllCount
193      * the sql sentence for "select count(1) .."
194      * @param sqlquery
195      * the sql sentence for "select id from xxxx";
196      * @param queryParam
197      * the parameter of String type for the sqlquery.
198      * @param start
199      * the starting number of a page in allCount;
200      * @param count
201      * the display number of a page
202      * @return
203      */

204     public PageIterator getPageIterator(String JavaDoc sqlqueryAllCount, String JavaDoc sqlquery, String JavaDoc queryParam, int start, int count) {
205         if (UtilValidate.isEmail(sqlqueryAllCount)){
206             logger.error(" the parameter sqlqueryAllCount is null");
207             return new PageIterator();
208         }
209         if (UtilValidate.isEmail(sqlquery)){
210             logger.error(" the parameter sqlquery is null");
211             return new PageIterator();
212         }
213         return getDatas(queryParam, sqlqueryAllCount, sqlquery, start, count);
214     }
215
216     /**
217      * get a PageIterator
218      *
219      * @param sqlqueryAllCount
220      * the sql sentence for "select count(1) .."
221      * @param sqlquery
222      * the sql sentence for "select id from xxxx";
223      * @param queryParams
224      * the parameter collection for the sqlquery.
225      * @param start
226      * the starting number of a page in allCount;
227      * @param count
228      * the display number of a page
229      * @return
230      */

231     public PageIterator getPageIterator(String JavaDoc sqlqueryAllCount, String JavaDoc sqlquery, Collection JavaDoc queryParams, int startIndex, int count) {
232         logger.debug("[JdonFramework]enter getPageIterator .. start= " + startIndex + " count=" + count);
233         if (queryParams == null){
234             logger.error(" the parameters collection is null");
235             return new PageIterator();
236         }
237         if ((count > blockStrategy.getBlockLength()) || (count <= 0)) { //every query max length must be less than blockLength
238
count = blockStrategy.getBlockLength();
239         }
240         Block currentBlock = getBlock(sqlquery, queryParams, startIndex, count);
241         if (currentBlock == null){
242             return new PageIterator();
243         }
244         startIndex = currentBlock.getStart();
245         int endIndex = startIndex + currentBlock.getCount();
246         Object JavaDoc[] keys = currentBlock.getList().toArray();
247         int allCount = getDatasAllCount(queryParams, sqlqueryAllCount);
248         logger.debug("[JdonFramework]currentBlock: startIndex=" + startIndex + " endIndex=" + endIndex + " keys length=" + keys.length);
249         if (endIndex < startIndex){
250             logger.warn("WARNNING : endIndex < startIndex");
251             return new PageIterator();
252         }else{
253             return new PageIterator(allCount, keys, startIndex, endIndex, count);
254         }
255     }
256     
257     /**
258      * looking for a block in that there is a primary key is equals to the locateId.
259      * for the sql sentence.
260      * @param sqlquery
261      * @param queryParams
262      * @param locateId
263      * @return if not locate, return null;
264      */

265     public Block locate(String JavaDoc sqlquery, Collection JavaDoc queryParams, Object JavaDoc locateId) {
266         return blockStrategy.locate(sqlquery, queryParams, locateId);
267     }
268     
269     
270   
271     
272     /**
273      * get a data block by the sql sentence.
274      * @param sqlqueryAllCount
275      * @param sqlquery
276      * @return if not found, return null;
277      */

278     public Block getBlock(String JavaDoc sqlquery, Collection JavaDoc queryParams, int startIndex, int count){
279         return blockStrategy.getBlock(sqlquery, queryParams, startIndex, count);
280     }
281
282
283     public int getDatasAllCount(String JavaDoc queryParam, String JavaDoc sqlquery) {
284         Collection JavaDoc queryParams = new ArrayList JavaDoc();
285         queryParams.add(queryParam);
286         return getDatasAllCount(queryParams, sqlquery);
287     }
288
289     public int getDatasAllCount(Collection JavaDoc queryParams, String JavaDoc sqlquery) {
290         QueryConditonDatakey qcdk = new QueryConditonDatakey(sqlquery, queryParams);
291         return getDatasAllCount(qcdk);
292
293     }
294
295     public int getDatasAllCount(QueryConditonDatakey qcdk) {
296         int allCountInt = 0;
297         try {
298             Integer JavaDoc allCount = (Integer JavaDoc) blockCacheManager.getAllCountsFromCache(qcdk);
299             if ((allCount == null) || (!cacheEnable)) {
300                 allCountInt = blockQueryJDBC.fetchDataAllCount(qcdk);
301                 if ((cacheEnable) && (allCountInt != 0)) {
302                     blockCacheManager.saveAllCounts(qcdk, new Integer JavaDoc(allCountInt));
303                 }
304             } else {
305                 allCountInt = allCount.intValue();
306             }
307         } catch (Exception JavaDoc e) {
308             logger.error(" getDatasAllCount error:" + e);
309         }
310         return allCountInt;
311
312     }
313
314     /**
315      * when a model insert/delete/update, call this method clear the cache
316      */

317     public void clearCache() {
318         logger.debug("[JdonFramework] clear the cache for the batch inquiry!");
319         blockCacheManager.clearCache();
320     }
321
322     /**
323      * change the JDBCTemplate
324      *
325      * @param pageIteratorJDBC
326      * PageIteratorString or PageIteratorInteger
327      */

328     public void setPageIteratorJDBC(BlockQueryJDBC pageIteratorJDBC) {
329         this.blockQueryJDBC = pageIteratorJDBC;
330     }
331
332     /**
333      * get current JDBCTemplate
334      *
335      * @return PageIteratorJDBC
336      */

337     public BlockQueryJDBC getPageIteratorJDBC() {
338         return this.blockQueryJDBC;
339     }
340
341     public boolean isCacheEnable() {
342         return this.cacheEnable;
343     }
344
345     /**
346      * setup if need cache
347      *
348      * @param cacheEnable
349      */

350     public void setCacheEnable(boolean cacheEnable) {
351         this.cacheEnable = cacheEnable;
352     }
353
354   
355 }
356
Popular Tags