<%@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.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.text.SimpleDateFormat"
%><%@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 = null;


    String processId = request.getParameter("processId");
    if (processId == null) {
        processId = (String) session.getAttribute("processId");
    }
    long id = Long.parseLong(processId);

    String owner = request.getParameter("owner");

    String sel = request.getParameter("selected");
    if(sel != null)
    {
        int selValue = new Integer(sel).intValue();
        session.setAttribute("selected", sel);
        switch(selValue){
            case 1: returnMsg = deleteProcess(wfSession, id);
                    break;
            case 2: returnMsg = abortProcess(wfSession, id);
                    break;
            case 3: returnMsg = suspendProcess(wfSession, id);
                    break;
            case 4: returnMsg = resumeProcess(wfSession, id);
                    break;
            case 5: returnMsg = modifyOwner(wfSession, id, owner);
                    break;
            case 6: returnMsg = archiveProcess(wfSession, id);
                    break;
            case 7: returnMsg = saveProcessAsXML(wfSession, id);
                    break;
        }
    }
    else if (sel != null && sel.equals("Copy"))
    {

    }

    session.setAttribute("TabIndex","1");
%>




<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, actionForm)
{
    actionForm.processId.value= id
    actionForm.selected.value= val
    actionForm.submit()
}

function gotoProcessList()
{
    document.location.href='SortedProcessList.jsp';
}


</script>
<center>

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

       <TABLE  border="0" cellpadding="2" cellspacing="1" class="tableClass" width="75%" 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()" value="Process Instance List" name="check" class="core-button">
            </td></tr>
          </TABLE>
</center>

</html>

<%!
    public String deleteProcess(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        wfSession.deleteProcessInstance(id);
        return "Process instance with ID " + id + " has been successfully deleted";
    }

    public String abortProcess(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        ProcessInstance pi = WFObjectFactory.getProcessInstance(id, wfSession);
        pi.abort();
        return "Process instance with ID " + id + " has been successfully aborted";
    }

    public String resumeProcess(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        ProcessInstance pi = WFObjectFactory.getProcessInstance(id, wfSession);
        pi.resume();
        return "Process instance with ID " + id + " has been successfully resumed";
    }

    public String suspendProcess(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        ProcessInstance pi = WFObjectFactory.getProcessInstance(id, wfSession);
        pi.suspend();
        return "Process instance with ID " + id + " has been successfully suspended";
    }

    public String modifyOwner(WFAdminSessionImpl wfSession, long id, String owner) throws Exception
    {
        StringTokenizer st = new StringTokenizer(owner, ",");
        String owners[] = new String[st.countTokens()];
        for (int i=0;i<owners.length;i++)
        {
            owners[i] = st.nextToken();
        }
        ProcessInstance pi = WFObjectFactory.getProcessInstance(id, wfSession);
        try
        {
        	pi.startEdit();
        	pi.setOwners(owners);
        	pi.commitEdit();
        	return "Owner for process instance with ID " + id + " has been successfully modified to " + owner;
	}
    	catch(Exception e)
    	{
    		pi.cancelEdit();
		return "Modify Owner for Process Instance with ID " + id + " failed. Please enter a valid User Name.";
    	}
    }

    public String archiveProcess(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        wfSession.archiveClosedProcess(id);
        return "Process instance with ID " + id + " has been successfully archived";
    }


    public String saveProcessAsXML(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        return "Not implemented Yet";
    }


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

%>
