2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.pithos.web.client.foldertree;
38 import com.google.gwt.http.client.Response;
39 import com.google.gwt.i18n.client.DateTimeFormat;
40 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
41 import com.google.gwt.i18n.client.NumberFormat;
42 import com.google.gwt.json.client.JSONObject;
43 import com.google.gwt.json.client.JSONParser;
44 import com.google.gwt.json.client.JSONValue;
45 import java.util.Date;
47 public class File extends Resource {
56 private String contentType;
58 private Date lastModified;
60 private String modifiedBy;
62 private Date versionTimestamp;
68 private boolean inTrash;
70 private String container;
72 private Folder parent;
74 public String getContentType() {
78 public String getHash() {
82 public Date getLastModified() {
86 public String getModifiedBy() {
90 public String getName() {
94 public int getVersion() {
98 public Date getVersionTimestamp() {
99 return versionTimestamp;
103 public String getLastModifiedSince() {
104 return null; //To change body of implemented methods use File | Settings | File Templates.
107 public String getUri() {
108 return path + "/" + name;
111 public String getOwner() {
115 public String getPath() {
119 public long getBytes() {
123 public String getSizeAsString() {
124 NumberFormat nf = NumberFormat.getFormat("######.#");
126 return String.valueOf(bytes) + " B";
127 else if (bytes < 1024 * 1024)
128 return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
129 else if (bytes < 1024 * 1024 * 1024)
130 return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
131 return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
134 public boolean isShared() {
138 public boolean isInTrash() {
142 public void populate(Folder parent, JSONObject o, String container) {
143 this.parent = parent;
144 path = unmarshallString(o, "name");
145 if (path.contains("/"))
146 name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
149 hash = unmarshallString(o, "hash");
150 bytes = unmarshallLong(o, "bytes");
151 version = unmarshallInt(o, "version");
152 contentType = unmarshallString(o, "content_type");
153 lastModified = unmarshallDate(o, "last_modified");
154 modifiedBy = unmarshallString(o, "modified_by");
155 versionTimestamp = unmarshallDate(o, "version_timestamp");
156 this.container = container;
159 public boolean equals(Object other) {
160 if (other instanceof File) {
161 File o = (File) other;
162 return name.equals(o.getName());
167 public int hashCode() {
168 return name.hashCode();
171 public String getContainer() {
175 public static File createFromResponse(Response response, File result) {
176 result.populate(response);
180 private void populate(Response response) {
181 String header = response.getHeader("X-Object-Meta-Trash");
183 inTrash = Boolean.valueOf(header);
187 JSONValue json = JSONParser.parseStrict(response.getText());
188 JSONObject o = json.isObject();