⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pageiteratorsolver.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * 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 javax.sql.*;
import java.util.*;

import com.jdon.controller.cache.*;
import com.jdon.controller.model.PageIterator;
import org.apache.log4j.Logger;

/**
 * 客户端调用本类,可获得符合查询条件的PageIterator
 *
 * 本类用于在EJB容器中调用,因此不能用Singleton
 *
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p></p>
 * @author banq
 * @version 1.0
 */
public class PageIteratorSolver {

  private final static Logger logger = Logger.getLogger(PageIteratorSolver.class);

  protected CacheFactory cacheFactory ;

  private CacheKeyFactory cacheKeyFactory;

  /** 本类的key列表,没有使用缓存机制中提供的Key表  */
  private List cacheKeyList ;

  private PageIteratorJDBC strategy;

  /**
   * 是否激活缓存,缺省激活使用cache
   */
  private boolean cacheEnable = true;


  /**
   * 缺省的PageIteratorJDBC 是PageIteratorString
   * PageIterator中返回的是String型
   *
   * @param dataSource
   */
  public PageIteratorSolver(DataSource dataSource) {
    strategy = new PageIteratorString(dataSource);
    cacheKeyList = new ArrayList();
    cacheFactory = new CacheFactory();
    cacheKeyFactory = new PageCacheKeyFactory(cacheFactory);
  }

  /**
   * 更换策略,例如可用PageIteratorInteger替换
   * @param pageIteratorJDBC
   */
  public void setPageIteratorJDBC(PageIteratorJDBC pageIteratorJDBC) {
    this.strategy = pageIteratorJDBC;
  }

  /**
   * 获得当前策略类型
   * @return PageIteratorJDBC
   */
  public PageIteratorJDBC getPageIteratorJDBC() {
    return this.strategy;
  }

  public boolean isCacheEnable() {
    return cacheEnable;
  }

  /**
   * 设置是否需要缓存,缺省是激活缓存
   * @param cacheEnable
   */
  public void setCacheEnable(boolean cacheEnable) {
    this.cacheEnable = cacheEnable;
  }

  /**
   * 获得符合查询条件sqlquery,且为key值的PageIterator
   * 返回pageIterator的clone,每个客户端一个对象。
   *
   * @param key
   * @param sqlqueryAllCount
   * @param sqlquery
   * @param start
   * @param count
   * @return PageIterator
   * @throws java.lang.Exception
   */
  public PageIterator getDatas(String key, String sqlqueryAllCount,
                               String sqlquery, int start,
                               int count) throws Exception {

    String queryKey = getQueryKey(key, sqlqueryAllCount,
                               sqlquery, start, count);

    CacheKey cacheKey = getCacheKey(queryKey);
    PageIterator pageIterator = (PageIterator) cacheFactory.getObect(cacheKey);
    if ( (pageIterator == null) || (!cacheEnable) ) {
      pageIterator = strategy.fetchDatas(key, sqlquery, start, count);
      int allCount = getDatasAllCount(key, sqlqueryAllCount);
      pageIterator.setAllCount(allCount);
      if ( (cacheEnable) && (pageIterator != null)){
        cacheFactory.putObect(cacheKey, pageIterator);
        cacheKeyList.add(cacheKey);
      }
    }
    return (PageIterator)pageIterator.clone();

  }

  /**
   *  获得符合查询条件sqlquery,且为key值的所有记录总数
   * @param key
   * @param sqlquery
   * @return
   * @throws java.lang.Exception
   */
  public int getDatasAllCount(String key, String sqlquery) throws Exception {
    String queryKey = getAllcountQueryKey(key, sqlquery);
    CacheKey cacheKey = getCacheKey(queryKey);
    Integer allCount = (Integer) cacheFactory.getObect(cacheKey);
    int allCountInt;
    if ( (allCount == null) || (!cacheEnable)) {
      allCountInt = strategy.fetchDataAllCount(key, sqlquery);
      if ( (cacheEnable) && (allCountInt != 0)){
        cacheFactory.putObect(cacheKey, new Integer(allCountInt));
        cacheKeyList.add(cacheKey);
      }
    } else {
      allCountInt = allCount.intValue();
    }
    return allCountInt;

  }

  private String getQueryKey(String key, String sqlqueryAllCount,
                               String sqlquery, int start,
                               int count) {
    StringBuffer buffer = new StringBuffer("KEYS");
    buffer.append(sqlquery);
    buffer.append(sqlqueryAllCount);
    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();
  }

  /**
   * 当缓存中数据可能变化时,使用本方法清除缓存
   */
  public void clearCache() {
    try{
      Iterator iter = cacheKeyList.iterator();
      while (iter.hasNext()) {
        CacheKey cacheKey = (CacheKey) iter.next();
        cacheFactory.removeObect(cacheKey);
      }
    }catch(Exception e){

    }
    cacheKeyList.clear();
  }


  /**
   * queryKey是一条sql语句,每条sql语句只有唯一结果
   *
   * typeName是PageIteratorSolver,表示这是本类可管理的缓存
   *
   * 本类中有两种东西需要缓存:
   * 1. 一个查询ID集合的sql语句
   * 2. 一个查询ID总数的sql语句
   *
   *
   * @param dataKey
   * @return
   */
  public CacheKey getCacheKey(String queryKey) {
     return cacheKeyFactory.createCacheKey(queryKey, "PageIteratorSolver");
   }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -