/* $Id: AppConfirmAction.java,v 1.16 2005/08/26 04:45:18 ysahara Exp $
*
* Copyright (c)ARGO 21, Corporation. 2005.  All rights reserved.
 * 
 * This file is part of Nautica Workflow.
 * 
 *  Nautica Workflow 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 2 of the License, or
 *  (at your option) any later version.
 * 
 *  Nautica Workflow 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 Nautica Workflow; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  
*/
package jp.co.argo21.nautica.workflow.sample.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.argo21.nautica.workflow.sample.actionform.AppOvertimeInBean;
import jp.co.argo21.nautica.workflow.sample.actionform.ApplicationContentBean;
import jp.co.argo21.nautica.workflow.sample.actionform.ApplicationFormInBean;
import jp.co.argo21.nautica.workflow.sample.actionform.BeanFactory;
import jp.co.argo21.nautica.workflow.sample.actionform.TimeBean;
import jp.co.argo21.nautica.workflow.sample.actionform.UserBean;
import jp.co.argo21.nautica.workflow.sample.model.AppKind;
import jp.co.argo21.nautica.workflow.sample.model.Application;
import jp.co.argo21.nautica.workflow.sample.model.OvertimeApp;
import jp.co.argo21.nautica.workflow.sample.model.dao.ApplicationDAO;
import jp.co.argo21.nautica.workflow.sample.model.dao.DAOFactory;
import jp.co.argo21.nautica.workflow.sample.util.ResourceManager;
import jp.co.argo21.nautica.workflow.sample.util.Constants;
import jp.co.argo21.nautica.workflow.sample.util.Validator;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 申請入力画面で確認ボタン押下時の処理を行うアクションクラス。
 * 
 * @author  ruchida(Argo 21, Corp.)
 * @version $Revision: 1.16 $
 * @since   Nautica Workflow 0.9
 */
public class AppConfirmAction extends Action {

    /**
     * 申請内容確認処理を行う。
     * 
     * @param mapping アクションマッピング
     * @param form アクションフォーム
     * @param request リクエスト
     * @param response レスポンス
     * @return アクションフォワード
     */
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
    {
        // リクエスト・セッションから申請種別、ユーザ、申請書番号を取得
        ApplicationFormInBean formbean = (ApplicationFormInBean)form;
        AppKind appkind = (AppKind)request.getSession().getAttribute("appkind");
        UserBean userbean = (UserBean) request.getSession().getAttribute("user");
        String appID = (String)request.getSession().getAttribute("appid");
        
        // 申請にリクエストから得た値を設定
        BeanFactory beanfactory = BeanFactory.getInstance(appkind.getAppkindID());
        Application model = beanfactory.getModel(formbean);
        model.setKind(appkind);
        model.setUserid(userbean.getUserID());
        model.setUser(userbean.getUser());
        model.setId(appID);
        
        // 入力された値をチェック
        List errMsgs = new ArrayList();
        ResourceManager rm = ResourceManager.getInstance();
        if ( (AppKind.OVERTIME_APPLICATION_ID).equals((appkind.getAppkindID())) ) {
            AppOvertimeInBean overtimeBean = (AppOvertimeInBean)formbean;
            OvertimeApp overtimeApp = (OvertimeApp) model;
            
            int startHour = overtimeApp.getStartHour();
            int startMin = overtimeApp.getStartMin();
            int endHour = overtimeApp.getEndHour();
            int endMin = overtimeApp.getEndMin();
            float overtimeHours = overtimeApp.getOvertimeHours();
            float hour = endHour - startHour;
            int min = endMin - startMin;
            if (min == 30) hour += 0.5; 
            
            // 終了予定時刻が開始予定時刻よりも早い場合はエラー
            if (startHour > endHour) {
                errMsgs.add(rm.getValue("error.time"));
            } else if (startHour == endHour) {
                // 開始予定時刻と終了予定時刻の間隔が30分未満の場合はエラー
                if ((endMin - startMin) < 30 ) errMsgs.add(rm.getValue("error.time"));
            } else {
                // 予定時刻が30分間隔で指定されていない場合はエラー
                if (min != 30 && min != 0 && min != -30) errMsgs.add(rm.getValue("error.time"));
            }
            
            // 予定時間が予定時刻をオーバーする場合はエラー
            if (hour < overtimeHours) errMsgs.add(rm.getValue("error.hour"));
                        
            // 事由が空の場合はエラー
            if (Validator.isEmpty(overtimeBean.getReason())) {
                errMsgs.add(rm.getValue("app.overtime.reason") + rm.getValue("error.empty"));
            }
        }
        
        // エラーがある場合、申請入力画面に遷移し、エラーを表示する。
        if (errMsgs.size() != 0) {
            request.setAttribute("errorMsg", errMsgs.toArray());
            request.setAttribute("time", TimeBean.getCurrentTimeBean());
            return mapping.findForward(Constants.ERROR);
        }
        
        try {
            // 申請内容を申請テーブルに登録
            ApplicationDAO appDao = DAOFactory.getApplicationDAO();
            appDao.updateApplication(model);
            ApplicationDAO appKindDao = DAOFactory.getApplicationDAOFromAppKindID(appkind.getAppkindID());
            appKindDao.updateApplication(model);
            
            // 画面表示用のモデルを生成
            ApplicationContentBean contentBeanFromModel = beanfactory.getContentBeanFromModel(model);
            
            // リクエスト、セッションに申請内容を設定
            request.setAttribute("app", contentBeanFromModel);
            request.getSession().setAttribute("appmodel", model);
            
            return mapping.findForward(Constants.SUCCESS);
            
        } catch (SQLException e) {
            // セッション無効化
            request.getSession().invalidate();
            // 致命的エラー
            request.setAttribute("errMsg", e.toString());
            request.setAttribute("err", e.getStackTrace());
            return mapping.findForward(Constants.FATAL_ERROR);
        } catch (Exception e) {
            // セッション無効化
            request.getSession().invalidate();
            // 予期せぬエラー
            request.setAttribute("errMsg", e.toString());
            request.setAttribute("err", e.getStackTrace());
            return mapping.findForward(Constants.UNEXPECTED_ERROR);
        }
    }
}