<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@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="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessDefinitionStatesEnum"
%><%@page import="java.util.Arrays"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
    String selectedPlanID = (String)session.getAttribute("planID");
    Plan[] plans = getSortedTemplates(wfSession);
    String select = "";
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
    session.setAttribute("TabIndex","2");
    session.removeAttribute("WFAppClientFiles");
    session.removeAttribute("WFAppServerFiles");
    session.removeAttribute("WFAppManifest");
    session.removeAttribute("WFAppPackagePath");

    String editorURL = "ProcessEditor.jsp?AppletCommand=EditTemplate&amp;TemplateID=";
    String appletViewURL = "PDGraphic.jsp?planId=";
    //set URL
    StringBuffer reqURL = request.getRequestURL();
    reqURL.append("?");
    if (request.getQueryString() != null) {
        reqURL.append(request.getQueryString());
    }
%>


<HTML>
<HEAD>
<title>Interstage Business Process Manager | Process Definition List</TITLE>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>


<script>
function setPlanId(id, pForm)
{
    document.planForm.planToProcess.value=id
    document.planForm.submit()
}
function packageApp()
{
    var plansNum = document.forms[0].WFAppPlansNum.value;
    var i;
    var planList = "";
    if(plansNum == 0)
    {
       alert("Please create at least one Process Definition for packaging.");
       return;
    }
    else if(plansNum == 1)
    {
       if(document.forms[0].WFAppPlans.checked==true)
          planList = planList + "," + document.forms[0].WFAppPlans.value;
    }
    else
    {
       for(i=0;i<plansNum;i++)
       {
          if(document.forms[0].WFAppPlans[i].checked==true)
          {
             if(planList == "")
                planList = document.forms[0].WFAppPlans[i].value;
             else
                planList = planList + "," + document.forms[0].WFAppPlans[i].value;
          }
       }
    }
    if(planList == "")
    {
       alert("Please choose at least one Process Definition for packaging.");
       return;
    }

    document.location.href='ExportWFAppClientFiles.jsp?planList='+planList;
    return;
}
</script>
</HEAD>
<body class="bodyClass" onLoad="goforit();">
<center>
<jsp:include page="Header.jsp" flush="true"/>
<br>
<form name="planForm" method="post" action="<%= response.encodeURL("ProcessInstantiator.jsp") %>">
    <input type="hidden" name="planToProcess" value="-1">
    <TABLE border="0" cellpadding=0 cellspacing=0 width="90%" align="center">
        <tr>
            <td>
		<table border="0" cellspacing="0" cellpadding="0">
		  <tr background="images/buttons/com_button_bg.gif">
		    <td width="15"><img src="images/buttons/but_create_new.gif" width="30" height="24"></td>
		    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ProcessEditor.jsp?AppletCommand=NewTemplate">
		    <div align="center" class="buttonlink"> Create New</div></a></td>
		    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
		  </tr>
		</table>
                </td><td align="center"><table border="0" cellpadding="0" cellspacing="5"><tr><td>

<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_import_pkg.gif" width="29" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportWFAppPackage.jsp">
    <div align="center" class="buttonlink">&nbsp;Import from Package</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
                </td><td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_import_bpel.gif" width="34" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportBPEL.jsp">
    <div align="center" class="buttonlink">&nbsp;Import from BPEL</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
        </td><td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_import_xpdl.gif" width="32" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportXPDL.jsp">
    <div align="center" class="buttonlink">&nbsp;Import from XPDL</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
                </td></tr></table></td>
                <td align="right">
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_download.gif" width="32" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="Download.jsp">
    <div align="center" class="buttonlink">Download</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
            </td>
        </tr>
</TABLE><br>
    <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
    <COL WIDTH="6%"><COL WIDTH="23%"><COL width="40%"><COL WIDTH="9%"><COL WIDTH="6%"><COL WIDTH="10%">
        <tr align="center" class="headrow">
            <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')" colspan="7"><font color="#000000" size="2">My Process Definitions</font></td>
        </tr>
        <tr class="headrow">
            <th>Start</th>
            <th>Process Definition Name</th>
            <th>Description</th>
            <th>Version</th>
            <th>Id</th>
            <th>Upload</th>
            <th>Package</th>
        </tr>
