<%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.Filter"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="java.util.Arrays"
%>

<%!
    /**
     * This method returns the plans sorted by Id.
     */
    public
    Plan[]
    getSortedTemplates(
        WFSession wfSession)
        throws Exception
    {
        WFObjectList wfObjectList = null;
        wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
        wfObjectList.openList(Filter.AllPlans);
        Object[] pList = wfObjectList.getAllObjects();
        wfObjectList.closeList();
        Plan[] plans = new Plan[0];
        if ( pList != null && pList.length != 0 ) {
            plans = new Plan[pList.length];
            for (int pListIndex = 0; pListIndex < pList.length; pListIndex++)
            {
                plans[pListIndex] = (Plan)pList[pListIndex];
            }
            Arrays.sort(plans, new PlanIDComparator());
        }

        return plans;

    }
	  /**
    *   This comparator is used to sort the template list into ascending order
    *   relatively to their ID.
    */
    class PlanIDComparator implements java.util.Comparator
    {
        public int compare(Object obj1, Object obj2)
        {
            if ( obj1 == null || obj2 == null ||
                !(obj1 instanceof Plan) || !(obj2 instanceof Plan) )
                return 0;
            Plan plan1 = (Plan)obj1;
            Plan plan2 = (Plan)obj2;
            if ( plan1.getId() < plan2.getId() ) return -1;
            else return 1;
        }
    }
	
	   /**
     * This method returns the name of the plan based on Id.
     */
    public
    long
    getProcessDefinitionId(
        WFSession wfSession,String planName)
        throws Exception
    {
	long pdId = -1;
        WFObjectList wfObjectList = null;
        wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
        wfObjectList.openList(Filter.AllPlans);
        Object[] pList = wfObjectList.getAllObjects();
        wfObjectList.closeList();
         if ( pList != null && pList.length != 0 ) {
            for (int pListIndex = 0; pListIndex < pList.length; pListIndex++)
            {
               Plan plan =  (Plan)pList[pListIndex];
			   if(plan.getName().equals(planName)){
			    return plan.getId();
			   }
            }
        }
        return pdId;
    }
    /**
	 * This method returns the Id of the process definition based on the latest latest version.
	 */
        public
        long
        getProcessDefinitionLatestId(
            WFSession wfSession,String planName)
            throws Exception
        {
    	long pdId = -1;
            WFObjectList wfObjectList = null;
            wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
            wfObjectList.openList(Filter.AllPlans);
            Object[] pList = wfObjectList.getAllObjects();
            wfObjectList.closeList();
             if ( pList != null && pList.length != 0 ) {
                for (int pListIndex = 0; pListIndex < pList.length; pListIndex++)
                {
                   Plan plan =  (Plan)pList[pListIndex];
    			   if(plan.getName().equals(planName)){
    			    pdId=plan.getId();
    			   }
                }
            }
            return pdId;
    }
	%>
