From 2f1920204ba7a151c34b4ead24b81e37a92dc07d Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Mon, 17 Dec 2018 23:32:20 +0000
Subject: [PATCH] Prepared for tree view of file system.

---
 borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java |    2 
 borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java          |    2 
 borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java      |   67 +++++++++++++++++++++++++++++++++
 borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/ArchivesRest.java |   11 ++++-
 4 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java
index a5acb19..2a21a76 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java
@@ -129,7 +129,7 @@
             log.error("Error while reading file list '" + file.getAbsolutePath() + "': " + ex.getMessage(), ex);
         }
         log.info("Loading done.");
-        return list;
+        return filter.reduce(list);
     }
 
     /**
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
index d3639aa..b0c9362 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
@@ -254,7 +254,7 @@
         if (items == null && forceLoad) {
             log.warn("Repo::archiv with name '" + archive.getBorgIdentifier() + "' not found.");
         }
-        return items;
+        return filter.reduce(items);
     }
 
     public List<BorgFilesystemItem> getArchiveContent(File file) {
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java
index 512b3d1..9100597 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java
@@ -9,9 +9,16 @@
 import java.util.List;
 
 public class FileSystemFilter {
+    public enum Mode {FLAT, TREE}
+
     @Getter
     private String searchString;
     @Getter
+    private Mode mode;
+    @Getter
+    @Setter
+    private String currentDirectory;
+    @Getter
     @Setter
     private int maxResultSize = -1;
     /**
@@ -68,6 +75,53 @@
     }
 
     /**
+     * After processing a list by using {@link #matches(BorgFilesystemItem)} you should call finally this method with
+     * your result list to reduce the files and directories for mode {@link Mode#TREE}. For the mode {@link Mode#FLAT}
+     * nothing is done.
+     * @param list
+     * @return The original list for mode {@link Mode#FLAT} or the reduced list for the tree view.
+     */
+    public List reduce(List<BorgFilesystemItem> list) {
+        if (mode == FileSystemFilter.Mode.TREE) {
+            List<BorgFilesystemItem> list2 = list;
+            list = new ArrayList<>();
+            for (BorgFilesystemItem item : list2) {
+                if (matchesDirectoryView(item)) {
+                    list.add(item);
+                }
+            }
+        }
+        return list;
+    }
+
+    /**
+     * After processing all files with {@link #matches(BorgFilesystemItem)} you should process the file list again
+     * through this filter (for tree view) for displaying only the sub items of the current directory (not recursive).
+     *
+     * @return
+     */
+    private boolean matchesDirectoryView(BorgFilesystemItem item) {
+        if (mode != Mode.TREE) {
+            return true;
+        }
+        String path = item.getPath();
+        if (StringUtils.isEmpty(currentDirectory)) {
+            // root dir
+            return path.indexOf('/') == -1; // Show only top level items.
+        }
+        if (!path.startsWith(currentDirectory)) {
+            // item is not a child of currentDirectory.
+            return false;
+        }
+        if (path.length() <= currentDirectory.length() + 1) {
+            // Don't show the current directory itself.
+            return false;
+        }
+        String subPath = path.substring(currentDirectory.length());
+        return subPath.indexOf('/') < 0;
+    }
+
+    /**
      * @param searchString The search string. If this string contains several key words separated by white chars,
      *                     all key words must be found.
      * @return this for chaining.
@@ -102,6 +156,19 @@
         return this;
     }
 
+    /**
+     * @param mode
+     * @return this for chaining.
+     */
+    public FileSystemFilter setMode(String mode) {
+        if (mode != null && mode.toLowerCase().equals("tree")) {
+            this.mode = Mode.TREE;
+        } else {
+            this.mode = Mode.FLAT;
+        }
+        return this;
+    }
+
     private void processFinishedFlag() {
         if (maxResultSize > 0 && ++counter >= maxResultSize) {
             this.finished = true;
diff --git a/borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/ArchivesRest.java b/borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/ArchivesRest.java
index f5e3a87..e704038 100644
--- a/borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/ArchivesRest.java
+++ b/borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/ArchivesRest.java
@@ -56,21 +56,28 @@
     /**
      *
      * @param archiveId Id or name of archive.
-     * @param forceLoad If false (default), non cached file lists will not be loaded by borg.
+     * @param searchString The string to search for (key words separated by white chars, trailing ! char represents exclude).
+     * @param mode Flat (default) or tree.
+     * @param currentDirectory The current displayed directory (only files and directories contained will be returned).
      * @param maxResultSize maximum number of file items to return (default is 50).
+     * @param force If false (default), non cached file lists will not be loaded by borg.
      * @param prettyPrinter If true then the json output will be in pretty format.
      * @return Repository (including list of archives) as json string.
      * @see JsonUtils#toJson(Object, boolean)
      */
     public String getArchiveFileLIst(@QueryParam("archiveId") String archiveId,
                                      @QueryParam("searchString") String searchString,
+                                     @QueryParam("mode") String mode,
+                                     @QueryParam("currentDirectory") String currentDirectory,
                                      @QueryParam("maxResultSize") String maxResultSize,
                                      @QueryParam("force") boolean force,
                                      @QueryParam("prettyPrinter") boolean prettyPrinter) {
         int maxSize = NumberUtils.toInt(maxResultSize, 50);
         FileSystemFilter filter = new FileSystemFilter()
                 .setSearchString(searchString)
-                .setMaxResultSize(maxSize);
+                .setMaxResultSize(maxSize)
+                .setMode(mode)
+                .setCurrentDirectory(currentDirectory);
         List<BorgFilesystemItem> items = ButlerCache.getInstance().getArchiveContent(archiveId, force,
                 filter);
         if (items == null) {

--
Gitblit v1.10.0