<%@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.model.util.URLEncoder"%>
<%@page import="com.fujitsu.iflow.model.util.WFAppPackager"%>
<%@page import="com.fujitsu.iflow.model.wfadapter.DMSObject"%>
<%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"%>
<%@page import="com.fujitsu.iflow.model.wfadapter.Folder"%>
<%@page import="com.fujitsu.iflow.model.workflow.DMS"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFSession"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Hashtable"%>
<%@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;
    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.ServerFile.value = fileList
    window.close();
    return true;
}
</script>

<body>

<form enctype="multipart/form-data" method="post" action="ExportWFAppServerFiles.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 Server Side File</font></td>
                  </tr>
                </table>
<br>



<%
   String packageRootPath = application.getRealPath("/").trim();
   if (packageRootPath.endsWith(File.separator))
       packageRootPath = packageRootPath + "packages";
   else
       packageRootPath = packageRootPath + File.separator + "packages";
   
   String pwd = (String)session.getAttribute("password");
   DocRepository docRepos = getDocRepository(packageRootPath, wfSession, pwd);
   DMS dms =  WFObjectFactory.getDMS(wfSession, wfSession.getUserName(), pwd);
   String path = request.getParameter("Path");
   Folder folder = null;
   Folder root = (Folder) docRepos.getRootFolder(dms);
   if(path == null || path.trim().equals("") == true)
   {
     folder = root;
   }
   else
   {
     folder = (Folder) docRepos.getFolder(path);
   }

   int filesNum = 0;
   if(folder != null)
   {
%>
     
     <TABLE>
<%
     DMSObject[] rootList = root.getContents();
     Hashtable myRoots = new Hashtable();
     for(int i=0; i < rootList.length; i++)
     {
         DMSObject dmsObject = rootList[i];
         if(dmsObject.getType() == DMSObject.TYPE_FOLDER)
         {
            String mroot= dmsObject.getPath().replace('\\','/');
            while(mroot.lastIndexOf("/") == (mroot.length()-1))
              mroot = mroot.substring(0,mroot.length()-1);
 
            myRoots.put(mroot,new Integer(1));
         }
     }

     if(folder.getPath().trim().equals("") == false)
     {
        String currentPath = folder.getPath().replace('\\','/');
        String parentPath = null;
        while(currentPath.lastIndexOf("/") == (currentPath.length()-1))
              currentPath = currentPath.substring(0,currentPath.length()-1);
        
        parentPath = currentPath;
        while(parentPath.lastIndexOf("/") == (parentPath.length()-1))
           parentPath = parentPath.substring(0,parentPath.length()-1);

        if(parentPath.lastIndexOf("/") <= 0)
           parentPath = "";
        else
        {
           parentPath = parentPath.substring(0,parentPath.lastIndexOf("/"));
           parentPath = parentPath + "/";
        }
        if(myRoots.get(currentPath) != null)
           parentPath = "";
        out.println("<TR><TD>");
        out.println("<font color=gold><b>[Current Folder: "+ currentPath + "]</b></font><br><br>");
        out.println("</TD></TR>");
        out.println("<TR><TD>");
        out.println("<a href = 'WFAppServerFileChooser.jsp?Path="+ parentPath + "'><font color=blue>" + 
                        "[..]" + "</font></a><br>");
        out.println("</TD></TR>");
     }
     DMSObject[] list = folder.getContents();
     if(list != null)
     {
       Vector myFolders = new Vector();
       Vector myFiles = new Vector();
       for(int i=0; i < list.length; i++)
       {
          DMSObject dmsObject = list[i];
         
          if(dmsObject.getType() == DMSObject.TYPE_FOLDER)
             myFolders.addElement(dmsObject);
          else
             myFiles.addElement(dmsObject);
       }
       myFolders = insertionSort(myFolders);
       myFiles = insertionSort(myFiles);
       filesNum = myFiles.size();
       for(int i=0; i < myFolders.size(); i++)
       {
          out.println("<tr align=left><td>");
          DMSObject dmsObject = (DMSObject)myFolders.elementAt(i);
          String mName = dmsObject.getName();
          if(mName.lastIndexOf("/") <= (mName.length()-2) )
             mName = mName.substring(mName.lastIndexOf("/")+1);
          String mPath = dmsObject.getPath().replace('\\','/');
          out.println("<a href = 'WFAppServerFileChooser.jsp?Path="+  mPath + "'><font color=blue>" + 
                        mName + "</font></a><br>");
          out.println("</td></tr>");
       }
       for(int i=0; i < myFiles.size(); i++)
       {
          out.println("<tr align=left><td>");
          DMSObject dmsObject = (DMSObject)myFiles.elementAt(i);
          String mName = dmsObject.getName();
          if(mName.indexOf("/") <= (mName.length()-2))
             mName = mName.substring(mName.lastIndexOf("/")+1);
          String mFile = dmsObject.getPath().replace('/',File.separatorChar);
          out.println("<input type='checkbox' name='Files' value='" + mFile + "'>" +  mName);
          out.println("</td></tr>");
       }

     }
%>
     </TABLE>
<%
   }
%> 
<%!
    public
    DocRepository
    getDocRepository(
        String documentRoot,
        WFSession wfSession,
        String password)
        throws Exception
    {
        DocRepository docRepository = null;
        String docRoot = null;
        if (documentRoot != null && documentRoot.length() > 0) {
            docRoot = documentRoot;
        }
        else {
            Properties sysProp = System.getProperties();
            docRoot = sysProp.getProperty("user.home");
        }
        DMS dms =  WFObjectFactory.getDMS(wfSession, wfSession.getUserName(), password);
        docRepository = WFObjectFactory.getDocRepository(dms, docRoot);

        return docRepository;
    }

    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;
       }       
    }
%>
<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;
       DMSObject 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 = (DMSObject)mfiles.elementAt(i);
          j = i;
          while ((j > 0) && ((((DMSObject)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>
