Fixed folder deletion with all subfolders and files
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / Folder.java
index 37b4d53..d7175cf 100644 (file)
@@ -1,5 +1,36 @@
 /*
- * Copyright (c) 2011 Greek Research and Technology Network
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
  */
 
 package gr.grnet.pithos.web.client.foldertree;
@@ -28,6 +59,8 @@ public class Folder extends Resource {
 
     private long bytesUsed = 0;
 
+    private Folder parent = null;
+    
     private Set<Folder> subfolders = new LinkedHashSet<Folder>();
     /*
      * The name of the container that this folder belongs to. If this folder is container, this field equals name
@@ -93,6 +126,8 @@ public class Folder extends Resource {
         if (header != null)
             bytesUsed = Long.valueOf(header);
 
+        subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
+        files.clear();
         JSONValue json = JSONParser.parseStrict(response.getText());
         JSONArray array = json.isArray();
         if (array != null) {
@@ -102,12 +137,12 @@ public class Folder extends Resource {
                     String contentType = unmarshallString(o, "content_type");
                     if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
                         Folder f = new Folder();
-                        f.populate(o, container);
+                        f.populate(this, o, container);
                         subfolders.add(f);
                     }
                     else {
                         File file = new File();
-                        file.populate(o, container);
+                        file.populate(this, o, container);
                         files.add(file);
                     }
                 }
@@ -115,7 +150,8 @@ public class Folder extends Resource {
         }
     }
 
-    public void populate(JSONObject o, String aContainer) {
+    public void populate(Folder parent, JSONObject o, String aContainer) {
+        this.parent = parent;
         String path = null;
         if (o.containsKey("subdir")) {
             path = unmarshallString(o, "subdir");
@@ -140,11 +176,6 @@ public class Folder extends Resource {
         }
     }
 
-    @Override
-    public String getLastModifiedSince() {
-        return null;  //To change body of implemented methods use File | Settings | File Templates.
-    }
-
     public static Folder createFromResponse(Response response, Folder result) {
         Folder f = null;
         if (result == null)
@@ -160,20 +191,25 @@ public class Folder extends Resource {
     public boolean equals(Object other) {
         if (other instanceof Folder) {
             Folder o = (Folder) other;
-            if (container != null)
-                return prefix.equals(o.getPrefix()) && container.equals(o.getContainer());
-            else
-                return o.getContainer() == null && name.equals(o.getName());
+            return (container + prefix).equals(o.getContainer() + o.getPrefix());
         }
         return false;
     }
 
     @Override
     public int hashCode() {
-        return prefix.hashCode() + name.hashCode();
+        return (container + prefix).hashCode();
     }
 
     public Set<File> getFiles() {
         return files;
     }
+
+    public Folder getParent() {
+        return parent;
+    }
+
+    public String getUri() {
+        return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
+    }
 }