<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppComponent"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppManifest"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppUtil"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFSession"%>
<%@page import="com.fujitsu.iflow.model.util.URLEncoder"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Hashtable"%>
<%@page import="java.util.StringTokenizer"%>
<%@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;
    }
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    session.setAttribute("TabIndex","2");
%>
<html>

<SCRIPT TYPE="text/javascript">
<!--
function chooseFile()
{

  window.open('WFAppServerFileChooser.jsp', 'File', 'width=600,height=500,scrollbars=yes,resizable=yes');

}
//-->
</SCRIPT>

<script language="javascript">
function selectOperation(operation)
{
   if(operation == 'Continue')
   {
      document.location.href='ConfirmWFAppPackage.jsp';
   }
   else
   {
      document.packageForm.ExportSelection.value = operation
      document.packageForm.submit();
   }
}
</script>

<head><title>Interstage Business Process Manager | Export Package</title></head>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>
<body class="bodyClass" onLoad="goforit();">
<jsp:include page="Header.jsp" flush="true"/>
<br/><br/>

<form method="post" action="ExportWFAppServerFiles.jsp" name=packageForm>
<center>


<TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="80%" align="center">
      <COL WIDTH="30%"><COL WIDTH="70%">
                  <tr align="center" class="headrow">
                    <td colspan="12" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">Package Workflow Application - Step 2</font></td>
                  </tr>


<%
   Hashtable serverFiles = (Hashtable)session.getAttribute("WFAppServerFiles");
   String serverFile = (String)request.getParameter("ServerFile");
   String exportSelection = (String)request.getParameter("ExportSelection");
   WFAppManifest manifest = (WFAppManifest)session.getAttribute("WFAppManifest");
   String selectedFile = (String)request.getParameter("SelectedFile");

   if(serverFiles == null)
      serverFiles = new Hashtable();

   if(exportSelection == null || exportSelection.trim().equals("") == true)
   {
      WFAppComponent[] serverComponents = manifest.getServerComponents();

      if(serverComponents != null)
      {
         for(int k=0; k < serverComponents.length; k++)
         {
            WFAppComponent serverComponent = serverComponents[k];
            serverFiles.put(new Integer(serverComponent.getLocation().hashCode()),serverComponent.getLocation());
         }
      }

   }
   else if(exportSelection.equals("Add") == true)
   {
      if(serverFile != null && serverFile.trim().equals("") != true)
      {
         StringTokenizer tokenizer = new StringTokenizer(serverFile,",");
         while (tokenizer.hasMoreTokens())
         {
            String virtualServerFile = WFAppUtil.rewritePattern(tokenizer.nextToken(), manifest.getRealToVirtualMapping());
            serverFiles.put(new Integer(virtualServerFile.hashCode()),virtualServerFile);
         }
      }
   }
   else if(exportSelection.indexOf("Remove_") == 0)
   {
      String indexStr = exportSelection.substring("Remove_".length());
      int index = Integer.parseInt(indexStr);
      serverFiles.remove(new Integer(index));
   }

   session.setAttribute("WFAppServerFiles", serverFiles);

%>

      <tr class="headrow">
      <th>Virtual Location</th>
      <th>Real Location</th>
      </tr>
<%
   Hashtable virtualToRealMappings = manifest.getVirtualToRealMapping();
   for (Enumeration sElements = virtualToRealMappings.keys() ; sElements.hasMoreElements() ;)
   {
      String virtual = (String)sElements.nextElement();
      String real = (String)virtualToRealMappings.get(virtual);
      if(virtual.indexOf("URL") >=0)
         continue;
%>
      <tr bgcolor="#FFFFFF">
      <td><%= virtual %></td>
      <td><%= real %></td>
      </tr>
<%
   }

%>
</TABLE>

<br>
<table class="tableclass">
<tr align=center>
<td>Server Side File <input type=text name=ServerFile value=''>
<input type="button" onClick="chooseFile()" value="Browse..." class="submitClass">&nbsp;
<input type="button" value="Add" class="submitClass" onClick="selectOperation('Add')">
</td>
</tr>
</table><br>


<TABLE border="0" cellpadding=4 cellspacing=1 class="tableClass" width="80%" bgcolor="#CCCCCC"  align="center">
      <tr class="headrow">
      <th colspan="23" >List of Server Side Files</th>
      </tr>

<%
      Vector mfiles = insertionSort(serverFiles.elements());
      for (int k=0; k < mfiles.size(); k++)
      {
           serverFile = (String)mfiles.elementAt(k);
           String virtualServerFile = WFAppUtil.rewritePattern(serverFile, manifest.getRealToVirtualMapping());
%>
           <TR bgcolor="#FFFFFF">
           <TD><%= virtualServerFile %></TD>
           <TD align="center"><input type="button" value="Remove" class="submitClass"
                onClick="selectOperation('Remove_<%= virtualServerFile.hashCode() %>')"></TD>
           </TR>
<%
      }
%>
</TABLE>


<br>
<center><input type="button" value="Continue" class="submitClass" onClick="selectOperation('Continue')"></center>

<input type=hidden name="ExportSelection" value="">

</form>

<%!
    public Vector insertionSort(Enumeration sElements)
    {
       int i, j;
       String fileName;
       Vector mfiles = new Vector();
       for (; sElements.hasMoreElements() ;)
       {
          mfiles.addElement(sElements.nextElement());
       }
       
       for (i=1; i < mfiles.size(); i++)
       {
          fileName = (String)mfiles.elementAt(i);
          j = i;
          while ((j > 0) && (((String)mfiles.elementAt(j-1)).compareTo(fileName)>0))
          {
               mfiles.set(j,mfiles.elementAt(j-1));
               j = j - 1;
          }
          mfiles.set(j,fileName);
       }
       return mfiles;
   }

%>
                     
</center>
</body>
</html>
