Fixed folder deletion with all subfolders and files
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / File.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35
36 package gr.grnet.pithos.web.client.foldertree;
37
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;
46
47 public class File extends Resource {
48     private String name;
49
50     private String hash;
51
52     private int version;
53
54     private long bytes;
55
56     private String contentType;
57
58     private Date lastModified;
59
60     private String modifiedBy;
61
62     private Date versionTimestamp;
63
64     private String path;
65
66     private String owner;
67
68     private boolean inTrash;
69
70     private String container;
71
72     private Folder parent;
73
74     public String getContentType() {
75         return contentType;
76     }
77
78     public String getHash() {
79         return hash;
80     }
81
82     public Date getLastModified() {
83         return lastModified;
84     }
85
86     public String getModifiedBy() {
87         return modifiedBy;
88     }
89
90     public String getName() {
91         return name;
92     }
93
94     public int getVersion() {
95         return version;
96     }
97
98     public Date getVersionTimestamp() {
99         return versionTimestamp;
100     }
101
102     public String getUri() {
103         return "/" + container + "/" + path;
104     }
105
106     public String getOwner() {
107         return owner;
108     }
109
110     public String getPath() {
111         return path;
112     }
113
114     public long getBytes() {
115         return bytes;
116     }
117
118     public String getSizeAsString() {
119         NumberFormat nf = NumberFormat.getFormat("######.#");
120         if (bytes < 1024)
121             return String.valueOf(bytes) + " B";
122         else if (bytes < 1024 * 1024)
123             return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
124         else if (bytes < 1024 * 1024 * 1024)
125             return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
126         return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
127     }
128
129     public boolean isShared() {
130         return false;
131     }
132
133     public boolean isInTrash() {
134         return inTrash;
135     }
136
137     public void populate(Folder parent, JSONObject o, String container) {
138         this.parent = parent;
139         path = unmarshallString(o, "name");
140         if (path.contains("/"))
141             name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
142         else
143             name = path;
144         hash = unmarshallString(o, "hash");
145         bytes = unmarshallLong(o, "bytes");
146         version = unmarshallInt(o, "version");
147         contentType = unmarshallString(o, "content_type");
148         lastModified = unmarshallDate(o, "last_modified");
149         modifiedBy = unmarshallString(o, "modified_by");
150         versionTimestamp = unmarshallDate(o, "version_timestamp");
151         this.container = container;
152     }
153
154     public boolean equals(Object other) {
155         if (other instanceof File) {
156             File o = (File) other;
157             return name.equals(o.getName());
158         }
159         return false;
160     }
161
162     public int hashCode() {
163         return name.hashCode();
164     }
165
166     public String getContainer() {
167         return container;
168     }
169
170     public static File createFromResponse(Response response, File result) {
171         result.populate(response);
172         return result;
173     }
174
175     private void populate(Response response) {
176         String header = response.getHeader("X-Object-Meta-Trash");
177         if (header != null)
178             inTrash = Boolean.valueOf(header);
179         else
180             inTrash = false;
181
182         JSONValue json = JSONParser.parseStrict(response.getText());
183         JSONObject o = json.isObject();
184     }
185
186     public Folder getParent() {
187         return parent;
188     }
189 }