/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2008 Aimluck,Inc.
 * http://aipostyle.com/
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.aimluck.eip.modules.actions.cashflow;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.modules.actions.common.ALBaseAction;
import com.aimluck.eip.cashflow.AccountPlanFormData;
import com.aimluck.eip.cashflow.AccountPlanResultData;
import com.aimluck.eip.cashflow.AccountPlanSelectData;
import com.aimluck.eip.cashflow.AccountPlanMultiDelete;
import com.aimluck.eip.cashflow.CashFlowSelectData;
import com.aimluck.eip.util.ALEipUtils;

/**
 * 入出金予定データのアクションクラスです。 <BR>
 * 
 */
public class CashFlowAction extends ALBaseAction {

  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(CashFlowAction.class.getName());

  /**
   * 通常表示の際の処理を記述します。 <BR>
   * 
   * @param portlet
   * @param context
   * @param rundata
   * @throws Exception
   * @see org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction#buildNormalContext(org.apache.jetspeed.portal.portlets.VelocityPortlet,
   *      org.apache.velocity.context.Context, org.apache.turbine.util.RunData)
   */
  protected void buildNormalContext(VelocityPortlet portlet, Context context,
      RunData rundata) throws Exception {
//    HttpServletRequest request = rundata.getRequest();
//    StringBuffer sb = new StringBuffer();
//    sb.append(request.getScheme()).append("://").append(request.getServerName())
//        .append(":").append(request.getServerPort()).append("/").append(request.getRequestURI());
//    String url = sb.toString();
    
    // セッション情報のクリア
    clearCashFlowSession(rundata, context);

    CashFlowSelectData listData = new CashFlowSelectData();
    listData.initField();
    listData.setRowsNum(Integer.parseInt(ALEipUtils
        .getPortlet(rundata, context).getPortletConfig().getInitParameter(
            "p1a-rows")));
    listData.doViewList(this, rundata, context);
    setTemplate(rundata, "cashflow");
  }

