<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.util.ModelException"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="java.util.Vector"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }

    long taskId = Long.parseLong(request.getParameter("taskId"));
    WorkItem task = WFObjectFactory.getWorkItem(taskId, wfSession);
    String reassignButton = request.getParameter("ReassignButton");
    if ( reassignButton == null )
    {
        String addUsers = request.getParameter("AddUsers");
        String delUsers = request.getParameter("DeleteUsers");
        if(addUsers == null && delUsers == null)
        {
            String[] params = request.getParameterValues("Groups");
            session.setAttribute("selectedGroup", params[0]);
        }
        if(addUsers != null)
        {
            String[] usersToAdd = request.getParameterValues("Users");
            if(usersToAdd != null)
            {
                String[] addedUsers = (String[])session.getAttribute("usersToAdd");
                if(addedUsers != null) {
                    Vector prepUsers = new Vector();
                    for(int i=0; i<addedUsers.length; i++)
                        prepUsers.addElement(addedUsers[i]);
                    for(int j=0; j<usersToAdd.length; j++)  {
                        boolean userFound = false;
                        for(int k=0; k<addedUsers.length; k++) {
                            if(usersToAdd[j].equals(addedUsers[k])) {
                                userFound = true;
                                break;
                            }
                        }
                        if(!userFound) {
                            prepUsers.addElement(usersToAdd[j]);
                        }
                    }
                    usersToAdd = new String[prepUsers.size()];
                    prepUsers.copyInto(usersToAdd);
                }
                session.setAttribute("usersToAdd", usersToAdd);
            }
        }
        if(delUsers != null)
        {
            String[] usersToDelete = request.getParameterValues("Reassign");
            if(usersToDelete != null)
            {
                String users[] = (String[])session.getAttribute("usersToAdd");
                Vector extractUsers = new Vector();
                for(int i=0; i<users.length; i++)
                    extractUsers.addElement(users[i]);

                for(int j=0; j<usersToDelete.length; j++)
                    extractUsers.removeElement(usersToDelete[j]);

                String[] finalUsers = new String[extractUsers.size()];
                extractUsers.copyInto(finalUsers);
                session.setAttribute("usersToAdd", finalUsers);
            }
        }
        response.sendRedirect("Reassign.jsp?taskId="+taskId);
        return;
    }
    else
    {
        //TODO: this is stored in session variable ... should be in request variables.
        String[] users = (String[])session.getAttribute("usersToAdd");
        if ( users == null || users.length == 0 )
        {
            throw new Exception("There are no users to add to the work item.  Please check you settings and try again.");
        }
        task.reassignTo(users);
        response.sendRedirect("WorkList.jsp");
    }
%>
