/*
 * 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 org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.resources.JetspeedResources;
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.JuchuCompanyFormData;
import com.aimluck.eip.juchu.JuchuPositionFormData;
import com.aimluck.eip.juchu.JuchuPositionMultiDelete;
import com.aimluck.eip.juchu.JuchuPositionSelectData;
import com.aimluck.eip.juchu.JuchuPostFormData;
import com.aimluck.eip.juchu.JuchuPostMultiDelete;
import com.aimluck.eip.juchu.JuchuPostSelectData;
import com.aimluck.eip.modules.actions.common.ALSecureBaseAction;
import com.aimluck.eip.util.ALEipUtils;

/**
 * システム管理画面にて、会社情報を管理するアクションクラスです。
 *
 */
public class JuchuCompanyAction extends ALSecureBaseAction {

  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(JuchuCompanyAction.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 {

    String chg_adminpasswd = JetspeedResources.getString("aipo.chg_adminpasswd", "false");
    context.put("chg_adminpasswd", chg_adminpasswd);

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

  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_company_form(RunData rundata, Context context)
      throws Exception {
    JuchuCompanyFormData formData = new JuchuCompanyFormData();
    formData.initField();
    formData.doViewForm(this, rundata, context);
    setTemplate(rundata, "juchu-company-form");
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_company_update(RunData rundata, Context context)
      throws Exception {

    JuchuCompanyFormData formData = new JuchuCompanyFormData();
    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());
      jsLink = null;
    } else {
      setTemplate(rundata, "juchu-company-form");
    }
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_post_form(RunData rundata, Context context)
      throws Exception {
    JuchuPostFormData formData = new JuchuPostFormData();
    formData.initField();
    formData.doViewForm(this, rundata, context);
    setTemplate(rundata, "juchu-post-form");
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_post_insert(RunData rundata, Context context)
      throws Exception {
    JuchuPostFormData formData = new JuchuPostFormData();
    formData.initField();
    if (formData.doInsert(this, rundata, context)) {
      // doJuchu_post_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doJuchu_post_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    } else {
      setTemplate(rundata, "juchu-post-form");
    }
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_post_delete(RunData rundata, Context context)
      throws Exception {
    JuchuPostFormData formData = new JuchuPostFormData();
    formData.initField();
    if (formData.doDelete(this, rundata, context)) {
      // doJuchu_post_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doJuchu_post_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    }
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_post_update(RunData rundata, Context context)
      throws Exception {
    JuchuPostFormData formData = new JuchuPostFormData();
    formData.initField();
    if (formData.doUpdate(this, rundata, context)) {
      // doJuchu_post_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doJuchu_post_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;

    } else {
      setTemplate(rundata, "juchu-post-form");
    }
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_post_list(RunData rundata, Context context)
      throws Exception {
    JuchuPostSelectData listData = new JuchuPostSelectData();
    listData.setRowsNum(Integer.parseInt(ALEipUtils
        .getPortlet(rundata, context).getPortletConfig().getInitParameter(
            "p1a-rows")));
    listData.doViewList(this, rundata, context);
    setTemplate(rundata, "juchu-post-list");
  }

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

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

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_position_form(RunData rundata, Context context)
      throws Exception {
    JuchuPositionFormData formData = new JuchuPositionFormData();
    formData.initField();
    formData.doViewForm(this, rundata, context);
    setTemplate(rundata, "juchu-position-form");
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_position_insert(RunData rundata, Context context)
      throws Exception {
    JuchuPositionFormData formData = new JuchuPositionFormData();
    formData.initField();
    if (formData.doInsert(this, rundata, context)) {
      // doJuchu_position_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doJuchu_position_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    } else {
      setTemplate(rundata, "juchu-position-form");
    }
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_position_delete(RunData rundata, Context context)
      throws Exception {
    JuchuPositionFormData formData = new JuchuPositionFormData();
    formData.initField();
    if (formData.doDelete(this, rundata, context)) {
      // doJuchu_position_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doJuchu_position_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    }
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_position_update(RunData rundata, Context context)
      throws Exception {
    JuchuPositionFormData formData = new JuchuPositionFormData();
    formData.initField();
    if (formData.doUpdate(this, rundata, context)) {
      // doJuchu_position_list(rundata, context);
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getPortletById(
          ALEipUtils.getPortlet(rundata, context).getID()).addQueryData(
          "eventSubmit_doJuchu_position_list", "1").toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    } else {
      setTemplate(rundata, "juchu-position-form");
    }
  }

  /**
   *
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doJuchu_position_list(RunData rundata, Context context)
      throws Exception {
    JuchuPositionSelectData listData = new JuchuPositionSelectData();
    listData.setRowsNum(Integer.parseInt(ALEipUtils
        .getPortlet(rundata, context).getPortletConfig().getInitParameter(
            "p1b-rows")));
    listData.doViewList(this, rundata, context);
    setTemplate(rundata, "juchu-position-list");
  }

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

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

}
