<%@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="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"
%><%
    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","3");
    
    String wiSortedURL= "SortedPlanList.jsp";
    
    int _pageSize = 15;
    int selIdValue=1;
    long id = -1;

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

    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[] processes = getProcesses(FilterEnum._AllProcesses, userId, wPageValue, _pageSize, wfSession);
    // stupid habit of returning null for empty set
    if (processes == null) {
        processes = new ProcessInstance[0];
    }     
    
%>
<html>
<head>
<title>Interstage Business Process Manager | User Management</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<script>

function setSelection(wiForm, selPage)
{
    location.href='UserProcessList.jsp?userId=' + wiForm.userId.value + '&wPageId=' + selPage;
}

function summary(id, userId)
{
    location.href="OwnedProcess.jsp?processId=" + id + "&currentOwner=" + userId;
}

function listWorkList(id)
{
    location.href="UserWorkitemList.jsp?userId=" + id;
}

function listOwnedTemplates(id)
{
    location.href="UserPlanList.jsp?userId=" + id;
}


function userProfile(id)
{
    location.href="UserProfile.jsp?userId=" + id;
}

 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, opt, userId) {
     if (opt.value)
     {
     	location.href="ModifyMultiplePlanOwners.jsp?planId=" + opt.value + "&userId=" + userId;
     }
     else
    {
    	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 modifying owner(s) for multiple process instances.");
    	}
    	else
    	{
    		location.href="ModifyMultiplePIOwners.jsp?processId=" + test + "&userId=" + userId;
    	}
    }
 }



</script>
</head>

<body class="adminBodyClass">
<center>

<jsp:include page="AdminHeader.jsp" flush="true"/>
<form name="adForm" method="get" action="WorkitemSummary.jsp">
  <input type="hidden" name="wPageId" value="<%= wPageValue %>">
  <input type="hidden" name="selId" value="<%= selIdValue %>">
  <input type="hidden" name="planId" value="-1">
  <input type="hidden" name="selected" value="1">
  <input type="hidden" name="userId" value=<%= userId %>>

<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center" >
<tr>
<td><input type="button" onClick="listWorkList('<%= userId %>')" value="List Workitems" name="check" class="core-button">&nbsp;
<input type="button" onClick="listOwnedTemplates('<%= userId %>')" value="List Owned Process Definitions" name="check" class="core-button">&nbsp;
<input type="button" onClick="userProfile('<%= userId %>')" value="User Profile" name="check" class="core-button"></td>
<%
    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">
<input value="Prev Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass"> 
<%
    btnEnabled = "";
    if (processes.length < _pageSize || _pageSize == 0)
        btnEnabled = "disabled";
     else
         selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
%><input value="Next Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">    
    </td>  
</tr>   
</table>
<br>
<table bgcolor="#CCCCCC" width="90%" border="0" cellpadding="4" cellspacing="1" align="center" class="tableclass">
<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">List of owned process instances for <%= userId %></font></td>  
</tr>
<tr align="center" class="headrow"> 
	<th width="15">Select</th>
	<td> Process Instance Id </td>
	<th> Process Instance Name </th>
	<th> Process Instance State </th>	
	<th> Process Definition Id </th>
	<th> Process Definition Name </th>
	<th>&nbsp;</th>
</tr>
<%
    for (int i = 0; i < processes.length; i++)
    {
        ProcessInstance pi = processes[i];
        id = pi.getId();
        String processName = pi.getName();
        int processState = pi.getState();
        String piState = "";
	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;
	}        
                 
	long planId = pi.getPlanId();
	String planName = pi.getPlanName();
        String piURL = "ProcessInstance.jsp?piId="+id;
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
        String summary ="Summary";
%>
<tr class="<%=rowColor%>"> 
	<td><INPUT TYPE=CHECKBOX NAME= selIds VALUE="<%= id %>"></td>
	<td><%= id %></td>
	<td><%= processName %> </td>
	<td><%= piState %></td>	
	<td nowrap><%= planId %></td>
	<td><%= planName %></td>


	<td>
	<input type="button" onClick="summary(<%= id %>, '<%= userId %>')" title="Go here to modify this process instance" value="Summary" name="check" class="core-button">
	</td>

</tr>
<%
}
%>
</table>

<%
if (processes.length != 0)
{
%>

<br>          	
<TABLE border="0" cellpadding="0" cellspacing="0" width="90%" align="center">
<tr>
	<td width="25">
	<input type="button" onClick="outputSelected('1', this.form.selIds, '<%= userId %>')" value="Modify Owners (Selected)" name="check" class="core-button"></td>
</tr>		
</table>
<%
}
%>
</form>
</center>
</body>
</html>

<%!

    public
    ProcessInstance[]
    getProcesses(
        int filterOption,
        String userId,
        int batchNumber,
        int batchSize,
        WFSession wfSession)
        throws Exception
    {
        WFObjectList wfObjectList = null;
        ProcessInstance[] wiList = null;

        try {
            wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
   	    wfObjectList.addFilter(WFObjectList.LISTFIELD_PROCESSINSTANCE_OWNER, WFObjectList.SQLOP_EQUALTO,"'" + userId + "'");
            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) {
                wiList = new ProcessInstance[objList.length];
                for (int i=0; i<objList.length; i++) {
                    wiList[i] = (ProcessInstance)objList[i];
                }
            }
        }
        finally {
            if (wfObjectList != null)
                wfObjectList.closeList();
        }

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

%>

