Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / resource / FolderResource.java @ bddcde7b

History | View | Annotate | Download (14.5 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 gr.ebs.gss.client.rest.resource;
21

    
22
import gr.ebs.gss.client.DisplayHelper;
23
import gr.ebs.gss.client.GSS;
24
import gr.ebs.gss.client.rest.MultipleGetCommand;
25
import gr.ebs.gss.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
        /**
83
         * Modify the parentName.
84
         *
85
         * @param aParentName the parentName to set
86
         */
87
        public void setParentName(String aParentName) {
88
                parentName = aParentName;
89
        }
90

    
91
        /**
92
         * Retrieve the name.
93
         *
94
         * @return the name
95
         */
96
        @Override
97
        public String getName() {
98
                return name;
99
        }
100

    
101
        /**
102
         * Modify the name.
103
         *
104
         * @param aName the name to set
105
         */
106
        public void setName(String aName) {
107
                name = aName;
108
        }
109

    
110
        /**
111
         * Retrieve the owner.
112
         *
113
         * @return the owner
114
         */
115
        public String getOwner() {
116
                return owner;
117
        }
118

    
119
        /**
120
         * Modify the owner.
121
         *
122
         * @param anOwner the owner to set
123
         */
124
        public void setOwner(String anOwner) {
125
                owner = anOwner;
126
        }
127

    
128
        /**
129
         * Retrieve the createdBy.
130
         *
131
         * @return the createdBy
132
         */
133
        public String getCreatedBy() {
134
                return createdBy;
135
        }
136

    
137
        /**
138
         * Modify the createdBy.
139
         *
140
         * @param aCreatedBy the createdBy to set
141
         */
142
        public void setCreatedBy(String aCreatedBy) {
143
                createdBy = aCreatedBy;
144
        }
145

    
146
        /**
147
         * Retrieve the modifiedBy.
148
         *
149
         * @return the modifiedBy
150
         */
151
        public String getModifiedBy() {
152
                return modifiedBy;
153
        }
154

    
155
        /**
156
         * Modify the modifiedBy.
157
         *
158
         * @param aModifiedBy the modifiedBy to set
159
         */
160
        public void setModifiedBy(String aModifiedBy) {
161
                modifiedBy = aModifiedBy;
162
        }
163

    
164
        /**
165
         * Retrieve the creationDate.
166
         *
167
         * @return the creationDate
168
         */
169
        public Date getCreationDate() {
170
                return creationDate;
171
        }
172

    
173
        /**
174
         * Modify the creationDate.
175
         *
176
         * @param aCreationDate the creationDate to set
177
         */
178
        public void setCreationDate(Date aCreationDate) {
179
                creationDate = aCreationDate;
180
        }
181

    
182
        /**
183
         * Retrieve the modificationDate.
184
         *
185
         * @return the modificationDate
186
         */
187
        public Date getModificationDate() {
188
                return modificationDate;
189
        }
190

    
191
        /**
192
         * Modify the modificationDate.
193
         *
194
         * @param aModificationDate the modificationDate to set
195
         */
196
        public void setModificationDate(Date aModificationDate) {
197
                modificationDate = aModificationDate;
198
        }
199

    
200
        /**
201
         * Retrieve the filePaths.
202
         *
203
         * @return the filePaths
204
         */
205
        public List<String> getFilePaths() {
206
                return filePaths;
207
        }
208

    
209
        /**
210
         * Modify the filePaths.
211
         *
212
         * @param newFilePaths the filePaths to set
213
         */
214
        public void setFilePaths(List<String> newFilePaths) {
215
                filePaths = newFilePaths;
216
        }
217

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

    
227
        /**
228
         * Modify the subfolderPaths.
229
         *
230
         * @param newSubfolderPaths the subfolderPaths to set
231
         */
232
        public void setSubfolderPaths(List<String> newSubfolderPaths) {
233
                subfolderPaths = newSubfolderPaths;
234
        }
235

    
236
        /**
237
         * Retrieve the permissions.
238
         *
239
         * @return the permissions
240
         */
241
        public Set<PermissionHolder> getPermissions() {
242
                return permissions;
243
        }
244

    
245
        /**
246
         * Modify the permissions.
247
         *
248
         * @param newPermissions the permissions to set
249
         */
250
        public void setPermissions(Set<PermissionHolder> newPermissions) {
251
                permissions = newPermissions;
252
        }
253

    
254
        /**
255
         * Retrieve the deleted.
256
         *
257
         * @return the deleted
258
         */
259
        public boolean isDeleted() {
260
                return deleted;
261
        }
262

    
263
        /**
264
         * Modify the deleted.
265
         *
266
         * @param newDeleted the deleted to set
267
         */
268
        public void setDeleted(boolean newDeleted) {
269
                deleted = newDeleted;
270
        }
271

    
272
        public void removeSubfolderPath(String spath) {
273
                if (subfolderPaths.remove(spath))
274
                        return;
275
                else if (subfolderPaths.remove(spath + "/"))
276
                        return;
277
                else
278
                        subfolderPaths.remove(spath.substring(0, spath.length() - 1));
279
        }
280

    
281
        /**
282
         * Retrieve the folders.
283
         *
284
         * @return the folders
285
         */
286
        public List<FolderResource> getFolders() {
287
                return folders;
288
        }
289

    
290
        /**
291
         * Modify the folders.
292
         *
293
         * @param newFolders the folders to set
294
         */
295
        public void setFolders(List<FolderResource> newFolders) {
296
                folders = newFolders;
297
        }
298

    
299
        /**
300
         * Retrieve the files.
301
         *
302
         * @return the files
303
         */
304
        public List<FileResource> getFiles() {
305
                return files;
306
        }
307

    
308
        /**
309
         * Modify the files.
310
         *
311
         * @param newFiles the files to set
312
         */
313
        public void setFiles(List<FileResource> newFiles) {
314
                files = newFiles;
315
        }
316

    
317
        /**
318
         * Retrieve the parentURI.
319
         *
320
         * @return the parentURI
321
         */
322
        public String getParentURI() {
323
                return parentURI;
324
        }
325

    
326
        /**
327
         * Modify the parentURI.
328
         *
329
         * @param aParentURI the parentURI to set
330
         */
331
        public void setParentURI(String aParentURI) {
332
                parentURI = aParentURI;
333
        }
334

    
335
        @Override
336
        public void createFromJSON(String text) {
337
                JSONObject json = (JSONObject) JSONParser.parse(text);
338
                name = unmarshallString(json, "name");
339
                owner = unmarshallString(json, "owner");
340
                createdBy = unmarshallString(json, "createdBy");
341
                modifiedBy = unmarshallString(json, "modifiedBy");
342
                deleted = unmarshallBoolean(json, "deleted");
343
                if (deleted)
344
                        GWT.log("FOUND A DELETED FOLDER:" + name, null);
345

    
346
                if (json.get("parent") != null) {
347
                        JSONObject parent = json.get("parent").isObject();
348
                        parentURI = unmarshallString(parent, "uri");
349
                        parentName = unmarshallString(parent, "name");
350
                }
351

    
352
                if (json.get("permissions") != null) {
353
                        JSONArray perm = json.get("permissions").isArray();
354
                        if (perm != null)
355
                                for (int i = 0; i < perm.size(); i++) {
356
                                        JSONObject obj = perm.get(i).isObject();
357
                                        if (obj != null) {
358
                                                PermissionHolder permission = new PermissionHolder();
359
                                                if (obj.get("user") != null)
360
                                                        permission.setUser(unmarshallString(obj, "user"));
361
                                                if (obj.get("group") != null)
362
                                                        permission.setGroup(URL.decodeComponent(unmarshallString(obj, "group")));
363
                                                permission.setRead(unmarshallBoolean(obj, "read"));
364
                                                permission.setWrite(unmarshallBoolean(obj, "write"));
365
                                                permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
366
                                                permissions.add(permission);
367
                                        }
368
                                }
369
                }
370
                if (json.get("folders") != null) {
371
                        JSONArray subs = json.get("folders").isArray();
372
                        if (subs != null)
373
                                for (int i = 0; i < subs.size(); i++) {
374
                                        JSONObject so = subs.get(i).isObject();
375
                                        if (so != null) {
376
                                                String subUri = unmarshallString(so, "uri");
377
                                                String subName = unmarshallString(so, "name");
378
                                                if (subUri != null && subName != null) {
379
                                                        if (!subUri.endsWith("/"))
380
                                                                subUri = subUri + "/";
381
                                                        FolderResource sub = new FolderResource(subUri);
382
                                                        sub.setName(subName);
383
                                                        sub.setParentURI(uri);
384
                                                        sub.setParentName(name);
385
                                                        sub.setNeedsExpanding(true);
386
                                                        folders.add(sub);
387
                                                        subfolderPaths.add(subUri);
388
                                                }
389
                                        }
390
                                }
391
                }
392
                if (json.get("files") != null) {
393
                        JSONArray subs = json.get("files").isArray();
394
                        if (subs != null)
395
                                for (int i = 0; i < subs.size(); i++) {
396
                                        JSONObject fo = subs.get(i).isObject();
397
                                        if (fo != null) {
398
                                                String fname = unmarshallString(fo, "name");
399
                                                String fowner = unmarshallString(fo, "owner");
400
                                                String fcontent = unmarshallString(fo, "content");
401
                                                String fpath = unmarshallString(fo, "path");
402
                                                fpath = URL.decodeComponent(fpath);
403
                                                Integer fversion = null;
404
                                                if (fo.get("version") != null)
405
                                                        fversion = new Integer(fo.get("version").toString());
406
                                                boolean fdeleted = unmarshallBoolean(fo, "deleted");
407
                                                Date fcreationDate = null;
408
                                                if (fo.get("creationDate") != null)
409
                                                        fcreationDate = new Date(new Long(fo.get("creationDate").toString()));
410
                                                Date fmodificationDate = null;
411
                                                if (fo.get("modificationDate") != null)
412
                                                        fmodificationDate = new Date(new Long(fo.get("modificationDate").toString()));
413
                                                String furi = unmarshallString(fo, "uri");
414
                                                Long fsize = 0L;
415
                                                if (fo.get("size") != null)
416
                                                        fsize = new Long(fo.get("size").toString());
417
                                                filePaths.add(furi);
418
                                                FileResource fs = new FileResource(furi);
419
                                                fs.setName(fname);
420
                                                fs.setOwner(fowner);
421
                                                fs.setPath(fpath);
422
                                                fs.setVersion(fversion);
423
                                                fs.setContentLength(fsize);
424
                                                fs.setDeleted(fdeleted);
425
                                                fs.setCreationDate(fcreationDate);
426
                                                fs.setModificationDate(fmodificationDate);
427
                                                fs.setContentType(fcontent);
428
                                                files.add(fs);
429
                                        }
430
                                }
431
                }
432
                if (json.get("creationDate") != null)
433
                        creationDate = new Date(new Long(json.get("creationDate").toString()));
434
                if (json.get("modificationDate") != null)
435
                        modificationDate = new Date(new Long(json.get("modificationDate").toString()));
436
        }
437

    
438
        public String getParentName(){
439
                return parentName;
440
        }
441

    
442
        /**
443
         * Retrieve the needsExpanding.
444
         *
445
         * @return the needsExpanding
446
         */
447
        public boolean isNeedsExpanding() {
448
                return needsExpanding;
449
        }
450

    
451
        /**
452
         * Modify the needsExpanding.
453
         *
454
         * @param newNeedsExpanding the needsExpanding to set
455
         */
456
        public void setNeedsExpanding(boolean newNeedsExpanding) {
457
                needsExpanding = newNeedsExpanding;
458
        }
459

    
460
        public boolean isShared(){
461

    
462
                for(PermissionHolder perm : permissions){
463
                        if(perm.getUser() != null && !owner.equals(perm.getUser()))
464
                                return true;
465
                        if(perm.getGroup() != null)
466
                                return true;
467
                }
468
                return false;
469
        }
470

    
471
        @Override
472
        public String getLastModifiedSince() {
473
                if(modificationDate != null)
474
                        return getDate(modificationDate.getTime());
475
                return null;
476
        }
477

    
478
        public MultipleGetCommand.Cached[] getCache(){
479
                if(getSubfolderPaths().size() != getFolders().size()){
480
                        GWT.log("MISMATCH IN PATH AND FOLDERS SIZE", null);
481
                        return null;
482
                }
483
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getSubfolderPaths().size()];
484
                for(int i=0; i<getFolders().size();i++){
485
                        FolderResource r = getFolders().get(i);
486
                        Cached c = new Cached();
487
                        c.cache=r;
488
                        c.uri=r.uri;
489
                        result[i] = c;
490
                }
491
                return result;
492
        }
