<%@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.WorkItem"
%><%@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;
    long processId = -1;
    long id = -1;    

    String workitemId = request.getParameter("workitemId");
    if (workitemId == null) {
        workitemId = (String) session.getAttribute("workitemId");
    }
    
    String newAssignee = request.getParameter("assignee");
    String userId = request.getParameter("userId"); 
    String procId = request.getParameter("processId");
    System.out.println("processId ::" + procId + "::");
    if (procId != null)
    {
    	processId = Long.parseLong(procId);
    }

    String sel = request.getParameter("selected");
    if(sel != null)
    {
    	int selValue = new Integer(sel).intValue();
        session.setAttribute("selected", sel);
        switch(selValue){
            case 1: returnMsg = reassignWorkitem(wfSession, userId, newAssignee, Long.parseLong(workitemId), processId);
                    break;
            case 2: returnMsg = reassignWorkitems(wfSession, userId, newAssignee, workitemId);
                    break;                      
                  
        }
    }
    else if (sel != null && sel.equals("Copy"))
    {

    }

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




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

<body class="adminBodyClass">
<script>

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

function gotoWorkList(id)
{
    document.location.href='UserWorkitemList.jsp?userId=' + id;
}


</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="gotoWorkList('<%= userId %>')" value="Work List" name="check" class="core-button">
            </td></tr>         
          </TABLE>
</center>
</body>
</html>

<%!
    public String reassignWorkitem(WFAdminSessionImpl wfSession, String userId, String assignee, long id, long processId) throws Exception
    {
    	String message = "";
    	StringTokenizer st = new StringTokenizer(assignee, ",");
    	String assignees[] = new String[st.countTokens()];
    	for (int i=0;i<assignees.length;i++)
    	{
    		assignees[i] = st.nextToken();
    	}    
    	try
    	{
    		wfSession.reassign(userId, assignee, processId, id, false);
    		message = "Workitem with ID " + id + " has been successfully reassigned to " + assignee;
    	}
    	catch (Exception ex)
    	{
    		message = "Reassignment for workitem ID " + id + " failed.";
    	}
    	return message;
    }

    public String reassignWorkitems(WFAdminSessionImpl wfSession, String userId, String assignee, String id) throws Exception
    {
    
    	String message = "";
    	String successIds = "";
    	String failedIds = "";
    	StringTokenizer st = new StringTokenizer(id, ",");
    	String workitems[] = new String[st.countTokens()];
    	long[] wiIds = new long[workitems.length];
    	for (int i=0;i<workitems.length;i++)
    	{
    		wiIds[i] = Long.parseLong(st.nextToken());
    	}     
    	
    	StringTokenizer stAssignee = new StringTokenizer(assignee, ",");
    	String assignees[] = new String[stAssignee.countTokens()];
    	for (int i=0;i<assignees.length;i++)
    	{
    		assignees[i] = stAssignee.nextToken();
    	}    	
    	for (int i=0;i<wiIds.length;i++)
    	{	
    		WorkItem wi = WFObjectFactory.getWorkItem(wiIds[i], wfSession);
    		long processInstanceId = wi.getProcessInstanceId();
    		try
    		{
    			wfSession.reassign(userId, assignee, processInstanceId, wiIds[i], false);
    			successIds = successIds + wiIds[i] + ", ";
    			
    		}
    		catch(Exception ex)
    		{
    			failedIds = failedIds + wiIds[i] + ", ";
    		}
    	}
    	
    	if (!((successIds.equals("")) || (successIds.equals(null))))
    	{
    		if(wiIds.length == 1)
    		   message = " The workitem with ID " + successIds.substring(0,successIds.length() -2) + " has been successfully reassigned to " + assignee + ".";
    		else
    		   message = "The workitems with IDs " + successIds.substring(0,successIds.length() -2) + " has been successfully reassigned to " + assignee + ".";
    	}
    	   	
	if (!(failedIds.equals("") || failedIds.equals(null)))
	{
		message = message + "Reassignment for workitems ID's " + failedIds.substring(0,failedIds.length()-2) + " failed.";
	}    	
    	
    	
    	return message;
    }
   
    public WFAdminSessionImpl getiFlowSession(Object modelObject)
    {
    		return (WFAdminSessionImpl)(modelObject);
    }         
    
%>
