<%@ page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true" %>
<%@ page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@ 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.ProcessDefinitionStatesEnum"
%><%@ 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="com.fujitsu.iflow.model.workflow.DirectoryServices"
%><%@ page import="com.fujitsu.iflow.model.workflow.ProfileProp"
%><%@ page import="java.util.Enumeration"
%><%@ page import="java.util.Vector"
%><%@ page import="java.text.SimpleDateFormat"
%><%@ page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%

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

    String password = (String) session.getAttribute("password");
    String user = (String) session.getAttribute("userName");



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

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

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

    ProfileProp[] proProps = updatePropList(wfSession,user,password,emailstate,email,dmsdir);

    String returnMsg = "";
    if (proProps != null)
    {
       returnMsg = "User profile for " + user + " was updated successfully!";
    } else
    {
       returnMsg = "User profile for " + user + " failed to be updated!";
    }
%>
<html>
<head><title>Interstage Business Process Manager | Admin Manager Update User Profile</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<script>

function goto()
{
    document.location.href='WorkList.jsp';
}


</script>


</head>
<body class="bodyClass">
<jsp:include page="WorkItemHeader.jsp" flush="true"/>
<center>
  <br>
  <br>
  <table width="75%" border="0" align="center" cellpadding="1" cellspacing="0" >
  <tr>
    <td><table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
    <tr>
      <td align="center">
      <img src="images/icons/info_excep.gif" border="0"> </td</tr>
      <tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&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="goto()" value="Work List" name="check2" class="core-button">            
      </td>
    </tr>
  </table></td>
        </tr>
      </table>
</center>
</BODY>
</html>

<%--
displayList(proProps,out);
--%>

<%!
    public String getPropValue(ProfileProp[] proProps, String propName) throws Exception
    {
        if (proProps != null)
        {
            for (int i=0; i < proProps.length; i++)
            {
                ProfileProp prop = proProps[i];
                String name = prop.getName();
                String type = prop.getType();
                String[] values = prop.getValues();

                if (propName.equalsIgnoreCase(name))
                {
                    return  getCommaSepString(values);
                }

            }
            return "";
        }
        else
        {
        return "";
        }
    }

    public void displayList(ProfileProp[] proProps,javax.servlet.jsp.JspWriter out) throws Exception
    {
        if (proProps != null)
        {
           for (int i=0; i < proProps.length; i++)
           {
                ProfileProp prop = proProps[i];
                String name = prop.getName();
                String type = prop.getType();
                String[] values = prop.getValues();
                String valueStr = getCommaSepString(values);
                out.println("<BR><B>Name:Type:Value::</B>"+name+":"+type+":"+valueStr);
            }
        }
    }

    public
    String
    getCommaSepString(String[] strArray)
        throws Exception
    {
        String returnStr = "";
        for (int i=0; i < strArray.length; i++)
        {
            if ((strArray.length != 0) && (i == (strArray.length-1)))
            {
                returnStr += strArray[i];
            }
            else
            {
                returnStr += strArray[i] + ",";
            }
        }
        return returnStr;
    }


    public
    ProfileProp[]
    getUserPropList(WFSession wfSession, String user, String password)
        throws Exception
    {
        ProfileProp[] profileProps = null;
        try {
            DirectoryServices ds = WFObjectFactory.getDirectoryServices(wfSession,wfSession.getUserName(),password);
        profileProps = ds.getUserAllPropList(user);
        }
        finally {
        }

        return profileProps;
    }


    public
    ProfileProp[]
    updatePropList(WFSession wfSession, String user, String password, String emailstate,
               String email, String dmsdir)
        throws Exception
    {
        DirectoryServices ds = WFObjectFactory.getDirectoryServices(wfSession,wfSession.getUserName(),password);
        ProfileProp[] proProps = ds.getUserAllPropList(user);
        //ProfileProp curProp;


        for (int i=0; proProps != null && i < proProps.length; i++)
        {
            ProfileProp curProp = proProps[i];
            proProps[i] = updateProp(curProp,emailstate,email,dmsdir);
        }


        ds.updateUserPropList(user,proProps);
        return ds.getUserAllPropList(user);
    }

    public
    ProfileProp[]
    appendToArray(ProfileProp[] thisPropList, ProfileProp newProp)
        throws Exception
    {
        Vector vec = new Vector();
        for (int i=0; i < thisPropList.length; i++)
        {
            vec.addElement(thisPropList[i]);
        }
        vec.addElement(newProp);

        ProfileProp[] newList = new ProfileProp[vec.size()];
        for (int j=0; j < vec.size(); j++)
        {
           newList[j] = (ProfileProp)vec.elementAt(j);
        }

        return newList;
    }


    public
    ProfileProp
    updateProp(ProfileProp thisProp, String emailstate, String email, String dmsdir)
        throws Exception
    {
        String defaultNotification = "iflownotification";
        String defaultDMSDir = "dmsdir";
        String defaultUID = "uid";
        String defaultEMAIL = "email";

            // check the type of thisProp
            String thisName = thisProp.getName();
        if (thisName.equals("dmsdir"))
        {
               String[] newDirs = new String[1];
               newDirs[0] = replaceWithBackSlash(dmsdir);
               thisProp.setValues(newDirs);
        } else if (thisName.equals("uid"))
        {
            // do nothing
        } else if (thisName.equals("iflownotification"))
        {
           Vector notVec = new Vector();
           if (emailstate != null)
           {
                notVec.addElement("email");
           }


           if (notVec != null && notVec.size() >= 1)
           {
             String[] newValues = new String[notVec.size()];
             for(int ind=0; ind < notVec.size(); ind++)
             {
                 newValues[ind] = (String)notVec.elementAt(ind);
             }
             thisProp.setValues(newValues);
           }

           if (emailstate == null)
           {
                String[] newStr = new String[1];
                newStr[0] = "";
                thisProp.setValues(newStr);
           }

        } else if (thisName.equals("email"))
        {
           String[] newEmail = new String[1];
           newEmail[0] = email;
           thisProp.setValues(newEmail);
        }

        return thisProp;

    }

    private String replaceWithBackSlash(String str)
    {
        return replace(str, "/", "\\");
    }

    private String replace(String str, String original, String replacement)
    {
        if (str == null || str.length()==0)
            return str;

        StringBuffer sb = new StringBuffer(str);
        for (int i=0; i<sb.length(); i++)
        {
            if (sb.substring(i,i+1).equals(original))
                sb.replace(i,i+1,replacement);
        }
        return sb.toString();
    }

%>