<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="AdminException.jsp"
%><%@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.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessInstanceStatesEnum"
%><%@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="java.util.Date"
%><%@page import="java.util.Enumeration"
%><%@page import="java.util.StringTokenizer"
%><%@page import="java.io.Writer"
%><%@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 returnMsg = "";

    long id = -1;
    Plan plan = null;
    String name = "";
    String planName = "";
    String desc = "";
    String version = "";
    int processState;
    String piState = "";
    String piOwners[] = null;
    String processOwners = "";
    String piURL = "";
    String piCreationDate ="";
    String piClosedDate ="";

    String sel = request.getParameter("selected");

    String processId = request.getParameter("processId");
    
    String planId = request.getParameter("planId");

    if(sel != null)
    {
        int selValue = new Integer(sel).intValue();
        session.setAttribute("selected", sel);
        switch(selValue){
            case 7: returnMsg = deleteMultipleProcesses(wfSession, processId);
                    break;
            case 8: returnMsg = archiveMultipleProcesses(wfSession, processId);
                    break;
        }

    }
    else
    {

        if (processId == null) {
        processId = (String) session.getAttribute("processId");
        }

        id = Long.parseLong(processId);

        try
        {
            ProcessInstance pi = WFObjectFactory.getProcessInstance(id, wfSession);
            name = pi.getName();
            desc = pi.getDescription();
            planName = pi.getPlanName();
            desc = (desc == null || desc.length() <=0)?"":desc;
            piCreationDate = "" + pi.getCreationTime();
            piClosedDate = "" + pi.getClosedTime();

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


            piOwners = pi.getOwners();
            String owners = "";
            processOwners = "";
            for(int j=0; j<piOwners.length; j++)
            {
            owners = owners + piOwners[j] + ",";
            
            }
            processOwners = owners.substring(0,owners.length() - 1);
        }
        catch (Exception ex)
        {
            returnMsg = "Could not retrieve process instance with id " + id + ".";
        }
        piURL = "ProcessInstance.jsp?piId="+id;
        session.setAttribute("TabIndex","1");
      }

      StringBuffer reqURL = request.getRequestURL();
      reqURL.append("?");
      if (request.getQueryString() != null) {
          reqURL.append(request.getQueryString());
      }

%>


<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>
</head>

<body class="adminBodyClass">
<script>

function setProcessId(val, id, adForm)
{
    processSumForm.processId.value= id
    processSumForm.selected.value= val
    processSumForm.submit()
}

function saveAsXML(val, id, adForm)
{
    location.href="ProcessAsXML.jsp?processId=" + id
}

function setProcessOwner(val, id, owner, adForm)
{
  if(owner == "")
  {
  	alert("Enter a valid User Name");
  	return false;
  }	
    processSumForm.processId.value= id
    processSumForm.selected.value= val
    processSumForm.owner.value= owner
    processSumForm.submit()
}


function gotoProcessList(planId)
{
  if ( planId != null)
  {
  	if ( planId != -1)
  	{
  		document.location.href='ListProcessInstances.jsp?planId='+ planId;
  	}  	
  }
  else	
    	{
    	  	document.location.href='SortedProcessList.jsp';
  	}
}

</script>

<center>

<jsp:include page="AdminHeader.jsp" flush="true"/>


