When last_modified is not in valid format the date will be set to null
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / Folder.java
index 30badae..3e73b16 100644 (file)
 
 package gr.grnet.pithos.web.client.foldertree;
 
+import gr.grnet.pithos.web.client.Pithos;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+
+import com.google.gwt.core.client.GWT;
 import com.google.gwt.http.client.Response;
 import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
@@ -42,15 +51,6 @@ import com.google.gwt.json.client.JSONArray;
 import com.google.gwt.json.client.JSONObject;
 import com.google.gwt.json.client.JSONParser;
 import com.google.gwt.json.client.JSONValue;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.StringTokenizer;
 
 public class Folder extends Resource {
     /*
@@ -79,13 +79,6 @@ public class Folder extends Resource {
 
     private Set<File> files = new LinkedHashSet<File>();
 
-    private boolean inTrash = false;
-
-    /*
-     * Flag that indicates that this folder is the Trash
-     */
-    private boolean trash = false;
-
     private Set<String> tags = new LinkedHashSet<String>();
 
     private String owner;
@@ -104,7 +97,8 @@ public class Folder extends Resource {
         return name;
     }
 
-    public Date getLastModified() {
+    @Override
+       public Date getLastModified() {
         return lastModified;
     }
 
@@ -112,10 +106,6 @@ public class Folder extends Resource {
         return bytesUsed;
     }
 
-    public void setLastModified(Date lastModified) {
-        this.lastModified = lastModified;
-    }
-
     public Set<Folder> getSubfolders() {
         return subfolders;
     }
@@ -159,20 +149,21 @@ public class Folder extends Resource {
         }
     }
 
-    public void populate(String owner, Response response) {
-        this.owner = owner;
+    public void populate(String _owner, Response response) {
+        this.owner = _owner;
         String header = response.getHeader("Last-Modified");
         if (header != null)
-            lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
+                       try {
+                               lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
+                       } catch (IllegalArgumentException e) {
+                               GWT.log("Last-Modified will be set to null", e);
+                               lastModified = null;
+                       }
 
         header = response.getHeader("X-Container-Bytes-Used");
-        if (header != null)
+        if (header != null && header.length() > 0)
             bytesUsed = Long.valueOf(header);
 
-        header = response.getHeader("X-Object-Meta-Trash");
-        if (header != null && header.equals("true"))
-            inTrash = true;
-
         header = response.getHeader("X-Container-Object-Meta");
         if (header != null && header.length() > 0) {
             for (String t : header.split(",")) {
@@ -180,11 +171,6 @@ public class Folder extends Resource {
             }
         }
 
-        inheritedPermissionsFrom = response.getHeader("X-Object-Shared-By");
-        String rawPermissions = response.getHeader("X-Object-Sharing");
-        if (rawPermissions != null)
-            parsePermissions(rawPermissions);
-
         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());
@@ -196,29 +182,21 @@ 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(this, o, owner, container);
+                        f.populate(this, o, _owner, container);
                         subfolders.add(f);
                     }
-                    else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) {
+                    else {
                         File file = new File();
-                        file.populate(this, o, owner, container);
+                        file.populate(this, o, _owner, container);
                         files.add(file);
                     }
                 }
             }
-            //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
-            //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
-            Iterator<Folder> iter = subfolders.iterator();
-            while (iter.hasNext()) {
-                Folder f = iter.next();
-                if (f.isInTrash())
-                    iter.remove();
-            }
         }
     }
 
-    public void populate(Folder parent, JSONObject o, String owner, String aContainer) {
-        this.parent = parent;
+    public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) {
+        this.parent = _parent;
         String path = null;
         if (o.containsKey("subdir")) {
             path = unmarshallString(o, "subdir");
@@ -241,9 +219,7 @@ public class Folder extends Resource {
             container = name;
             prefix = "";
         }
-        this.owner = owner;
-        if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
-            inTrash = true;
+        this.owner = _owner;
 
         inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
         String rawPermissions = unmarshallString(o, "x_object_sharing");
@@ -288,22 +264,10 @@ public class Folder extends Resource {
         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
     }
 
-    public boolean isInTrash() {
-        return inTrash;
-    }
-
     public boolean isContainer() {
         return parent == null;
     }
 
-    public boolean isTrash() {
-        return trash;
-    }
-
-    public void setTrash(boolean trash) {
-        this.trash = trash;
-    }
-
     public void setContainer(String container) {
         this.container = container;
     }
@@ -334,4 +298,37 @@ public class Folder extends Resource {
                 return true;
         return false;
     }
+
+       public boolean isShared() {
+               return !permissions.isEmpty();
+       }
+
+       /**
+        * I am THE trash
+        * 
+        * @return
+        */
+       public boolean isTrash() {
+               return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
+       }
+       
+       /**
+        * I am IN THE trash
+        * 
+        * @return
+        */
+       public boolean isInTrash() {
+               return container.equals(Pithos.TRASH_CONTAINER);
+       }
+
+       public boolean isHome() {
+               return isContainer() && name.equals(Pithos.HOME_CONTAINER);
+       }
+
+       public boolean contains(Folder folder) {
+               for (Folder f : subfolders)
+                       if (f.equals(folder) || f.contains(folder))
+                               return true;
+               return false;
+       }
 }