<%@ 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 type = request.getParameter("type");
    String bodyClassType="bodyClass";
    if(type == null || type.length() <=0)
    {
        type = "devmgr";
    }
    if(type.equalsIgnoreCase("admin"))
    {
        bodyClassType = "adminBodyClass";
    }
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
    session.setAttribute("TabIndex","-1");

    long id = -1;

//    String[] members = (String[]) session.getAttribute("members");
//    int memberNum = Integer.parseInt((String) request.getParameter("memberNum"));
    String password = (String) session.getAttribute("password");
//    String user = members[memberNum];
    String user = (String) session.getAttribute("userName");
    ProfileProp[] proProps = getUserPropList(wfSession, user, password);

    final String emailName = "email";
    final String dmsdirName = "dmsdir";
    final String notificationName = "iflownotification";

    String email = getPropValue(proProps,emailName);
    String dmsdir = getPropValue(proProps,dmsdirName);
    String notification = getPropValue(proProps,notificationName);

    boolean emailOn = false;
    Vector vecNotification = stringParserByDelimiter(notification,",");
    if (vecNotification != null && vecNotification.size() > 0)
    {
        for (int i=0; i < vecNotification.size(); i++)
        {
            String curStr = (String)vecNotification.elementAt(i);
            if (curStr.equalsIgnoreCase("email"))
            {
                emailOn = true;
            }
        }
    }
    String actionURL = "UserPreferencesAction.jsp";
%>
<html>
<head>
    <title>Interstage Business Process Manager | User Profile Management</title>
    <jsp:include page="StyleSheet.jsp" flush="true"/>
    <script language="javascript" src="styles/clockscripts.js"></script>

</head>

<body class=<%=bodyClassType%>>
<% if(type.equalsIgnoreCase("admin")){ %>
    <jsp:include page="Header.jsp?type=admin" flush="true"/><br/><br/>
<%} else {%>
    <jsp:include page="Header.jsp?type=devmgr" flush="true"/><br/><br/>
<%}%>
<SCRIPT TYPE="text/javascript">

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


function openDmsBrowser(url)
{

    var w = '450';
    var h = '400';
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
    var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+'yes'+',';
      settings +='resizable=no';
    var childWindow = window.open(url+"?ShowFile=false&ParentPage=UserPreferences", "DmsBrowser",settings);
    if (childWindow.opener == null)
        childWindow.opener = self;
}


function updateProfile(estate, eAddr, dmsDIR, adForm)
{
    if (eAddr != null && eAddr != "")
    {
        var index1 = eAddr.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)
        if (index1 == -1)
        {
            alert("E-mail address entered for 'E-mail Notification' is not valid.");
            return;
        }
    }
    if(estate.checked==true && eAddr == "")    {
       	alert("Please enter the valid email address for 'E-mail Notification'");
		return;
    }
    updateProfileForm.emailstate.value = estate
    updateProfileForm.email.value = eAddr
    updateProfileForm.dmsdir.value = dmsDIR
    updateProfileForm.submit()
}

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

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

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

</script>

<center>
<FORM NAME="updateProfileForm" ENCTYPE="multipart/form-data" METHOD="GET" ACTION="<%= actionURL %>">
   <input type="hidden" name="userId" value="<%= (String)request.getParameter("userId") %>">
    <table bgcolor="#CCCCCC" width="60%" border="0" cellpadding="0" cellspacing="0"><tr><td>
    <table width="100%" border="0" cellpadding="4" cellspacing="1" align="center" class="tableclass">
<tr>
    <td colspan="2" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2"><b>User Profile for <%= user %></b></font></td>
</tr>
       <tr><td class="headrow" width="200">
                           <%
                               if (emailOn)
                               {
                                  out.println("<INPUT TYPE=CHECKBOX NAME=emailstate VALUE=evalue CHECKED>&nbsp;E-mail Notification");
                               }
                               else
                               {
                                  out.println("<INPUT TYPE=CHECKBOX NAME=emailstate VALUE=evalue>&nbsp;E-mail Notification");
                               }
                           %>
                       </td>
                       <td bgcolor="#FFFFFF">&nbsp;
                       <input type=text value="<%= email %>" NAME="email" size="55">
                       </td>
                   </tr>
                   <tr><td class="headrow">&nbsp;Default DMS Directory</td>
                       <td bgcolor="#EEEEEE">&nbsp;
                       <input type=text value = "<%= dmsdir %>" NAME="dmsdir" size="55">
                               <input type="button" name="brwDms" value="Browse.." class="core-button" onClick="openDmsBrowser('DmsBrowser.jsp')"/>
                               </td>

      </tr>
            <tr>
            <td colspan ="2" valign="middle" align="center" bgcolor="#FFFFFF">
            <input type="button" value="Update" name="check" onClick="updateProfile(this.form.emailstate, this.form.email.value,this.form.dmsdir.value,this.form)" class="core-button">
            <input type="button" value="Cancel" name="check" onClick="javascript:history.back();" class="core-button">
            </td>
            </tr>
          </TABLE>
          </td></tr></table>
          </FORM>
      </center>
</BODY>
</html>

<%!

    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 Vector stringParserByDelimiter(String resultArray, String delimiter) throws Exception
    {
        int containsDelimFlag = -1;
        Vector tokenNames = new Vector();
        String curTokenName = "";

        try {
            while (resultArray.indexOf(delimiter) != containsDelimFlag)
            {
                int delimIndex = resultArray.indexOf(delimiter);
                curTokenName = resultArray.substring(0,delimIndex);
                resultArray = resultArray.substring(delimIndex+1,resultArray.length());
                tokenNames.addElement(curTokenName);
                if (resultArray == "" || resultArray == null)
                {
                    break;
                }
            }
            tokenNames.addElement(resultArray);
        } catch (Exception e)
        {

        }
        return tokenNames;
    }

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