📄 pageiteratorsolver.java
字号:
/**
* Copyright 2003-2005 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jdon.model.query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import com.jdon.controller.cache.Cache;
import com.jdon.controller.cache.CacheKey;
import com.jdon.controller.cache.CacheKeyFactory;
import com.jdon.controller.cache.CacheManager;
import com.jdon.controller.cache.LRUCache;
import com.jdon.controller.model.PageIterator;
/**
* this class will supply a API that create PagfeIterator object
* this class is a cache proxy for JDBCTemp
*
* this class is a standard POJO, it can be called by Session Bean
* or directly by other pojos.
* default this class is not configured in container, because the container
* not existed in EJB container. currently only existed in Web container.
* if you only use web, you can configure it in pojoService way in jdonframework.xml
*
*
* @author banq
* @see
*/
public class PageIteratorSolver {
private final static Logger logger = Logger.getLogger(PageIteratorSolver.class);
protected CacheManager cacheManager ;
private CacheKeyFactory cacheKeyFactory;
/** itself cache key list */
private List cacheKeyList ;
private PageIteratorJDBC pageIteratorJDBC;
private JdbcTemp jdbcTemp;
/**
* active cache?
* default is yes
*/
private boolean cacheEnable = true;
/**
* Default Construtor
* Model's PRIMARY key Type is String
*
*
* @param dataSource DataBase datasource that can be obtained by JNDI
*
*/
public PageIteratorSolver(DataSource dataSource) {
pageIteratorJDBC = new PageIteratorJDBCTemp(dataSource);
jdbcTemp = new JdbcTemp (dataSource);
cacheKeyList = Collections.synchronizedList(new ArrayList());
cacheManager = new CacheManager(new LRUCache("log.xml"));
cacheKeyFactory = new PageCacheKeyFactory(cacheManager);
}
/**
* if need change the cache implemention, use this construtor
* @param dataSource
* @param pageIteratorJDBC your type:String or Integer
* PageIteratorString or PageIteratorInteger
*/
public PageIteratorSolver(DataSource dataSource, Cache cache) {
pageIteratorJDBC = new PageIteratorJDBCTemp(dataSource);
jdbcTemp = new JdbcTemp (dataSource);
cacheKeyList = Collections.synchronizedList(new ArrayList());
cacheManager = new CacheManager(cache);
cacheKeyFactory = new PageCacheKeyFactory(cacheManager);
}
/**
* change the JDBCTemplate
*
* @param pageIteratorJDBC PageIteratorString or PageIteratorInteger
*/
public void setPageIteratorJDBC(PageIteratorJDBC pageIteratorJDBC) {
this.pageIteratorJDBC = pageIteratorJDBC;
}
/**
* get current JDBCTemplate
* @return PageIteratorJDBC
*/
public PageIteratorJDBC getPageIteratorJDBC() {
return this.pageIteratorJDBC;
}
public boolean isCacheEnable() {
return cacheEnable;
}
/**
* 设置是否需要缓存,缺省是激活缓存
* @param cacheEnable
*/
public void setCacheEnable(boolean cacheEnable) {
this.cacheEnable = cacheEnable;
}
/**
* query one object from database
* delgate to JdbCQueryTemp's querySingleObject method
*
* @param queryParams query sql parameter (?)
* @param sqlquery query sql
* @return Object
* @throws Exception
*/
public Object querySingleObject(Collection queryParams, String sqlquery)
throws Exception {
return jdbcTemp.querySingleObject(queryParams, sqlquery);
}
/**
* query multi object from database
* delgate to JdbCQueryTemp's queryMultiObject method
* @param queryParams
* @param sqlquery
* @return
* @throws Exception
*/
public List queryMultiObject(Collection queryParams, String sqlquery)
throws Exception {
return jdbcTemp.queryMultiObject(queryParams, sqlquery);
}
/**
* create a PageIterator instance
*
*
* @param queryParam the value of sqlquery's "?"
* @param sqlqueryAllCount the sql for query all count that fit for condition
* @param sqlquery the sql for query that fit for condtion, return id collection
* @param start
* @param count
* @return PageIterator
* @throws java.lang.Exception
*/
public PageIterator getDatas(String queryParam, String sqlqueryAllCount,
String sqlquery, int start,
int count) {
Collection queryParams = new ArrayList();
queryParams.add(queryParam);
return getPageIterator(sqlqueryAllCount, sqlquery, queryParams, start, count);
}
/**
* same as getDatas, this method is better than it.
*/
public PageIterator getPageIterator(String sqlqueryAllCount,
String sqlquery, String queryParam, int start, int count) {
return getDatas(queryParam, sqlqueryAllCount, sqlquery, start, count);
}
public PageIterator getPageIterator(String sqlqueryAllCount,
String sqlquery, Collection queryParams, int start, int count) {
logger.debug("enter getPageIterator .." );
PageIterator pageIterator = null;
try {
StringBuffer sb = new StringBuffer();
Iterator iter = queryParams.iterator();
while(iter.hasNext()){
Object queryParamO = iter.next();
if (queryParamO != null)
sb.append(queryParamO.toString());
}
String queryParam = sb.toString();
String queryKey = getQueryKey(queryParam, sqlquery, start, count);
CacheKey cacheKey = getCacheKey(queryKey);
pageIterator = (PageIterator) cacheManager.fetchObject(cacheKey);
if ( (pageIterator == null) || (!cacheEnable) ) {
pageIterator = pageIteratorJDBC.fetchDatas(queryParams, sqlquery, start, count);
int allCount = getDatasAllCount(queryParams, sqlqueryAllCount);
pageIterator.setAllCount(allCount);
if ( (cacheEnable) && (pageIterator != null)){
cacheManager.putObect(cacheKey, pageIterator);
cacheKeyList.add(cacheKey);
}
}
pageIterator = (PageIterator)pageIterator.clone();
} catch (Exception e) {
logger.error(" getPageIterator error:" +e);
}
return pageIterator;
}
public int getDatasAllCount(String queryParam, String sqlquery) {
Collection queryParams = new ArrayList();
queryParams.add(queryParam);
return getDatasAllCount(queryParams, sqlquery);
}
public int getDatasAllCount(Collection queryParams, String sqlquery) {
int allCountInt = 0;
try {
StringBuffer sb = new StringBuffer();
Iterator iter = queryParams.iterator();
while(iter.hasNext()){
Object queryParamO = iter.next();
if (queryParamO != null)
sb.append(queryParamO.toString());
}
String queryParam = sb.toString();
String queryKey = getAllcountQueryKey(queryParam, sqlquery);
CacheKey cacheKey = getCacheKey(queryKey);
Integer allCount = (Integer) cacheManager.fetchObject(cacheKey);
if ( (allCount == null) || (!cacheEnable)) {
allCountInt = pageIteratorJDBC.fetchDataAllCount(queryParams, sqlquery);
if ( (cacheEnable) && (allCountInt != 0)){
cacheManager.putObect(cacheKey, new Integer(allCountInt));
cacheKeyList.add(cacheKey);
}
} else {
allCountInt = allCount.intValue();
}
} catch (Exception e) {
logger.error(" getDatasAllCount error:" +e);
}
return allCountInt;
}
private String getQueryKey(String key, String sqlquery, int start,
int count) {
StringBuffer buffer = new StringBuffer("KEYS");
buffer.append(sqlquery);
buffer.append(key);
buffer.append(start);
buffer.append(count);
return buffer.toString();
}
private String getAllcountQueryKey(String key, String sqlquery) {
StringBuffer buffer = new StringBuffer("KEYSCOUNT");
buffer.append(sqlquery);
buffer.append(key);
return buffer.toString();
}
/**
* when a model insert/delete/update, call this method clear the cache
*/
public synchronized void clearCache() {
try{
Iterator iter = cacheKeyList.iterator();
while (iter.hasNext()) {
CacheKey cacheKey = (CacheKey) iter.next();
cacheManager.removeObect(cacheKey);
}
}catch(Exception e){
logger.error(e);
}finally{
cacheKeyList.clear();
}
}
/**
* every sql has unique queryKey
*
* @param dataKey
* @return
*/
public CacheKey getCacheKey(String queryKey) {
return cacheKeyFactory.createCacheKey(queryKey, "PageIteratorSolver");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -