Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / rest / resource / FolderResource.java @ 1206:292dec4eae08

History | View | Annotate | Download (15.3 kB)

1
/*
2
 * Copyright 2009, 2010 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 org.gss_project.gss.web.client.rest.resource;
21

    
22
import org.gss_project.gss.web.client.DisplayHelper;
23
import org.gss_project.gss.web.client.GSS;
24
import org.gss_project.gss.web.client.rest.MultipleGetCommand;
25
import org.gss_project.gss.web.client.rest.MultipleGetCommand.Cached;
26

    
27
import java.util.ArrayList;
28
import java.util.Date;
29
import java.util.HashSet;
30
import java.util.LinkedList;
31
import java.util.List;
32
import java.util.Set;
33

    
34
import com.google.gwt.core.client.GWT;
35
import com.google.gwt.http.client.URL;
36
import com.google.gwt.json.client.JSONArray;
37
import com.google.gwt.json.client.JSONObject;
38
import com.google.gwt.json.client.JSONParser;
39
import com.google.gwt.user.client.ui.TreeItem;
40

    
41
/**
42
 * @author kman
43
 */
44
public class FolderResource extends RestResource {
45

    
46
        public FolderResource(String aUri) {
47
                super(aUri);
48
        }
49

    
50
        String name;
51

    
52
        String owner;
53

    
54
        String createdBy;
55

    
56
        String modifiedBy;
57

    
58
        Date creationDate;
59

    
60
        Date modificationDate;
61

    
62
        List<String> filePaths = new LinkedList<String>();
63

    
64
        List<String> subfolderPaths = new LinkedList<String>();
65

    
66
        Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
67

    
68
        List<FolderResource> folders = new ArrayList<FolderResource>();
69

    
70
        List<FileResource> files = new ArrayList<FileResource>();
71

    
72
        String parentURI;
73

    
74
        boolean deleted = false;
75

    
76
        boolean needsExpanding = false;
77

    
78
        String parentName;
79

    
80
        private boolean filesExpanded=false;
81

    
82
        boolean readForAll;
83
        
84
        Boolean shared;
85
        
86
        
87
        
88
        
89
        
90
        /**
91
         * Modify the shared.
92
         *
93
         * @param shared the shared to set
94
         */
95
        public void setShared(Boolean shared) {
96
                this.shared = shared;
97
        }
98

    
99
        /**
100
         * Modify the parentName.
101
         *
102
         * @param aParentName the parentName to set
103
         */
104
        public void setParentName(String aParentName) {
105
                parentName = aParentName;
106
        }
107

    
108
        /**
109
         * Retrieve the name.
110
         *
111
         * @return the name
112
         */
113
        @Override
114
        public String getName() {
115
                return name;
116
        }
117

    
118
        /**
119
         * Modify the name.
120
         *
121
         * @param aName the name to set
122
         */
123
        public void setName(String aName) {
124
                name = aName;
125
        }
126

    
127
        /**
128
         * Retrieve the owner.
129
         *
130
         * @return the owner
131
         */
132
        public String getOwner() {
133
                return owner;
134
        }
135

    
136
        /**
137
         * Modify the owner.
138
         *
139
         * @param anOwner the owner to set
140
         */
141
        public void setOwner(String anOwner) {
142
                owner = anOwner;
143
        }
144

    
145
        /**
146
         * Retrieve the createdBy.
147
         *
148
         * @return the createdBy
149
         */
150
        public String getCreatedBy() {
151
                return createdBy;
152
        }
153

    
154
        /**
155
         * Modify the createdBy.
156
         *
157
         * @param aCreatedBy the createdBy to set
158
         */
159
        public void setCreatedBy(String aCreatedBy) {
160
                createdBy = aCreatedBy;
161
        }
162

    
163
        /**
164
         * Retrieve the modifiedBy.
165
         *
166
         * @return the modifiedBy
167
         */
168
        public String getModifiedBy() {
169
                return modifiedBy;
170
        }
171

    
172
        /**
173
         * Modify the modifiedBy.
174
         *
175
         * @param aModifiedBy the modifiedBy to set
176
         */
177
        public void setModifiedBy(String aModifiedBy) {
178
                modifiedBy = aModifiedBy;
179
        }
180

    
181
        /**
182
         * Retrieve the creationDate.
183
         *
184
         * @return the creationDate
185
         */
186
        public Date getCreationDate() {
187
                return creationDate;
188
        }
189

    
190
        /**
191
         * Modify the creationDate.
192
         *
193
         * @param aCreationDate the creationDate to set
194
         */
195
        public void setCreationDate(Date aCreationDate) {
196
                creationDate = aCreationDate;
197
        }
198

    
199
        /**
200
         * Retrieve the modificationDate.
201
         *
202
         * @return the modificationDate
203
         */
204
        public Date getModificationDate() {
205
                return modificationDate;
206
        }
207

    
208
        /**
209
         * Modify the modificationDate.
210
         *
211
         * @param aModificationDate the modificationDate to set
212
         */
213
        public void setModificationDate(Date aModificationDate) {
214
                modificationDate = aModificationDate;
215
        }
216

    
217
        /**
218
         * Retrieve the filePaths.
219
         *
220
         * @return the filePaths
221
         */
222
        public List<String> getFilePaths() {
223
                return filePaths;
224
        }
225

    
226
        /**
227
         * Modify the filePaths.
228
         *
229
         * @param newFilePaths the filePaths to set
230
         */
231
        public void setFilePaths(List<String> newFilePaths) {
232
                filePaths = newFilePaths;
233
        }
234

    
235
        /**
236
         * Retrieve the subfolderPaths.
237
         *
238
         * @return the subfolderPaths
239
         */
240
        public List<String> getSubfolderPaths() {
241
                return subfolderPaths;
242
        }
243

    
244
        /**
245
         * Modify the subfolderPaths.
246
         *
247
         * @param newSubfolderPaths the subfolderPaths to set
248
         */
249
        public void setSubfolderPaths(List<String> newSubfolderPaths) {
250
                subfolderPaths = newSubfolderPaths;
251
        }
252

    
253
        /**
254
         * Retrieve the permissions.
255
         *
256
         * @return the permissions
257
         */
258
        public Set<PermissionHolder> getPermissions() {
259
                return permissions;
260
        }
261

    
262
        /**
263
         * Modify the permissions.
264
         *
265
         * @param newPermissions the permissions to set
266
         */
267
        public void setPermissions(Set<PermissionHolder> newPermissions) {
268
                permissions = newPermissions;
269
        }
270

    
271
        /**
272
         * Retrieve the deleted.
273
         *
274
         * @return the deleted
275
         */
276
        public boolean isDeleted() {
277
                return deleted;
278
        }
279

    
280
        /**
281
         * Modify the deleted.
282
         *
283
         * @param newDeleted the deleted to set
284
         */
285
        public void setDeleted(boolean newDeleted) {
286
                deleted = newDeleted;
287
        }
288

    
289
        public void removeSubfolderPath(String spath) {
290
                if (subfolderPaths.remove(spath))
291
                        return;
292
                else if (subfolderPaths.remove(spath + "/"))
293
                        return;
294
                else
295
                        subfolderPaths.remove(spath.substring(0, spath.length() - 1));
296
        }
297

    
298
        /**
299
         * Retrieve the folders.
300
         *
301
         * @return the folders
302
         */
303
        public List<FolderResource> getFolders() {
304
                return folders;
305
        }
306

    
307
        /**
308
         * Modify the folders.
309
         *
310
         * @param newFolders the folders to set
311
         */
312
        public void setFolders(List<FolderResource> newFolders) {
313
                folders = newFolders;
314
        }
315

    
316
        /**
317
         * Retrieve the files.
318
         *
319
         * @return the files
320
         */
321
        public List<FileResource> getFiles() {
322
                return files;
323
        }
324

    
325
        /**
326
         * Modify the files.
327
         *
328
         * @param newFiles the files to set
329
         */
330
        public void setFiles(List<FileResource> newFiles) {
331
                files = newFiles;
332
        }
333

    
334
        /**
335
         * Retrieve the parentURI.
336
         *
337
         * @return the parentURI
338
         */
339
        public String getParentURI() {
340
                return parentURI;
341
        }
342

    
343
        /**
344
         * Modify the parentURI.
345
         *
346
         * @param aParentURI the parentURI to set
347
         */
348
        public void setParentURI(String aParentURI) {
349
                parentURI = aParentURI;
350
        }
351

    
352
        @Override
353
        public void createFromJSON(String text) {
354
                JSONObject json = (JSONObject) JSONParser.parse(text);
355
                name = unmarshallString(json, "name");
356
                owner = unmarshallString(json, "owner");
357
                createdBy = unmarshallString(json, "createdBy");
358
                modifiedBy = unmarshallString(json, "modifiedBy");
359
                deleted = unmarshallBoolean(json, "deleted");
360
                shared = unmarshallBoolean(json, "shared");
361
                readForAll = unmarshallBoolean(json, "readForAll");
362
                if (deleted)
363
                        GWT.log("FOUND A DELETED FOLDER:" + name, null);
364

    
365
                if (json.get("parent") != null) {
366
                        JSONObject parent = json.get("parent").isObject();
367
                        parentURI = unmarshallString(parent, "uri");
368
                        parentName = unmarshallString(parent, "name");
369
                }
370

    
371
                if (json.get("permissions") != null) {
372
                        JSONArray perm = json.get("permissions").isArray();
373
                        if (perm != null)
374
                                for (int i = 0; i < perm.size(); i++) {
375
                                        JSONObject obj = perm.get(i).isObject();
376
                                        if (obj != null) {
377
                                                PermissionHolder permission = new PermissionHolder();
378
                                                if (obj.get("user") != null)
379
                                                        permission.setUser(unmarshallString(obj, "user"));
380
                                                if (obj.get("group") != null)
381
                                                        permission.setGroup(URL.decodeComponent(unmarshallString(obj, "group")));
382
                                                permission.setRead(unmarshallBoolean(obj, "read"));
383
                                                permission.setWrite(unmarshallBoolean(obj, "write"));
384
                                                permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
385
                                                permissions.add(permission);
386
                                        }
387
                                }
388
                }
389
                if (json.get("folders") != null) {
390
                        JSONArray subs = json.get("folders").isArray();
391
                        if (subs != null)
392
                                for (int i = 0; i < subs.size(); i++) {
393
                                        JSONObject so = subs.get(i).isObject();
394
                                        if (so != null) {
395
                                                String subUri = unmarshallString(so, "uri");
396
                                                String subName = unmarshallString(so, "name");
397
                                                if (subUri != null && subName != null) {
398
                                                        if (!subUri.endsWith("/"))
399
                                                                subUri = subUri + "/";
400
                                                        FolderResource sub = new FolderResource(subUri);
401
                                                        sub.setName(subName);
402
                                                        sub.setParentURI(uri);
403
                                                        sub.setParentName(name);
404
                                                        sub.setNeedsExpanding(true);
405
                                                        folders.add(sub);
406
                                                        subfolderPaths.add(subUri);
407
                                                }
408
                                        }
409
                                }
410
                }
411
                if (json.get("files") != null) {
412
                        JSONArray subs = json.get("files").isArray();
413
                        if (subs != null)
414
                                for (int i = 0; i < subs.size(); i++) {
415
                                        JSONObject fo = subs.get(i).isObject();
416
                                        if (fo != null) {
417
                                                String fname = unmarshallString(fo, "name");
418
                                                String fowner = unmarshallString(fo, "owner");
419
                                                String fcontent = unmarshallString(fo, "content");
420
                                                String fpath = unmarshallString(fo, "path");
421
                                                Boolean fshared = unmarshallBoolean(fo, "shared");
422
                                                fpath = URL.decodeComponent(fpath);
423
                                                Integer fversion = null;
424
                                                if (fo.get("version") != null)
425
                                                        fversion = new Integer(fo.get("version").toString());
426
                                                boolean fdeleted = unmarshallBoolean(fo, "deleted");
427
                                                Date fcreationDate = null;
428
                                                if (fo.get("creationDate") != null)
429
                                                        fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
430
                                                Date fmodificationDate = null;
431
                                                if (fo.get("modificationDate") != null)
432
                                                        fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
433
                                                String furi = unmarshallString(fo, "uri");
434
                                                Long fsize = 0L;
435
                                                if (fo.get("size") != null)
436
                                                        fsize = new Long(fo.get("size").toString());
437
                                                filePaths.add(furi);
438
                                                FileResource fs = new FileResource(furi);
439
                                                fs.setName(fname);
440
                                                fs.setOwner(fowner);
441
                                                fs.setPath(fpath);
442
                                                fs.setVersioned(unmarshallBoolean(fo, "versioned"));
443
                                                fs.setVersion(fversion);
444
                                                fs.setContentLength(fsize);
445
                                                fs.setDeleted(fdeleted);
446
                                                fs.setShared(fshared);
447
                                                fs.setCreationDate(fcreationDate);
448
                                                fs.setModificationDate(fmodificationDate);
449
                                                fs.setContentType(fcontent);
450
                                                files.add(fs);
451
                                        }
452
                                }
453
                }
454
                if (json.get("creationDate") != null)
455
                        creationDate = new Date(new Long(json.get("creationDate").toString()));
456
                if (json.get("modificationDate") != null)
457
                        modificationDate = new Date(new Long(json.get("modificationDate").toString()));
458
        }
459

    
460
        public String getParentName(){
461
                return parentName;
462
        }
463

    
464
        /**
465
         * Retrieve the needsExpanding.
466
         *
467
         * @return the needsExpanding
468
         */
469
        public boolean isNeedsExpanding() {
470
                return needsExpanding;
471
        }
472

    
473
        /**
474
         * Modify the needsExpanding.
475
         *
476
         * @param newNeedsExpanding the needsExpanding to set
477
         */
478
        public void setNeedsExpanding(boolean newNeedsExpanding) {
479
                needsExpanding = newNeedsExpanding;
480
        }
481

    
482
        public boolean isShared(){
483
                return shared;
484
        }
485

    
486
        @Override
487
        public String getLastModifiedSince() {
488
                if(modificationDate != null)
489
                        return getDate(modificationDate.getTime());
490
                return null;
491
        }
492

    
493
        public MultipleGetCommand.Cached[] getCache(){
494
                if(getSubfolderPaths().size() != getFolders().size()){
495
                        GWT.log("MISMATCH IN PATH AND FOLDERS SIZE", null);
496
                        return null;
497
                }
498
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getSubfolderPaths().size()];
499
                for(int i=0; i<getFolders().size();i++){
500
                        FolderResource r = getFolders().get(i);
501
                        Cached c = new Cached();
502
                        c.cache=r;
503
                        c.uri=r.uri;
504
                        result[i] = c;
505
                }
506
                return result;
507
        }
