1 15 16 package com.jdon.model.query; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.List ; 21 22 import javax.sql.DataSource ; 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 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 75 private boolean cacheEnable = true; 76 77 91 public PageIteratorSolver(DataSource 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 101 public PageIteratorSolver(DataSource dataSource, CacheManager cacheManager, BlockStrategy blockStrategy) { 102 this(dataSource, cacheManager); 103 this.blockStrategy = blockStrategy; 104 } 105 106 120 public PageIteratorSolver(DataSource 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 139 public Object querySingleObject(Collection queryParams, String sqlquery) throws Exception { 140 return jdbcTemp.querySingleObject(queryParams, sqlquery); 141 } 142 143 152 public List queryMultiObject(Collection queryParams, String sqlquery) throws Exception { 153 return jdbcTemp.queryMultiObject(queryParams, sqlquery); 154 } 155 156 171 public PageIterator getDatas(String queryParam, String sqlqueryAllCount, String 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 queryParams = new ArrayList (); 182 if (!UtilValidate.isEmpty(queryParam)) 183 queryParams.add(queryParam); 184 return getPageIterator(sqlqueryAllCount, sqlquery, queryParams, start, count); 185 186 } 187 188 204 public PageIterator getPageIterator(String sqlqueryAllCount, String sqlquery, String 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 231 public PageIterator getPageIterator(String sqlqueryAllCount, String sqlquery, Collection 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)) { 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 [] 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 265 public Block locate(String sqlquery, Collection queryParams, Object locateId) { 266 return blockStrategy.locate(sqlquery, queryParams, locateId); 267 } 268 269 270 271 272 278 public Block getBlock(String sqlquery, Collection queryParams, int startIndex, int count){ 279 return blockStrategy.getBlock(sqlquery, queryParams, startIndex, count); 280 } 281 282 283 public int getDatasAllCount(String queryParam, String sqlquery) { 284 Collection queryParams = new ArrayList (); 285 queryParams.add(queryParam); 286 return getDatasAllCount(queryParams, sqlquery); 287 } 288 289 public int getDatasAllCount(Collection queryParams, String 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 allCount = (Integer ) blockCacheManager.getAllCountsFromCache(qcdk); 299 if ((allCount == null) || (!cacheEnable)) { 300 allCountInt = blockQueryJDBC.fetchDataAllCount(qcdk); 301 if ((cacheEnable) && (allCountInt != 0)) { 302 blockCacheManager.saveAllCounts(qcdk, new Integer (allCountInt)); 303 } 304 } else { 305 allCountInt = allCount.intValue(); 306 } 307 } catch (Exception e) { 308 logger.error(" getDatasAllCount error:" + e); 309 } 310 return allCountInt; 311 312 } 313 314 317 public void clearCache() { 318 logger.debug("[JdonFramework] clear the cache for the batch inquiry!"); 319 blockCacheManager.clearCache(); 320 } 321 322 328 public void setPageIteratorJDBC(BlockQueryJDBC pageIteratorJDBC) { 329 this.blockQueryJDBC = pageIteratorJDBC; 330 } 331 332 337 public BlockQueryJDBC getPageIteratorJDBC() { 338 return this.blockQueryJDBC; 339 } 340 341 public boolean isCacheEnable() { 342 return this.cacheEnable; 343 } 344 345 350 public void setCacheEnable(boolean cacheEnable) { 351 this.cacheEnable = cacheEnable; 352 } 353 354 355 } 356 | Popular Tags |