www/filemanager/directories/list_directories.jsp

Code
Comments
Other
Rev Date Author Line
1794 19 Jan 06 nicklas 1 <%-- $Id$
1794 19 Jan 06 nicklas 2   ------------------------------------------------------------------
5425 23 Sep 10 nicklas 3   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg
1794 19 Jan 06 nicklas 4
2304 22 May 06 jari 5   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 6   Available at http://base.thep.lu.se/
1794 19 Jan 06 nicklas 7
1794 19 Jan 06 nicklas 8   BASE is free software; you can redistribute it and/or
1794 19 Jan 06 nicklas 9   modify it under the terms of the GNU General Public License
4476 05 Sep 08 jari 10   as published by the Free Software Foundation; either version 3
1794 19 Jan 06 nicklas 11   of the License, or (at your option) any later version.
1794 19 Jan 06 nicklas 12
1794 19 Jan 06 nicklas 13   BASE is distributed in the hope that it will be useful,
1794 19 Jan 06 nicklas 14   but WITHOUT ANY WARRANTY; without even the implied warranty of
1794 19 Jan 06 nicklas 15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1794 19 Jan 06 nicklas 16   GNU General Public License for more details.
1794 19 Jan 06 nicklas 17
1794 19 Jan 06 nicklas 18   You should have received a copy of the GNU General Public License
4510 11 Sep 08 jari 19   along with BASE. If not, see <http://www.gnu.org/licenses/>.
1794 19 Jan 06 nicklas 20   ------------------------------------------------------------------
1794 19 Jan 06 nicklas 21
1794 19 Jan 06 nicklas 22   @author Nicklas
1794 19 Jan 06 nicklas 23   @version 2.0
1794 19 Jan 06 nicklas 24 --%>
5426 24 Sep 10 nicklas 25 <%@ page pageEncoding="UTF-8" session="false"
1794 19 Jan 06 nicklas 26   import="net.sf.basedb.core.SessionControl"
1794 19 Jan 06 nicklas 27   import="net.sf.basedb.core.DbControl"
1794 19 Jan 06 nicklas 28   import="net.sf.basedb.core.SystemItems"
1794 19 Jan 06 nicklas 29   import="net.sf.basedb.core.Item"
1803 23 Jan 06 nicklas 30   import="net.sf.basedb.core.ItemContext"
1794 19 Jan 06 nicklas 31   import="net.sf.basedb.core.Directory"
4742 09 Feb 09 martin 32   import="net.sf.basedb.core.Experiment"
1794 19 Jan 06 nicklas 33   import="net.sf.basedb.core.User"
1794 19 Jan 06 nicklas 34   import="net.sf.basedb.core.ItemQuery"
1794 19 Jan 06 nicklas 35   import="net.sf.basedb.core.ItemResultIterator"
1794 19 Jan 06 nicklas 36   import="net.sf.basedb.core.Permission"
1794 19 Jan 06 nicklas 37   import="net.sf.basedb.core.query.Restrictions"
1794 19 Jan 06 nicklas 38   import="net.sf.basedb.core.query.Expressions"
1794 19 Jan 06 nicklas 39   import="net.sf.basedb.core.query.Orders"
4742 09 Feb 09 martin 40   import="net.sf.basedb.core.query.Hql"
1794 19 Jan 06 nicklas 41   import="net.sf.basedb.util.FileUtil"
1794 19 Jan 06 nicklas 42   import="net.sf.basedb.clients.web.Base"
1794 19 Jan 06 nicklas 43   import="net.sf.basedb.clients.web.util.HTML"
2753 20 Oct 06 nicklas 44   import="net.sf.basedb.util.Values"
1794 19 Jan 06 nicklas 45   import="java.util.Map"
4794 02 Mar 09 nicklas 46   import="java.util.Set"
4794 02 Mar 09 nicklas 47   import="java.util.HashSet"
1794 19 Jan 06 nicklas 48   import="java.util.List"
6385 18 Dec 13 nicklas 49   import="org.json.simple.JSONArray"
6385 18 Dec 13 nicklas 50   import="org.json.simple.JSONObject"
1794 19 Jan 06 nicklas 51 %>
1794 19 Jan 06 nicklas 52 <%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
1794 19 Jan 06 nicklas 53 <%@ taglib prefix="tbl" uri="/WEB-INF/table.tld" %>
1794 19 Jan 06 nicklas 54 <%!
6385 18 Dec 13 nicklas 55
6385 18 Dec 13 nicklas 56 void generateSubTree(JSONObject jsonParent, Directory parent, Map<Directory, List<Directory>> tree, Set<Directory> ignore)
1794 19 Jan 06 nicklas 57 {
6385 18 Dec 13 nicklas 58   if (ignore.contains(parent)) return;
4794 02 Mar 09 nicklas 59   ignore.add(parent);
6385 18 Dec 13 nicklas 60
1794 19 Jan 06 nicklas 61   List<Directory> children = tree.get(parent);
6385 18 Dec 13 nicklas 62   if (children == null) return;
7983 22 Jun 21 nicklas 63   boolean createFilePermission = parent.getSessionControl().hasPermission(Permission.CREATE, Item.FILE);
6385 18 Dec 13 nicklas 64   
1794 19 Jan 06 nicklas 65   for (Directory child : children)
1794 19 Jan 06 nicklas 66   {
4794 02 Mar 09 nicklas 67     if (ignore.contains(child)) continue;
3719 12 Sep 07 nicklas 68     String folderIcon = "Folder";
3719 12 Sep 07 nicklas 69     if (child.isHomeDirectory())
3719 12 Sep 07 nicklas 70     {
3719 12 Sep 07 nicklas 71       folderIcon = "UserHome";
3719 12 Sep 07 nicklas 72     }
3719 12 Sep 07 nicklas 73     else if (child.isRemoved())
3719 12 Sep 07 nicklas 74     {
3719 12 Sep 07 nicklas 75       folderIcon = "FolderDeleted";
3719 12 Sep 07 nicklas 76     }
3719 12 Sep 07 nicklas 77     else if (child.getAutoCompress())
3719 12 Sep 07 nicklas 78     {
3719 12 Sep 07 nicklas 79       folderIcon = "FolderCompressed";
3719 12 Sep 07 nicklas 80     }
6385 18 Dec 13 nicklas 81     
6385 18 Dec 13 nicklas 82     JSONObject jsonDir = newJoustEntry(jsonParent, folderIcon, HTML.encodeTags(child.getName()), Integer.toString(child.getId()));
6385 18 Dec 13 nicklas 83     jsonDir.put("path", child.getPath().toString());
6385 18 Dec 13 nicklas 84     jsonDir.put("isLazy", 1);
7983 22 Jun 21 nicklas 85     jsonDir.put("allowUpload", createFilePermission && child.hasPermission(Permission.USE));
6385 18 Dec 13 nicklas 86     generateSubTree(jsonDir, child, tree, ignore);
1794 19 Jan 06 nicklas 87   }
1794 19 Jan 06 nicklas 88 }
6385 18 Dec 13 nicklas 89 JSONObject newJoustEntry(JSONObject jsonParent, String icon, String text, String id)
6385 18 Dec 13 nicklas 90 {
6385 18 Dec 13 nicklas 91   JSONObject jsonJoust = new JSONObject();
6385 18 Dec 13 nicklas 92   jsonJoust.put("icon", icon);
6385 18 Dec 13 nicklas 93   jsonJoust.put("text", text);
6385 18 Dec 13 nicklas 94   jsonJoust.put("id", id);
6385 18 Dec 13 nicklas 95   if (jsonParent != null)
6385 18 Dec 13 nicklas 96   {
6385 18 Dec 13 nicklas 97     JSONArray jsonChildren = (JSONArray)jsonParent.get("children");
6385 18 Dec 13 nicklas 98     if (jsonChildren == null)
6385 18 Dec 13 nicklas 99     {
6385 18 Dec 13 nicklas 100       jsonChildren = new JSONArray();
6385 18 Dec 13 nicklas 101       jsonParent.put("children", jsonChildren);
6385 18 Dec 13 nicklas 102       jsonParent.remove("isLazy");
6385 18 Dec 13 nicklas 103     }
6385 18 Dec 13 nicklas 104     jsonChildren.add(jsonJoust);
6385 18 Dec 13 nicklas 105   }
6385 18 Dec 13 nicklas 106   return jsonJoust;
6385 18 Dec 13 nicklas 107 }
1794 19 Jan 06 nicklas 108 %>
1794 19 Jan 06 nicklas 109 <%
1794 19 Jan 06 nicklas 110 final Item itemType = Item.DIRECTORY;
1794 19 Jan 06 nicklas 111 final SessionControl sc = Base.getExistingSessionControl(pageContext, Permission.DENIED, itemType);
1794 19 Jan 06 nicklas 112 final String ID = sc.getId();
7954 12 May 21 nicklas 113 final DbControl dc = sc.newDbControl(":List "+itemType);
1794 19 Jan 06 nicklas 114 final String mode = request.getParameter("mode");
1794 19 Jan 06 nicklas 115 final String callback = request.getParameter("callback");
1794 19 Jan 06 nicklas 116 final float scale = Base.getScale(sc);
1794 19 Jan 06 nicklas 117 final String requestTitle = request.getParameter("title");
6385 18 Dec 13 nicklas 118 final boolean syncWithDirectory = Values.getBoolean(request.getParameter("sync"), true);
1803 23 Jan 06 nicklas 119 final ItemContext cc = sc.getCurrentContext(itemType);
2432 27 Jun 06 nicklas 120 int directoryId = cc.getId();
1794 19 Jan 06 nicklas 121 try
1794 19 Jan 06 nicklas 122 {
1794 19 Jan 06 nicklas 123   final User user = User.getById(dc, sc.getLoggedInUserId());
1794 19 Jan 06 nicklas 124   final Directory userHome = user.getHomeDirectory();
1794 19 Jan 06 nicklas 125   final boolean readDirectories = sc.hasPermission(Permission.READ, Item.DIRECTORY);
4951 27 May 09 nicklas 126   final Directory root = Directory.getById(dc, SystemItems.getId(readDirectories ? Directory.ROOT : Directory.HOME));
4951 27 May 09 nicklas 127   
4951 27 May 09 nicklas 128   Directory current = null;
4951 27 May 09 nicklas 129   // Try to get current/last visited directory. If not successful, use default directory.
4951 27 May 09 nicklas 130   try
4951 27 May 09 nicklas 131   {
4951 27 May 09 nicklas 132     current = directoryId != 0 ? Directory.getById(dc, directoryId) : null;
4951 27 May 09 nicklas 133   }
4951 27 May 09 nicklas 134   catch (Throwable ex){}  
4794 02 Mar 09 nicklas 135   if (current == null) current = userHome;
4794 02 Mar 09 nicklas 136   if (current == null) current = root;
4794 02 Mar 09 nicklas 137   
1794 19 Jan 06 nicklas 138   final String rootIcon;
1794 19 Jan 06 nicklas 139   final String rootTitle;
1794 19 Jan 06 nicklas 140   if (root.isRootDirectory())
1794 19 Jan 06 nicklas 141   {
1794 19 Jan 06 nicklas 142     rootIcon = "Root";
1794 19 Jan 06 nicklas 143     rootTitle = "Root";
1794 19 Jan 06 nicklas 144   }
1794 19 Jan 06 nicklas 145   else
1794 19 Jan 06 nicklas 146   {
1794 19 Jan 06 nicklas 147     rootIcon = "Home";
1794 19 Jan 06 nicklas 148     rootTitle = "Other users";
1794 19 Jan 06 nicklas 149   }
4794 02 Mar 09 nicklas 150   Map<Directory, List<Directory>> tree = FileUtil.loadMinimalDirectoryTree(dc, root, userHome, current);
4794 02 Mar 09 nicklas 151   Set<Directory> ignore = new HashSet<Directory>();
6385 18 Dec 13 nicklas 152   
6385 18 Dec 13 nicklas 153   // Create directory tree
6385 18 Dec 13 nicklas 154   JSONArray json = new JSONArray();
4794 02 Mar 09 nicklas 155
6385 18 Dec 13 nicklas 156   // Search results -- hidden to begin with
6385 18 Dec 13 nicklas 157   JSONObject jsonSearch = newJoustEntry(null, "Search", "Search results", "search");
6385 18 Dec 13 nicklas 158   jsonSearch.put("noOutlineIcon" , 1);
6385 18 Dec 13 nicklas 159   jsonSearch.put("className", "search");
6385 18 Dec 13 nicklas 160   json.add(jsonSearch);
1794 19 Jan 06 nicklas 161   
6385 18 Dec 13 nicklas 162   // User home directory
6385 18 Dec 13 nicklas 163   if (userHome != null)
4794 02 Mar 09 nicklas 164   {
6385 18 Dec 13 nicklas 165     JSONObject jsonHome = newJoustEntry(null, "Home", "My home", Integer.toString(userHome.getId()));
6385 18 Dec 13 nicklas 166     jsonHome.put("noOutlineIcon" , 1);
6385 18 Dec 13 nicklas 167     jsonHome.put("isOpen", "1");
6385 18 Dec 13 nicklas 168     jsonHome.put("path", userHome.getPath().toString());
6385 18 Dec 13 nicklas 169     json.add(jsonHome);
6385 18 Dec 13 nicklas 170     generateSubTree(jsonHome, userHome, tree, ignore);
4794 02 Mar 09 nicklas 171   }
6385 18 Dec 13 nicklas 172   
6385 18 Dec 13 nicklas 173   // Main root directory
6385 18 Dec 13 nicklas 174   JSONObject jsonRoot = newJoustEntry(null, rootIcon, HTML.encodeTags(rootTitle), Integer.toString(root.getId()));
6385 18 Dec 13 nicklas 175   jsonRoot.put("noOutlineIcon" , 1);
6385 18 Dec 13 nicklas 176   jsonRoot.put("isOpen" , 1);
6385 18 Dec 13 nicklas 177   jsonRoot.put("path", root.getPath().toString());
6385 18 Dec 13 nicklas 178   json.add(jsonRoot);
6385 18 Dec 13 nicklas 179   generateSubTree(jsonRoot, root, tree, ignore);
6308 20 Aug 13 nicklas 180
6385 18 Dec 13 nicklas 181   // Add 'current' as root entry if we have not seen it before
6385 18 Dec 13 nicklas 182   if (!ignore.contains(current))
1794 19 Jan 06 nicklas 183   {
6385 18 Dec 13 nicklas 184     // but... move up as far as the user has 'read' permission...
6385 18 Dec 13 nicklas 185     Directory currentRoot = current;
6385 18 Dec 13 nicklas 186     try
1794 19 Jan 06 nicklas 187     {
6385 18 Dec 13 nicklas 188       while (currentRoot != null && !ignore.contains(currentRoot.getParent()))
2115 27 Mar 06 nicklas 189       {
6385 18 Dec 13 nicklas 190         currentRoot = currentRoot.getParent();
2115 27 Mar 06 nicklas 191       }
1794 19 Jan 06 nicklas 192     }
6385 18 Dec 13 nicklas 193     catch (Throwable t)
6385 18 Dec 13 nicklas 194     {}
6385 18 Dec 13 nicklas 195     if (currentRoot == null) currentRoot = current;
6385 18 Dec 13 nicklas 196     JSONObject jsonCurrentRoot = newJoustEntry(null, "Folder", HTML.encodeTags(currentRoot.getPath().toString()), Integer.toString(currentRoot.getId()));
6385 18 Dec 13 nicklas 197     jsonCurrentRoot.put("noOutlineIcon" , 1);
6385 18 Dec 13 nicklas 198     jsonCurrentRoot.put("isOpen" , 1);
6385 18 Dec 13 nicklas 199     jsonCurrentRoot.put("path", currentRoot.getPath().toString());
6385 18 Dec 13 nicklas 200     json.add(jsonCurrentRoot);
6385 18 Dec 13 nicklas 201     generateSubTree(jsonCurrentRoot, currentRoot, tree, ignore);
1794 19 Jan 06 nicklas 202   }
1794 19 Jan 06 nicklas 203   %>
6385 18 Dec 13 nicklas 204   <base:page title="<%=requestTitle != null ? requestTitle : ""%>" 
6385 18 Dec 13 nicklas 205     type="<%="selectonedirectory".equals(mode) ? "popup" : "iframe"%>" id="list-page">
6386 18 Dec 13 nicklas 206   <base:head scripts="joust-2.js,dragdrop.js,~directories.js" styles="joust-2.css,toolbar.css">
6385 18 Dec 13 nicklas 207   <style>
6385 18 Dec 13 nicklas 208   #search
5179 04 Nov 09 nicklas 209   {
6385 18 Dec 13 nicklas 210     display:none; 
6385 18 Dec 13 nicklas 211     font-style: italic; 
6385 18 Dec 13 nicklas 212     border-bottom: 1px solid #cccccc; 
6385 18 Dec 13 nicklas 213     margin-bottom: 2px; 
6385 18 Dec 13 nicklas 214     padding-bottom: 2px;
5179 04 Nov 09 nicklas 215   }
7983 22 Jun 21 nicklas 216   .drop-upload
7983 22 Jun 21 nicklas 217   {
7983 22 Jun 21 nicklas 218     outline: 1px solid black !important;
7983 22 Jun 21 nicklas 219   }
7983 22 Jun 21 nicklas 220
7983 22 Jun 21 nicklas 221   #uploadTooltip
7983 22 Jun 21 nicklas 222   {
7983 22 Jun 21 nicklas 223     position: absolute;
7983 22 Jun 21 nicklas 224     background-color: white;
7983 22 Jun 21 nicklas 225     padding: 0.25em;
7983 22 Jun 21 nicklas 226     z-index: 999;
7983 22 Jun 21 nicklas 227     max-width: 30em;
7983 22 Jun 21 nicklas 228     border-radius: 5px;
7983 22 Jun 21 nicklas 229     box-shadow: 0 0 6px;
7983 22 Jun 21 nicklas 230   }
6385 18 Dec 13 nicklas 231   </style>
6385 18 Dec 13 nicklas 232   </base:head>
6385 18 Dec 13 nicklas 233   
6385 18 Dec 13 nicklas 234   <base:body>
6385 18 Dec 13 nicklas 235     <div id="page-data" class="datacontainer"
6385 18 Dec 13 nicklas 236       data-mode="<%=mode == null ? "" : mode%>"
6385 18 Dec 13 nicklas 237       data-sync-with-directory="<%=syncWithDirectory ? 1 : 0%>"
6385 18 Dec 13 nicklas 238       data-initial-directory="<%=current.getId() %>"
6385 18 Dec 13 nicklas 239     ></div>
7983 22 Jun 21 nicklas 240     <div id="uploadTooltip" class="messagecontainer" style="display: none;"></div>
6385 18 Dec 13 nicklas 241     <%
6385 18 Dec 13 nicklas 242     if ("selectonedirectory".equals(mode))
5179 04 Nov 09 nicklas 243     {
6385 18 Dec 13 nicklas 244       %>
6385 18 Dec 13 nicklas 245         <h1><%=requestTitle == null ? "Select one directory" : requestTitle%></h1>
6385 18 Dec 13 nicklas 246         
6385 18 Dec 13 nicklas 247         <form name="directory">
6385 18 Dec 13 nicklas 248         <input type="hidden" name="directory_id" value="<%=current.getId()%>">
6385 18 Dec 13 nicklas 249         <input type="hidden" name="callback" value="<%=callback%>">
6385 18 Dec 13 nicklas 250   
6385 18 Dec 13 nicklas 251         <div class="content bottomborder">
6385 18 Dec 13 nicklas 252           <div id="main" class="absolutefull" style="bottom: 3em;">
6605 18 Nov 14 nicklas 253             <tbl:toolbar subclass="bottomborder bg-filled-50">
6385 18 Dec 13 nicklas 254               <tbl:button 
6385 18 Dec 13 nicklas 255                 id="btnNewDirectory"
6385 18 Dec 13 nicklas 256                 image="directory_new.png" 
6385 18 Dec 13 nicklas 257                 title="New&hellip;" 
6385 18 Dec 13 nicklas 258                 tooltip="Create a new directory" 
6385 18 Dec 13 nicklas 259               />
6385 18 Dec 13 nicklas 260             </tbl:toolbar>
6385 18 Dec 13 nicklas 261             <div id="joust" class="joust absolutefull" 
6385 18 Dec 13 nicklas 262               data-joust-tree="<%=HTML.encodeTags(json.toJSONString()) %>"
6385 18 Dec 13 nicklas 263               style="overflow: auto; top: 1.8em;"></div>
6385 18 Dec 13 nicklas 264           </div>
6385 18 Dec 13 nicklas 265         
6385 18 Dec 13 nicklas 266           <div class="absolutefull topborder" style="top: auto; height: 3em;">
6385 18 Dec 13 nicklas 267             <table class="fullform input100">
6385 18 Dec 13 nicklas 268               <tr>
6385 18 Dec 13 nicklas 269                 <th><%=requestTitle == null ? "Path" : requestTitle%></th>
6385 18 Dec 13 nicklas 270                 <td><input type="text" class="text" readonly name="path"></td>
6385 18 Dec 13 nicklas 271               </tr>
6385 18 Dec 13 nicklas 272             </table>
6385 18 Dec 13 nicklas 273           </div>
6385 18 Dec 13 nicklas 274         </div>
6385 18 Dec 13 nicklas 275         </form>
6385 18 Dec 13 nicklas 276   
6385 18 Dec 13 nicklas 277         <base:buttongroup subclass="dialogbuttons">
6385 18 Dec 13 nicklas 278           <base:button id="btnOk" title="Ok" />
6385 18 Dec 13 nicklas 279           <base:button id="close" title="Cancel" />
6385 18 Dec 13 nicklas 280         </base:buttongroup>
6385 18 Dec 13 nicklas 281       <%
5179 04 Nov 09 nicklas 282     }
5179 04 Nov 09 nicklas 283     else
5179 04 Nov 09 nicklas 284     {
6385 18 Dec 13 nicklas 285       %>
6385 18 Dec 13 nicklas 286       <div id="main" class="absolutefull auto-init" data-auto-init="drag-support" style="overflow: hidden;">
6605 18 Nov 14 nicklas 287         <tbl:toolbar subclass="bottomborder bg-filled-50">
6385 18 Dec 13 nicklas 288           <tbl:button 
6385 18 Dec 13 nicklas 289             id="btnRefresh"
6385 18 Dec 13 nicklas 290             image="refresh.png" 
6385 18 Dec 13 nicklas 291             title="Refresh" 
6385 18 Dec 13 nicklas 292             tooltip="Refresh the directory tree" 
6385 18 Dec 13 nicklas 293           />
6385 18 Dec 13 nicklas 294           <tbl:button 
6385 18 Dec 13 nicklas 295             id="btnSearch"
6385 18 Dec 13 nicklas 296             image="search.png" 
6385 18 Dec 13 nicklas 297             title="Search" 
6385 18 Dec 13 nicklas 298             tooltip="Search for files and directories" 
6385 18 Dec 13 nicklas 299           />
6385 18 Dec 13 nicklas 300         </tbl:toolbar>
6385 18 Dec 13 nicklas 301         <div id="joust" class="joust absolutefull" 
6385 18 Dec 13 nicklas 302           data-joust-tree="<%=HTML.encodeTags(json.toJSONString()) %>"
6385 18 Dec 13 nicklas 303           style="overflow: auto; top: 2em;"></div>
6385 18 Dec 13 nicklas 304       </div>
6385 18 Dec 13 nicklas 305       <%
5179 04 Nov 09 nicklas 306     }
1794 19 Jan 06 nicklas 307     %>
1794 19 Jan 06 nicklas 308     </base:body>
1794 19 Jan 06 nicklas 309   </base:page>
1794 19 Jan 06 nicklas 310   <%
1794 19 Jan 06 nicklas 311 }
1794 19 Jan 06 nicklas 312 finally
1794 19 Jan 06 nicklas 313 {
1794 19 Jan 06 nicklas 314   if (dc != null) dc.close();
1794 19 Jan 06 nicklas 315 }
3675 16 Aug 07 jari 316 %>