modellistaction.java

来自「一个非常好的FRAMWRK!是一个外国组织做的!不!」· Java 代码 · 共 245 行

JAVA
245
字号
/**
 * 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.strutsutil;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdon.util.Debug;
import java.util.*;

import com.jdon.container.finder.ComponentKeys;
import com.jdon.controller.WebAppUtil;
import com.jdon.controller.model.Model;
import com.jdon.controller.model.PageIterator;

import com.jdon.model.ModelManager;
import com.jdon.model.ModelKey;

import javax.servlet.ServletContext;

/**
 * batch query action
 *
 * @author banq
 */
public abstract class ModelListAction extends Action {

  private final static String module = ModelListAction.class.getName();
  private ModelManager modelManager;

  public ActionForward execute(ActionMapping actionMapping,
                               ActionForm actionForm,
                               HttpServletRequest request,
                               HttpServletResponse response) throws
      Exception {

    if (modelManager == null){
        ServletContext sc = this.getServlet().getServletContext();
        modelManager = (ModelManager)WebAppUtil.getComponentInstance(ComponentKeys.MODEL_MANAGER, sc);
    }


    int start = 0;
    int count = 20;

    String startStr = request.getParameter("start");
    if ( (startStr != null) && (startStr.length() != 0)) {
      start = Integer.parseInt(startStr);
    }
    String countStr = request.getParameter("count");
    if ( (countStr != null) && (countStr.length() != 0)) {
      count = Integer.parseInt(countStr);
    }

    PageIterator pageIterator = getPageIterator(request, start, count);
    if (pageIterator == null) {
      throw new Exception(
          "getPageIterator's result is null, check your ModelListAction subclass");
    }

    ModelListForm listForm = getModelListForm(actionMapping, actionForm,
                                              request, pageIterator);

    listForm.setStart(start);
    listForm.setCount(count);
    listForm.setAllCount(pageIterator.getAllCount());
    listForm.setHasNextPage(pageIterator.isNextPageAvailable());
    Collection c = getModelList(request, pageIterator);
    Debug.logVerbose(" listForm 's property: getList size is " + c.size(), module);
    listForm.setList(c);
    listForm.setOneModel(setupOneModel(request));
    customizeListForm(actionMapping, actionForm, request, listForm);

    if (actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME) == null)
      Debug.logError(
          "not found the forward name '" + FormBeanUtil.FORWARD_SUCCESS_NAME +
          "' in struts-config.xml", module);

    return actionMapping.findForward(FormBeanUtil.FORWARD_SUCCESS_NAME);
  }

  /**
   * 获得ModelListForm实例
   *
   * @param actionMapping
   * @param actionForm
   * @param request
   * @return
   * @throws java.lang.Exception
   */
  public ModelListForm getModelListForm(ActionMapping actionMapping,
                                        ActionForm actionForm,
                                        HttpServletRequest request,
                                        PageIterator pageIterator) throws
      Exception {
    ModelListForm modelListForm = null;
    if (actionForm == null){
      modelListForm = new ModelListForm();
      FormBeanUtil.saveActionForm(modelListForm, actionMapping, request);
    }else if (actionForm instanceof ModelListForm)
      modelListForm = (ModelListForm) actionForm;

    if (modelListForm == null)
      throw new Exception(
          "not found the bean of com.jdon.strutsutil.ModelListForm");
    else
      return modelListForm;

  }

  /**
   * 根据PageIterator中ID集合,获得相应的Model集合
   * @param request
   * @param pageIterator
   * @return Model集合
   * @throws java.lang.Exception
   */
  private List getModelList(HttpServletRequest request,
                            PageIterator pageIterator) throws Exception {
    List list = new ArrayList(pageIterator.getSize());
    Model model = null;
    Class modelClass = null;
    while (pageIterator.hasNext()) {
      Object dataKey = pageIterator.next();
      if ( (modelClass == null) || (!isEnableCache())) { //第一个model从数据库中获取
        Debug.logVerbose(" getCache from db. " , module);
        model = fetchModel(request, dataKey);
        if (model != null)
          modelClass = model.getClass();
        else
          continue;        
      } else {
        //首先从缓存中获取
        ModelKey modelKey = new ModelKey(dataKey, modelClass);
        model = modelManager.getCache(modelKey);
        if (model == null) {
          model = fetchModel(request, dataKey);
          modelManager.addCache(modelKey, model);
        }
      }
      if (model != null)
          list.add(model);
    }
    pageIterator.reset();
    return list;
  }

  /**
   * 定制ModelListForm
   *
   * 缺省ModelListForm是只有一个List,包含一种Model集合
   * 有的应用可能是两种Model集合,可以继承ModelListForm实现Map-backed ActionForms
   * 再继承本Action,实现本方法。
   *
   * @param actionMapping
   * @param actionForm
   * @param request
   * @param modelListForm
   * @throws java.lang.Exception
   */
  public void customizeListForm(ActionMapping actionMapping,
                                    ActionForm actionForm,
                               HttpServletRequest request,
                              ModelListForm modelListForm ) throws Exception{


  }


  /**
   * 
   * decide if enable cacche?
   * the sub-class can unable cache by overrid this method
   * @param enable
   */
  protected boolean isEnableCache() {
    return true;
  }

  private Model fetchModel(HttpServletRequest request, Object dataKey) throws
      Exception {
    Model model = null;
    try {
      model = findModelByKey(request, dataKey);
      if (model == null)
          Debug.logWarning(" the model Id=(" + dataKey+") maybe be deleted!", module);
    } catch (Exception ex) {
      Debug.logError(ex, module);
    }
    return model;
  }

  protected Model setupOneModel(HttpServletRequest request) {
     return null;
   }


  /**
   * 必须实现的方法
   *
   * PageIterator中包含的是Model的key或称ID集合
   * PageIterator中的key(ID)集合用于findModelByKey来查询
   * Model实例。
   *
   * @param request HttpServletRequest
   * @param start int
   * @param count int
   * @return PageIterator
   */
  public abstract PageIterator getPageIterator(HttpServletRequest request,
                                               int start,
                                               int count);

  /**
   * obtain a Model instance from service, the model type
   * is that will be displayed by batch query.
   *
   * 根据Model的key或称ID获取Model实例,key(ID)是getPageIterator
   * 方法返回集合中的值
   *
   * @param request HttpServletRequest
   * @param key Object
   * @return Model
   */
  public abstract Model findModelByKey(HttpServletRequest request, Object key);

}

⌨️ 快捷键说明

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