Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / client / rest / resource / FolderResource.java
1 /*
2  * Copyright 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package gr.ebs.gss.client.rest.resource;
21
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.HashSet;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Set;
28
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.http.client.URL;
31 import com.google.gwt.json.client.JSONArray;
32 import com.google.gwt.json.client.JSONObject;
33 import com.google.gwt.json.client.JSONParser;
34
35 /**
36  * @author kman
37  */
38 public class FolderResource extends RestResource {
39
40         public FolderResource(String aUri) {
41                 super(aUri);
42         }
43
44         String name;
45
46         String owner;
47
48         String createdBy;
49
50         String modifiedBy;
51
52         Date creationDate;
53
54         Date modificationDate;
55
56         List<String> filePaths = new LinkedList<String>();
57
58         List<String> subfolderPaths = new LinkedList<String>();
59
60         Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
61
62         List<FolderResource> folders = new ArrayList<FolderResource>();
63
64         List<FileResource> files = new ArrayList<FileResource>();
65
66         String parentURI;
67
68         boolean deleted = false;
69
70         boolean needsExpanding = false;
71
72         String parentName;
73
74         /**
75          * Modify the parentName.
76          *
77          * @param aParentName the parentName to set
78          */
79         public void setParentName(String aParentName) {
80                 parentName = aParentName;
81         }
82
83         /**
84          * Retrieve the name.
85          *
86          * @return the name
87          */
88         public String getName() {
89                 return name;
90         }
91
92         /**
93          * Modify the name.
94          *
95          * @param aName the name to set
96          */
97         public void setName(String aName) {
98                 name = aName;
99         }
100
101         /**
102          * Retrieve the owner.
103          *
104          * @return the owner
105          */
106         public String getOwner() {
107                 return owner;
108         }
109
110         /**
111          * Modify the owner.
112          *
113          * @param anOwner the owner to set
114          */
115         public void setOwner(String anOwner) {
116                 owner = anOwner;
117         }
118
119         /**
120          * Retrieve the createdBy.
121          *
122          * @return the createdBy
123          */
124         public String getCreatedBy() {
125                 return createdBy;
126         }
127
128         /**
129          * Modify the createdBy.
130          *
131          * @param aCreatedBy the createdBy to set
132          */
133         public void setCreatedBy(String aCreatedBy) {
134                 createdBy = aCreatedBy;
135         }
136
137         /**
138          * Retrieve the modifiedBy.
139          *
140          * @return the modifiedBy
141          */
142         public String getModifiedBy() {
143                 return modifiedBy;
144         }
145
146         /**
147          * Modify the modifiedBy.
148          *
149          * @param aModifiedBy the modifiedBy to set
150          */
151         public void setModifiedBy(String aModifiedBy) {
152                 modifiedBy = aModifiedBy;
153         }
154
155         /**
156          * Retrieve the creationDate.
157          *
158          * @return the creationDate
159          */
160         public Date getCreationDate() {
161                 return creationDate;
162         }
163
164         /**
165          * Modify the creationDate.
166          *
167          * @param aCreationDate the creationDate to set
168          */
169         public void setCreationDate(Date aCreationDate) {
170                 creationDate = aCreationDate;
171         }
172
173         /**
174          * Retrieve the modificationDate.
175          *
176          * @return the modificationDate
177          */
178         public Date getModificationDate() {
179                 return modificationDate;
180         }
181
182         /**
183          * Modify the modificationDate.
184          *
185          * @param aModificationDate the modificationDate to set
186          */
187         public void setModificationDate(Date aModificationDate) {
188                 modificationDate = aModificationDate;
189         }
190
191         /**
192          * Retrieve the filePaths.
193          *
194          * @return the filePaths
195          */
196         public List<String> getFilePaths() {
197                 return filePaths;
198         }
199
200         /**
201          * Modify the filePaths.
202          *
203          * @param newFilePaths the filePaths to set
204          */
205         public void setFilePaths(List<String> newFilePaths) {
206                 filePaths = newFilePaths;
207         }
208
209         /**
210          * Retrieve the subfolderPaths.
211          *
212          * @return the subfolderPaths
213          */
214         public List<String> getSubfolderPaths() {
215                 return subfolderPaths;
216         }
217
218         /**
219          * Modify the subfolderPaths.
220          *
221          * @param newSubfolderPaths the subfolderPaths to set
222          */
223         public void setSubfolderPaths(List<String> newSubfolderPaths) {
224                 subfolderPaths = newSubfolderPaths;
225         }
226
227         /**
228          * Retrieve the permissions.
229          *
230          * @return the permissions
231          */
232         public Set<PermissionHolder> getPermissions() {
233                 return permissions;
234         }
235
236         /**
237          * Modify the permissions.
238          *
239          * @param newPermissions the permissions to set
240          */
241         public void setPermissions(Set<PermissionHolder> newPermissions) {
242                 permissions = newPermissions;
243         }
244
245         /**
246          * Retrieve the deleted.
247          *
248          * @return the deleted
249          */
250         public boolean isDeleted() {
251                 return deleted;
252         }
253
254         /**
255          * Modify the deleted.
256          *
257          * @param newDeleted the deleted to set
258          */
259         public void setDeleted(boolean newDeleted) {
260                 deleted = newDeleted;
261         }
262
263         public void removeSubfolderPath(String spath) {
264                 if (subfolderPaths.remove(spath))
265                         return;
266                 else if (subfolderPaths.remove(spath + "/"))
267                         return;
268                 else
269                         subfolderPaths.remove(spath.substring(0, spath.length() - 1));
270         }
271
272         /**
273          * Retrieve the folders.
274          *
275          * @return the folders
276          */
277         public List<FolderResource> getFolders() {
278                 return folders;
279         }
280
281         /**
282          * Modify the folders.
283          *
284          * @param newFolders the folders to set
285          */
286         public void setFolders(List<FolderResource> newFolders) {
287                 folders = newFolders;
288         }
289
290         /**
291          * Retrieve the files.
292          *
293          * @return the files
294          */
295         public List<FileResource> getFiles() {
296                 return files;
297         }
298
299         /**
300          * Modify the files.
301          *
302          * @param newFiles the files to set
303          */
304         public void setFiles(List<FileResource> newFiles) {
305                 files = newFiles;
306         }
307
308         /**
309          * Retrieve the parentURI.
310          *
311          * @return the parentURI
312          */
313         public String getParentURI() {
314                 return parentURI;
315         }
316
317         /**
318          * Modify the parentURI.
319          *
320          * @param aParentURI the parentURI to set
321          */
322         public void setParentURI(String aParentURI) {
323                 parentURI = aParentURI;
324         }
325
326         @Override
327         public void createFromJSON(String text) {
328                 JSONObject json = (JSONObject) JSONParser.parse(text);
329                 name = unmarshallString(json, "name");
330                 owner = unmarshallString(json, "owner");
331                 deleted = unmarshallBoolean(json, "deleted");
332                 if (deleted)
333                         GWT.log("FOUND A DELETED FOLDER:" + name, null);
334
335                 if (json.get("parent") != null) {
336                         JSONObject parent = json.get("parent").isObject();
337                         parentURI = unmarshallString(parent, "uri");
338                         parentName = unmarshallString(parent, "name");
339                         if(parentName != null)
340                                 parentName = URL.decodeComponent(parentName);
341                 }
342
343                 if (json.get("permissions") != null) {
344                         JSONArray perm = json.get("permissions").isArray();
345                         if (perm != null)
346                                 for (int i = 0; i < perm.size(); i++) {
347                                         JSONObject obj = perm.get(i).isObject();
348                                         if (obj != null) {
349                                                 PermissionHolder permission = new PermissionHolder();
350                                                 if (obj.get("user") != null)
351                                                         permission.setUser(unmarshallString(obj, "user"));
352                                                 if (obj.get("group") != null)
353                                                         permission.setGroup(URL.decodeComponent(unmarshallString(obj, "group")));
354                                                 permission.setRead(unmarshallBoolean(obj, "read"));
355                                                 permission.setWrite(unmarshallBoolean(obj, "write"));
356                                                 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
357                                                 permissions.add(permission);
358                                         }
359                                 }
360                 }
361                 if (json.get("folders") != null) {
362                         JSONArray subs = json.get("folders").isArray();
363                         if (subs != null)
364                                 for (int i = 0; i < subs.size(); i++) {
365                                         JSONObject so = subs.get(i).isObject();
366                                         if (so != null) {
367                                                 String subUri = unmarshallString(so, "uri");
368                                                 String subName = unmarshallString(so, "name");
369                                                 if (subUri != null && subName != null) {
370                                                         if (!subUri.endsWith("/"))
371                                                                 subUri = subUri + "/";
372                                                         FolderResource sub = new FolderResource(subUri);
373                                                         sub.setName(subName);
374                                                         sub.setNeedsExpanding(true);
375                                                         folders.add(sub);
376                                                         subfolderPaths.add(subUri);
377                                                 }
378                                         }
379                                 }
380                 }
381                 if (json.get("files") != null) {
382                         JSONArray subs = json.get("files").isArray();
383                         if (subs != null)
384                                 for (int i = 0; i < subs.size(); i++) {
385                                         JSONObject fo = subs.get(i).isObject();
386                                         if (fo != null) {
387                                                 String fname = unmarshallString(fo, "name");
388                                                 String fowner = unmarshallString(fo, "owner");
389                                                 String fcontent = unmarshallString(fo, "content");
390                                                 String fpath = unmarshallString(fo, "path");
391                                                 fpath = URL.decodeComponent(fpath);
392                                                 Integer fversion = null;
393                                                 if (fo.get("version") != null)
394                                                         fversion = new Integer(fo.get("version").toString());
395                                                 boolean fdeleted = unmarshallBoolean(fo, "deleted");
396                                                 Date fcreationDate = null;
397                                                 if (fo.get("creationDate") != null)
398                                                         fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
399                                                 String furi = unmarshallString(fo, "uri");
400                                                 Long fsize = 0L;
401                                                 if (fo.get("size") != null)
402                                                         fsize = new Long(fo.get("size").toString());
403                                                 filePaths.add(furi);
404                                                 FileResource fs = new FileResource(furi);
405                                                 fs.setName(fname);
406                                                 fs.setOwner(fowner);
407                                                 fs.setPath(fpath);
408                                                 fs.setVersion(fversion);
409                                                 fs.setContentLength(fsize);
410                                                 fs.setDeleted(fdeleted);
411                                                 fs.setCreationDate(fcreationDate);
412                                                 fs.setContentType(fcontent);
413                                                 files.add(fs);
414                                         }
415                                 }
416                 }
417                 if (json.get("creationDate") != null)
418                         creationDate = new Date(new Long(json.get("creationDate").toString()));
419                 if (json.get("modificationDate") != null)
420                         modificationDate = new Date(new Long(json.get("modificationDate").toString()));
421         }
422
423         public String getParentName(){
424                 return parentName;
425         }
426
427         /**
428          * Retrieve the needsExpanding.
429          *
430          * @return the needsExpanding
431          */
432         public boolean isNeedsExpanding() {
433                 return needsExpanding;
434         }
435
436         /**
437          * Modify the needsExpanding.
438          *
439          * @param newNeedsExpanding the needsExpanding to set
440          */
441         public void setNeedsExpanding(boolean newNeedsExpanding) {
442                 needsExpanding = newNeedsExpanding;
443         }
444
445         public boolean isShared(){
446
447                 for(PermissionHolder perm : permissions){
448                         if(perm.getUser() != null && !owner.equals(perm.getUser()))
449                                 return true;
450                         if(perm.getGroup() != null)
451                                 return true;
452                 }
453                 return false;
454         }
455 }