Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.5 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.rest.MultipleGetCommand;
23
import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
24

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

    
32
import com.google.gwt.core.client.GWT;
33
import com.google.gwt.http.client.URL;
34
import com.google.gwt.json.client.JSONArray;
35
import com.google.gwt.json.client.JSONObject;
36
import com.google.gwt.json.client.JSONParser;
37

    
38
/**
39
 * @author kman
40
 */
41
public class FolderResource extends RestResource {
42

    
43
        public FolderResource(String aUri) {
44
                super(aUri);
45
        }
46

    
47
        String name;
48

    
49
        String owner;
50

    
51
        String createdBy;
52

    
53
        String modifiedBy;
54

    
55
        Date creationDate;
56

    
57
        Date modificationDate;
58

    
59
        List<String> filePaths = new LinkedList<String>();
60

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

    
63
        Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
64

    
65
        List<FolderResource> folders = new ArrayList<FolderResource>();
66

    
67
        List<FileResource> files = new ArrayList<FileResource>();
68

    
69
        String parentURI;
70

    
71
        boolean deleted = false;
72

    
73
        boolean needsExpanding = false;
74

    
75
        String parentName;
76

    
77
        private boolean filesExpanded=false;
78

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
432
        public String getParentName(){
433
                return parentName;
434
        }
435

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

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

    
454
        public boolean isShared(){
455

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

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

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

    
486
                return result;
487
        }
488

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

    
505
                return result;
506
        }
507

    
508

    
509
        public void setFilesExpanded(boolean filesExpanded) {
510
                this.filesExpanded = filesExpanded;
511
        }
512

    
513

    
514
}