<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="java.util.ArrayList"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.UserInfo"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.utilities.TextEncrypter"
%><%@page import="java.util.Enumeration"
%><%@page import="java.io.File"
%><%@page import="java.io.FileReader"
%><%@page import="java.io.BufferedReader"
%><%@page import="java.util.ArrayList"
%><%@page import="java.util.List"
%><%@page import="java.util.Properties"
%><%@page import="java.lang.Long"
%><%
    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;
    }
    String isArchived = request.getParameter("isArchived");
    String appletCommand = request.getParameter("AppletCommand");
    String itemID="";
    if (appletCommand == null){
        throw new Exception("Page needs an 'appletCommand' parameter.");
    }
    if(appletCommand.equals("EditTemplate") || appletCommand.equals("DisplayTemplate"))
    {
        itemID = request.getParameter("TemplateID");
    }
    else if(appletCommand.equals("EditProcess") || appletCommand.equals("DisplayProcess"))
    {
        itemID = request.getParameter("ProcessID");
    }
    else if(appletCommand.equals("NewTemplate")){
        itemID = "";
    }
    else {
        throw new Exception("Command '"+appletCommand+"' not supported");
    }

    // following check is to just verify that there are now processes spawned from this
    // template, so that it will reduce the overhead of lading the applet (process designer)
    // and then applet throwing an exception for the same.
    if(appletCommand.equals("EditTemplate"))
    {
        Plan tmpPlan = bps.getPlan(Long.parseLong(itemID));
        tmpPlan.startEdit();
        tmpPlan.cancelEdit();
    }

    String referralURL = (String) request.getParameter("referralURL");
    if(referralURL==null) referralURL = "TemplateList.jsp";

    String hostName = (String) session.getAttribute("HostName");
    String userID = (String) session.getAttribute("userName");
    String token = (String) session.getAttribute("password");
    String pluginVersion = (String) session.getAttribute("PluginVersion");
    String appletCodebase = (String) session.getAttribute("AppletCodebase");
    String appletClassid = (String) session.getAttribute("AppletClassid");

    if (token == null || token.trim().length() == 0) {
        token = "dummy";
    }

    // do not password directly in the OBJECT TAG, instead encrypt it and then send.
    TextEncrypter encrypter = new TextEncrypter(TextEncrypter.DES_ENCRYPTION_SCHEME);
    token = encrypter.encrypt(token);

    String transportType = (String) session.getAttribute("TransportType");

    // read the "Client.Jars" parameter from the twf.ini file
    // which lists additional jar files that are customer specific
    // and that will be used while loading the Process Designer Applet.
    String cJars = bps.getServerOption("Client.Jars");

    //For LE we need to Logout inorder to Login from Applet
    if(transportType.equalsIgnoreCase("LE")) {
        String isRemoteCall = (String)session.getAttribute("isRemoteCall");
        if(isRemoteCall !=null && isRemoteCall.equalsIgnoreCase("true")){
            throw new Exception("For the Lite Edition, the Process Designer can only be invoked in a browser that is on the same machine as the server.");
        }
        BPMAdmin bpas = bps.getAdmin();
        UserInfo[] usrInfo = bpas.getAllUserAgentInfo();

        // first entry is always the current user, so log out that one last
        if(usrInfo != null && usrInfo.length > 0)
        {
           for(int i=usrInfo.length-1; i>=0; i--)
            {
                bpas.logoutUser(usrInfo[i].getClientID());
            }
        }
        session.removeAttribute("BPMSession");
        session.removeAttribute("ModelsessionID");
    }


    StringBuffer reqURL = request.getRequestURL();
    reqURL.append("?");
    if (request.getQueryString() != null) {
        reqURL.append(request.getQueryString());
    }
    StringBuffer encodedRefURL = new StringBuffer();
    QuickForm.urlEncode(encodedRefURL,reqURL.toString());
    
    String designerURL = "IBPMProcessDesigner.jsp?referralURL="+encodedRefURL.toString();
    String jarSigned = (String)session.getAttribute("ProcessDesignerEnabled");
    if(jarSigned != null && (jarSigned.equalsIgnoreCase("yes") || jarSigned.equalsIgnoreCase("true")))
        jarSigned = "yes";
    else
        jarSigned = "no";

%>
<HTML><HEAD>

