/*
 * $Id: SetupWorklistAction.java,v 1.2 2004/10/08 14:01:26 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 08.09.2004
 *
 * $Log: SetupWorklistAction.java,v $
 * Revision 1.2  2004/10/08 14:01:26  koppy
 * bugfixes
 *
 * Revision 1.1  2004/09/20 10:26:13  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.db.serializer.VKCUserSerializer;
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 struts action class is used
 * to set neccessary request attributes so the view component
 * can access them.
 * 
 * @author Andreas Koop
 */
public class SetupWorklistAction 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();
   
        try {
            // Not accepted workitems
            List workItems = wlManager.getOpenWorkItems( sConn );
            request.setAttribute( "workitems.open", workItems );
            
            // Accepted
            workItems = wlManager.getAcceptedWorkItems( sConn);
            request.setAttribute( "workitems.accepted", workItems );
            
            if( form.getDetailId() != null ){
                List actContext = wlManager.getContextVariables( sConn, user.getLanguage().getLocale(), form.getDetailId());
                request.setAttribute( "activity.context", actContext);
            }
            
            sConn.disconnect();
            
            if( "reassign.display".equals(form.getAction())){
                form.setUserInfos( VKCUserSerializer.retrieveUserInfos());
            }
        } catch (WorkflowException e) {
            WorkflowUtils.handle(request, e);
        } 

        
        return mapping.getInputForward();
    }
}
