<%@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.ProcessInstance"
%><%@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","1");


    int _pageSize = 20;
    long id = -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;


    ProcessInstance[] procInsts = getProcesses(Filter.AllArchivedProcesses, wPageValue, _pageSize, wfSession);
    // stupid habit of returning null for empty set
    if (procInsts == null) {
        procInsts = new ProcessInstance[0];
    }

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

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

function archiveQuery()
{
    document.location.href='ArchiveProcesses.jsp';
}

function filter()
{
    location.href="FilterProcesses.jsp";
}

</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>
   <td valign="top" height="35">
<!-- blue buttons -->
<table border="0" cellspacing="0" cellpadding="0"><tr><td>
    <table border="0" cellspacing="0" cellpadding="0">
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><a href="ArchiveProcesses.jsp"><img src="images/buttons/but_package.gif" width="31" height="24" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="ArchiveProcesses.jsp"><div align="center" class="buttonlink">&nbsp;Archive by Date</span></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>&nbsp;</td><td>
    <table border="0" cellspacing="0" cellpadding="0">
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><a href="FilterProcesses.jsp"><img src="images/buttons/but_search.gif" height="24" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="FilterProcesses.jsp"><div align="center" class="buttonlink">&nbsp;Search</span></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>

<!-- 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 align="right" valign="top">
<input value="Prev Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">
<%
    btnEnabled = "";
    selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
    if (procInsts.length < _pageSize || _pageSize == 0)
            btnEnabled = "disabled";
    else
    {
        ProcessInstance[] mprocInsts = getProcesses(Filter.AllArchivedProcesses, wPageValue, _pageSize, wfSession);
        if(mprocInsts == null || mprocInsts.length == 0)
            btnEnabled = "disabled";
    }

    %>
    <input value="Next Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">
</td>
</tr>

</table>

<table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
<tr align="center" class="headrow">
    <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">All Archived Process Instances</font></td>
</tr>
</table>

<TABLE bgcolor="#CCCCCC" border="0" cellpadding="2" cellspacing="1" class="tableClass" width="90%" align="center">
                  <tr align="center" class="headrow">
                    <td> Id </td>
                    <th> Name </th>
                    <th> Process Definition Id </th>
                    <th> Date Started </th>
                    <th>  </th>
                  </tr>
                  <%
    for (int i = 0; i < procInsts.length; i++)
    {
        ProcessInstance pi = procInsts[i];
        id = pi.getId();
        String processName = pi.getName();
        long planId = pi.getTemplateId();
        String piCreationDate = "" + pi.getCreationTime();
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
%>
                  <tr class="<%=rowColor%>">
                    <td width=10%><%= id %></td>
                    <td nowrap>
                        <table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td nowrap>
                        <font face="arial" size="2"><% QuickForm.writeHtml(out, processName); %></font></td>
                        <td align="right">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><a href="PIGraphic.jsp?piId=<%= id %>&isArchived=true"><img src="images/buttons/view_small_icon.gif" width="17" height="17" border="0"></a></td>
    <td rowspan="2">&nbsp;</td>
    <td><div align="center"><a href="ShowHistory.jsp?id=<%=id%>&isArchived=true"><img src="images/buttons/history_small_icon.gif" width="18" height="17" border="0"></a></div></td>
  </tr>
  <tr>
    <td><a href="PIGraphic.jsp?piId=<%= id %>&isArchived=true">
      <div class="smalllinks">VIEW</div>
    </a></td>
    <td><a href="ShowHistory.jsp?id=<%=id%>&isArchived=true">
      <div class="smalllinks">HISTORY</div>
    </a></td>
  </tr>
</table>
                        </td></tr></table>
                    </td>
                    <td width=20%><%= planId %> </td>
                    <td width=15%><script> showDate('<%=piCreationDate%>'); </script></td>
                    <td width=10%>
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tr background="images/buttons/com_button_bg.gif">
                             <td width="15"><a href="PIGraphicBPMN.jsp?piId=<%=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="PIGraphicBPMN.jsp?piId=<%=id%>&isArchived=true"><div align="center" class="buttonlink">BPMN</span></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>
</html>

<%!

    public
    ProcessInstance[]
    getProcesses(
        int filterOption,
        int batchNumber,
        int batchSize,
        WFSession wfSession)
        throws Exception
    {
        WFObjectList wfObjectList = null;
        ProcessInstance[] piList = 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) {
                piList = new ProcessInstance[objList.length];
                for (int i=0; i<objList.length; i++) {
                    piList[i] = (ProcessInstance)objList[i];
                }
            }
        }
        finally {
            if (wfObjectList != null)
                wfObjectList.closeList();
        }

        return piList;
    }

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


%>