<%
        for (int i = 0; i < plans.length; i++)
        {
            String planID = String.valueOf( plans[i].getId() );
            String state = "("+ProcessDefinitionStatesEnum.from_int(plans[i].getState()).toString()+")";
            String planName = plans[i].getName();
            String desc = plans[i].getDesc();
            if (desc==null) {
                desc = " ";
            }
            if ( selectedPlanID == null || !selectedPlanID.equals(planID) ) {
                select = "";
            }
            else {
                select = "checked";
            }
            boolean isEditable = true;
            if(plans[i].getState() == Plan.STATE_PUBLISHED){
                isEditable = false;
            }
            rowColor = bcolor[nextColor];
            nextColor = (nextColor + 1)%2;
%>
            <tr class="<%=rowColor%>">
                <td>

<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_start.gif" width="21" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" bgcolor="#D4D0C8">
<%
    if(plans[i].isLockedByOthers() == false)
    {
%>
    <a href="javascript:setPlanId(<%= planID %>, this.form)">
<%
    }
    else
    {
%>
    <a href="javascript:alert('The process definition is currently locked for editing by another user')">
<%
    }
%>
    <div align="center" class="buttonlink">Start</div></a>
    </td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
                </td>
                <td nowrap>
                <table width="100%" cellpadding="0" cellspacing="0" border="0" class="tableclass">
                <tr><td>
                <a href="ProcessDefinition.jsp?planID=<%=planID%>&amp;isEditable=<%=isEditable%>">
                    <% QuickForm.writeHtml(out, planName); %>
                    </a><br><font size="-2">
                    <% QuickForm.writeHtml(out, state); %>
                    </font></td>
               <td align="right">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
        <td align="center"><a href="<%=(editorURL+planID)%>"><img src="images/buttons/edit_small_icon.gif" width="17" height="17" border="0"></a></td><td rowspan="2">&nbsp;</td>
	    <td align="center"><a href="<%=(appletViewURL+planID)%>"><img src="images/buttons/view_small_icon.gif" width="17" height="17" border="0"></a></td>
  </tr>
  <tr>
    <td class="smalllinks" align="center"><a href="<%=(editorURL+planID)%>">EDIT</a></td>
     <td class="smalllinks" align="center"><a href="<%=(appletViewURL+planID)%>">VIEW</a></td>
  </tr>
</table>
</td></tr></table>

                </td>
                <td>
                    <% QuickForm.writeHtml(out, desc); %>
                </td>
                <td> <%= plans[i].getVersion() %> </td>
                <td><%= planID %></td>
                <td>

                <table border="0" cellspacing="0" cellpadding="0">
          <tr background="images/buttons/com_button_bg.gif">
            <td width="15"><img src="images/buttons/but_upload.gif" width="29" height="24"></td>
            <td background="images/buttons/com_button_bg.gif" nowrap><a href="ProcessInstantiator.jsp?planToProcess=<%= planID %>&amp;Upload=Upload"><div align="center" class="buttonlink">Upload</div></a></td>
            <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
          </tr>
        </table>

                    <!--<input type="submit" onClick="setPlanId(<%= planID %>, this.form)"
                           value="Upload" name="Upload" class="submitClass">-->
                </td>
                <td align="center">
                    <input type="checkbox" name="WFAppPlans" value="<%= planID %>">
                </td>
            </tr>
<%
        }
%>
<input type=hidden name=WFAppPlansNum value="<%= plans == null ? 0 : plans.length %>">

    </TABLE>
    </form>

<TABLE border="0" cellpadding=0 cellspacing=0 width="90%" align="center"><tr><td align="right" height="40">
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_package.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:packageApp()"><div align="center" class="buttonlink">Export into Package</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
<!-- <input type="button" onClick="return packageApp()" value="Export into Pkg" name="" class="core-button">-->
</td></tr></table>
        <TABLE border="0" cellpadding=0 cellspacing=0 width="90%" align="center">
        <tr>
            <td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_create_new.gif" width="30" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ProcessEditor.jsp?AppletCommand=NewTemplate">
    <div align="center" class="buttonlink"> Create New</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
                </td><td align="center"><table border="0" cellpadding="0" cellspacing="5"><tr><td>

<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_import_pkg.gif" width="29" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportWFAppPackage.jsp">
    <div align="center" class="buttonlink">&nbsp;Import from Package</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
                </td><td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_import_bpel.gif" width="34" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportBPEL.jsp">
    <div align="center" class="buttonlink">&nbsp;Import from BPEL</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
        </td><td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_import_xpdl.gif" width="32" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportXPDL.jsp">
    <div align="center" class="buttonlink">&nbsp;Import from XPDL</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
                </td></tr></table></td>
                <td align="right">
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_download.gif" width="32" height="24"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="Download.jsp">
    <div align="center" class="buttonlink">Download</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>
            </td>
        </tr>
</TABLE>

</center>
</BODY>
</HTML>

<%!
    /**
     * 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 i = 0; i < pList.length; i++)
            {
                plans[i] = (Plan)pList[i];
            }
            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;
        }
    }

%>
