Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / rest / resource / FileResource.java @ 555e8e59

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

    
21
import java.util.ArrayList;
22
import java.util.Date;
23
import java.util.HashSet;
24
import java.util.List;
25
import java.util.Set;
26

    
27
import com.google.gwt.core.client.GWT;
28
import com.google.gwt.http.client.URL;
29
import com.google.gwt.i18n.client.NumberFormat;
30
import com.google.gwt.json.client.JSONArray;
31
import com.google.gwt.json.client.JSONObject;
32
import com.google.gwt.json.client.JSONParser;
33
import com.google.gwt.json.client.JSONString;
34

    
35
/**
36
 * @author kman
37
 */
38
public class FileResource extends RestResource {
39

    
40
        public FileResource(String aUri) {
41
                super(aUri);
42
        }
43

    
44
        String name;
45

    
46
        String owner;
47

    
48
        String createdBy;
49

    
50
        String modifiedBy;
51

    
52
        Date creationDate;
53

    
54
        Date modificationDate;
55

    
56
        String contentType;
57

    
58
        Long contentLength;
59

    
60
        boolean readForAll;
61

    
62
        boolean versioned;
63

    
64
        Integer version;
65

    
66
        String etag;
67

    
68
        boolean deleted = false;
69

    
70
        List<String> tags = new ArrayList<String>();
71

    
72
        Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
73

    
74
        String folderURI;
75

    
76
        String path;
77

    
78
        String folderName;
79

    
80
        /**
81
         * Retrieve the folderName.
82
         *
83
         * @return the folderName
84
         */
85
        public String getFolderName() {
86
                return folderName;
87
        }
88

    
89
        /**
90
         * Modify the folderName.
91
         *
92
         * @param aFolderName the folderName to set
93
         */
94
        public void setFolderName(String aFolderName) {
95
                folderName = aFolderName;
96
        }
97

    
98
        /**
99
         * Retrieve the path.
100
         *
101
         * @return the path
102
         */
103
        public String getPath() {
104
                return path;
105
        }
106

    
107
        /**
108
         * Modify the path.
109
         *
110
         * @param aPath the path to set
111
         */
112
        public void setPath(String aPath) {
113
                path = aPath;
114
        }
115

    
116
        /**
117
         * Retrieve the name.
118
         *
119
         * @return the name
120
         */
121
        public String getName() {
122
                return name;
123
        }
124

    
125
        /**
126
         * Modify the name.
127
         *
128
         * @param aName the name to set
129
         */
130
        public void setName(String aName) {
131
                name = aName;
132
        }
133

    
134
        /**
135
         * Retrieve the owner.
136
         *
137
         * @return the owner
138
         */
139
        public String getOwner() {
140
                return owner;
141
        }
142

    
143
        /**
144
         * Modify the owner.
145
         *
146
         * @param newOwner the owner to set
147
         */
148
        public void setOwner(String newOwner) {
149
                owner = newOwner;
150
        }
151

    
152
        /**
153
         * Retrieve the createdBy.
154
         *
155
         * @return the createdBy
156
         */
157
        public String getCreatedBy() {
158
                return createdBy;
159
        }
160

    
161
        /**
162
         * Modify the createdBy.
163
         *
164
         * @param aCreatedBy the createdBy to set
165
         */
166
        public void setCreatedBy(String aCreatedBy) {
167
                createdBy = aCreatedBy;
168
        }
169

    
170
        /**
171
         * Retrieve the modifiedBy.
172
         *
173
         * @return the modifiedBy
174
         */
175
        public String getModifiedBy() {
176
                return modifiedBy;
177
        }
178

    
179
        /**
180
         * Modify the modifiedBy.
181
         *
182
         * @param aModifiedBy the modifiedBy to set
183
         */
184
        public void setModifiedBy(String aModifiedBy) {
185
                modifiedBy = aModifiedBy;
186
        }
187

    
188
        /**
189
         * Retrieve the creationDate.
190
         *
191
         * @return the creationDate
192
         */
193
        public Date getCreationDate() {
194
                return creationDate;
195
        }
196

    
197
        /**
198
         * Modify the creationDate.
199
         *
200
         * @param aCreationDate the creationDate to set
201
         */
202
        public void setCreationDate(Date aCreationDate) {
203
                creationDate = aCreationDate;
204
        }
205

    
206
        /**
207
         * Retrieve the modificationDate.
208
         *
209
         * @return the modificationDate
210
         */
211
        public Date getModificationDate() {
212
                return modificationDate;
213
        }
214

    
215
        /**
216
         * Modify the modificationDate.
217
         *
218
         * @param aModificationDate the modificationDate to set
219
         */
220
        public void setModificationDate(Date aModificationDate) {
221
                modificationDate = aModificationDate;
222
        }
223

    
224
        /**
225
         * Retrieve the contentType.
226
         *
227
         * @return the contentType
228
         */
229
        public String getContentType() {
230
                return contentType;
231
        }
232

    
233
        /**
234
         * Modify the contentType.
235
         *
236
         * @param newContentType the contentType to set
237
         */
238
        public void setContentType(String newContentType) {
239
                contentType = newContentType;
240
        }
241

    
242
        /**
243
         * Retrieve the contentLength.
244
         *
245
         * @return the contentLength
246
         */
247
        public Long getContentLength() {
248
                return contentLength;
249
        }
250

    
251
        /**
252
         * Modify the contentLength.
253
         *
254
         * @param newContentLength the contentLength to set
255
         */
256
        public void setContentLength(Long newContentLength) {
257
                contentLength = newContentLength;
258
        }
259

    
260
        /**
261
         * Retrieve the readForAll.
262
         *
263
         * @return the readForAll
264
         */
265
        public boolean isReadForAll() {
266
                return readForAll;
267
        }
268

    
269
        /**
270
         * Modify the readForAll.
271
         *
272
         * @param newReadForAll the readForAll to set
273
         */
274
        public void setReadForAll(boolean newReadForAll) {
275
                readForAll = newReadForAll;
276
        }
277

    
278
        /**
279
         * Retrieve the versioned.
280
         *
281
         * @return the versioned
282
         */
283
        public boolean isVersioned() {
284
                return versioned;
285
        }
286

    
287
        /**
288
         * Modify the versioned.
289
         *
290
         * @param newVersioned the versioned to set
291
         */
292
        public void setVersioned(boolean newVersioned) {
293
                versioned = newVersioned;
294
        }
295

    
296
        /**
297
         * Retrieve the version.
298
         *
299
         * @return the version
300
         */
301
        public Integer getVersion() {
302
                return version;
303
        }
304

    
305
        /**
306
         * Modify the version.
307
         *
308
         * @param aVersion the version to set
309
         */
310
        public void setVersion(Integer aVersion) {
311
                version = aVersion;
312
        }
313

    
314
        /**
315
         * Retrieve the etag.
316
         *
317
         * @return the etag
318
         */
319
        public String getEtag() {
320
                return etag;
321
        }
322

    
323
        /**
324
         * Modify the etag.
325
         *
326
         * @param anEtag the etag to set
327
         */
328
        public void setEtag(String anEtag) {
329
                etag = anEtag;
330
        }
331

    
332
        /**
333
         * Retrieve the tags.
334
         *
335
         * @return the tags
336
         */
337
        public List<String> getTags() {
338
                return tags;
339
        }
340

    
341
        /**
342
         * Modify the tags.
343
         *
344
         * @param newTags the tags to set
345
         */
346
        public void setTags(List<String> newTags) {
347
                tags = newTags;
348
        }
349

    
350
        /**
351
         * Retrieve the permissions.
352
         *
353
         * @return the permissions
354
         */
355
        public Set<PermissionHolder> getPermissions() {
356
                return permissions;
357
        }
358

    
359
        /**
360
         * Modify the permissions.
361
         *
362
         * @param newPermissions the permissions to set
363
         */
364
        public void setPermissions(Set<PermissionHolder> newPermissions) {
365
                permissions = newPermissions;
366
        }
367

    
368
        /**
369
         * Retrieve the deleted.
370
         *
371
         * @return the deleted
372
         */
373
        public boolean isDeleted() {
374
                return deleted;
375
        }
376

    
377
        /**
378
         * Modify the deleted.
379
         *
380
         * @param newDeleted the deleted to set
381
         */
382
        public void setDeleted(boolean newDeleted) {
383
                deleted = newDeleted;
384
        }
385

    
386
        /**
387
         * Retrieve the folderURI.
388
         *
389
         * @return the folderURI
390
         */
391
        public String getFolderURI() {
392
                return folderURI;
393
        }
394

    
395
        /**
396
         * Modify the folderURI.
397
         *
398
         * @param aFolderURI the folderURI to set
399
         */
400
        public void setFolderURI(String aFolderURI) {
401
                folderURI = aFolderURI;
402
        }
403

    
404
        @Override
405
        public void createFromJSON(String text) {
406
                JSONObject metadata = (JSONObject) JSONParser.parse(text);
407
                name = unmarshallString(metadata, "name");
408
                name = URL.decodeComponent(name);
409
                path = unmarshallString(metadata, "path");
410
                path = URL.decodeComponent(path);
411
                folderName = unmarshallString(metadata, "folderName");
412
                if(folderName != null)
413
                        folderName = URL.decodeComponent(folderName);
414
                owner = unmarshallString(metadata, "owner");
415
                contentType = unmarshallString(metadata, "content");
416
                readForAll = unmarshallBoolean(metadata, "readForAll");
417
                versioned = unmarshallBoolean(metadata, "versioned");
418

    
419
                if (metadata.get("version") != null)
420
                        version = new Integer(metadata.get("version").toString());
421

    
422
                folderURI = unmarshallString(metadata, "folder");
423
                deleted = unmarshallBoolean(metadata, "deleted");
424
                if (deleted)
425
                        GWT.log("FOUND A DELETED FILE:" + name, null);
426

    
427
                if (metadata.get("permissions") != null) {
428
                        JSONArray perm = metadata.get("permissions").isArray();
429
                        if (perm != null)
430
                                for (int i = 0; i < perm.size(); i++) {
431
                                        JSONObject obj = perm.get(i).isObject();
432
                                        if (obj != null) {
433
                                                PermissionHolder permission = new PermissionHolder();
434
                                                if (obj.get("user") != null)
435
                                                        permission.setUser(unmarshallString(obj, "user"));
436
                                                if (obj.get("group") != null)
437
                                                        permission.setGroup(unmarshallString(obj, "group"));
438
                                                permission.setRead(unmarshallBoolean(obj, "read"));
439
                                                permission.setWrite(unmarshallBoolean(obj, "write"));
440
                                                permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
441
                                                permissions.add(permission);
442
                                        }
443
                                }
444

    
445
                }
446
                if (metadata.get("tags") != null) {
447
                        JSONArray perm = metadata.get("tags").isArray();
448
                        if (perm != null)
449
                                for (int i = 0; i < perm.size(); i++) {
450
                                        JSONString obj = perm.get(i).isString();
451
                                        if(obj != null)
452
                                                tags.add(obj.stringValue());
453
                                }
454
                }
455
                if (metadata.get("creationDate") != null)
456
                        creationDate = new Date(new Long(metadata.get("creationDate").toString()));
457
                if (metadata.get("modificationDate") != null)
458
                        modificationDate = new Date(new Long(metadata.get("modificationDate").toString()));
459
                if (metadata.get("size") != null)
460
                        contentLength = Long.parseLong(metadata.get("size").toString());
461
        }
462

    
463
        /**
464
         * Return the file size in a humanly readable form, using SI units to denote
465
         * size information, e.g. 1 KB = 1000 B (bytes).
466
         *
467
         * @return the fileSize
468
         */
469
        public String getFileSizeAsString() {
470
                return getFileSizeAsString(contentLength);
471
        }
472

    
473
        /**
474
         * Return the given size in a humanly readable form, using SI units to denote
475
         * size information, e.g. 1 KB = 1000 B (bytes).
476
         *
477
         * @param size in bytes
478
         * @return the size in human readable string
479
         */
480
        public static String getFileSizeAsString(long size) {
481
                if (size < 1024)
482
                        return String.valueOf(size) + " B";
483
                else if (size <= 1024 * 1024)
484
                        return getSize(size, 1024D) + " KB";
485
                else if (size <= 1024 * 1024 * 1024)
486
                        return getSize(size, (1024D * 1024D)) + " MB";
487
                return getSize(size, (1024D * 1024D * 1024D)) + " GB";
488
        }
489

    
490
        private static String getSize(Long size, Double division) {
491
                Double res = Double.valueOf(size.toString()) / division;
492
                NumberFormat nf = NumberFormat.getFormat("######.#");
493
                return nf.format(res);
494
        }
495
}