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

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.office.OfficeFormData;
import com.aimluck.eip.office.OfficeMultiDelete;
import com.aimluck.eip.office.OfficeSelectData;
import com.aimluck.eip.common.ALCsvTokenizer;
import com.aimluck.eip.modules.actions.common.ALSecureBaseAction;
import com.aimluck.eip.util.ALEipUtils;

/**
 * 勤務先トの取り扱いに関するアクションクラスです。
 * 
 */
public class OfficeAction extends ALSecureBaseAction {

  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(OfficeAction.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) {
      doOffice_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 doOffice_form(RunData rundata, Context context) throws Exception {
    OfficeFormData formData = new OfficeFormData();
    formData.initField();
    if (formData.doViewForm(this, rundata, context)) {
      setTemplate(rundata, "office-form");
    } else {
      // setTemplate(rundata, "office");
      doOffice_list(rundata, context);
    }
  }

  /**
   * 勤務先の登録を行います。
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doOffice_insert(RunData rundata, Context context)
      throws Exception {
    OfficeFormData formData = new OfficeFormData();
    formData.initField();
    if (formData.doInsert(this, rundata, context)) {
      // データの登録に成功したとき
      // doOffice_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doOffice_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      JetspeedLinkFactory.putInstance(jsLink);
      jsLink = null;
    } else {
      setTemplate(rundata, "office-form");
    }
  }

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

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

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

  /**
   * 勤務先一覧を表示します。
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doOffice_list(RunData rundata, Context context) throws Exception {
    OfficeSelectData listData = new OfficeSelectData();
    // 会社/部署/役職テーブルデータをマップへロード
    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 doOffice_detail(RunData rundata, Context context)
      throws Exception {
    OfficeSelectData detailData = new OfficeSelectData();
    detailData.initField();
    if (detailData.doViewDetail(this, rundata, context)) {
      setTemplate(rundata, "office-detail");
    } else {
      doOffice_list(rundata, context);
    }
  }

  /**
   * 勤務先を削除します（複数） <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doOffice_multi_delete(RunData rundata, Context context)
      throws Exception {
    OfficeMultiDelete delete = new OfficeMultiDelete();
    delete.doMultiAction(this, rundata, context);
    JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
    rundata.setRedirectURI(jsLink.getPortletById(
        ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
        "eventSubmit_doOffice_list", "1").toString());
    rundata.getResponse().sendRedirect(rundata.getRedirectURI());
    jsLink = null;
  }
}
