<%@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.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessInstanceStatesEnum"
%><%@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="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"
%><%
    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");

    String piSortedURL= "SortedProcessList.jsp";

    int _pageSize = 20;
    int selIdValue=1;
    long id = -1;
    long processId = -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(FilterEnum._AllProcesses, 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 | Process Instance List</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<script>
function setSelection(wiForm, selPage)
{
    location.href='AdminProcessList.jsp?wPageId=' + selPage
}

function setProcessId(val, id, adProcessForm)
{
    adProcessForm.processId.value= id;
    adProcessForm.selected.value= val;
    adProcessForm.submit();
}

function gotoProcessId()
{
    location.href="ProcessSummary.jsp?processId=" + document.adProcessForm.processInstanceId.value;
}

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

function archivedProcesses()
{
    location.href="ArchivedProcesses.jsp";
}

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

function getSelected(opt) {
    var selected = new Array();
    var index = 0;
    for (var intLoop = 0; intLoop < opt.length; intLoop++) {
       if ((opt[intLoop].selected) ||
       (opt[intLoop].checked)) {
      index = selected.length;
      selected[index] = new Object;
      selected[index].value = opt[intLoop].value;
      selected[index].index = intLoop;
       }
    }
    return selected;
 }

function outputSelected(val) {
    var opt = document.adProcessForm.selIds
    var sel = getSelected(opt);
    var strSel = "";
    for (var item in sel)
       strSel += sel[item].value + ",";
    var test = strSel.substring(0,strSel.length-1);
    if(test == "")
    {
        var selected;
        if (val == 7) {
            selected = 'deleting';
        }
        else
        {
            selected = 'archiving';
        }
        alert("Select process instance id's for " + selected + " multiple process instances.");
    }
    else
    {
    document.location.href = 'ProcessSummary.jsp?processId=' + test + '&selected='  + val;
    }
 }

 function migrateProcesses() {
    var opt = document.adProcessForm.selIds
    var sel = getSelected(opt);
    var strSel = "";
    for (var item in sel)
       strSel += sel[item].value + ",";
    var test = strSel.substring(0,strSel.length-1);
    if(test == "")
    {
        alert("Select process instance id's for migrating multiple process instances.");
    }
    else
    {
    document.location.href = 'MigrateProcesses.jsp?processIds=' + test;
    }
 }

function disableEnterKey()
{
var code = window.event.keyCode;
    if (code == 13)
    {
        window.event.keyCode = 0;
    }

}

</script>
</head>

<body class="adminBodyClass">
<center>

<jsp:include page="AdminHeader.jsp" flush="true"/>
 <form name="adProcessForm" method="get" action="ProcessSummary.jsp" onKeyPress="disableEnterKey()">

  <input type="hidden" name="selId" value="<%= selIdValue %>">
  <input type="hidden" name="processId" value="-1">
  <input type="hidden" name="selected" value="1">

<!-- top butons-->
<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td align="right">
<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"><img src="images/buttons/but_package.gif" width="31" height="24"></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"><img src="images/buttons/but_search.gif" height="24"></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>
</td>
</table><br>
<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>
<font face="arial" size="2">Process Instance Id:</font><input type= "textbox" name ="processInstanceId" value = "" size = 8><input type = "button" onClick=gotoProcessId() value = "Go To" class="submitClass">
</td>
<td align="right">
    <%
    String selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
    String btnEnabled = "";
    if (procInsts.length < _pageSize || _pageSize == 0)
        btnEnabled = "disabled";
    %>
   <input value="Next Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">
   <%
    btnEnabled = "";
    if (wPageValue > 1 && _pageSize != 0)
        selOption = "setSelection(this.form, " + (wPageValue -1) +")";
    else {
        //selOption = "setSelection(this.form, 1)";
        btnEnabled = "disabled";
    }
   %>
   <input value="Prev Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">
</td></tr>
</table>
 <!-- end top buttons-->
<table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
<tr align="center" class="headrow">
    <td colspan="8" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">All Process Instances</font></td>
</tr>
                  <tr align="center" class="headrow">
                    <th>Select</th>
                    <td><img src="images/buttons/sort_icon.gif"><a href="<%=piSortedURL%>?sort=processId"><font color="#FFFFFF"> Id <img src=images/icons/d_sorted.gif border=0></font></a></td>
                    <th><img src="images/buttons/sort_icon.gif"><a href="<%=piSortedURL%>?sort=processName"><font color="#FFFFFF">  Process Instance Name </font></a></th>
                    <th><img src="images/buttons/sort_icon.gif"><a href="<%=piSortedURL%>?sort=planName"><font color="#FFFFFF"> Process Definition Name </font></a></th>
                    <th>Date Started </th>
                    <th><img src="images/buttons/sort_icon.gif"><a href="<%=piSortedURL%>?sort=processStatus"><font color="#FFFFFF"> State </font></a></th>
                    <th> Owner(s) </th>
                  </tr>
                  <%
    for (int i = 0; i < procInsts.length; i++)
    {
        ProcessInstance pi = procInsts[i];
        long piId = pi.getId();
        String piName = pi.getName();
        String planName = pi.getPlanName();
        String piState = null;
        int processState = pi.getState();
        switch(processState)
        {
            case 0: piState =  "Open";
                    break;
            case 1: piState = "NotRunning";
                    break;
            case 2: piState = "NotStarted";
                    break;
            case 3: piState = "Created";
                    break;
            case 4: piState = "Initialized";
                    break;
            case 5: piState = "Dormant";
                    break;
            case 6: piState = "Locked";
                    break;
            case 7: piState = "Running";
                    break;
            case 8: piState =  "Closed";
                    break;
            case 9: piState =  "Completed";
                    break;
            case 10: piState =  "Error";
                    break;
            case 11: piState =  "Aborted";
                    break;
            case 12: piState =  "Suspended";
                    break;
        }
            //piState = ProcessInstanceStatesEnum.from_int(pi.getState()).toString();
        String[] piOwners = pi.getOwners();
        String piURL = "ProcessInstance.jsp?piId="+pi.getId();
        String pdURL = "ProcessDefinition.jsp?planId="+pi.getPlanId();
        String summaryURL = "ProcessSummary.jsp?processId="+piId;
        String piDate = ""+pi.getCreationTime();
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
        String summary ="Summary";
        String Copy = "Copy";
%>
                  <tr class="<%=rowColor%>">
                    <td align="center"><INPUT TYPE=CHECKBOX NAME= selIds VALUE="<%= piId %>"></td>
                    <td><%= piId %></td>
                    <td nowrap>
                        <table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td nowrap>
                        <a href="<%=piURL%>"><font face="arial" size="2"><%= piName %></font></a></td>
                        <td align="right">

<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><img src="images/buttons/view_small_icon.gif" width="17" height="17"></td>
    <td rowspan="2">&nbsp;</td>
    <td align="center"><div align="center"><img src="images/buttons/details_small_icon.gif" width="20" height="17" border="0"></div></td>
    <td rowspan="2">&nbsp;</td>
    <td><div align="center"><img src="images/buttons/history_small_icon.gif" width="18" height="17"></div></td>
  </tr>
  <tr>
    <td><a href="PIGraphic.jsp?piId=<%= piId %>">
      <div class="smalllinks">VIEW</div>
    </a></td>
    <td><a href="<%=summaryURL%>" title="Go here to modify this process instance">
      <div class="smalllinks">SUMMARY</div></a></td>
    <td><a href="ShowHistory.jsp?id=<%=piId%>">
      <div class="smalllinks">HISTORY</div>
    </a></td>
  </tr>
</table>

                        </td></tr></table>
                    </td>
                    <td><a href="<%=pdURL%>"> <%= planName %> </a></td>
                    <td nowrap><script> showDate('<%=piDate%>'); </script></td>
                    <td><%= piState %></td>
                    <td>
                      <% for(int j=0; j<piOwners.length; j++){%>

                      <%= piOwners[j] %>
                      <% if(j!=piOwners.length-1) out.print(","); %>

                      <%}%>
                    </td>
                  </tr>
                  <%
   }
%>

</TABLE>

<%
if (procInsts.length != 0)
{
%>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="90%" align="center">
    <tr>
        <td>

<TABLE border="0" cellpadding=0 cellspacing=0>
        <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_delete.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=outputSelected('7')"><div align="center" class="buttonlink"> Delete</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"><img src="images/buttons/but_package.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=outputSelected('8')"><div align="center" class="buttonlink"> Archive</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"><img src="images/buttons/but_migrate.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=migrateProcesses()"><div align="center" class="buttonlink"> Migrate</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>

</td><td>&nbsp;</td><td>

<table border="0" cellspacing="0" cellpadding="0" align="right">
  <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="ArchivedProcesses.jsp"><div align="center" class="buttonlink"> Archived Process Instances</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>

    </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);
    }


%>