<%
if (!(returnMsg == ""))
{
%>

       <TABLE  border="0" cellpadding="2" cellspacing="1" class="tableClass" width="90%" align="center">
            <tr><td>&nbsp;</td></tr>
            <tr><td align="center">
            <img src="images/icons/info_excep.gif" border="0"><br/></td></tr>
            <tr><td>&nbsp;</td></tr>
        <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"><%= returnMsg %></font></td>
            </tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
            <tr><td>&nbsp;</td></tr>
            <tr><td align ="center">
            <input type="button" onClick="gotoProcessList(<%= planId%>)" value="Process Instance List" name="check" class="core-button">
            </td></tr>
       </TABLE>
<%
}
else
{
%>
<br>
<table cellpadding=0 cellspacing=0 align="center" width="90%">
<tr><td align="right" valign="top" height="35">
  <table cellpadding=2 cellspacing=1 class="tableclass" align="center" width="100%">
    <tr>
      <td align="left">

        <jsp:include page="ProcessMenu.jsp">
        <jsp:param name="piId" value="<%=String.valueOf(id) %>" />
        <jsp:param name="referralURL" value="<%=reqURL.toString() %>" /></jsp:include>

      </td>
    </tr>
</td></tr></table>
    <TABLE  bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">

    <form name="processSumForm" method="post" action="ProcessAction.jsp">
      <input type="hidden" name="processId" value="-1">
      <input type="hidden" name="selected" value="Edit">
      <input type="hidden" name="planId" value="-1">
            <tr align="center" class="headrow">
            <td colspan="4" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">Process Instance Summary</font></td>
            </tr>
            <tr><td colspan="1" class="headrow">ID</td><td bgcolor="#FFFFFF" colspan="3"><%= id %></td></tr>
            <tr><td colspan="1" class="headrow">Name</td><td colspan="3" bgcolor="#FFFFFF" ><a href="<%=piURL%>"><% QuickForm.writeHtml(out, name); %></td></td></tr>
            <tr><td colspan="1" class="headrow">Description</td><td colspan="3" bgcolor="#FFFFFF"> <% QuickForm.writeHtml(out, desc); %> </td></tr>
            <tr><td colspan="1" class="headrow" width="20%">Process Definition Name</td><td colspan="3" bgcolor="#FFFFFF"> <% QuickForm.writeHtml(out, planName); %> </td></tr>
            <tr><td colspan="1" class="headrow">State</td><td bgcolor="#FFFFFF" colspan="3"><%= piState %>&nbsp;
            <%
            if ((piState.equals("Running")) || (piState.equals("Error"))){
            %>
            <input type="button" onClick="setProcessId('3',<%= id %>, this.form)" value="Suspend" name="check" class="core-button">&nbsp;
            <input type="button" onClick="setProcessId('2',<%= id %>, this.form)" value="Abort" name="check" class="core-button">
            </td></tr>

            <%
            }
            else if ((piState.equals("Suspended"))){
            %>
            <input type="button" onClick="setProcessId('4',<%= id %>, this.form)" value="Resume" name="check" class="core-button"></td></tr>
            <%
            }
            %>
            <tr><td colspan="1" class="headrow">Owner(s)</td><td colspan="3" bgcolor="#FFFFFF"><input type=text value = "<% QuickForm.writeHtml(out, processOwners); %>" name=owner size=30><input type="button" onClick="setProcessOwner('5',<%= id %>, this.form.owner.value, this.form)" value="Modify Owner" name="check" class="core-button">
            <tr><td colspan="1" class="headrow"> Date Started </td><td bgcolor="#FFFFFF" colspan="3"><script> showDate('<%=piCreationDate%>'); </script></td>
                    <%
                    if (piState.equals("Closed"))
                    {
            %>
                    <tr><td colspan="1" class="headrow"> Date Closed </td><td colspan="3" bgcolor="#FFFFFF">
                    <script> showDate('<%=piClosedDate%>'); </script></td>
            </tr>
            <%
            }
            %>

    </TABLE>
<br>

<!-- blue buttons -->
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center"><tr><td>
<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="javascript:onClick=setProcessId('1',<%= id %>, this.form)"><img src="images/buttons/but_delete.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=setProcessId('1',<%= id %>, this.form)"><div align="center" class="buttonlink">&nbsp;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"><a href="javascript:onClick=setProcessId('6',<%= id %>, this.form)"><img src="images/buttons/but_package.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=setProcessId('6',<%= id %>, this.form)"><div align="center" class="buttonlink">&nbsp;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></table>
</td><td align="right">
    <table border="0" cellspacing="0" cellpadding="0">
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><a href="javascript:onClick=saveAsXML('7',<%= id %>, this.form)"><img src="images/buttons/but_save.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=saveAsXML('7',<%= id %>, this.form)"><div align="center" class="buttonlink">&nbsp;Save As XML</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 -->

        </form>
        <!-- Added Larer for changing the layout -->
          </td>
        </tr>
      </table>

<%
}
%>

</center>
</html>

<%!
    public String archiveMultipleProcesses(WFAdminSessionImpl wfSession, String processIds) throws Exception
    {
        StringTokenizer st = new StringTokenizer(processIds, ",");
        String processIdsArchived = "";
        String processIdsFailed = "";
        String message = "";
		String errormsg = "";
        long id = -1;
        while (st.hasMoreTokens()){
            try{
                id = Long.parseLong(st.nextToken());
                wfSession.archiveClosedProcess(id);
                processIdsArchived = processIdsArchived + id + ", ";
            }
            catch (Exception ex) {
                processIdsFailed = processIdsFailed + id + ", ";
                if(errormsg == "")
		    {
                 Throwable thr = ex.getCause();
		      if(thr != null)
				errormsg = thr.getMessage();
			else
				errormsg = ex.getMessage();
		    }
            }
        }

        if (!((processIdsArchived.equals("")) || (processIdsArchived.equals(null))))
        {
            message = "Process instance with ID's " + processIdsArchived.substring(0,processIdsArchived.length() -2) + " have been successfully archived. ";
        }

    if (!(processIdsFailed.equals("") || processIdsFailed.equals(null)))
    {
        message = message + "Archival of process instance with ID's " + processIdsFailed.substring(0,processIdsFailed.length()-2) + " failed. "+errormsg;
    }

        return message;
    }

    public String deleteMultipleProcesses(WFAdminSessionImpl wfSession, String processIds) throws Exception
    {
        StringTokenizer st = new StringTokenizer(processIds, ",");
        String processIdsDeleted = "";
        String processIdsFailed = "";
        long id = -1;
        String message = "";

        while (st.hasMoreTokens()){
            try{
                id = Long.parseLong(st.nextToken());
                wfSession.deleteProcessInstance(id);
                processIdsDeleted = processIdsDeleted + id + ", ";
            }
            catch (Exception ex) {
                processIdsFailed = processIdsFailed + id + ", ";
            }
        }

        if (!((processIdsDeleted.equals("")) || (processIdsDeleted.equals(null))))
        {
            message = "Process instance with ID's " + processIdsDeleted.substring(0,processIdsDeleted.length() -2) + " have been successfully deleted. ";
        }

    if (!(processIdsFailed.equals("") || processIdsFailed.equals(null)))
    {
        message = message + "Deletion of process instance with ID's " + processIdsFailed.substring(0,processIdsFailed.length()-2) + " failed.";
    }

        return message;
    }


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

    public String doNothing() throws Exception
    {
        return "this does nothing";
    }

%>
