Merge branch 'master' into packaging
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / Folder.java
index 5ece0e9..7f6b4a9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
 
 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.http.client.URL;
 import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
 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.Iterator;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Set;
 
 public class Folder extends Resource {
     /*
@@ -76,7 +80,11 @@ public class Folder extends Resource {
 
     private Set<File> files = new LinkedHashSet<File>();
 
-    private boolean inTrash = false;
+    private String owner;
+
+    private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
+
+    private String inheritedPermissionsFrom;
 
     public Folder() {};
 
@@ -88,7 +96,8 @@ public class Folder extends Resource {
         return name;
     }
 
-    public Date getLastModified() {
+    @Override
+       public Date getLastModified() {
         return lastModified;
     }
 
@@ -96,10 +105,6 @@ public class Folder extends Resource {
         return bytesUsed;
     }
 
-    public void setLastModified(Date lastModified) {
-        this.lastModified = lastModified;
-    }
-
     public Set<Folder> getSubfolders() {
         return subfolders;
     }
@@ -116,75 +121,100 @@ public class Folder extends Resource {
         return prefix;
     }
 
-    public void setPrefix(String prefix) {
-        this.prefix = prefix;
+    private void parsePermissions(String rawPermissions) {
+        String[] readwrite = rawPermissions.split(";");
+        for (String s : readwrite) {
+            String[] part = s.split("=");
+            String perm = part[0].trim();
+            String[] users = part[1].split(",");
+            for (String u : users) {
+                String user = u.trim();
+                Boolean[] userPerm = permissions.get(u);
+                if (userPerm == null) {
+                    userPerm = new Boolean[2];
+                    permissions.put(user, userPerm);
+                }
+                if (perm.equals("read")) {
+                    userPerm[0] = Boolean.TRUE;
+                }
+                else if (perm.equals("write")) {
+                    userPerm[1] = Boolean.TRUE;
+                }
+            }
+        }
     }
 
-    public void populate(Response response) {
+    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;
-
-        subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
-        files.clear();
+        String rawPermissions = response.getHeader("X-Object-Sharing");
+        if (rawPermissions != null && rawPermissions.length() > 0) {
+            parsePermissions(URL.decodePathSegment(rawPermissions));
+        }
+        
+        if (response.getText() == null || response.getText().isEmpty())
+               return;
         JSONValue json = JSONParser.parseStrict(response.getText());
         JSONArray array = json.isArray();
         if (array != null) {
+            subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
+            files.clear();
             for (int i=0; i<array.size(); i++) {
                 JSONObject o = array.get(i).isObject();
                 if (o != null) {
                     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, container);
-                        subfolders.add(f);
+                        f.populate(this, o, _owner, container);
+                        if (f.getName().length() > 0)
+                               subfolders.add(f);
                     }
                     else {
                         File file = new File();
-                        file.populate(this, o, container);
-                        files.add(file);
+                        file.populate(this, o, _owner, container);
+                        if (file.getName().length() > 0)
+                               files.add(file);
                     }
                 }
             }
-            Iterator<Folder> iter = subfolders.iterator();
-            while (iter.hasNext()) {
-                Folder f = iter.next();
-                if (f.isInTrash())
-                    iter.remove();
-            }
-            Iterator<File> it = files.iterator();
-            while (it.hasNext()) {
-                File f = it.next();
-                if (f.isInTrash())
-                    it.remove();
-            }
         }
     }
 
-    public void populate(Folder parent, JSONObject o, 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");
+            if (path.endsWith("/")) { //Always true for "subdir"
+                path = path.substring(0, path.length() - 1);
+            }
+            if (parent != null && parent.getPrefix().length() > 0)
+               name = path.substring(parent.getPrefix().length() + 1);
+            else
+               name = path;
+            if (name.equals("/"))
+               name = "";
         }
         else {
             path = unmarshallString(o, "name");
             lastModified = unmarshallDate(o, "last_modified");
+            if (parent != null && parent.getPrefix().length() > 0)
+               name = path.substring(parent.getPrefix().length() + 1);
+            else
+               name = path;
         }
-        if (path.endsWith("/"))
-            path = path.substring(0, path.length() - 1);
-        if (path.contains("/"))
-            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
-        else
-            name = path;
         if (aContainer != null) {
             container = aContainer;
             prefix = path;
@@ -193,18 +223,22 @@ public class Folder extends Resource {
             container = name;
             prefix = "";
         }
-        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");
+        if (rawPermissions != null)
+            parsePermissions(rawPermissions);
     }
 
-    public static Folder createFromResponse(Response response, Folder result) {
+    public static Folder createFromResponse(String owner, Response response, Folder result) {
         Folder f = null;
         if (result == null)
             f = new Folder();
         else
             f = result;
 
-        f.populate(response);
+        f.populate(owner, response);
         return f;
     }
 
@@ -212,14 +246,14 @@ public class Folder extends Resource {
     public boolean equals(Object other) {
         if (other instanceof Folder) {
             Folder o = (Folder) other;
-            return (container + prefix).equals(o.getContainer() + o.getPrefix());
+            return owner.equals(o.getOwner()) && getUri().equals(o.getUri());
         }
         return false;
     }
 
     @Override
     public int hashCode() {
-        return (container + prefix).hashCode();
+        return getUri().hashCode();
     }
 
     public Set<File> getFiles() {
@@ -234,7 +268,56 @@ public class Folder extends Resource {
         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
     }
 
-    public boolean isInTrash() {
-        return inTrash;
+    public boolean isContainer() {
+        return parent == null;
     }
+
+    public void setContainer(String container) {
+        this.container = container;
+    }
+
+    public String getInheritedPermissionsFrom() {
+        return inheritedPermissionsFrom;
+    }
+
+    public Map<String, Boolean[]> getPermissions() {
+        return permissions;
+    }
+
+    public String getOwner() {
+        return owner;
+    }
+
+       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;
+       }
 }