  /**
   * 最大化表示の際の処理を記述します。 <BR>
   * 
   * @param portlet
   * @param context
   * @param rundata
   */
  protected void buildMaximizedContext(VelocityPortlet portlet,
      Context context, RunData rundata) {

    // MODEを取得
    String mode = rundata.getParameters().getString(ALEipConstants.MODE);
    context.put("rundata", rundata);
    
    try {
      if (ALEipConstants.MODE_FORM.equals(mode)) {
        doAccountPlan_form(rundata, context);
      } else if (ALEipConstants.MODE_DETAIL.equals(mode)) {
        doAccountPlan_detail(rundata, context);
      } else if (ALEipConstants.MODE_LIST.equals(mode)) {
        rundata.getSession().setAttribute("yojitsu", "yotei");
        doAccountPlan_list(rundata, context);
      } else if ("cashflow_list".equals(mode)) {
        doCashflow_list(rundata, context);
      } else if ("jisseki_list".equals(mode)) {
        rundata.getSession().setAttribute("yojitsu", "jisseki");
        doAccountPlan_list(rundata, context);
      }
      if (getMode() == null) {
        rundata.getSession().setAttribute("yojitsu", "yotei");
        doAccountPlan_list(rundata, context);
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
    }

  }

  /**
   * 帳票を一覧表示します。 <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doCashflow_list(RunData rundata, Context context)
      throws Exception {
    CashFlowSelectData listData = new CashFlowSelectData();
    listData.initField();
    listData.setRowsNum(Integer.parseInt(ALEipUtils
        .getPortlet(rundata, context).getPortletConfig().getInitParameter(
            "p1a-rows")));
    listData.doViewList(this, rundata, context);
    setTemplate(rundata, "cashflow-list");
  }

  /**
   * 入出金予定登録のフォームを表示します。 <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountPlan_form(RunData rundata, Context context) throws Exception {
    AccountPlanFormData formData = new AccountPlanFormData();
    formData.initField();
    formData.loadComboList(rundata, context);
    formData.doViewForm(this, rundata, context);
    setTemplate(rundata, "seikyu-form");
  }

  /**
   * 入出金予定を登録します。 <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountPlan_insert(RunData rundata, Context context) throws Exception {
    AccountPlanFormData formData = new AccountPlanFormData();
    formData.initField();
    formData.loadComboList(rundata, context);
    if (formData.doInsert(this, rundata, context)) {
      // データ登録が成功したとき
      doAccountPlan_list(rundata, context);
      // JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      // rundata.setRedirectURI(
      // jsLink
      // .getPortletById(ALEipUtils.getPortlet(rundata, context).getID())
      // .addQueryData("eventSubmit_doTodo_list", "1")
      // .toString());
      // rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      // jsLink = null;
    } else {
      setTemplate(rundata, "seikyu-form");
    }
  }

  /**
   * 入出金予定を更新します。 <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountPlan_update(RunData rundata, Context context) throws Exception {
    AccountPlanFormData formData = new AccountPlanFormData();
    formData.initField();
    formData.loadComboList(rundata, context);
    if (formData.doUpdate(this, rundata, context)) {
      // データ更新が成功したとき
      doAccountPlan_list(rundata, context);
      // JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      // rundata.setRedirectURI(jsLink.getPortletById(
      // ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
      // "eventSubmit_doTodo_list", "1").toString());
      // rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      // jsLink = null;
    } else {
      setTemplate(rundata, "seikyu-form");
    }
  }

  /**
   * 入出金予定を削除します。 <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountPlan_delete(RunData rundata, Context context) throws Exception {
    AccountPlanFormData formData = new AccountPlanFormData();
    formData.initField();
    if (formData.doDelete(this, rundata, context)) {
      // データ削除が成功したとき
      doAccountPlan_list(rundata, context);
      // JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      // rundata.setRedirectURI(jsLink.getPortletById(
      // ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
      // "eventSubmit_doTodo_list", "1").toString());
      // rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      // jsLink = null;
    }
  }

  /**
   * 入出金予定を削除します。（複数） <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountPlan_multi_delete(RunData rundata, Context context)
      throws Exception {
    AccountPlanMultiDelete delete = new AccountPlanMultiDelete();
    delete.doMultiAction(this, rundata, context);
    doAccountPlan_list(rundata, context);
    // JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
    // rundata.setRedirectURI(jsLink.getPortletById(
    // ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
    // "eventSubmit_doTodo_list", "1").toString());
    // rundata.getResponse().sendRedirect(rundata.getRedirectURI());
    // jsLink = null;
  }

  /**
   * 入出金予定を一覧表示します。 <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountPlan_list(RunData rundata, Context context) throws Exception {
    AccountPlanSelectData listData = new AccountPlanSelectData();
    listData.initField();
    listData.loadDataList(rundata, context);
    // PSMLからパラメータをロードする
    // 最大表示件数（最大化時）
    listData.setRowsNum(Integer.parseInt(ALEipUtils
        .getPortlet(rundata, context).getPortletConfig().getInitParameter(
            "p1b-rows")));
    listData.setStrLength(Integer.parseInt(ALEipUtils.getPortlet(rundata,
        context).getPortletConfig().getInitParameter("p4a-strlen")));
    listData.doViewList(this, rundata, context);
    setTemplate(rundata, "seikyu-list");
  }

  /**
   * 入出金予定を詳細表示します。 <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountPlan_detail(RunData rundata, Context context) throws Exception {
    AccountPlanSelectData detailData = new AccountPlanSelectData();
    detailData.initField();
    if (detailData.doViewDetail(this, rundata, context)) {
      setTemplate(rundata, "seikyu-detail");
    } else {
      doAccountPlan_list(rundata, context);
    }
  }
 
   private void clearCashFlowSession(RunData rundata, Context context) {
    List list = new ArrayList();
    // エンティティIDの初期化
    list.add("entityid");
    // 選択しているタブ情報の削除
    list.add("tab");
    list.add("publictab");
    list.add("target_user_id");
    list.add("target_group_name");
    list.add("com.aimluck.eip.cashflow.SeikyuSelectDatafilter");
    list.add("com.aimluck.eip.cashflow.SeikyuSelectDatafiltertype");
    list.add("com.aimluck.eip.cashflow.SeikyuCategorySelectDatasort");
    list.add("com.aimluck.eip.cashflow.SeikyuCategorySelectDatasorttype");
    list.add("com.aimluck.eip.cashflow.SeikyuPublicSelectDatasort");
    list.add("com.aimluck.eip.cashflow.SeikyuPublicSelectDatasorttype");
    list.add("com.aimluck.eip.cashflow.CashFlowSelectDatasort");
    list.add("com.aimluck.eip.cashflow.CashFlowSelectDatasorttype");
    ALEipUtils.removeTemp(rundata, context, list);
  }

}