<TITLE>Interstage Business Process Manager | Process Definition Designer</TITLE>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</HEAD>
<BODY topmargin="0" leftmargin="0">
<script language="javascript">
 <%
 if(jarSigned != null && jarSigned.equalsIgnoreCase("no"))
 {
 %>
     alert("The button you clicked launches the Process Designer. However, this version of Interstage Business Process Manager does not have the downloadable Process Designer enabled. Please contact your Interstage support team if you need to enable the Process Designer.");
     window.location = "<%=referralURL%>";
 <%
 }
 if(bps.getServerOption("ServerType").equals("EE-WS"))
 {
 %>
     var isFramed = (top != self);
     if (!isFramed){
        top.location.href = '<%=designerURL%>';
     }
 <%
 }
 %>
   var agt=navigator.userAgent.toLowerCase()
    var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
        && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
        && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

    function appletClosed(){
    <%
        //For LE we need to Login back to Applet so Change the referralURL to LoginAction.jsp
        if(transportType.equalsIgnoreCase("LE")) {
            referralURL = "LoginAction.jsp?userName=" + URLEncoder.encode(userID,"UTF-8")
                + "&password=" + URLEncoder.encode(token,"UTF-8")
                + "&go=" +  URLEncoder.encode(referralURL,"UTF-8");
        }
    %>
    window.location = "<%=referralURL%>";
}
</script>
<%
    String fileName = application.getRealPath("/").trim();
    if (fileName.endsWith(File.separator))
        fileName = fileName + "Taskmanager.conf";
    else
        fileName = fileName + File.separator + "Taskmanager.conf";
    Properties envProp = new Properties();
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    for(line = in.readLine(); line != null; line = in.readLine())
    {
        int pos = line.indexOf("=");
        if(pos == -1 || line.charAt(1) == '#') {
            continue;
        }
        String name = line.substring(0,pos);
        String value = line.substring(pos+1);
        envProp.setProperty(name.trim(),value.trim());
    }
    if (envProp.isEmpty()) {
        throw new Exception("A properties file is expected for this application at the following location: "+fileName
         +"   A system administrator will need to place a configuration file with the right settings there.");
    }

    // include iflow.jar as default in the list.
    String cacheArchiveJars = "iFlow.jar";
    if (transportType.equalsIgnoreCase("LE")) {
        cacheArchiveJars = "lt.jar";
    }

    //Add client jars if any as the required jar
    if (cJars != null && cJars.trim().length() > 0) {
        cacheArchiveJars = cacheArchiveJars + "," + cJars;
    }

%>
<table width="99%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="164"><img src="images/interstage_logo.gif" width="164" height="36"/></td>
    <td width="207"><img src="images/bpm_title.gif" width="207" height="36"/></td>
    <td><img src="images/process_designer_title.gif" width="144" height="36"/></td>
    <%
    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    BPMSession bpmSession = (BPMSession)session.getAttribute("BPMSession");
    if (wfSession != null || bpmSession != null)
    {

%>
    <td align="right">&nbsp;</td>
    <%
    }
%>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="headrow">
    <td height="21">
      <div align="right"> <small><font color='#ffffff' face='verdana' size='1'><span id="clock">
        <script> showDate('<%=new java.util.Date().getTime() %>'); </script>
        &nbsp;&nbsp; </span></font></small></div>
    </td>
  </tr>
  <tr>
    <td height="2"><font size="-6"></font> </td>
  </tr>
  <tr>
    <td>
     <script language="javascript"> if(!is_nav)
      {
        var isArchived = "<%= isArchived%>";

        document.writeln('<object classid="<%QuickForm.writeHtmlToJScript(out,appletClassid);%>" width = 750px height = 30 name = "Toolbar" align = top>')
        document.writeln('<param name="CODE"          value="com.fujitsu.iflow.apps.devclient.IflowUI.class" >')
        document.writeln('<param name="cache_option"  value="Plugin" >')
        document.writeln('<param name="cache_archive" value="<%=cacheArchiveJars%>" >')
        document.writeln('<param name="NAME"          value="Toolbar" >')
        document.writeln('<param name = "type"        value="application/x-java-applet;version=<%QuickForm.writeHtmlToJScript(out,pluginVersion);%>">')
        document.writeln('<param name="scriptable"    value="true">')
        document.writeln('<param name="DebugLevel"    value="0">')
        document.writeln('<param name="AutoLoginDialog" value="false">')
        document.writeln('<param name="AppletCommand" value="<%=appletCommand%>">')
        document.writeln('<param name="ItemID"        value="<%QuickForm.writeHtmlToJScript(out,itemID);%>">')
        if (isArchived!= null && isArchived == "true") {
            document.writeln('<param name="isArchived" value="<%QuickForm.writeHtmlToJScript(out,isArchived);%>">')
            document.writeln('<param name="AdminLogin" value="true">')
        }
        document.writeln('<param name="HostName"      value="<%QuickForm.writeHtmlToJScript(out,hostName);%>Flow">')
        document.writeln('<param name="UserID"        value="<%QuickForm.writeHtmlToJScript(out,userID);%>">')
        document.writeln('<param name="Token"         value="<%QuickForm.writeHtmlToJScript(out,token);%>">')
        document.writeln('<param name="DisableNotification"    value="true">')
        <%
            Enumeration sElements = envProp.keys();
            while(sElements.hasMoreElements())
            {
                String key = (String)sElements.nextElement();
                if(key.equalsIgnoreCase("HostName"))
                {
                    continue;
                }
                String value = envProp.getProperty(key);
            %>
            document.writeln('<param name="<%QuickForm.writeHtmlToJScript(out,key);%>" value="<%QuickForm.writeHtmlToJScript(out,value);%>" >')
            <%
            }
            %>
      document.writeln('</NOEMBED></EMBED></OBJECT>')
      }
      else
      {
        document.writeln('<APPLET width = 750px height = 30 name = "Toolbar" align = "top"')
        document.writeln('CODE="com.fujitsu.iflow.apps.devclient.IflowUI.class"')
        document.writeln('cache_option="Plugin"')
        document.writeln('cache_archive="<%=cacheArchiveJars%>"')
        document.writeln('type="application/x-java-applet;version=<%QuickForm.writeHtmlToJScript(out,pluginVersion);%>"')
        document.writeln('NAME="Toolbar"')
        document.writeln('MAYSCRIPT="true"')
        document.writeln('DebugLevel="0"')
        document.writeln('AutoLoginDialog="false"')
        document.writeln('AppletCommand="<%=appletCommand%>"')
        document.writeln('ItemID="<%QuickForm.writeHtmlToJScript(out,itemID);%>"')
        if (isArchived!= null && isArchived == "true") {
               document.writeln('isArchived="<%QuickForm.writeHtmlToJScript(out,isArchived);%>"')
               document.writeln('AdminLogin="true"')
        }
        document.writeln('HostName="<%QuickForm.writeHtmlToJScript(out,hostName);%>Flow"')
        document.writeln('UserID="<%QuickForm.writeHtmlToJScript(out,userID);%>"')
        document.writeln('Token="<%QuickForm.writeHtmlToJScript(out,token);%>"')
        document.writeln('<param name="DisableNotification"    value="true">')
        <%
            Enumeration nsElements = envProp.keys();
            while(nsElements.hasMoreElements())
            {
                String key = (String)nsElements.nextElement();
                if(key.equalsIgnoreCase("HostName"))
                {
                    continue;
                }
                String value = envProp.getProperty(key);
            %>
            document.writeln('<%QuickForm.writeHtmlToJScript(out,key);%>="<%QuickForm.writeHtmlToJScript(out,value);%>"')
            <%
            }
            %>
        document.writeln('</APPLET>')
      }
    </script>
    </td>
  </tr>
  <tr>
    <td height="3"><font size="-6"></font> </td>
  </tr>
