Fixed permission update on folder to show error message when deep children have alrea...
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / Folder.java
index ec38f62..81bb4b7 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.http.client.Response;
 import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
@@ -42,11 +50,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.LinkedHashSet;
-import java.util.List;
-import java.util.Set;
 
 public class Folder extends Resource {
     /*
@@ -75,6 +78,14 @@ public class Folder extends Resource {
 
     private Set<File> files = new LinkedHashSet<File>();
 
+    private Set<String> tags = new LinkedHashSet<String>();
+
+    private String owner;
+
+    private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
+
+    private String inheritedPermissionsFrom;
+
     public Folder() {};
 
     public Folder(String name) {
@@ -85,7 +96,8 @@ public class Folder extends Resource {
         return name;
     }
 
-    public Date getLastModified() {
+    @Override
+       public Date getLastModified() {
         return lastModified;
     }
 
@@ -117,7 +129,31 @@ public class Folder extends Resource {
         this.prefix = prefix;
     }
 
-    public void populate(Response response) {
+    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(String _owner, Response response) {
+        this.owner = _owner;
         String header = response.getHeader("Last-Modified");
         if (header != null)
             lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
@@ -126,7 +162,15 @@ public class Folder extends Resource {
         if (header != null)
             bytesUsed = Long.valueOf(header);
 
+        header = response.getHeader("X-Container-Object-Meta");
+        if (header != null && header.length() > 0) {
+            for (String t : header.split(",")) {
+                tags.add(t.toLowerCase().trim());
+            }
+        }
+
         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) {
@@ -136,12 +180,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(this, o, container);
+                        f.populate(this, o, _owner, container);
                         subfolders.add(f);
                     }
                     else {
                         File file = new File();
-                        file.populate(this, o, container);
+                        file.populate(this, o, _owner, container);
                         files.add(file);
                     }
                 }
@@ -149,8 +193,8 @@ public class Folder extends Resource {
         }
     }
 
-    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");
@@ -173,21 +217,22 @@ public class Folder extends Resource {
             container = name;
             prefix = "";
         }
-    }
+        this.owner = _owner;
 
-    @Override
-    public String getLastModifiedSince() {
-        return null;  //To change body of implemented methods use File | Settings | File Templates.
+        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;
     }
 
@@ -195,14 +240,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 getUri().equals(o.getUri());
         }
         return false;
     }
 
     @Override
     public int hashCode() {
-        return (container + prefix).hashCode();
+        return getUri().hashCode();
     }
 
     public Set<File> getFiles() {
@@ -216,4 +261,72 @@ public class Folder extends Resource {
     public String getUri() {
         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
     }
+
+    public boolean isContainer() {
+        return parent == null;
+    }
+
+    public void setContainer(String container) {
+        this.container = container;
+    }
+
+    public Set<String> getTags() {
+        return tags;
+    }
+
+    public String getInheritedPermissionsFrom() {
+        return inheritedPermissionsFrom;
+    }
+
+    public Map<String, Boolean[]> getPermissions() {
+        return permissions;
+    }
+
+    public String getOwner() {
+        return owner;
+    }
+
+    public boolean existChildrenPermissions() {
+        for (File f : files)
+            if (!f.getPermissions().isEmpty() && f.getInheritedPermissionsFrom() == null)
+                return true;
+
+        for (Folder fo : subfolders)
+            if ((!fo.getPermissions().isEmpty() && fo.getInheritedPermissionsFrom() == null) || fo.existChildrenPermissions())
+                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;
+       }
 }