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) {
122 public String getName() {
129 * @param aName the name to set
131 public void setName(String aName) {
136 * Retrieve the owner.
140 public String getOwner() {
147 * @param newOwner the owner to set
149 public void setOwner(String newOwner) {
154 * Retrieve the createdBy.
156 * @return the createdBy
158 public String getCreatedBy() {
163 * Modify the createdBy.
165 * @param aCreatedBy the createdBy to set
167 public void setCreatedBy(String aCreatedBy) {
168 createdBy = aCreatedBy;
172 * Retrieve the modifiedBy.
174 * @return the modifiedBy
176 public String getModifiedBy() {
181 * Modify the modifiedBy.
183 * @param aModifiedBy the modifiedBy to set
185 public void setModifiedBy(String aModifiedBy) {
186 modifiedBy = aModifiedBy;
190 * Retrieve the creationDate.
192 * @return the creationDate
194 public Date getCreationDate() {
199 * Modify the creationDate.
201 * @param aCreationDate the creationDate to set
203 public void setCreationDate(Date aCreationDate) {
204 creationDate = aCreationDate;
208 * Retrieve the modificationDate.
210 * @return the modificationDate
212 public Date getModificationDate() {
213 return modificationDate;
217 * Modify the modificationDate.
219 * @param aModificationDate the modificationDate to set
221 public void setModificationDate(Date aModificationDate) {
222 modificationDate = aModificationDate;
226 * Retrieve the contentType.
228 * @return the contentType
230 public String getContentType() {
235 * Modify the contentType.
237 * @param newContentType the contentType to set
239 public void setContentType(String newContentType) {
240 contentType = newContentType;
244 * Retrieve the contentLength.
246 * @return the contentLength
248 public Long getContentLength() {
249 return contentLength;
253 * Modify the contentLength.
255 * @param newContentLength the contentLength to set
257 public void setContentLength(Long newContentLength) {
258 contentLength = newContentLength;
262 * Retrieve the readForAll.
264 * @return the readForAll
266 public boolean isReadForAll() {
271 * Modify the readForAll.
273 * @param newReadForAll the readForAll to set
275 public void setReadForAll(boolean newReadForAll) {
276 readForAll = newReadForAll;
280 * Retrieve the versioned.
282 * @return the versioned
284 public boolean isVersioned() {
289 * Modify the versioned.
291 * @param newVersioned the versioned to set
293 public void setVersioned(boolean newVersioned) {
294 versioned = newVersioned;
298 * Retrieve the version.
300 * @return the version
302 public Integer getVersion() {
307 * Modify the version.
309 * @param aVersion the version to set
311 public void setVersion(Integer aVersion) {
320 public String getEtag() {
327 * @param anEtag the etag to set
329 public void setEtag(String anEtag) {
338 public List<String> getTags() {
345 * @param newTags the tags to set
347 public void setTags(List<String> newTags) {
352 * Retrieve the permissions.
354 * @return the permissions
356 public Set<PermissionHolder> getPermissions() {
361 * Modify the permissions.
363 * @param newPermissions the permissions to set
365 public void setPermissions(Set<PermissionHolder> newPermissions) {
366 permissions = newPermissions;
370 * Retrieve the deleted.
372 * @return the deleted
374 public boolean isDeleted() {
379 * Modify the deleted.
381 * @param newDeleted the deleted to set
383 public void setDeleted(boolean newDeleted) {
384 deleted = newDeleted;
388 * Retrieve the folderURI.
390 * @return the folderURI
392 public String getFolderURI() {
397 * Modify the folderURI.
399 * @param aFolderURI the folderURI to set
401 public void setFolderURI(String aFolderURI) {
402 folderURI = aFolderURI;
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");
419 if (metadata.get("version") != null)
420 version = new Integer(metadata.get("version").toString());
422 deleted = unmarshallBoolean(metadata, "deleted");
424 GWT.log("FOUND A DELETED FILE:" + name, null);
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);
434 if (metadata.get("permissions") != null) {
435 JSONArray perm = metadata.get("permissions").isArray();
437 for (int i = 0; i < perm.size(); i++) {
438 JSONObject obj = perm.get(i).isObject();
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);
448 permission.setRead(unmarshallBoolean(obj, "read"));
449 permission.setWrite(unmarshallBoolean(obj, "write"));
450 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
451 permissions.add(permission);
456 if (metadata.get("tags") != null) {
457 JSONArray perm = metadata.get("tags").isArray();
459 for (int i = 0; i < perm.size(); i++) {
460 JSONString obj = perm.get(i).isString();
462 tags.add(URL.decodeComponent(obj.stringValue()));
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());
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).
477 * @return the fileSize
479 public String getFileSizeAsString() {
480 return getFileSizeAsString(contentLength);
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).
487 * @param size in bytes
488 * @return the size in human readable string
490 public static String getFileSizeAsString(long size) {
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";
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);
506 public boolean isShared(){
509 if(permissions != null)
510 for(PermissionHolder perm : permissions){
511 if(perm.getUser() != null && !owner.equals(perm.getUser()))
513 if(perm.getGroup() != null)
519 public boolean isShared(String ownerUser){
520 GWT.log("OWNER USER:"+ownerUser, null);
523 if(permissions != null)
524 for(PermissionHolder perm : permissions){
525 if(perm.getUser() != null && !ownerUser.equals(perm.getUser()))
527 if(perm.getGroup() != null)
534 public String getLastModifiedSince() {
535 if(modificationDate != null)
536 return getDate(modificationDate.getTime());