/*
 * $Id: WorklistAction.java,v 1.4 2004/11/24 00:41:50 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 07.07.2004
 *
 * $Log: WorklistAction.java,v $
 * Revision 1.4  2004/11/24 00:41:50  koppy
 * Anzeige der Variablen nach dem Akzeptieren
 *
 * Revision 1.3  2004/10/08 14:01:26  koppy
 * bugfixes
 *
 * Revision 1.2  2004/09/20 10:26:13  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 org.apache.struts.action.ActionMessage;

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.service.manager.WorklistManager;
import vkc.workflow.util.WorkflowUtils;

/**
 * This class represents a struts action for 
 * the following workflow operations:
 * <ul>
 * <li>accepting/completing workitems</li>
 * <li>updating workflow variables</li>
 * <li>reassigning workitems to other users</li>
  * </ul>
 * @author Andreas Koop
 */
public class WorklistAction 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 {
        
        VKCUser 		user 			= ServletUtils.getUser( request );
        WorklistForm 	form			= (WorklistForm)f;
        
        VKCWorkflowService wfService= WorkflowServiceFactory.getWorkflowService();
        WfMConnection sConn 		= wfService.connect( user.getUser(), user.getClearPassword());
        WorklistManager wlManager   = wfService.getWorklistManager();
        
        String activityId 	= form.getActivityId();
        String action 		= form.getAction();
        
       try {
            if ("complete".equals(action)) {
                if(request.getParameter("updateVariables") != null)
                    wlManager.updateVariables(sConn, activityId, form.getDynaValueMap());
                
                wlManager.completeActivity(sConn, activityId);
            } else if ("accept".equals(action)) {
                wlManager.acceptActivity(sConn, form.getDetailId());
            } else if ("updateVariables".equals(action)) {
                wlManager.updateVariables(sConn, activityId, form
                        .getDynaValueMap());
                WorkflowUtils.addMessage(request, "wf.message.variables_updated" );
                
                return new ActionForward( mapping.getInputForward().getPath()+ "?detailId=" + activityId);
            } else if ("reassign.save".equals(action)) {
                wlManager.reassign(sConn, activityId, form.getNewAssigneeName());
                WorkflowUtils.addMessage(request, new ActionMessage("wf.message.reassigned", form.getNewAssigneeName()) );
            } 
            
        } catch (WorkflowException e) {
            WorkflowUtils.handle(request,e);
        }
        
        sConn.disconnect();

        
        return mapping.getInputForward();
    }
}
