Finished folder permissions dialog
[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.HashMap;
48 import java.util.Iterator;
49 import java.util.LinkedHashSet;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Set;
53 import java.util.StringTokenizer;
54
55 public class Folder extends Resource {
56     /*
57      * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
58      * last part of its path
59      */
60     private String name = null;
61
62     private Date lastModified = null;
63
64     private long bytesUsed = 0;
65
66     private Folder parent = null;
67     
68     private Set<Folder> subfolders = new LinkedHashSet<Folder>();
69     /*
70      * The name of the container that this folder belongs to. If this folder is container, this field equals name
71      */
72     private String container = null;
73
74     /*
75      * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
76      * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
77      */
78     private String prefix = "";
79
80     private Set<File> files = new LinkedHashSet<File>();
81
82     private boolean inTrash = false;
83
84     /*
85      * Flag that indicates that this folder is the Trash
86      */
87     private boolean trash = false;
88
89     private Set<String> tags = new LinkedHashSet<String>();
90
91     private String owner;
92
93     private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
94
95     private String inheritedPermissionsFrom;
96
97     public Folder() {};
98
99     public Folder(String name) {
100         this.name = name;
101     }
102     
103     public String getName() {
104         return name;
105     }
106
107     public Date getLastModified() {
108         return lastModified;
109     }
110
111     public long getBytesUsed() {
112         return bytesUsed;
113     }
114
115     public void setLastModified(Date lastModified) {
116         this.lastModified = lastModified;
117     }
118
119     public Set<Folder> getSubfolders() {
120         return subfolders;
121     }
122
123     public void setSubfolders(Set<Folder> subfolders) {
124         this.subfolders = subfolders;
125     }
126
127     public String getContainer() {
128         return container;
129     }
130
131     public String getPrefix() {
132         return prefix;
133     }
134
135     public void setPrefix(String prefix) {
136         this.prefix = prefix;
137     }
138
139     private void parsePermissions(String rawPermissions) {
140         String[] readwrite = rawPermissions.split(";");
141         for (String s : readwrite) {
142             String[] part = s.split("=");
143             String perm = part[0].trim();
144             String[] users = part[1].split(",");
145             for (String u : users) {
146                 String user = u.trim();
147                 Boolean[] userPerm = permissions.get(u);
148                 if (userPerm == null) {
149                     userPerm = new Boolean[2];
150                     permissions.put(user, userPerm);
151                 }
152                 if (perm.equals("read")) {
153                     userPerm[0] = Boolean.TRUE;
154                 }
155                 else if (perm.equals("write")) {
156                     userPerm[1] = Boolean.TRUE;
157                 }
158             }
159         }
160     }
161
162     public void populate(String owner, Response response) {
163         this.owner = owner;
164         String header = response.getHeader("Last-Modified");
165         if (header != null)
166             lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
167
168         header = response.getHeader("X-Container-Bytes-Used");
169         if (header != null)
170             bytesUsed = Long.valueOf(header);
171
172         header = response.getHeader("X-Object-Meta-Trash");
173         if (header != null && header.equals("true"))
174             inTrash = true;
175
176         header = response.getHeader("X-Container-Object-Meta");
177         if (header != null && header.length() > 0) {
178             for (String t : header.split(",")) {
179                 tags.add(t.toLowerCase().trim());
180             }
181         }
182
183         inheritedPermissionsFrom = response.getHeader("X-Object-Shared-By");
184         String rawPermissions = response.getHeader("X-Object-Sharing");
185         if (rawPermissions != null)
186             parsePermissions(rawPermissions);
187
188         subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
189         files.clear();
190         JSONValue json = JSONParser.parseStrict(response.getText());
191         JSONArray array = json.isArray();
192         if (array != null) {
193             for (int i=0; i<array.size(); i++) {
194                 JSONObject o = array.get(i).isObject();
195                 if (o != null) {
196                     String contentType = unmarshallString(o, "content_type");
197                     if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
198                         Folder f = new Folder();
199                         f.populate(this, o, owner, container);
200                         subfolders.add(f);
201                     }
202                     else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) {
203                         File file = new File();
204                         file.populate(this, o, owner, container);
205                         files.add(file);
206                     }
207                 }
208             }
209             //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
210             //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
211             Iterator<Folder> iter = subfolders.iterator();
212             while (iter.hasNext()) {
213                 Folder f = iter.next();
214                 if (f.isInTrash())
215                     iter.remove();
216             }
217         }
218     }
219
220     public void populate(Folder parent, JSONObject o, String owner, String aContainer) {
221         this.parent = parent;
222         String path = null;
223         if (o.containsKey("subdir")) {
224             path = unmarshallString(o, "subdir");
225         }
226         else {
227             path = unmarshallString(o, "name");
228             lastModified = unmarshallDate(o, "last_modified");
229         }
230         if (path.endsWith("/"))
231             path = path.substring(0, path.length() - 1);
232         if (path.contains("/"))
233             name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
234         else
235             name = path;
236         if (aContainer != null) {
237             container = aContainer;
238             prefix = path;
239         }
240         else {
241             container = name;
242             prefix = "";
243         }
244         this.owner = owner;
245         if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))
246             inTrash = true;
247
248         inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
249         String rawPermissions = unmarshallString(o, "x_object_sharing");
250         if (rawPermissions != null)
251             parsePermissions(rawPermissions);
252     }
253
254     public static Folder createFromResponse(String owner, Response response, Folder result) {
255         Folder f = null;
256         if (result == null)
257             f = new Folder();
258         else
259             f = result;
260
261         f.populate(owner, response);
262         return f;
263     }
264
265     @Override
266     public boolean equals(Object other) {
267         if (other instanceof Folder) {
268             Folder o = (Folder) other;
269             return getUri().equals(o.getUri());
270         }
271         return false;
272     }
273
274     @Override
275     public int hashCode() {
276         return getUri().hashCode();
277     }
278
279     public Set<File> getFiles() {
280         return files;
281     }
282
283     public Folder getParent() {
284         return parent;
285     }
286
287     public String getUri() {
288         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
289     }
290
291     public boolean isInTrash() {
292         return inTrash;
293     }
294
295     public boolean isContainer() {
296         return parent == null;
297     }
298
299     public boolean isTrash() {
300         return trash;
301     }
302
303     public void setTrash(boolean trash) {
304         this.trash = trash;
305     }
306
307     public void setContainer(String container) {
308         this.container = container;
309     }
310
311     public Set<String> getTags() {
312         return tags;
313     }
314
315     public String getInheritedPermissionsFrom() {
316         return inheritedPermissionsFrom;
317     }
318
319     public Map<String, Boolean[]> getPermissions() {
320         return permissions;
321     }
322
323     public String getOwner() {
324         return owner;
325     }
326
327     public boolean existChildrenPermissions() {
328         for (File f : files)
329             if (!f.getPermissions().isEmpty() && f.getInheritedPermissionsFrom() == null)
330                 return true;
331
332         for (Folder fo : subfolders)
333             if ((!fo.getPermissions().isEmpty() && fo.getInheritedPermissionsFrom() == null) || fo.existChildrenPermissions())
334                 return true;
335         return false;
336     }
337 }