508

    
509
        public MultipleGetCommand.Cached[] getFileCache(){
510
                if(getFilePaths().size() != getFiles().size()){
511
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
512
                        return null;
513
                }
514
                if(!filesExpanded)
515
                        return null;
516
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
517
                for(int i=0; i<getFiles().size();i++){
518
                        FileResource r = getFiles().get(i);
519
                        Cached c = new Cached();
520
                        c.cache=r;
521
                        c.uri=r.uri;
522
                        result[i] = c;
523
                }
524
                return result;
525
        }
526

    
527
        public void setFilesExpanded(boolean newFilesExpanded) {
528
                filesExpanded = newFilesExpanded;
529
        }
530
        /**
531
         * this method constructs the partial path of a given TreeItem using it's text
532
         *
533
         * @param selectedItem the selectedItem to check
534
         */
535
        private String constructPartialPath(TreeItem selectedItem){
536
           String result = DisplayHelper.trim(selectedItem.getText());
537
           TreeItem parent = selectedItem.getParentItem();
538
           while (!(DisplayHelper.trim(parent.getText()).equals("My Shared") || DisplayHelper.trim(parent.getText()).equals("Other's Shared")||DisplayHelper.trim(parent.getText()).equals("Trash"))){
539
              result = DisplayHelper.trim(parent.getText()) + "/" + result;
540
              if(result.equals("My Shared")||result.equals("Other's Shared")) return result;
541
              parent = parent.getParentItem();
542
           }
543

    
544
           return result;
545
        }
