X-Git-Url: https://code.grnet.gr/git/pithos-web-client/blobdiff_plain/ecf95c9e5031029245f92ec63b4c1f6c7077e362..e028046b4316244b571c75bf88a55238c88629da:/src/gr/grnet/pithos/web/client/foldertree/Folder.java diff --git a/src/gr/grnet/pithos/web/client/foldertree/Folder.java b/src/gr/grnet/pithos/web/client/foldertree/Folder.java index 960c852..5ce0b72 100644 --- a/src/gr/grnet/pithos/web/client/foldertree/Folder.java +++ b/src/gr/grnet/pithos/web/client/foldertree/Folder.java @@ -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 @@ -35,22 +35,23 @@ 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.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,15 +80,6 @@ public class Folder extends Resource { private Set files = new LinkedHashSet(); - private boolean inTrash = false; - - /* - * Flag that indicates that this folder is the Trash - */ - private boolean trash = false; - - private Set tags = new LinkedHashSet(); - private String owner; private Map permissions = new HashMap(); @@ -104,7 +96,8 @@ public class Folder extends Resource { return name; } - public Date getLastModified() { + @Override + public Date getLastModified() { return lastModified; } @@ -112,10 +105,6 @@ public class Folder extends Resource { return bytesUsed; } - public void setLastModified(Date lastModified) { - this.lastModified = lastModified; - } - public Set getSubfolders() { return subfolders; } @@ -132,10 +121,6 @@ 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) { @@ -159,80 +144,77 @@ 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(",")) { - tags.add(t.toLowerCase().trim()); - } - } - - 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(); + 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 0) + 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); - files.add(file); + file.populate(this, o, _owner, container); + if (file.getName().length() > 0) + 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 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"); + 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; @@ -241,9 +223,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"); @@ -266,14 +246,15 @@ 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 == null ? true : owner.equals(o.getOwner())) + && (getUri().equals(o.getUri())); } return false; } @Override public int hashCode() { - return (container + prefix).hashCode(); + return getUri().hashCode(); } public Set getFiles() { @@ -288,30 +269,14 @@ 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; } - public Set getTags() { - return tags; - } - public String getInheritedPermissionsFrom() { return inheritedPermissionsFrom; } @@ -324,14 +289,36 @@ public class Folder extends Resource { 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; + } }