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