<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="AdminException.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WFAdminSession"
%><%@page import="com.fujitsu.iflow.model.wfcore.WFAdminSessionImpl"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessDefinitionStatesEnum"
%><%@page import="com.fujitsu.iflow.server.dataobjects.FilterEnum"
%><%@page import="com.fujitsu.iflow.model.workflow.Filter"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="java.util.Date"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.SimpleDateFormat"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMAdminImpl"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }

    BPMAdmin bpas = bps.getAdmin();
    WFAdminSessionImpl wfSession = ((BPMAdminImpl)bpas)._wfas;

    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
    session.setAttribute("TabIndex","0");

    String pdSortedURL= "SortedArchivedPlanList.jsp";

    int _pageSize = 20;
    long id = -1;
    long planId = -1;

    String pageOption = request.getParameter("wPageId");
    if (!(pageOption != null && pageOption.length() >= 1))
    {
        pageOption = "1";
    }

    //DO NOT MERGE THE ABOVE CODE INTO THE IF-LOOP below
    int wPageValue = Integer.parseInt(pageOption);
    if (wPageValue < 1)
        wPageValue = 1;


    Plan[] plans = getTemplates(Filter.AllArchivedPlans, wPageValue, _pageSize, wfSession);
    // stupid habit of returning null for empty set
    if (plans == null) {
        plans = new Plan[0];
    }

%>
<html>
<head>
<title>Interstage Business Process Manager | Archived Process Definitions</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<script>

function setSelection(wiForm, selPage)
{
    location.href='ArchivedPlans.jsp?wPageId=' + selPage
}

function importXPDL(id)
{
    location.href="ImportXPDL.jsp?type=xpdl";
}

function importXML(id)
{
    location.href="ImportXPDL.jsp?type=xml";
}

</script>
</head>

<body class="adminBodyClass">
<center>

<jsp:include page="AdminHeader.jsp" flush="true"/>
<form name="adForm" method="get" action="PlanSummary.jsp">
    <input type="hidden" name="wPageId" value="<%= wPageValue %>">
<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center" class="tableclass">
<tr>
<!-- end buttons -->
    <%
    String selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
    String btnEnabled = "";
    if (wPageValue > 1 && _pageSize != 0)
        selOption = "setSelection(this.form, " + (wPageValue -1) +")";
    else {
        //selOption = "setSelection(this.form, 1)";
        btnEnabled = "disabled";
    }
%>
    <td width="100%" align="right" valign="top" bgcolor="#FFFFFF">
<input value="Prev Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">
<%
    btnEnabled = "";
    selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
    if (plans.length < _pageSize || _pageSize == 0)
            btnEnabled = "disabled";
    else
    {
        Plan[] mplans = getTemplates(Filter.AllArchivedPlans, wPageValue, _pageSize, wfSession);
        if(mplans == null || mplans.length == 0)
	        btnEnabled = "disabled";
    }
    %>
    <input value="Next Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">
</td>
</tr>
</table>
<br>
<table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
<tr align="center" class="headrow">
    <td colspan="10" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">All Archived Process Definitions</font></td>
</tr>
  <tr align="center" class="headrow">
                    <td> Id </td>
                    <th> Process Definitions Name </th>
                    <th></th>
                  </tr>
                  <%
    for (int i = 0; i < plans.length; i++)
    {
        Plan plan = plans[i];
        id = plan.getId();
        String planName = plan.getName();
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
%>
                  <tr class="<%=rowColor%>">
                    <td width=15%><%= id %></td>
                    <td width=60%>
	                    <table border="0" width="100%" cellpadding="0" cellspacing="0">
	                    	<tr>
	                    		<td nowrap>
						        	<font face="arial" size="2">
						        		<% QuickForm.writeHtmlWithNBSPs(out, planName); %>
					        		</font>
				        		</td>
					            <td align="right">
									<table border="0" cellspacing="0" cellpadding="0">
									  <tr>
									    <td align="center"><div align="center"><a href="CopyPlan.jsp?planId=<%=id%>&isArchived=true" title="Click here to copy this Process Definition"><img src="images/buttons/copy_small_icon.gif" width="20" height="17" border="0"></a></div></td>
									    <td rowspan="2">&nbsp;</td>
									    <td><div align="center"><a href="PDGraphic.jsp?planId=<%=id%>&isArchived=true" title="Click here to view this Process Definition"><img src="images/buttons/view_small_icon.gif" width="17" height="17" border="0"></a></div></td>
									  </tr>
									  <tr>
									    <td><a href="CopyPlan.jsp?planId=<%=id%>&isArchived=true" title="Click here to copy this Process Definition"><div align="center" class="smalllinks">COPY</div></a></td>
									    <td><a href="PDGraphic.jsp?planId=<%=id%>&isArchived=true" title="Click here to view this Process Definition"><div class="smalllinks">VIEW</div></a></td>
									  </tr>
									</table>
				            	</td>
			            	</tr>
		            	</table>
					</td>
                    <td width=10%>
                        <table border="0" cellspacing="0" cellpadding="0">
                          <tr background="images/buttons/com_button_bg.gif">
                            <td width="15"><a href="PDGraphicBPMN.jsp?planId=<%=id%>&isArchived=true"><img src="images/buttons/but_graphical_view.gif" border="0"></a></td>
                            <td background="images/buttons/com_button_bg.gif" nowrap><a href="PDGraphicBPMN.jsp?planId=<%=id%>&isArchived=true"><div align="center" class="buttonlink">BPMN</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>
</form>

</center>
</body>
</html>

<%!

    public
    Plan[]
    getTemplates(
        int filterOption,
        int batchNumber,
        int batchSize,
        WFSession wfSession)
        throws Exception
    {
        WFObjectList wfObjectList = null;
        Plan[] planList = null;

        try {
            wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
            Object[] objList = null;
            wfObjectList.openBatchedList(filterOption);
            objList = wfObjectList.getNextBatch(batchSize);
            if (batchNumber > 1) {
                for (int i=1; i<batchNumber; i++) {
                    objList = wfObjectList.getNextBatch(batchSize);
                }
            }

            if (objList != null && objList.length > 0) {
                planList = new Plan[objList.length];
                for (int i=0; i<objList.length; i++) {
                    planList[i] = (Plan)objList[i];
                }
            }
        }
        finally {
            if (wfObjectList != null)
                wfObjectList.closeList();
        }

        return planList;
    }

    public WFAdminSessionImpl getiFlowSession(Object modelObject)
    {
            return (WFAdminSessionImpl)(modelObject);
    }


%>
