/*
 * $Id: ProcessInstancesAction.java,v 1.2 2004/09/20 10:26:12 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 14.07.2004
 *
 * $Log: ProcessInstancesAction.java,v $
 * Revision 1.2  2004/09/20 10:26:12  koppy
 * +javadoc Dokumentation
 *
 * Revision 1.1  2004/08/30 14:49:57  koppy
 * Initiale Version der Workflow-Komponente
 *
 */
package vkc.workflow.web;

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

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

import vkc.db.entities.VKCUser;
import vkc.util.ServletUtils;
import vkc.workflow.WorkflowException;
import vkc.workflow.WorkflowServiceFactory;
import vkc.workflow.service.VKCWorkflowService;
import vkc.workflow.service.WfMConnection;
import vkc.workflow.util.WorkflowUtils;

/**
 * This class represents a struts action for 
 * the following workflow operations:
 * <ul>
 * <li>deleting of closed workflow instances</li>
 * <li>updating workflow variables</li>
 * <li>starting a workflow (after setting initial variables)</li>
 * <li>aborting a running workflow</li>
 * </ul>
 * @author Andreas Koop
 */
public class ProcessInstancesAction extends Action {
    /** 
     * (non-Javadoc)
     * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward execute(ActionMapping mapping, ActionForm f,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ProcessInstancesForm 	form	= (ProcessInstancesForm)f;
        VKCUser 				user 	= ServletUtils.getUser( request );
        VKCWorkflowService wfService 	= WorkflowServiceFactory.getWorkflowService();
        WfMConnection 			sConn   = new WfMConnection( user.getUser(), user.getClearPassword());
        
        
        String action = form.getAction();
        try {
            if( "deleteClosed".equals(action)){
                
                if(form.getProcId() != null)
                    wfService.getProcessManager()
            			.deleteClosedProcess( sConn, form.getProcId());
                else
                    wfService.getProcessManager()
        				.deleteClosedProcesses( sConn);
                
            }else if( "updateVariables".equals(action)){
                System.out.println( form.getDynaValueMap().get( "category_id" ) );
                wfService.getProcessManager().updateVariables(
                        sConn, form.getProcId(), form.getDynaValueMap());
                WorkflowUtils.addMessage(request, "wf.message.variables_updated" );
                return new ActionForward( mapping.getInputForward().getPath()+ "?detailId=" + form.getProcId());
                
            }else if( "startProcess".equals(action)){
                
                wfService.getProcessManager().updateInitialVariables(
                        sConn, form.getProcId(), form.getDynaValueMap());
                wfService.getProcessManager()
           				.startProcess( user, form.getProcId());
                
                WorkflowUtils.addMessage(request, "wf.message.process_started" );
            }else if( "abortProcess".equals(action)){
                
                wfService.getProcessManager()
           				.abortProcess( user, form.getProcId());
                
                WorkflowUtils.addMessage(request, "wf.message.process_aborted" );
            } 
            
        } catch (WorkflowException e) {
            WorkflowUtils.handle(request,e);
        }
        
        return mapping.getInputForward();
    }
}
