2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.rest.resource;
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.List;
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;
38 public class FileResource extends RestResource {
40 public FileResource(String aUri) {
54 Date modificationDate;
68 boolean deleted = false;
70 List<String> tags = new ArrayList<String>();
72 Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
81 * Retrieve the folderName.
83 * @return the folderName
85 public String getFolderName() {
90 * Modify the folderName.
92 * @param aFolderName the folderName to set
94 public void setFolderName(String aFolderName) {
95 folderName = aFolderName;
103 public String getPath() {
110 * @param aPath the path to set
112 public void setPath(String aPath) {
121 public String getName() {
128 * @param aName the name to set
130 public void setName(String aName) {
135 * Retrieve the owner.
139 public String getOwner() {
146 * @param newOwner the owner to set
148 public void setOwner(String newOwner) {
153 * Retrieve the createdBy.
155 * @return the createdBy
157 public String getCreatedBy() {
162 * Modify the createdBy.
164 * @param aCreatedBy the createdBy to set
166 public void setCreatedBy(String aCreatedBy) {
167 createdBy = aCreatedBy;
171 * Retrieve the modifiedBy.
173 * @return the modifiedBy
175 public String getModifiedBy() {
180 * Modify the modifiedBy.
182 * @param aModifiedBy the modifiedBy to set
184 public void setModifiedBy(String aModifiedBy) {
185 modifiedBy = aModifiedBy;
189 * Retrieve the creationDate.
191 * @return the creationDate
193 public Date getCreationDate() {
198 * Modify the creationDate.
200 * @param aCreationDate the creationDate to set
202 public void setCreationDate(Date aCreationDate) {
203 creationDate = aCreationDate;
207 * Retrieve the modificationDate.
209 * @return the modificationDate
211 public Date getModificationDate() {
212 return modificationDate;
216 * Modify the modificationDate.
218 * @param aModificationDate the modificationDate to set
220 public void setModificationDate(Date aModificationDate) {
221 modificationDate = aModificationDate;
225 * Retrieve the contentType.
227 * @return the contentType
229 public String getContentType() {
234 * Modify the contentType.
236 * @param newContentType the contentType to set
238 public void setContentType(String newContentType) {
239 contentType = newContentType;
243 * Retrieve the contentLength.
245 * @return the contentLength
247 public Long getContentLength() {
248 return contentLength;
252 * Modify the contentLength.
254 * @param newContentLength the contentLength to set
256 public void setContentLength(Long newContentLength) {
257 contentLength = newContentLength;
261 * Retrieve the readForAll.
263 * @return the readForAll
265 public boolean isReadForAll() {
270 * Modify the readForAll.
272 * @param newReadForAll the readForAll to set
274 public void setReadForAll(boolean newReadForAll) {
275 readForAll = newReadForAll;
279 * Retrieve the versioned.
281 * @return the versioned
283 public boolean isVersioned() {
288 * Modify the versioned.
290 * @param newVersioned the versioned to set
292 public void setVersioned(boolean newVersioned) {
293 versioned = newVersioned;
297 * Retrieve the version.
299 * @return the version
301 public Integer getVersion() {
306 * Modify the version.
308 * @param aVersion the version to set
310 public void setVersion(Integer aVersion) {
319 public String getEtag() {
326 * @param anEtag the etag to set
328 public void setEtag(String anEtag) {
337 public List<String> getTags() {
344 * @param newTags the tags to set
346 public void setTags(List<String> newTags) {
351 * Retrieve the permissions.
353 * @return the permissions
355 public Set<PermissionHolder> getPermissions() {
360 * Modify the permissions.
362 * @param newPermissions the permissions to set
364 public void setPermissions(Set<PermissionHolder> newPermissions) {
365 permissions = newPermissions;
369 * Retrieve the deleted.
371 * @return the deleted
373 public boolean isDeleted() {
378 * Modify the deleted.
380 * @param newDeleted the deleted to set
382 public void setDeleted(boolean newDeleted) {
383 deleted = newDeleted;
387 * Retrieve the folderURI.
389 * @return the folderURI
391 public String getFolderURI() {
396 * Modify the folderURI.
398 * @param aFolderURI the folderURI to set
400 public void setFolderURI(String aFolderURI) {
401 folderURI = aFolderURI;
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 owner = unmarshallString(metadata, "owner");
412 contentType = unmarshallString(metadata, "content");
413 readForAll = unmarshallBoolean(metadata, "readForAll");
414 versioned = unmarshallBoolean(metadata, "versioned");
416 if (metadata.get("version") != null)
417 version = new Integer(metadata.get("version").toString());
419 deleted = unmarshallBoolean(metadata, "deleted");
421 GWT.log("FOUND A DELETED FILE:" + name, null);
423 if (metadata.get("folder") != null) {
424 JSONObject folder = metadata.get("folder").isObject();
425 folderURI = unmarshallString(folder, "uri");
426 folderName = unmarshallString(folder, "name");
427 if(folderName != null)
428 folderName = URL.decodeComponent(folderName);
431 if (metadata.get("permissions") != null) {
432 JSONArray perm = metadata.get("permissions").isArray();
434 for (int i = 0; i < perm.size(); i++) {
435 JSONObject obj = perm.get(i).isObject();
437 PermissionHolder permission = new PermissionHolder();
438 if (obj.get("user") != null)
439 permission.setUser(unmarshallString(obj, "user"));
440 if (obj.get("group") != null) {
441 String group = unmarshallString(obj, "group");
442 group = URL.decodeComponent(group);
443 permission.setGroup(group);
445 permission.setRead(unmarshallBoolean(obj, "read"));
446 permission.setWrite(unmarshallBoolean(obj, "write"));
447 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
448 permissions.add(permission);
453 if (metadata.get("tags") != null) {
454 JSONArray perm = metadata.get("tags").isArray();
456 for (int i = 0; i < perm.size(); i++) {
457 JSONString obj = perm.get(i).isString();
459 tags.add(URL.decodeComponent(obj.stringValue()));
462 if (metadata.get("creationDate") != null)
463 creationDate = new Date(new Long(metadata.get("creationDate").toString()));
464 if (metadata.get("modificationDate") != null)
465 modificationDate = new Date(new Long(metadata.get("modificationDate").toString()));
466 if (metadata.get("size") != null)
467 contentLength = Long.parseLong(metadata.get("size").toString());
471 * Return the file size in a humanly readable form, using SI units to denote
472 * size information, e.g. 1 KB = 1000 B (bytes).
474 * @return the fileSize
476 public String getFileSizeAsString() {
477 return getFileSizeAsString(contentLength);
481 * Return the given size in a humanly readable form, using SI units to denote
482 * size information, e.g. 1 KB = 1000 B (bytes).
484 * @param size in bytes
485 * @return the size in human readable string
487 public static String getFileSizeAsString(long size) {
489 return String.valueOf(size) + " B";
490 else if (size <= 1024 * 1024)
491 return getSize(size, 1024D) + " KB";
492 else if (size <= 1024 * 1024 * 1024)
493 return getSize(size, (1024D * 1024D)) + " MB";
494 return getSize(size, (1024D * 1024D * 1024D)) + " GB";
497 private static String getSize(Long size, Double division) {
498 Double res = Double.valueOf(size.toString()) / division;
499 NumberFormat nf = NumberFormat.getFormat("######.#");
500 return nf.format(res);
503 public boolean isShared(){
506 for(PermissionHolder perm : permissions){
507 if(perm.getUser() != null && !owner.equals(perm.getUser()))
509 if(perm.getGroup() != null)