root / web_client / src / gr / grnet / pithos / web / client / foldertree / Folder.java @ 5eef5c8a
History | View | Annotate | Download (8.1 kB)
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.json.client.JSONArray; |
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.ArrayList; |
46 |
import java.util.Date; |
47 |
import java.util.Iterator; |
48 |
import java.util.LinkedHashSet; |
49 |
import java.util.List; |
50 |
import java.util.Set; |
51 |
|
52 |
public class Folder extends Resource { |
53 |
/*
|
54 |
* The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
|
55 |
* last part of its path
|
56 |
*/
|
57 |
private String name = null; |
58 |
|
59 |
private Date lastModified = null; |
60 |
|
61 |
private long bytesUsed = 0; |
62 |
|
63 |
private Folder parent = null; |
64 |
|
65 |
private Set<Folder> subfolders = new LinkedHashSet<Folder>(); |
66 |
/*
|
67 |
* The name of the container that this folder belongs to. If this folder is container, this field equals name
|
68 |
*/
|
69 |
private String container = null; |
70 |
|
71 |
/*
|
72 |
* This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
|
73 |
* parameter in the request that fetches its children). If the folder is a cointainer this is empty string
|
74 |
*/
|
75 |
private String prefix = ""; |
76 |
|
77 |
private Set<File> files = new LinkedHashSet<File>(); |
78 |
|
79 |
private boolean inTrash = false; |
80 |
|
81 |
/*
|
82 |
* Flag that indicates that this folder is the Trash
|
83 |
*/
|
84 |
private boolean trash = false; |
85 |
|
86 |
public Folder() {};
|
87 |
|
88 |
public Folder(String name) { |
89 |
this.name = name;
|
90 |
} |
91 |
|
92 |
public String getName() { |
93 |
return name;
|
94 |
} |
95 |
|
96 |
public Date getLastModified() { |
97 |
return lastModified;
|
98 |
} |
99 |
|
100 |
public long getBytesUsed() { |
101 |
return bytesUsed;
|
102 |
} |
103 |
|
104 |
public void setLastModified(Date lastModified) { |
105 |
this.lastModified = lastModified;
|
106 |
} |
107 |
|
108 |
public Set<Folder> getSubfolders() { |
109 |
return subfolders;
|
110 |
} |
111 |
|
112 |
public void setSubfolders(Set<Folder> subfolders) { |
113 |
this.subfolders = subfolders;
|
114 |
} |
115 |
|
116 |
public String getContainer() { |
117 |
return container;
|
118 |
} |
119 |
|
120 |
public String getPrefix() { |
121 |
return prefix;
|
122 |
} |
123 |
|
124 |
public void setPrefix(String prefix) { |
125 |
this.prefix = prefix;
|
126 |
} |
127 |
|
128 |
public void populate(Response response) { |
129 |
String header = response.getHeader("Last-Modified"); |
130 |
if (header != null) |
131 |
lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header); |
132 |
|
133 |
header = response.getHeader("X-Container-Bytes-Used");
|
134 |
if (header != null) |
135 |
bytesUsed = Long.valueOf(header);
|
136 |
|
137 |
header = response.getHeader("X-Object-Meta-Trash");
|
138 |
if (header != null && header.equals("true")) |
139 |
inTrash = true;
|
140 |
|
141 |
subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
|
142 |
files.clear(); |
143 |
JSONValue json = JSONParser.parseStrict(response.getText()); |
144 |
JSONArray array = json.isArray(); |
145 |
if (array != null) { |
146 |
for (int i=0; i<array.size(); i++) { |
147 |
JSONObject o = array.get(i).isObject(); |
148 |
if (o != null) { |
149 |
String contentType = unmarshallString(o, "content_type"); |
150 |
if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) { |
151 |
Folder f = new Folder();
|
152 |
f.populate(this, o, container);
|
153 |
subfolders.add(f); |
154 |
} |
155 |
else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) { |
156 |
File file = new File(); |
157 |
file.populate(this, o, container);
|
158 |
files.add(file); |
159 |
} |
160 |
} |
161 |
} |
162 |
//This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
|
163 |
//avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
|
164 |
Iterator<Folder> iter = subfolders.iterator();
|
165 |
while (iter.hasNext()) {
|
166 |
Folder f = iter.next(); |
167 |
if (f.isInTrash())
|
168 |
iter.remove(); |
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
public void populate(Folder parent, JSONObject o, String aContainer) { |
174 |
this.parent = parent;
|
175 |
String path = null; |
176 |
if (o.containsKey("subdir")) { |
177 |
path = unmarshallString(o, "subdir");
|
178 |
} |
179 |
else {
|
180 |
path = unmarshallString(o, "name");
|
181 |
lastModified = unmarshallDate(o, "last_modified");
|
182 |
} |
183 |
if (path.endsWith("/")) |
184 |
path = path.substring(0, path.length() - 1); |
185 |
if (path.contains("/")) |
186 |
name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix |
187 |
else
|
188 |
name = path; |
189 |
if (aContainer != null) { |
190 |
container = aContainer; |
191 |
prefix = path; |
192 |
} |
193 |
else {
|
194 |
container = name; |
195 |
prefix = "";
|
196 |
} |
197 |
if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true")) |
198 |
inTrash = true;
|
199 |
} |
200 |
|
201 |
public static Folder createFromResponse(Response response, Folder result) { |
202 |
Folder f = null;
|
203 |
if (result == null) |
204 |
f = new Folder();
|
205 |
else
|
206 |
f = result; |
207 |
|
208 |
f.populate(response); |
209 |
return f;
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public boolean equals(Object other) { |
214 |
if (other instanceof Folder) { |
215 |
Folder o = (Folder) other; |
216 |
return (container + prefix).equals(o.getContainer() + o.getPrefix());
|
217 |
} |
218 |
return false; |
219 |
} |
220 |
|
221 |
@Override
|
222 |
public int hashCode() { |
223 |
return (container + prefix).hashCode();
|
224 |
} |
225 |
|
226 |
public Set<File> getFiles() { |
227 |
return files;
|
228 |
} |
229 |
|
230 |
public Folder getParent() {
|
231 |
return parent;
|
232 |
} |
233 |
|
234 |
public String getUri() { |
235 |
return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix); |
236 |
} |
237 |
|
238 |
public boolean isInTrash() { |
239 |
return inTrash;
|
240 |
} |
241 |
|
242 |
public boolean isContainer() { |
243 |
return parent == null; |
244 |
} |
245 |
|
246 |
public boolean isTrash() { |
247 |
return trash;
|
248 |
} |
249 |
|
250 |
public void setTrash(boolean trash) { |
251 |
this.trash = trash;
|
252 |
} |
253 |
|
254 |
public void setContainer(String container) { |
255 |
this.container = container;
|
256 |
} |
257 |
} |