/*
 * $Id: SetupInstancesAction.java,v 1.1 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 07.09.2004
 *
 * $Log: SetupInstancesAction.java,v $
 * Revision 1.1  2004/09/20 10:26:12  koppy
 * +javadoc Dokumentation
 *
 */
package vkc.workflow.web;

import java.util.List;

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.WfConstants;
import vkc.workflow.WorkflowServiceFactory;
import vkc.workflow.service.VKCWorkflowService;
import vkc.workflow.service.WfMConnection;

/**
 * This struts action class is used
 * to set neccessary request attributes so the view component
 * can access them.
 * 
 * @author Andreas Koop
 */
public class SetupInstancesAction extends Action{


    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());
        
        // My open processes
        List processes = wfService.getProcessManager()
			.getProcessInstances( 
			        user.getUser(), user.getClearPassword(), WfConstants.STATEPREFIX_OPEN );
        
        request.setAttribute( "open.workflows", processes);
        
        // My completed processes
        processes = wfService.getProcessManager()
			.getProcessInstances( 
			        user.getUser(), user.getClearPassword(), WfConstants.STATEPREFIX_CLOSED );
        
        request.setAttribute( "closed.completed", processes );
        
        // Context variables
        if( form.getDetailId() != null ){
            List context = wfService.getProcessManager().
            				getContextVariables( sConn, user.getLanguage().getLocale(),
            				        form.getDetailId(), request.getParameter("initVariables")!=null);
            request.setAttribute( "process.context", context);
        }
        
        return mapping.getInputForward();
    }
}
