34e42cb782e19e0cf470a3ba642e83d656a65729
[pithos] / web_client / src / gr / grnet / pithos / web / client / rest / resource / FolderResource.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.rest.resource;
37
38 import gr.grnet.pithos.web.client.DisplayHelper;
39 import gr.grnet.pithos.web.client.Pithos;
40 import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
41 import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
42
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.HashSet;
46 import java.util.LinkedList;
47 import java.util.List;
48 import java.util.Set;
49
50 import com.google.gwt.core.client.GWT;
51 import com.google.gwt.http.client.URL;
52 import com.google.gwt.json.client.JSONArray;
53 import com.google.gwt.json.client.JSONObject;
54 import com.google.gwt.json.client.JSONParser;
55 import com.google.gwt.user.client.ui.TreeItem;
56
57 public class FolderResource extends RestResource {
58
59         public FolderResource(String aUri) {
60                 super(aUri);
61         }
62
63         String name;
64
65         String owner;
66
67         String createdBy;
68
69         String modifiedBy;
70
71         Date creationDate;
72
73         Date modificationDate;
74
75         List<String> filePaths = new LinkedList<String>();
76
77         List<String> subfolderPaths = new LinkedList<String>();
78
79         Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
80
81         List<FolderResource> folders = new ArrayList<FolderResource>();
82
83         List<FileResource> files = new ArrayList<FileResource>();
84
85         String parentURI;
86
87         boolean deleted = false;
88
89         boolean needsExpanding = false;
90
91         String parentName;
92
93         private boolean filesExpanded=false;
94
95         boolean readForAll;
96         
97         Boolean shared;
98         
99         
100         
101         
102         
103         /**
104          * Modify the shared.
105          *
106          * @param shared the shared to set
107          */
108         public void setShared(Boolean shared) {
109                 this.shared = shared;
110         }
111
112         /**
113          * Modify the parentName.
114          *
115          * @param aParentName the parentName to set
116          */
117         public void setParentName(String aParentName) {
118                 parentName = aParentName;
119         }
120
121         /**
122          * Retrieve the name.
123          *
124          * @return the name
125          */
126         @Override
127         public String getName() {
128                 return name;
129         }
130
131         /**
132          * Modify the name.
133          *
134          * @param aName the name to set
135          */
136         public void setName(String aName) {
137                 name = aName;
138         }
139
140         /**
141          * Retrieve the owner.
142          *
143          * @return the owner
144          */
145         public String getOwner() {
146                 return owner;
147         }
148
149         /**
150          * Modify the owner.
151          *
152          * @param anOwner the owner to set
153          */
154         public void setOwner(String anOwner) {
155                 owner = anOwner;
156         }
157
158         /**
159          * Retrieve the createdBy.
160          *
161          * @return the createdBy
162          */
163         public String getCreatedBy() {
164                 return createdBy;
165         }
166
167         /**
168          * Modify the createdBy.
169          *
170          * @param aCreatedBy the createdBy to set
171          */
172         public void setCreatedBy(String aCreatedBy) {
173                 createdBy = aCreatedBy;
174         }
175
176         /**
177          * Retrieve the modifiedBy.
178          *
179          * @return the modifiedBy
180          */
181         public String getModifiedBy() {
182                 return modifiedBy;
183         }
184
185         /**
186          * Modify the modifiedBy.
187          *
188          * @param aModifiedBy the modifiedBy to set
189          */
190         public void setModifiedBy(String aModifiedBy) {
191                 modifiedBy = aModifiedBy;
192         }
193
194         /**
195          * Retrieve the creationDate.
196          *
197          * @return the creationDate
198          */
199         public Date getCreationDate() {
200                 return creationDate;
201         }
202
203         /**
204          * Modify the creationDate.
205          *
206          * @param aCreationDate the creationDate to set
207          */
208         public void setCreationDate(Date aCreationDate) {
209                 creationDate = aCreationDate;
210         }
211
212         /**
213          * Retrieve the modificationDate.
214          *
215          * @return the modificationDate
216          */
217         public Date getModificationDate() {
218                 return modificationDate;
219         }
220
221         /**
222          * Modify the modificationDate.
223          *
224          * @param aModificationDate the modificationDate to set
225          */
226         public void setModificationDate(Date aModificationDate) {
227                 modificationDate = aModificationDate;
228         }
229
230         /**
231          * Retrieve the filePaths.
232          *
233          * @return the filePaths
234          */
235         public List<String> getFilePaths() {
236                 return filePaths;
237         }
238
239         /**
240          * Modify the filePaths.
241          *
242          * @param newFilePaths the filePaths to set
243          */
244         public void setFilePaths(List<String> newFilePaths) {
245                 filePaths = newFilePaths;
246         }
247
248         /**
249          * Retrieve the subfolderPaths.
250          *
251          * @return the subfolderPaths
252          */
253         public List<String> getSubfolderPaths() {
254                 return subfolderPaths;
255         }
256
257         /**
258          * Modify the subfolderPaths.
259          *
260          * @param newSubfolderPaths the subfolderPaths to set
261          */
262         public void setSubfolderPaths(List<String> newSubfolderPaths) {
263                 subfolderPaths = newSubfolderPaths;
264         }
265
266         /**
267          * Retrieve the permissions.
268          *
269          * @return the permissions
270          */
271         public Set<PermissionHolder> getPermissions() {
272                 return permissions;
273         }
274
275         /**
276          * Modify the permissions.
277          *
278          * @param newPermissions the permissions to set
279          */
280         public void setPermissions(Set<PermissionHolder> newPermissions) {
281                 permissions = newPermissions;
282         }
283
284         /**
285          * Retrieve the deleted.
286          *
287          * @return the deleted
288          */
289         public boolean isDeleted() {
290                 return deleted;
291         }
292
293         /**
294          * Modify the deleted.
295          *
296          * @param newDeleted the deleted to set
297          */
298         public void setDeleted(boolean newDeleted) {
299                 deleted = newDeleted;
300         }
301
302         public void removeSubfolderPath(String spath) {
303                 if (subfolderPaths.remove(spath))
304                         return;
305                 else if (subfolderPaths.remove(spath + "/"))
306                         return;
307                 else
308                         subfolderPaths.remove(spath.substring(0, spath.length() - 1));
309         }
310
311         /**
312          * Retrieve the folders.
313          *
314          * @return the folders
315          */
316         public List<FolderResource> getFolders() {
317                 return folders;
318         }
319
320         /**
321          * Modify the folders.
322          *
323          * @param newFolders the folders to set
324          */
325         public void setFolders(List<FolderResource> newFolders) {
326                 folders = newFolders;
327         }
328
329         /**
330          * Retrieve the files.
331          *
332          * @return the files
333          */
334         public List<FileResource> getFiles() {
335                 return files;
336         }
337
338         /**
339          * Modify the files.
340          *
341          * @param newFiles the files to set
342          */
343         public void setFiles(List<FileResource> newFiles) {
344                 files = newFiles;
345         }
346
347         /**
348          * Retrieve the parentURI.
349          *
350          * @return the parentURI
351          */
352         public String getParentURI() {
353                 return parentURI;
354         }
355
356         /**
357          * Modify the parentURI.
358          *
359          * @param aParentURI the parentURI to set
360          */
361         public void setParentURI(String aParentURI) {
362                 parentURI = aParentURI;
363         }
364
365         @Override
366         public void createFromJSON(String text) {
367                 JSONObject json = (JSONObject) JSONParser.parse(text);
368                 name = unmarshallString(json, "name");
369                 owner = unmarshallString(json, "owner");
370                 createdBy = unmarshallString(json, "createdBy");
371                 modifiedBy = unmarshallString(json, "modifiedBy");
372                 deleted = unmarshallBoolean(json, "deleted");
373                 shared = unmarshallBoolean(json, "shared");
374                 readForAll = unmarshallBoolean(json, "readForAll");
375                 if (deleted)
376                         GWT.log("FOUND A DELETED FOLDER:" + name, null);
377
378                 if (json.get("parent") != null) {
379                         JSONObject parent = json.get("parent").isObject();
380                         parentURI = unmarshallString(parent, "uri");
381                         parentName = unmarshallString(parent, "name");
382                 }
383
384                 if (json.get("permissions") != null) {
385                         JSONArray perm = json.get("permissions").isArray();
386                         if (perm != null)
387                                 for (int i = 0; i < perm.size(); i++) {
388                                         JSONObject obj = perm.get(i).isObject();
389                                         if (obj != null) {
390                                                 PermissionHolder permission = new PermissionHolder();
391                                                 if (obj.get("user") != null)
392                                                         permission.setUser(unmarshallString(obj, "user"));
393                                                 if (obj.get("group") != null)
394                                                         permission.setGroup(URL.decodeComponent(unmarshallString(obj, "group")));
395                                                 permission.setRead(unmarshallBoolean(obj, "read"));
396                                                 permission.setWrite(unmarshallBoolean(obj, "write"));
397                                                 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
398                                                 permissions.add(permission);
399                                         }
400                                 }
401                 }
402                 if (json.get("folders") != null) {
403                         JSONArray subs = json.get("folders").isArray();
404                         if (subs != null)
405                                 for (int i = 0; i < subs.size(); i++) {
406                                         JSONObject so = subs.get(i).isObject();
407                                         if (so != null) {
408                                                 String subUri = unmarshallString(so, "uri");
409                                                 String subName = unmarshallString(so, "name");
410                                                 if (subUri != null && subName != null) {
411                                                         if (!subUri.endsWith("/"))
412                                                                 subUri = subUri + "/";
413                                                         FolderResource sub = new FolderResource(subUri);
414                                                         sub.setName(subName);
415                                                         sub.setParentURI(uri);
416                                                         sub.setParentName(name);
417                                                         sub.setNeedsExpanding(true);
418                                                         folders.add(sub);
419                                                         subfolderPaths.add(subUri);
420                                                 }
421                                         }
422                                 }
423                 }
424                 if (json.get("files") != null) {
425                         JSONArray subs = json.get("files").isArray();
426                         if (subs != null)
427                                 for (int i = 0; i < subs.size(); i++) {
428                                         JSONObject fo = subs.get(i).isObject();
429                                         if (fo != null) {
430                                                 String fname = unmarshallString(fo, "name");
431                                                 String fowner = unmarshallString(fo, "owner");
432                                                 String fcontent = unmarshallString(fo, "content");
433                                                 String fpath = unmarshallString(fo, "path");
434                                                 Boolean fshared = unmarshallBoolean(fo, "shared");
435                                                 fpath = URL.decodeComponent(fpath);
436                                                 Integer fversion = null;
437                                                 if (fo.get("version") != null)
438                                                         fversion = new Integer(fo.get("version").toString());
439                                                 boolean fdeleted = unmarshallBoolean(fo, "deleted");
440                                                 Date fcreationDate = null;
441                                                 if (fo.get("creationDate") != null)
442                                                         fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
443                                                 Date fmodificationDate = null;
444                                                 if (fo.get("modificationDate") != null)
445                                                         fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
446                                                 String furi = unmarshallString(fo, "uri");
447                                                 Long fsize = 0L;
448                                                 if (fo.get("size") != null)
449                                                         fsize = new Long(fo.get("size").toString());
450                                                 filePaths.add(furi);
451                                                 FileResource fs = new FileResource(furi);
452                                                 fs.setName(fname);
453                                                 fs.setOwner(fowner);
454                                                 fs.setPath(fpath);
455                                                 fs.setVersioned(unmarshallBoolean(fo, "versioned"));
456                                                 fs.setVersion(fversion);
457                                                 fs.setContentLength(fsize);
458                                                 fs.setDeleted(fdeleted);
459                                                 fs.setShared(fshared);
460                                                 fs.setCreationDate(fcreationDate);
461                                                 fs.setModificationDate(fmodificationDate);
462                                                 fs.setContentType(fcontent);
463                                                 files.add(fs);
464                                         }
465                                 }
466                 }
467                 if (json.get("creationDate") != null)
468                         creationDate = new Date(new Long(json.get("creationDate").toString()));
469                 if (json.get("modificationDate") != null)
470                         modificationDate = new Date(new Long(json.get("modificationDate").toString()));
471         }
472
473         public String getParentName(){
474                 return parentName;
475         }
476
477         /**
478          * Retrieve the needsExpanding.
479          *
480          * @return the needsExpanding
481          */
482         public boolean isNeedsExpanding() {
483                 return needsExpanding;
484         }
485
486         /**
487          * Modify the needsExpanding.
488          *
489          * @param newNeedsExpanding the needsExpanding to set
490          */
491         public void setNeedsExpanding(boolean newNeedsExpanding) {
492                 needsExpanding = newNeedsExpanding;
493         }
494
495         public boolean isShared(){
496                 return shared;
497         }
498
499         @Override
500         public String getLastModifiedSince() {
501                 if(modificationDate != null)
502                         return getDate(modificationDate.getTime());
503                 return null;
504         }
505
506         public MultipleGetCommand.Cached[] getCache(){
507                 if(getSubfolderPaths().size() != getFolders().size()){
508                         GWT.log("MISMATCH IN PATH AND FOLDERS SIZE", null);
509                         return null;
510                 }
511                 MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getSubfolderPaths().size()];
512                 for(int i=0; i<getFolders().size();i++){
513                         FolderResource r = getFolders().get(i);
514                         Cached c = new Cached();
515                         c.cache=r;
516                         c.uri=r.uri;
517                         result[i] = c;
518                 }
519                 return result;
520         }
521
522         public MultipleGetCommand.Cached[] getFileCache(){
523                 if(getFilePaths().size() != getFiles().size()){
524                         GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
525                         return null;
526                 }
527                 if(!filesExpanded)
528                         return null;
529                 MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
530                 for(int i=0; i<getFiles().size();i++){
531                         FileResource r = getFiles().get(i);
532                         Cached c = new Cached();
533                         c.cache=r;
534                         c.uri=r.uri;
535                         result[i] = c;
536                 }
537                 return result;
538         }
539
540         public void setFilesExpanded(boolean newFilesExpanded) {
541                 filesExpanded = newFilesExpanded;
542         }
543         /**
544          * this method constructs the partial path of a given TreeItem using it's text
545          *
546          * @param selectedItem the selectedItem to check
547          */
548         private String constructPartialPath(TreeItem selectedItem){
549            String result = DisplayHelper.trim(selectedItem.getText());
550            TreeItem parent = selectedItem.getParentItem();
551            while (!(DisplayHelper.trim(parent.getText()).equals("My Shared") || DisplayHelper.trim(parent.getText()).equals("Other's Shared")||DisplayHelper.trim(parent.getText()).equals("Trash"))){
552               result = DisplayHelper.trim(parent.getText()) + "/" + result;
553               if(result.equals("My Shared")||result.equals("Other's Shared")) return result;
554               parent = parent.getParentItem();
555            }
556
557            return result;
558         }
559         /**
560          * examine whether a folder name like "Trash", "My Shared", "Other's Shared" is inside path
561          *
562          * @param selectedItem the selectedTreeItem to check
563          */
564
565         private boolean containsFolder(TreeItem selectedItem, String folderName){
566                 TreeItem parent = selectedItem.getParentItem();
567                 while (parent != null){
568                         String parentItemText = parent.getText();
569                         String parentItemTextTr = DisplayHelper.trim(parentItemText);
570                         if(parentItemTextTr.equals(folderName)) return true;
571                         parent = parent.getParentItem();
572                         }
573                 return false;
574         }
575         @Override
576         public String constructUri(TreeItem treeItem, String path){
577                 String constructedUri = "";
578                 if(containsFolder(treeItem, "My Shared")){
579                         //case: selected folders below My Shared folder
580                         String partialUri = constructPartialPath(treeItem);
581                         constructedUri = constructedUri + "Files/shared/" + partialUri;
582                         return constructedUri;
583                 }else if(containsFolder(treeItem, "Other's Shared")){
584                         //case: selected folders below Other's Shared folder
585                         String partialPath = constructPartialPath(treeItem);
586                         constructedUri = constructedUri + "Files/others/"+ partialPath;
587                         return constructedUri;
588                 }
589                 else if(getParentURI()==null){
590                         if(containsFolder(treeItem, "Trash")){
591                                 //case: selected folders below Trash folder
592                                 String partialUri = constructPartialPath(treeItem);
593                                 constructedUri = constructedUri + "Files/trash/" + partialUri;
594                                 return constructedUri;
595                         }
596                         //case: home folder is selected
597                         constructedUri = constructedUri + "Files/files/" + getName();
598                         return constructedUri;
599                 }
600                 else if(treeItem.getParentItem() == null){
601                         //this is the case when the user uses the browser's forward arrow to navigate through other's
602                         //shared folders and item.getParentItem is null only inside other's shared folder
603                         String apiPath = Pithos.get().getApiPath();
604                         String newPath = getParentURI().substring(apiPath.lastIndexOf("/"));
605                         constructedUri = constructedUri + "Files"+ newPath + getName();
606                         return constructedUri;
607                 }
608                 else{
609                         String finalUri = getParentURI().substring(path.lastIndexOf("/")) + getName();
610                         constructedUri = constructedUri + "Files"+ finalUri;
611                         return constructedUri;
612                 }
613
614         }
615
616         /**
617          * Retrieve the readForAll.
618          *
619          * @return the readForAll
620          */
621         public boolean isReadForAll() {
622                 return readForAll;
623         }
624         /**
625          * Modify the readForAll.
626          *
627          * @param newReadForAll the readForAll to set
628          */
629         public void setReadForAll(boolean newReadForAll) {
630                 readForAll = newReadForAll;
631         }
632         
633         
634         public int countNotDeletedSubfolders(){
635                 int count=0;
636                 for(FolderResource r : folders){
637                         if(!r.isDeleted())
638                                 count++;
639                 }
640                 return count;
641         }
642
643 }