<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppManifest"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppUtil"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppConstants"%>
<%@page import="com.fujitsu.iflow.model.workflow.Plan"%>
<%@page import="com.fujitsu.iflow.model.workflow.ProcessInstantiator"%>
<%@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="com.fujitsu.iflow.model.util.URLEncoder"%>
<%@page import="com.fujitsu.iflow.model.util.WFAppPackager"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Hashtable"%>
<%@page import="java.util.StringTokenizer"%>
<%@page import="java.util.Properties"%>
<%@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;
    String rowColor = bcolor[nextColor];

    session.setAttribute("TabIndex","2");
%>
<html>
<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>

<script language="javascript">
function chooseFile()
{

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

}

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

</head>
<body class="bodyClass" onLoad="goforit();">
<jsp:include page="Header.jsp" flush="true"/>
<br/><br/>

<form method="post" action="ExportWFAppClientFiles.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 1</font></td>
                  </tr>

<%
   Hashtable clientFiles = (Hashtable)session.getAttribute("WFAppClientFiles");
   String clientFile = (String)request.getParameter("ClientFile");
   String exportSelection = (String)request.getParameter("ExportSelection");
   WFAppManifest manifest = (WFAppManifest)session.getAttribute("WFAppManifest");

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

   if(exportSelection == null || exportSelection.trim().equals("") == true)
   {
      WFAppPackager packager = new WFAppPackager();
      Vector planIDStrs = new Vector();
      long [] planIDs = null;
      String planList = (String)request.getParameter("planList");
      if(planList != null)
      {
         StringTokenizer st = new StringTokenizer(planList,",");
         while (st.hasMoreTokens())
         {
            planIDStrs.addElement(st.nextToken());
         }
         planIDs = new long[planIDStrs.size()];
         for(int i=0; i < planIDStrs.size(); i++)
         {
            planIDs[i] = Long.parseLong((String)planIDStrs.elementAt(i));
         }

         manifest = packager.getManifest(wfSession,planIDs);
      }
      Hashtable virtualToRealMappings = manifest.getClientVirtualToRealMapping();
      if(virtualToRealMappings == null)
         virtualToRealMappings = new Hashtable();
      virtualToRealMappings.put(WFAppConstants.IBPM_PORTAL_ROOT_PATTERN, application.getRealPath("/").trim());
      String currentFolderPath = new File(".").getAbsolutePath();
      String rootFolderPath = null;
      Properties sysprops = System.getProperties();
      if(sysprops.getProperty("user.home") != null)
      {
         rootFolderPath = (new File(sysprops.getProperty("user.home"))).getAbsolutePath();
         if(rootFolderPath.charAt(rootFolderPath.length()-1) != File.separatorChar)
            rootFolderPath = rootFolderPath + File.separatorChar;
          
      }
      else
         rootFolderPath = currentFolderPath.substring(0,currentFolderPath.indexOf(File.separator)+1);
      virtualToRealMappings.put(WFAppConstants.CLIENT_ROOT_PATTERN, rootFolderPath);
      manifest.setClientVirtualToRealMapping(virtualToRealMappings);
      session.setAttribute("WFAppManifest",manifest);
   }
   else if(exportSelection.equals("Add") == true)
   {
      if(clientFile != null && clientFile.trim().equals("") != true)
      {
         StringTokenizer tokenizer = new StringTokenizer(clientFile,",");
         while (tokenizer.hasMoreTokens())
         {
            String virtualClientFile = WFAppUtil.rewritePattern(tokenizer.nextToken(), manifest.getClientRealToVirtualMapping());
            clientFiles.put(new Integer(virtualClientFile.hashCode()),virtualClientFile);
         }
      }
   }
   else if(exportSelection.indexOf("Remove_") == 0)
   {
      String indexStr = exportSelection.substring("Remove_".length());
      int index = Integer.parseInt(indexStr);
      clientFiles.remove(new Integer(index));
   }

   session.setAttribute("WFAppClientFiles", clientFiles);
%>

      <tr class="headrow">
      <th>Virtual Location</th>
      <th>Real Location</th>
      </tr>
<%
   Hashtable virtualToRealMappings = manifest.getClientVirtualToRealMapping();
   if(virtualToRealMappings == null)
      virtualToRealMappings = new Hashtable();

   for (Enumeration sElements = virtualToRealMappings.keys() ; sElements.hasMoreElements() ;)
   {
      String virtual = (String)sElements.nextElement();
      String real = (String)virtualToRealMappings.get(virtual);

      rowColor = bcolor[nextColor];
      nextColor = (nextColor + 1)%2;

%>
      <tr bgcolor="#FFFFFF">
      <td><%= virtual %></td>
      <td><%= real %></td>
      </tr>
<%
   }

%>
</TABLE>

<br>
<table class="tableclass">
<tr align=center>
<td>Client Side File <input type=text name=ClientFile value=''>
<input type="button" onClick="chooseFile()" value="Browse..." class="submitClass">
<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="12">List of Client Side Files</th>
      </tr>

<%
      Vector mfiles = insertionSort(clientFiles.elements());
      for (int k=0; k < mfiles.size(); k++)
      {
           clientFile = (String)mfiles.elementAt(k);
           String virtualClientFile = WFAppUtil.rewritePattern(clientFile, manifest.getClientRealToVirtualMapping());
%>
           <TR bgcolor="#FFFFFF">
           <TD><%= virtualClientFile %></TD>
           <TD align="center"><input type="button" value="Remove" class="submitClass"
                onClick="selectOperation('Remove_<%= virtualClientFile.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>
