Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (13.3 kB)

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 gr.ebs.gss.client.GSS;
23
import gr.ebs.gss.client.rest.MultipleGetCommand;
24
import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
25

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

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

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

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

    
49
        String name;
50

    
51
        String owner;
52

    
53
        String createdBy;
54

    
55
        String modifiedBy;
56

    
57
        Date creationDate;
58

    
59
        Date modificationDate;
60

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

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

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

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

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

    
71
        String parentURI;
72

    
73
        boolean deleted = false;
74

    
75
        boolean needsExpanding = false;
76

    
77
        String parentName;
78

    
79
        private boolean filesExpanded=false;
80

    
81
        /**
82
         * Modify the parentName.
83
         *
84
         * @param aParentName the parentName to set
85
         */
86
        public void setParentName(String aParentName) {
87
                parentName = aParentName;
88
        }
89

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
342
                if (json.get("parent") != null) {
343
                        JSONObject parent = json.get("parent").isObject();
344
                        parentURI = unmarshallString(parent, "uri");
345
                        parentName = unmarshallString(parent, "name");
346
                        if(parentName != null)
347
                                parentName = URL.decodeComponent(parentName);
348
                }
349

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

    
434
        public String getParentName(){
435
                return parentName;
436
        }
437

    
438
        /**
439
         * Retrieve the needsExpanding.
440
         *
441
         * @return the needsExpanding
442
         */
443
        public boolean isNeedsExpanding() {
444
                return needsExpanding;
445
        }
446

    
447
        /**
448
         * Modify the needsExpanding.
449
         *
450
         * @param newNeedsExpanding the needsExpanding to set
451
         */
452
        public void setNeedsExpanding(boolean newNeedsExpanding) {
453
                needsExpanding = newNeedsExpanding;
454
        }
455

    
456
        public boolean isShared(){
457

    
458
                for(PermissionHolder perm : permissions){
459
                        if(perm.getUser() != null && !owner.equals(perm.getUser()))
460
                                return true;
461
                        if(perm.getGroup() != null)
462
                                return true;
463
                }
464
                return false;
465
        }
466

    
467
        @Override
468
        public String getLastModifiedSince() {
469
                if(modificationDate != null)
470
                        return getDate(modificationDate.getTime());
471
                return null;
472
        }
473

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

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

    
508
        public void setFilesExpanded(boolean newFilesExpanded) {
509
                filesExpanded = newFilesExpanded;
510
        }
511

    
512
        @Override
513
        public void updateHistoryAbs(TreeItem item, String path){
514
                try{
515
                        if(getParentURI() == null){
516
                                if(GSS.get().getFolders().getPopupTree().containsFolder(item, "Trash")){
517
//                                        case: selected folders below Trash folder
518
                                        String partialUri = GSS.get().getFolders().getPopupTree().constructPartialPath(item);
519
                                        GSS.get().updateHistory("Files/trash/" + partialUri, item);
520
                                } else
521
//                                        case: home folders are selected
522
                                        GSS.get().updateHistory("Files/files/" + getName(), item);
523
                        }
524
                        else if(item.getParentItem() == null){
525
//                                this is the case when the user uses the browser's forward arrow to navigate through other's
526
//                                shared folders and        item.getParentItem is null only inside other's shared folder
527
                                String apiPath = GSS.get().getApiPath();
528
                                String newPath = getParentURI().substring(apiPath.lastIndexOf("/"));
529
                                GSS.get().updateHistory("Files"+ newPath + getName(), item);
530
                        }
531
                        else if(GSS.get().getFolders().getPopupTree().containsFolder(item, "My Shared")){
532
//                                case: selected folders below My Shared folder
533
                                String partialUri = GSS.get().getFolders().getPopupTree().constructPartialPath(item);
534
                                GSS.get().updateHistory("Files/shared/" + partialUri, item);
535
                        }else if(GSS.get().getFolders().getPopupTree().containsFolder(item, "Other's Shared")){
536
//                                case: selected folders below Other's Shared folder
537
                                String partialPath = GSS.get().getFolders().getPopupTree().constructPartialPath(item);
538
                                GSS.get().updateHistory("Files/others/"+ partialPath, item);
539
                        }
540
                        else{
541
//                                case:all folders in user's folders tree
542
                                String finalUri = getParentURI().substring(path.lastIndexOf("/")) + getName();
543
                                GSS.get().updateHistory("Files"+ finalUri, item);
544
                        }
545

    
546
                }catch (Exception e){
547
                        throw new UnsupportedOperationException(e);
548
                }
549

    
550
        }
551

    
552
}