Merge with 8a09c1ebdba3597db3331cfa487f1fca5c78690b
[pithos] / src / gr / ebs / gss / client / rest / resource / FileResource.java
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         @Override
122         public String getName() {
123                 return name;
124         }
125
126         /**
127          * Modify the name.
128          *
129          * @param aName the name to set
130          */
131         public void setName(String aName) {
132                 name = aName;
133         }
134
135         /**
136          * Retrieve the owner.
137          *
138          * @return the owner
139          */
140         public String getOwner() {
141                 return owner;
142         }
143
144         /**
145          * Modify the owner.
146          *
147          * @param newOwner the owner to set
148          */
149         public void setOwner(String newOwner) {
150                 owner = newOwner;
151         }
152
153         /**
154          * Retrieve the createdBy.
155          *
156          * @return the createdBy
157          */
158         public String getCreatedBy() {
159                 return createdBy;
160         }
161
162         /**
163          * Modify the createdBy.
164          *
165          * @param aCreatedBy the createdBy to set
166          */
167         public void setCreatedBy(String aCreatedBy) {
168                 createdBy = aCreatedBy;
169         }
170
171         /**
172          * Retrieve the modifiedBy.
173          *
174          * @return the modifiedBy
175          */
176         public String getModifiedBy() {
177                 return modifiedBy;
178         }
179
180         /**
181          * Modify the modifiedBy.
182          *
183          * @param aModifiedBy the modifiedBy to set
184          */
185         public void setModifiedBy(String aModifiedBy) {
186                 modifiedBy = aModifiedBy;
187         }
188
189         /**
190          * Retrieve the creationDate.
191          *
192          * @return the creationDate
193          */
194         public Date getCreationDate() {
195                 return creationDate;
196         }
197
198         /**
199          * Modify the creationDate.
200          *
201          * @param aCreationDate the creationDate to set
202          */
203         public void setCreationDate(Date aCreationDate) {
204                 creationDate = aCreationDate;
205         }
206
207         /**
208          * Retrieve the modificationDate.
209          *
210          * @return the modificationDate
211          */
212         public Date getModificationDate() {
213                 return modificationDate;
214         }
215
216         /**
217          * Modify the modificationDate.
218          *
219          * @param aModificationDate the modificationDate to set
220          */
221         public void setModificationDate(Date aModificationDate) {
222                 modificationDate = aModificationDate;
223         }
224
225         /**
226          * Retrieve the contentType.
227          *
228          * @return the contentType
229          */
230         public String getContentType() {
231                 return contentType;
232         }
233
234         /**
235          * Modify the contentType.
236          *
237          * @param newContentType the contentType to set
238          */
239         public void setContentType(String newContentType) {
240                 contentType = newContentType;
241         }
242
243         /**
244          * Retrieve the contentLength.
245          *
246          * @return the contentLength
247          */
248         public Long getContentLength() {
249                 return contentLength;
250         }
251
252         /**
253          * Modify the contentLength.
254          *
255          * @param newContentLength the contentLength to set
256          */
257         public void setContentLength(Long newContentLength) {
258                 contentLength = newContentLength;
259         }
260
261         /**
262          * Retrieve the readForAll.
263          *
264          * @return the readForAll
265          */
266         public boolean isReadForAll() {
267                 return readForAll;
268         }
269
270         /**
271          * Modify the readForAll.
272          *
273          * @param newReadForAll the readForAll to set
274          */
275         public void setReadForAll(boolean newReadForAll) {
276                 readForAll = newReadForAll;
277         }
278
279         /**
280          * Retrieve the versioned.
281          *
282          * @return the versioned
283          */
284         public boolean isVersioned() {
285                 return versioned;
286         }
287
288         /**
289          * Modify the versioned.
290          *
291          * @param newVersioned the versioned to set
292          */
293         public void setVersioned(boolean newVersioned) {
294                 versioned = newVersioned;
295         }
296
297         /**
298          * Retrieve the version.
299          *
300          * @return the version
301          */
302         public Integer getVersion() {
303                 return version;
304         }
305
306         /**
307          * Modify the version.
308          *
309          * @param aVersion the version to set
310          */
311         public void setVersion(Integer aVersion) {
312                 version = aVersion;
313         }
314
315         /**
316          * Retrieve the etag.
317          *
318          * @return the etag
319          */
320         public String getEtag() {
321                 return etag;
322         }
323
324         /**
325          * Modify the etag.
326          *
327          * @param anEtag the etag to set
328          */
329         public void setEtag(String anEtag) {
330                 etag = anEtag;
331         }
332
333         /**
334          * Retrieve the tags.
335          *
336          * @return the tags
337          */
338         public List<String> getTags() {
339                 return tags;
340         }
341
342         /**
343          * Modify the tags.
344          *
345          * @param newTags the tags to set
346          */
347         public void setTags(List<String> newTags) {
348                 tags = newTags;
349         }
350
351         /**
352          * Retrieve the permissions.
353          *
354          * @return the permissions
355          */
356         public Set<PermissionHolder> getPermissions() {
357                 return permissions;
358         }
359
360         /**
361          * Modify the permissions.
362          *
363          * @param newPermissions the permissions to set
364          */
365         public void setPermissions(Set<PermissionHolder> newPermissions) {
366                 permissions = newPermissions;
367         }
368
369         /**
370          * Retrieve the deleted.
371          *
372          * @return the deleted
373          */
374         public boolean isDeleted() {
375                 return deleted;
376         }
377
378         /**
379          * Modify the deleted.
380          *
381          * @param newDeleted the deleted to set
382          */
383         public void setDeleted(boolean newDeleted) {
384                 deleted = newDeleted;
385         }
386
387         /**
388          * Retrieve the folderURI.
389          *
390          * @return the folderURI
391          */
392         public String getFolderURI() {
393                 return folderURI;
394         }
395
396         /**
397          * Modify the folderURI.
398          *
399          * @param aFolderURI the folderURI to set
400          */
401         public void setFolderURI(String aFolderURI) {
402                 folderURI = aFolderURI;
403         }
404
405         @Override
406         public void createFromJSON(String text) {
407                 JSONObject metadata = (JSONObject) JSONParser.parse(text);
408                 name = unmarshallString(metadata, "name");
409                 name = URL.decodeComponent(name);
410                 path = unmarshallString(metadata, "path");
411                 path = URL.decodeComponent(path);
412                 owner = unmarshallString(metadata, "owner");
413                 contentType = unmarshallString(metadata, "content");
414                 readForAll = unmarshallBoolean(metadata, "readForAll");
415                 versioned = unmarshallBoolean(metadata, "versioned");
416                 createdBy = unmarshallString(metadata, "createdBy");
417                 modifiedBy = unmarshallString(metadata, "modifiedBy");
418
419                 if (metadata.get("version") != null)
420                         version = new Integer(metadata.get("version").toString());
421
422                 deleted = unmarshallBoolean(metadata, "deleted");
423                 if (deleted)
424                         GWT.log("FOUND A DELETED FILE:" + name, null);
425
426                 if (metadata.get("folder") != null) {
427                         JSONObject folder = metadata.get("folder").isObject();
428                         folderURI = unmarshallString(folder, "uri");
429                         folderName = unmarshallString(folder, "name");
430                         if(folderName != null)
431                                 folderName = URL.decodeComponent(folderName);
432                 }
433
434                 if (metadata.get("permissions") != null) {
435                         JSONArray perm = metadata.get("permissions").isArray();
436                         if (perm != null)
437                                 for (int i = 0; i < perm.size(); i++) {
438                                         JSONObject obj = perm.get(i).isObject();
439                                         if (obj != null) {
440                                                 PermissionHolder permission = new PermissionHolder();
441                                                 if (obj.get("user") != null)
442                                                         permission.setUser(unmarshallString(obj, "user"));
443                                                 if (obj.get("group") != null) {
444                                                         String group = unmarshallString(obj, "group");
445                                                         group = URL.decodeComponent(group);
446                                                         permission.setGroup(group);
447                                                 }
448                                                 permission.setRead(unmarshallBoolean(obj, "read"));
449                                                 permission.setWrite(unmarshallBoolean(obj, "write"));
450                                                 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
451                                                 permissions.add(permission);
452                                         }
453                                 }
454
455                 }
456                 if (metadata.get("tags") != null) {
457                         JSONArray perm = metadata.get("tags").isArray();
458                         if (perm != null)
459                                 for (int i = 0; i < perm.size(); i++) {
460                                         JSONString obj = perm.get(i).isString();
461                                         if(obj != null)
462                                                 tags.add(URL.decodeComponent(obj.stringValue()));
463                                 }
464                 }
465                 if (metadata.get("creationDate") != null)
466                         creationDate = new Date(new Long(metadata.get("creationDate").toString()));
467                 if (metadata.get("modificationDate") != null)
468                         modificationDate = new Date(new Long(metadata.get("modificationDate").toString()));
469                 if (metadata.get("size") != null)
470                         contentLength = Long.parseLong(metadata.get("size").toString());
471         }
472
473         /**
474          * Return the file size in a humanly readable form, using SI units to denote
475          * size information, e.g. 1 KB = 1000 B (bytes).
476          *
477          * @return the fileSize
478          */
479         public String getFileSizeAsString() {
480                 return getFileSizeAsString(contentLength);
481         }
482
483         /**
484          * Return the given size in a humanly readable form, using SI units to denote
485          * size information, e.g. 1 KB = 1000 B (bytes).
486          *
487          * @param size in bytes
488          * @return the size in human readable string
489          */
490         public static String getFileSizeAsString(long size) {
491                 if (size < 1024)
492                         return String.valueOf(size) + " B";
493                 else if (size < 1024 * 1024)
494                         return getSize(size, 1024D) + " KB";
495                 else if (size < 1024 * 1024 * 1024)
496                         return getSize(size, (1024D * 1024D)) + " MB";
497                 return getSize(size, (1024D * 1024D * 1024D)) + " GB";
498         }
499
500         private static String getSize(Long size, Double division) {
501                 Double res = Double.valueOf(size.toString()) / division;
502                 NumberFormat nf = NumberFormat.getFormat("######.#");
503                 return nf.format(res);
504         }
505
506         public boolean isShared(){
507                 if (isReadForAll())
508                         return true;
509                 if(permissions != null)
510                         for(PermissionHolder perm : permissions){
511                                 if(perm.getUser() != null && !owner.equals(perm.getUser()))
512                                         return true;
513                                 if(perm.getGroup() != null)
514                                         return true;
515                         }
516                 return false;
517         }
518
519         public boolean isShared(String ownerUser){
520                 GWT.log("OWNER USER:"+ownerUser, null);
521                 if (isReadForAll())
522                         return true;
523                 if(permissions != null)
524                         for(PermissionHolder perm : permissions){
525                                 if(perm.getUser() != null && !ownerUser.equals(perm.getUser()))
526                                         return true;
527                                 if(perm.getGroup() != null)
528                                         return true;
529                         }
530                 return false;
531         }
532
533         @Override
534         public String getLastModifiedSince() {
535                 if(modificationDate != null)
536                         return getDate(modificationDate.getTime());
537                 return null;
538         }
539 }
540