/*
 * $Id: SetupDefinitionsAction.java,v 1.3 2004/11/12 00:24:35 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 17.07.2004
 *
 * $Log: SetupDefinitionsAction.java,v $
 * Revision 1.3  2004/11/12 00:24:35  marbobo
 * + getGroupInfos() in retrieveGroupInfos() umgebannt
 *
 * 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 java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
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.VKCGroupSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.util.ServletUtils;
import vkc.workflow.WorkflowServiceFactory;
import vkc.workflow.model.MappingInfo;
import vkc.workflow.model.PackageInfo;
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 SetupDefinitionsAction extends Action{

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ProcessDefinitionsForm f		= (ProcessDefinitionsForm)form;
        VKCUser 		user 			= ServletUtils.getUser( request );
        VKCWorkflowService wfService 	= WorkflowServiceFactory.getWorkflowService();
        WfMConnection sConn				= new WfMConnection( user.getUser(), user.getClearPassword());
        
        
        String action = f.getAction();
        String detailId = f.getDetailId();
        
        if( "partMapping.show".equals(action)){
        
            List mappings = wfService
				.getUserMappingManager()
				.getUserMappings( sConn, f.getPkgId(), f.getProcDefId() );
            debug( mappings );
            MappingInfo [] partMappings = new MappingInfo[mappings.size()];
            mappings.toArray(partMappings);
            
            f.setPartMappings( partMappings );
            f.setUserInfos( VKCUserSerializer.retrieveUserInfos());
            
            Collection newCol = new ArrayList();
            newCol.addAll( VKCGroupSerializer.retrieveGroupInfos() );
            f.setGroupInfos( newCol ); 
	    }
        
        // Processes to Start
        List packages = wfService.getPackageManager().getOpenPackages( sConn );
        if( detailId != null){
            List defs = wfService.getPackageManager().getProcessesToStart( user, detailId);
            this.compose( packages, defs, detailId );
        }
        request.setAttribute( "packages", packages);
        
        return mapping.getInputForward();
    }
    
    /**
     * @param packages
     * @param processes
     */
    private void compose(List packages, List defs, String pkgId) {
        if( defs == null || defs.isEmpty())
            return;
        
        for (Iterator iter = packages.iterator(); iter.hasNext();) {
            PackageInfo pkg = (PackageInfo) iter.next();
            if(pkgId.equals( pkg.getPackageId())){
                pkg.setProcessDefinitions( defs );
                return;
            }
        }
        
    }
    
    /**
     * @param mappings
     */
    private void debug(List mappings) {
        for (Iterator iter = mappings.iterator(); iter.hasNext();) {
            MappingInfo element = (MappingInfo) iter.next();
            System.err.println( element );
        }
    }
}
