<%@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.util.URLEncoder"
%>
<%@page import="java.io.File"%>
<%@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>

<head><title>Interstage Business Process Manager | File Chooser</title></head>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>

<script>
function checkFiles()
{
    var filesNum = document.forms[0].FilesNum.value;
    var i;
    var fileList = "";

    if(filesNum == 0)
       return false;
    else if(filesNum == 1)
    {
       if(document.forms[0].Files.checked==true) 
          fileList = document.forms[0].Files.value;
    }
    else
    {
       for(i=0;i<filesNum;i++) 
       {
          if(document.forms[0].Files[i].checked==true)
          {
             if(fileList == "")
                fileList = document.forms[0].Files[i].value;
             else
                fileList = fileList + "," + document.forms[0].Files[i].value;
          }
       } 
    }
    if(fileList == "")
    {
       alert("Please choose at least one file");
       return false;
    } 
    window.opener.document.packageForm.ClientFile.value = fileList
    window.close();
    return true;
}
</script>

<body >



<form enctype="multipart/form-data" method="post" action="ExportWFAppClientFiles.jsp">
<center>


<TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="650" align="center">
                  <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">Package Workflow Application - Select Client Side File</font></td>
                  </tr>
                </table>
<br>



<%
   String rootPath = application.getRealPath("/").trim();
   String path = request.getParameter("Path");
   File folder = null;
   File root = new File(rootPath);
  
   if(path == null || path.trim().equals("") == true)
     folder = new File(rootPath);
   else
   {
     path = handleBackSlash(path);
     folder = new File(path);
   }
   

   int filesNum = 0;
   if(folder != null)
   {
%>
     
     <TABLE>
<%
     out.println("<TR><TD>");
     out.println("<font color=gold><b>[Current Folder: "+ folder.getAbsolutePath() + "]</b></font><br><br>");
     out.println("</TD></TR>");
     if(folder.getAbsolutePath().equals(root.getAbsolutePath()) == false)
     {
        out.println("<TR><TD>");
        out.println("<a href = 'WFAppClientFileChooser.jsp?Path="+folder.getParentFile().getAbsolutePath() + "'><font color=blue>" + 
                        "[..]" + "</font></a><br>");
        out.println("</TD></TR>");
     }
     File[] list = folder.listFiles();
     if(list != null)
     {
       Vector myFolders = new Vector();
       Vector myFiles = new Vector();
       for(int i=0; i < list.length; i++)
       {
          File myFile = list[i];
          if(myFile.isDirectory() == true)
             myFolders.addElement(myFile);
          else
             myFiles.addElement(myFile);
       }
       myFolders = insertionSort(myFolders);
       myFiles = insertionSort(myFiles);
       filesNum = myFiles.size();
       for(int i=0; i < myFolders.size(); i++)
       {
          out.println("<tr align=left><td>");
          File myFile = (File)myFolders.elementAt(i);
          out.println("<a href = 'WFAppClientFileChooser.jsp?Path="+myFile.getAbsolutePath() + "'><font color=blue>" + 
                        myFile.getName() + "</font></a><br>");
          out.println("</td></tr>");
       }
       for(int i=0; i < myFiles.size(); i++)
       {
          out.println("<tr align=left><td>");
          File myFile = (File)myFiles.elementAt(i);
          out.println("<input type='checkbox' name='Files' value='" + myFile.getAbsolutePath() + "'>" + myFile.getName());
          out.println("</td></tr>");
       }
       
     }
%>
     </TABLE>
<%
   }
%>   

<%!
    
    public String handleBackSlash(String str)
    {
       if(str.indexOf("\\") == -1)
          return str;
       else
       {
          int index = str.indexOf("\\");
          if(str.length() > (index+1))
          {
             if(str.charAt(index+1) == '\\')
             {
                String result = str.substring(0,index+1);
                if(str.length() > (index+2))
                   result = result + handleBackSlash(str.substring(index+2));
                return result;
             }
             
          }
          String result = str.substring(0,index+1);
          if(str.length() > (index+1))
             result = result + handleBackSlash(str.substring(index+1));
          return result;
       }       
    }
%>
<p>

<input type="hidden" name="FilesNum" value="<%= filesNum %>">
<input type="button" onClick="return checkFiles();" value="Submit" name="" class="core-button">
<input type="button" onClick="window.close();" value="Cancel" name="" class="core-button">
</form>
<%!
    public Vector insertionSort(Vector sElements)
    {
       int i, j;
       File file;
       Vector mfiles = new Vector();
       for (int p=0; p < sElements.size(); p++)
       {
          mfiles.addElement(sElements.elementAt(p));
       }
       
       for (i=1; i < mfiles.size(); i++)
       {
          file = (File)mfiles.elementAt(i);
          j = i;
          while ((j > 0) && ((((File)mfiles.elementAt(j-1)).getName()).compareToIgnoreCase(file.getName())>0))
          {
               mfiles.set(j,mfiles.elementAt(j-1));
               j = j - 1;
          }
          mfiles.set(j,file);
       }
       return mfiles;
   }

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