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

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;

import net.sf.json.JSONArray;

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.juchu.JuchuChangeTurnFormData;
import com.aimluck.eip.juchu.JuchuPasswdFormData;
import com.aimluck.eip.juchu.JuchuUserFormData;
import com.aimluck.eip.juchu.JuchuUserMultiDelete;
import com.aimluck.eip.juchu.JuchuUserMultiDisable;
import com.aimluck.eip.juchu.JuchuUserMultiEnable;
import com.aimluck.eip.juchu.JuchuUserSelectData;
import com.aimluck.eip.common.ALCsvTokenizer;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.modules.actions.common.ALAction;
import com.aimluck.eip.modules.actions.common.ALSecureBaseAction;
import com.aimluck.eip.util.ALEipUtils;

import net.sf.json.JSONArray;

/**
 * ユーザーアカウントの取り扱いに関するアクションクラスです。
 * 
 */
public class JuchuAction extends ALSecureBaseAction {

  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(JuchuAction.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) {
      doJuchu_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 doJuchu_form(RunData rundata, Context context) throws Exception {
    JuchuUserFormData formData = new JuchuUserFormData();
    formData.initField();
    if (formData.doViewForm(this, rundata, context)) {
      setTemplate(rundata, "juchu-form");
    } else {
      // setTemplate(rundata, "juchu");
      doJuchu_list(rundata, context);
    }
  }

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

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

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

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

  /**
   * アカウント一覧を表示します。
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_list(RunData rundata, Context context) throws Exception {
    JuchuUserSelectData listData = new JuchuUserSelectData();
    // 会社/部署/役職テーブルデータをマップへロード
    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 doJuchuSearch_list(ALAction action,RunData rundata, Context context) throws Exception {
    JuchuUserSelectData listData = new JuchuUserSelectData();
    // 会社/部署/役職テーブルデータをマップへロード
    listData.setRowsNum(Integer.parseInt(ALEipUtils
        .getPortlet(rundata, context).getPortletConfig().getInitParameter(
            "p1a-rows")));
    listData.doViewListSearch(action, rundata, context);
    // 一時ファイルを削除
    // deleteTempFiles();
  }

  /**
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_detail(RunData rundata, Context context)
      throws Exception {
    JuchuUserSelectData detailData = new JuchuUserSelectData();
    detailData.initField();
    if (detailData.doViewDetail(this, rundata, context)) {
      setTemplate(rundata, "juchu-detail");
    } else {
      doJuchu_list(rundata, context);
    }
  }

  /**
   * ユーザーアカウントを削除します（複数） <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_multi_delete(RunData rundata, Context context)
      throws Exception {
    JuchuUserMultiDelete delete = new JuchuUserMultiDelete();
    delete.doMultiAction(this, rundata, context);
    JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
    rundata.setRedirectURI(jsLink.getPortletById(
        ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
        "eventSubmit_doJuchu_list", "1").toString());
    rundata.getResponse().sendRedirect(rundata.getRedirectURI());
    jsLink = null;
  }

  /**
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_change_turn_form(RunData rundata, Context context)
      throws Exception {
    // ユーザ情報の詳細画面や編集画面からの遷移時に，
    // セッションに残る ENTITY_ID を削除する．
    ALEipUtils.removeTemp(rundata, context, ALEipConstants.ENTITY_ID);

    JuchuChangeTurnFormData formData = new JuchuChangeTurnFormData();
    formData.initField();

    if (formData.doViewForm(this, rundata, context)) {
      setTemplate(rundata, "juchu-change-turn");
    } else {
      doJuchu_list(rundata, context);
    }
  }

  /**
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_change_turn_update(RunData rundata, Context context)
      throws Exception {
    JuchuChangeTurnFormData formData = new JuchuChangeTurnFormData();
    formData.initField();

    if (formData.doUpdate(this, rundata, context)) {
      // データの更新に成功したとき
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doJuchu_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      JetspeedLinkFactory.putInstance(jsLink);
      jsLink = null;
    } else {
      setTemplate(rundata, "juchu-change-turn");
    }
  }

}
