2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.pithos.web.client.foldertree;
38 import com.google.gwt.http.client.Response;
39 import com.google.gwt.i18n.client.DateTimeFormat;
40 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
41 import com.google.gwt.json.client.JSONArray;
42 import com.google.gwt.json.client.JSONObject;
43 import com.google.gwt.json.client.JSONParser;
44 import com.google.gwt.json.client.JSONValue;
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.Iterator;
48 import java.util.LinkedHashSet;
49 import java.util.List;
52 public class Folder extends Resource {
54 * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
55 * last part of its path
57 private String name = null;
59 private Date lastModified = null;
61 private long bytesUsed = 0;
63 private Folder parent = null;
65 private Set<Folder> subfolders = new LinkedHashSet<Folder>();
67 * The name of the container that this folder belongs to. If this folder is container, this field equals name
69 private String container = null;
72 * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
73 * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
75 private String prefix = "";
77 private Set<File> files = new LinkedHashSet<File>();
79 private boolean inTrash = false;
82 * Flag that indicates that this folder is the Trash
84 private boolean trash = false;
86 private Set<String> tags = new LinkedHashSet<String>();
90 public Folder(String name) {
94 public String getName() {
98 public Date getLastModified() {
102 public long getBytesUsed() {
106 public void setLastModified(Date lastModified) {
107 this.lastModified = lastModified;
110 public Set<Folder> getSubfolders() {
114 public void setSubfolders(Set<Folder> subfolders) {
115 this.subfolders = subfolders;
118 public String getContainer() {
122 public String getPrefix() {
126 public void setPrefix(String prefix) {
127 this.prefix = prefix;
130 public void populate(Response response) {
131 String header = response.getHeader("Last-Modified");
133 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
135 header = response.getHeader("X-Container-Bytes-Used");
137 bytesUsed = Long.valueOf(header);
139 header = response.getHeader("X-Object-Meta-Trash");
140 if (header != null && header.equals("true"))
143 header = response.getHeader("X-Container-Object-Meta");
144 if (header != null) {
149 subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
151 JSONValue json = JSONParser.parseStrict(response.getText());
152 JSONArray array = json.isArray();
154 for (int i=0; i<array.size(); i++) {
155 JSONObject o = array.get(i).isObject();
157 String contentType = unmarshallString(o, "content_type");
158 if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
159 Folder f = new Folder();
160 f.populate(this, o, container);
163 else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) {
164 File file = new File();
165 file.populate(this, o, container);
170 //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
171 //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
172 Iterator<Folder> iter = subfolders.iterator();
173 while (iter.hasNext()) {
174 Folder f = iter.next();
181 public void populate(Folder parent, JSONObject o, String aContainer) {
182 this.parent = parent;
184 if (o.containsKey("subdir")) {
185 path = unmarshallString(o, "subdir");
188 path = unmarshallString(o, "name");
189 lastModified = unmarshallDate(o, "last_modified");
191 if (path.endsWith("/"))
192 path = path.substring(0, path.length() - 1);
193 if (path.contains("/"))
194 name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
197 if (aContainer != null) {
198 container = aContainer;
205 if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
209 public static Folder createFromResponse(Response response, Folder result) {
216 f.populate(response);
221 public boolean equals(Object other) {
222 if (other instanceof Folder) {
223 Folder o = (Folder) other;
224 return (container + prefix).equals(o.getContainer() + o.getPrefix());
230 public int hashCode() {
231 return (container + prefix).hashCode();
234 public Set<File> getFiles() {
238 public Folder getParent() {
242 public String getUri() {
243 return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
246 public boolean isInTrash() {
250 public boolean isContainer() {
251 return parent == null;
254 public boolean isTrash() {
258 public void setTrash(boolean trash) {
262 public void setContainer(String container) {
263 this.container = container;
266 public Set<String> getTags() {