First implementation of public permission setting
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / Folder.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35
36 package gr.grnet.pithos.web.client.foldertree;
37
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;
50 import java.util.Set;
51 import java.util.StringTokenizer;
52
53 public class Folder extends Resource {
54     /*
55      * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
56      * last part of its path
57      */
58     private String name = null;
59
60     private Date lastModified = null;
61
62     private long bytesUsed = 0;
63
64     private Folder parent = null;
65     
66     private Set<Folder> subfolders = new LinkedHashSet<Folder>();
67     /*
68      * The name of the container that this folder belongs to. If this folder is container, this field equals name
69      */
70     private String container = null;
71
72     /*
73      * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
74      * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
75      */
76     private String prefix = "";
77
78     private Set<File> files = new LinkedHashSet<File>();
79
80     private boolean inTrash = false;
81
82     /*
83      * Flag that indicates that this folder is the Trash
84      */
85     private boolean trash = false;
86
87     private Set<String> tags = new LinkedHashSet<String>();
88
89     private String owner;
90
91     public Folder() {};
92
93     public Folder(String name) {
94         this.name = name;
95     }
96     
97     public String getName() {
98         return name;
99     }
100
101     public Date getLastModified() {
102         return lastModified;
103     }
104
105     public long getBytesUsed() {
106         return bytesUsed;
107     }
108
109     public void setLastModified(Date lastModified) {
110         this.lastModified = lastModified;
111     }
112
113     public Set<Folder> getSubfolders() {
114         return subfolders;
115     }
116
117     public void setSubfolders(Set<Folder> subfolders) {
118         this.subfolders = subfolders;
119     }
120
121     public String getContainer() {
122         return container;
123     }
124
125     public String getPrefix() {
126         return prefix;
127     }
128
129     public void setPrefix(String prefix) {
130         this.prefix = prefix;
131     }
132
133     public void populate(String owner, Response response) {
134         this.owner = owner;
135         String header = response.getHeader("Last-Modified");
136         if (header != null)
137             lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
138
139         header = response.getHeader("X-Container-Bytes-Used");
140         if (header != null)
141             bytesUsed = Long.valueOf(header);
142
143         header = response.getHeader("X-Object-Meta-Trash");
144         if (header != null && header.equals("true"))
145             inTrash = true;
146
147         header = response.getHeader("X-Container-Object-Meta");
148         if (header != null && header.length() > 0) {
149             for (String t : header.split(",")) {
150                 tags.add(t.toLowerCase().trim());
151             }
152         }
153
154         subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
155         files.clear();
156         JSONValue json = JSONParser.parseStrict(response.getText());
157         JSONArray array = json.isArray();
158         if (array != null) {
159             for (int i=0; i<array.size(); i++) {
160                 JSONObject o = array.get(i).isObject();
161                 if (o != null) {
162                     String contentType = unmarshallString(o, "content_type");
163                     if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
164                         Folder f = new Folder();
165                         f.populate(this, o, owner, container);
166                         subfolders.add(f);
167                     }
168                     else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) {
169                         File file = new File();
170                         file.populate(this, o, owner, container);
171                         files.add(file);
172                     }
173                 }
174             }
175             //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
176             //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
177             Iterator<Folder> iter = subfolders.iterator();
178             while (iter.hasNext()) {
179                 Folder f = iter.next();
180                 if (f.isInTrash())
181                     iter.remove();
182             }
183         }
184     }
185
186     public void populate(Folder parent, JSONObject o, String owner, String aContainer) {
187         this.parent = parent;
188         String path = null;
189         if (o.containsKey("subdir")) {
190             path = unmarshallString(o, "subdir");
191         }
192         else {
193             path = unmarshallString(o, "name");
194             lastModified = unmarshallDate(o, "last_modified");
195         }
196         if (path.endsWith("/"))
197             path = path.substring(0, path.length() - 1);
198         if (path.contains("/"))
199             name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
200         else
201             name = path;
202         if (aContainer != null) {
203             container = aContainer;
204             prefix = path;
205         }
206         else {
207             container = name;
208             prefix = "";
209         }
210         this.owner = owner;
211         if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
212             inTrash = true;
213     }
214
215     public static Folder createFromResponse(String owner, Response response, Folder result) {
216         Folder f = null;
217         if (result == null)
218             f = new Folder();
219         else
220             f = result;
221
222         f.populate(owner, response);
223         return f;
224     }
225
226     @Override
227     public boolean equals(Object other) {
228         if (other instanceof Folder) {
229             Folder o = (Folder) other;
230             return (container + prefix).equals(o.getContainer() + o.getPrefix());
231         }
232         return false;
233     }
234
235     @Override
236     public int hashCode() {
237         return (container + prefix).hashCode();
238     }
239
240     public Set<File> getFiles() {
241         return files;
242     }
243
244     public Folder getParent() {
245         return parent;
246     }
247
248     public String getUri() {
249         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
250     }
251
252     public boolean isInTrash() {
253         return inTrash;
254     }
255
256     public boolean isContainer() {
257         return parent == null;
258     }
259
260     public boolean isTrash() {
261         return trash;
262     }
263
264     public void setTrash(boolean trash) {
265         this.trash = trash;
266     }
267
268     public void setContainer(String container) {
269         this.container = container;
270     }
271
272     public Set<String> getTags() {
273         return tags;
274     }
275 }