/*
 * 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.hanyo;

import java.io.File;
import java.util.Calendar;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.util.template.JetspeedLink;
import org.apache.jetspeed.util.template.JetspeedLinkFactory;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.eip.hanyo.HanyoFormData;
import com.aimluck.eip.hanyo.HanyoMultiDelete;
import com.aimluck.eip.hanyo.HanyoSelectData;

import com.aimluck.eip.common.ALCsvTokenizer;
import com.aimluck.eip.modules.actions.common.ALSecureBaseAction;
import com.aimluck.eip.util.ALEipUtils;

/**
 * 汎用マスタの取り扱いに関するアクションクラスです。
 * 
 */
public class HanyoAction extends ALSecureBaseAction {

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

  /**
   * @param portlet
   * @param context
   * @param rundata
   * @throws java.lang.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 {

    if (getMode() == null) {
      doHanyo_list(rundata, context);
    }
  }

  /**
   * 古くなった一時ファイルを消します
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */

  private void deleteTempFiles() throws Exception {
    String tmp_path = ALCsvTokenizer.CSV_TEMP_FOLDER;
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, -90); // ここで設定された時間より前のファイルを削除
    ALEipUtils.deleteOldFolder(new File(tmp_path), cal);
  }

  /**
   * 登録画面用のフォームを表示します。
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doHanyo_form(RunData rundata, Context context) throws Exception {
    HanyoFormData formData = new HanyoFormData();
    formData.initField();
    if (formData.doViewForm(this, rundata, context)) {
      setTemplate(rundata, "hanyo-form");
    } else {
      // setTemplate(rundata, "hanyo");
      doHanyo_list(rundata, context);
    }
  }

  /**
   * アカウントの登録を行います。
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doHanyo_insert(RunData rundata, Context context) throws Exception {
    HanyoFormData formData = new HanyoFormData();
    formData.initField();
    if (formData.doInsert(this, rundata, context)) {
      // データの登録に成功したとき
      // doHanyo_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doHanyo_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      JetspeedLinkFactory.putInstance(jsLink);
      jsLink = null;
    } else {
      setTemplate(rundata, "hanyo-form");
    }
  }

  /**
   * アカウントの削除を行います。
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doHanyo_delete(RunData rundata, Context context) throws Exception {
    HanyoFormData formData = new HanyoFormData();
    formData.initField();
    if (formData.doDelete(this, rundata, context)) {
      // データの削除に成功したとき
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doHanyo_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    }
  }

  /**
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doHanyo_update(RunData rundata, Context context) throws Exception {
    HanyoFormData formData = new HanyoFormData();

    formData.initField();
    if (formData.doUpdate(this, rundata, context)) {
      // データの更新に成功したとき
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doHanyo_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      // TODO add by Haruo Kaneko
      JetspeedLinkFactory.putInstance(jsLink);
      jsLink = null;
    } else {
      setTemplate(rundata, "hanyo-form");
    }
  }

  /**
   * 汎用一覧を表示します。
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doHanyo_list(RunData rundata, Context context) throws Exception {
    HanyoSelectData listData = new HanyoSelectData();
    // 会社/部署/役職テーブルデータをマップへロード
    listData.setRowsNum(Integer.parseInt(ALEipUtils
        .getPortlet(rundata, context).getPortletConfig().getInitParameter(
            "p1a-rows")));
    listData.doViewList(this, rundata, context);
    // 一時ファイルを削除
    // deleteTempFiles();
  }

  /**
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doHanyo_detail(RunData rundata, Context context) throws Exception {
    HanyoSelectData detailData = new HanyoSelectData();
    detailData.initField();
    if (detailData.doViewDetail(this, rundata, context)) {
      setTemplate(rundata, "hanyo-detail");
    } else {
      doHanyo_list(rundata, context);
    }
  }

  /**
   * 汎用マスタを削除します（複数） <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doHanyo_multi_delete(RunData rundata, Context context)
      throws Exception {
    HanyoMultiDelete delete = new HanyoMultiDelete();
    delete.doMultiAction(this, rundata, context);
    JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
    rundata.setRedirectURI(jsLink.getPortletById(
        ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
        "eventSubmit_doHanyo_list", "1").toString());
    rundata.getResponse().sendRedirect(rundata.getRedirectURI());
    jsLink = null;
  }
}
