/* $Id: AppCancelAction.java,v 1.8 2005/08/26 04:45:11 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.rmi.RemoteException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.rpc.ServiceException;

import jp.co.argo21.nautica.workflow.sample.actionform.UserBean;
import jp.co.argo21.nautica.workflow.sample.model.dao.AppTransactionDAO;
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.SingletonServices;
import jp.co.argo21.nautica.workflow.sample.util.Constants;
import jp.co.argo21.nautica.workflow.soap.WorkItemHandler;
import jp.co.argo21.nautica.workflow.soap.message.Attribute;
import jp.co.argo21.nautica.workflow.soap.message.Filter;
import jp.co.argo21.nautica.workflow.soap.message.WorkItem;

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.8 $
 * @since   Nautica Workflow 0.9
 */
public class AppCancelAction extends Action {
    
    /**
     * 申請のキャンセル処理を行う。
     * 
     * @param mapping アクションマッピング
     * @param form    アクションフォーム
     * @param request リクエスト
     * @param response レスポンス
     * @return アクションフォワード
     */
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
    {
        // リクエストから申請書番号、申請種別、ユーザを取得
        String appID = (String)request.getSession().getAttribute("appid");
        UserBean user = (UserBean)request.getSession().getAttribute("user");
        String sessionID = user.getSessionID();
        
        try {
            // 申請トランザクションテーブルと個別テーブルの該当レコードを削除
            ApplicationDAO appDao = DAOFactory.getApplicationDAO();
            appDao.remove(appID);
            AppTransactionDAO appTransDao = DAOFactory.getAppTransactionDAO();
            appTransDao.remove(appID);
            
            // 申請書番号をフィルターに指定して、作業項目を取得
            WorkItemHandler wfWorkitemHandler = SingletonServices.getInstance().getWfWorkitemHandler();
            Filter[] filters = new Filter[1];
            Filter filter = new Filter();
            filter.setFilterName(Constants.APPLICATION_NO);
            filter.setFilterString(appID);
            filter.setType(5003);
            filter.setComparison(1);
            filters[0]=filter;
            WorkItem[] workItems = wfWorkitemHandler.getWorkItems(sessionID, filters);
            String wid = workItems[0].getID();
            
            // 作業項目のプロセス変数「申請書番号」に空文字を設定
            Attribute attr = new Attribute();
            attr.setName(Constants.APPLICATION_NO);
            attr.setValue("");
            attr.setType(1);
            wfWorkitemHandler.assignWorkItemAttribute(sessionID, wid, attr);
            
            // 作業項目の完了
            wfWorkitemHandler.completeWorkItem(sessionID,wid);
           
            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 (RemoteException e) {
            // セッション無効化
            request.getSession().invalidate();
            // 致命的エラー
            request.setAttribute("errMsg", e.toString());
            request.setAttribute("err", e.getStackTrace());
            return mapping.findForward(Constants.FATAL_ERROR);
        } catch (ServiceException 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);
        }    
    }
}