546
        /**
547
         * examine whether a folder name like "Trash", "My Shared", "Other's Shared" is inside path
548
         *
549
         * @param selectedItem the selectedTreeItem to check
550
         */
551

    
552
        private boolean containsFolder(TreeItem selectedItem, String folderName){
553
                TreeItem parent = selectedItem.getParentItem();
554
                while (parent != null){
555
                        String parentItemText = parent.getText();
556
                        String parentItemTextTr = DisplayHelper.trim(parentItemText);
557
                        if(parentItemTextTr.equals(folderName)) return true;
558
                        parent = parent.getParentItem();
559
                        }
560
                return false;
561
        }
562
        @Override
563
        public String constructUri(TreeItem treeItem, String path){
564
                String constructedUri = "";
565
                if(containsFolder(treeItem, "My Shared")){
566
                        //case: selected folders below My Shared folder
567
                        String partialUri = constructPartialPath(treeItem);
568
                        constructedUri = constructedUri + "Files/shared/" + partialUri;
569
                        return constructedUri;
570
                }else if(containsFolder(treeItem, "Other's Shared")){
571
                        //case: selected folders below Other's Shared folder
572
                        String partialPath = constructPartialPath(treeItem);
573
                        constructedUri = constructedUri + "Files/others/"+ partialPath;
574
                        return constructedUri;
575
                }
576
                else if(getParentURI()==null){
577
                        if(containsFolder(treeItem, "Trash")){
578
                                //case: selected folders below Trash folder
579
                                String partialUri = constructPartialPath(treeItem);
580
                                constructedUri = constructedUri + "Files/trash/" + partialUri;
581
                                return constructedUri;
582
                        }
583
                        //case: home folder is selected
584
                        constructedUri = constructedUri + "Files/files/" + getName();
585
                        return constructedUri;
586
                }
587
                else if(treeItem.getParentItem() == null){
588
                        //this is the case when the user uses the browser's forward arrow to navigate through other's
589
                        //shared folders and item.getParentItem is null only inside other's shared folder
590
                        String apiPath = GSS.get().getApiPath();
591
                        String newPath = getParentURI().substring(apiPath.lastIndexOf("/"));
592
                        constructedUri = constructedUri + "Files"+ newPath + getName();
593
                        return constructedUri;
594
                }
595
                else{
596
                        String finalUri = getParentURI().substring(path.lastIndexOf("/")) + getName();
597
                        constructedUri = constructedUri + "Files"+ finalUri;
598
                        return constructedUri;
599
                }
600

    
601
        }
602

    
603
        /**
604
         * Retrieve the readForAll.
605
         *
606
         * @return the readForAll
607
         */
608
        public boolean isReadForAll() {
609
                return readForAll;
610
        }
611
        /**
612
         * Modify the readForAll.
613
         *
614
         * @param newReadForAll the readForAll to set
615
         */
616
        public void setReadForAll(boolean newReadForAll) {
617
                readForAll = newReadForAll;
618
        }
619
        
620
        
621
        public int countNotDeletedSubfolders(){
622
                int count=0;
623
                for(FolderResource r : folders){
624
                        if(!r.isDeleted())
625
                                count++;
626
                }
627
                return count;
628
        }
629

    
630
}