493

    
494
        public MultipleGetCommand.Cached[] getFileCache(){
495
                if(getFilePaths().size() != getFiles().size()){
496
                        GWT.log("MISMATCH IN PATH AND FILES SIZE", null);
497
                        return null;
498
                }
499
                if(!filesExpanded)
500
                        return null;
501
                MultipleGetCommand.Cached[] result = new MultipleGetCommand.Cached[getFilePaths().size()];
502
                for(int i=0; i<getFiles().size();i++){
503
                        FileResource r = getFiles().get(i);
504
                        Cached c = new Cached();
505
                        c.cache=r;
506
                        c.uri=r.uri;
507
                        result[i] = c;
508
                }
509
                return result;
510
        }
511

    
512
        public void setFilesExpanded(boolean newFilesExpanded) {
513
                filesExpanded = newFilesExpanded;
514
        }
515
        /**
516
         * this method constructs the partial path of a given TreeItem using it's text
517
         *
518
         * @param selectedItem the selectedItem to check
519
         */
520
        private String constructPartialPath(TreeItem selectedItem){
521
           String result = DisplayHelper.trim(selectedItem.getText());
522
           TreeItem parent = selectedItem.getParentItem();
523
           while (!(DisplayHelper.trim(parent.getText()).equals("My Shared") || DisplayHelper.trim(parent.getText()).equals("Other's Shared")||DisplayHelper.trim(parent.getText()).equals("Trash"))){
524
              result = DisplayHelper.trim(parent.getText()) + "/" + result;
525
              if(result.equals("My Shared")||result.equals("Other's Shared")) return result;
526
              parent = parent.getParentItem();
527
           }
528

    
529
           return result;
530
        }
531
        /**
532
         * examine whether a folder name like "Trash", "My Shared", "Other's Shared" is inside path
533
         *
534
         * @param selectedItem the selectedTreeItem to check
535
         */
536

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

    
586
        }
587

    
588
}