CSS fun
[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 gr.grnet.pithos.web.client.Pithos;
39
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.LinkedHashSet;
43 import java.util.Map;
44 import java.util.Set;
45
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.http.client.Response;
48 import com.google.gwt.i18n.client.DateTimeFormat;
49 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
50 import com.google.gwt.json.client.JSONArray;
51 import com.google.gwt.json.client.JSONObject;
52 import com.google.gwt.json.client.JSONParser;
53 import com.google.gwt.json.client.JSONValue;
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 Set<String> tags = new LinkedHashSet<String>();
83
84     private String owner;
85
86     private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
87
88     private String inheritedPermissionsFrom;
89
90     public Folder() {};
91
92     public Folder(String name) {
93         this.name = name;
94     }
95     
96     public String getName() {
97         return name;
98     }
99
100     @Override
101         public Date getLastModified() {
102         return lastModified;
103     }
104
105     public long getBytesUsed() {
106         return bytesUsed;
107     }
108
109     public Set<Folder> getSubfolders() {
110         return subfolders;
111     }
112
113     public void setSubfolders(Set<Folder> subfolders) {
114         this.subfolders = subfolders;
115     }
116
117     public String getContainer() {
118         return container;
119     }
120
121     public String getPrefix() {
122         return prefix;
123     }
124
125     public void setPrefix(String prefix) {
126         this.prefix = prefix;
127     }
128
129     private void parsePermissions(String rawPermissions) {
130         String[] readwrite = rawPermissions.split(";");
131         for (String s : readwrite) {
132             String[] part = s.split("=");
133             String perm = part[0].trim();
134             String[] users = part[1].split(",");
135             for (String u : users) {
136                 String user = u.trim();
137                 Boolean[] userPerm = permissions.get(u);
138                 if (userPerm == null) {
139                     userPerm = new Boolean[2];
140                     permissions.put(user, userPerm);
141                 }
142                 if (perm.equals("read")) {
143                     userPerm[0] = Boolean.TRUE;
144                 }
145                 else if (perm.equals("write")) {
146                     userPerm[1] = Boolean.TRUE;
147                 }
148             }
149         }
150     }
151
152     public void populate(String _owner, Response response) {
153         this.owner = _owner;
154         String header = response.getHeader("Last-Modified");
155         if (header != null)
156                         try {
157                                 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
158                         } catch (IllegalArgumentException e) {
159                                 GWT.log("Last-Modified will be set to null", e);
160                                 lastModified = null;
161                         }
162
163         header = response.getHeader("X-Container-Bytes-Used");
164         if (header != null && header.length() > 0)
165             bytesUsed = Long.valueOf(header);
166
167         header = response.getHeader("X-Container-Object-Meta");
168         if (header != null && header.length() > 0) {
169             for (String t : header.split(",")) {
170                 tags.add(t.toLowerCase().trim());
171             }
172         }
173
174         subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
175         files.clear();
176         JSONValue json = JSONParser.parseStrict(response.getText());
177         JSONArray array = json.isArray();
178         if (array != null) {
179             for (int i=0; i<array.size(); i++) {
180                 JSONObject o = array.get(i).isObject();
181                 if (o != null) {
182                     String contentType = unmarshallString(o, "content_type");
183                     if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
184                         Folder f = new Folder();
185                         f.populate(this, o, _owner, container);
186                         subfolders.add(f);
187                     }
188                     else {
189                         File file = new File();
190                         file.populate(this, o, _owner, container);
191                         files.add(file);
192                     }
193                 }
194             }
195         }
196     }
197
198     public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) {
199         this.parent = _parent;
200         String path = null;
201         if (o.containsKey("subdir")) {
202             path = unmarshallString(o, "subdir");
203         }
204         else {
205             path = unmarshallString(o, "name");
206             lastModified = unmarshallDate(o, "last_modified");
207         }
208         if (path.endsWith("/"))
209             path = path.substring(0, path.length() - 1);
210         if (path.contains("/"))
211             name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
212         else
213             name = path;
214         if (aContainer != null) {
215             container = aContainer;
216             prefix = path;
217         }
218         else {
219             container = name;
220             prefix = "";
221         }
222         this.owner = _owner;
223
224         inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
225         String rawPermissions = unmarshallString(o, "x_object_sharing");
226         if (rawPermissions != null)
227             parsePermissions(rawPermissions);
228     }
229
230     public static Folder createFromResponse(String owner, Response response, Folder result) {
231         Folder f = null;
232         if (result == null)
233             f = new Folder();
234         else
235             f = result;
236
237         f.populate(owner, response);
238         return f;
239     }
240
241     @Override
242     public boolean equals(Object other) {
243         if (other instanceof Folder) {
244             Folder o = (Folder) other;
245             return getUri().equals(o.getUri());
246         }
247         return false;
248     }
249
250     @Override
251     public int hashCode() {
252         return getUri().hashCode();
253     }
254
255     public Set<File> getFiles() {
256         return files;
257     }
258
259     public Folder getParent() {
260         return parent;
261     }
262
263     public String getUri() {
264         return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
265     }
266
267     public boolean isContainer() {
268         return parent == null;
269     }
270
271     public void setContainer(String container) {
272         this.container = container;
273     }
274
275     public Set<String> getTags() {
276         return tags;
277     }
278
279     public String getInheritedPermissionsFrom() {
280         return inheritedPermissionsFrom;
281     }
282
283     public Map<String, Boolean[]> getPermissions() {
284         return permissions;
285     }
286
287     public String getOwner() {
288         return owner;
289     }
290
291     public boolean existChildrenPermissions() {
292         for (File f : files)
293             if (!f.getPermissions().isEmpty() && f.getInheritedPermissionsFrom() == null)
294                 return true;
295
296         for (Folder fo : subfolders)
297             if ((!fo.getPermissions().isEmpty() && fo.getInheritedPermissionsFrom() == null) || fo.existChildrenPermissions())
298                 return true;
299         return false;
300     }
301
302         public boolean isShared() {
303                 return !permissions.isEmpty();
304         }
305
306         /**
307          * I am THE trash
308          * 
309          * @return
310          */
311         public boolean isTrash() {
312                 return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
313         }
314         
315         /**
316          * I am IN THE trash
317          * 
318          * @return
319          */
320         public boolean isInTrash() {
321                 return container.equals(Pithos.TRASH_CONTAINER);
322         }
323
324         public boolean isHome() {
325                 return isContainer() && name.equals(Pithos.HOME_CONTAINER);
326         }
327
328         public boolean contains(Folder folder) {
329                 for (Folder f : subfolders)
330                         if (f.equals(folder) || f.contains(folder))
331                                 return true;
332                 return false;
333         }
334 }