</table>
  <script language="javascript"> if(!is_nav)
  {
    document.writeln('<OBJECT classid="<%QuickForm.writeHtmlToJScript(out,appletClassid);%>" WIDTH="100%" HEIGHT="94%" NAME="GPUI" codebase="<%QuickForm.writeHtmlToJScript(out,appletCodebase);%>">')
    document.writeln('<PARAM NAME="CODE"           VALUE="com.fujitsu.iflow.gui.planview.GVApplet.class" >')
    document.writeln('<PARAM NAME="cache_option"   VALUE="Plugin" >')
    document.writeln('<PARAM NAME="cache_archive"  VALUE="<%=cacheArchiveJars%>" >')
    document.writeln('<PARAM NAME="NAME"           VALUE="GPUI" >')
    document.writeln('<PARAM NAME = "type" VALUE="application/x-java-applet;version=<%QuickForm.writeHtmlToJScript(out,pluginVersion);%>">')
    document.writeln('<PARAM NAME="scriptable"     VALUE="true">')
    document.writeln('<param name="DebugLevel"    value="0">')
    document.writeln('<PARAM NAME="ParentApplication" value="IflowUI">')
    document.writeln('<param name="DisableNotification"    value="true">')
    <%
        Enumeration enumKey = envProp.keys();
        while(enumKey.hasMoreElements())
        {
            String key = (String)enumKey.nextElement();
            if(key.equalsIgnoreCase("HostName")) {
                continue;
            }
            String value = envProp.getProperty(key);
            %>
            document.writeln('<param name = "<%QuickForm.writeHtmlToJScript(out,key);%>" value = "<%QuickForm.writeHtmlToJScript(out,value);%>" >')
            <%
        }
     %>
      document.writeln('</NOEMBED></EMBED></OBJECT>')
  }
  else
  {
    document.writeln('<APPLET WIDTH="100%" HEIGHT="94%" NAME="GPUI"')
    document.writeln('CODE="com.fujitsu.iflow.gui.planview.GVApplet.class"')
    document.writeln('archive="<%=cacheArchiveJars%>"')
    document.writeln('type="application/x-java-applet;version=<%QuickForm.writeHtmlToJScript(out,pluginVersion);%>"')
    document.writeln('NAME="GPUI"')
    document.writeln('DebugLevel="1"')
    document.writeln('MAYSCRIPT="true"')
    document.writeln('ParentApplication="IflowUI"')
    document.writeln('<param name="DisableNotification"    value="true">')
    <%
      Enumeration nsenumKey = envProp.keys();
      while(nsenumKey.hasMoreElements()) {
          String key = (String)nsenumKey.nextElement();
          if(key.equalsIgnoreCase("HostName")) {
              continue;
          }
          String value = envProp.getProperty(key);
          %>
            document.writeln('<%QuickForm.writeHtmlToJScript(out,key);%>="<%QuickForm.writeHtmlToJScript(out,value);%>"')
          <%
      }
    %>
    document.writeln('</APPLET>')
  }
  </script>
</BODY>
</HTML>
