root / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java @ a2411896
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 gr.grnet.pithos.web.client.Pithos; |
39 |
import gr.grnet.pithos.web.client.grouptree.Group; |
40 |
|
41 |
import java.util.ArrayList; |
42 |
import java.util.Date; |
43 |
import java.util.List; |
44 |
|
45 |
import com.google.gwt.http.client.Header; |
46 |
import com.google.gwt.http.client.Response; |
47 |
import com.google.gwt.http.client.URL; |
48 |
import com.google.gwt.i18n.client.DateTimeFormat; |
49 |
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; |
50 |
import com.google.gwt.i18n.client.NumberFormat; |
51 |
import com.google.gwt.json.client.JSONArray; |
52 |
import com.google.gwt.json.client.JSONObject; |
53 |
import com.google.gwt.json.client.JSONParser; |
54 |
import com.google.gwt.json.client.JSONValue; |
55 |
|
56 |
/**
|
57 |
* Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
|
58 |
* File Templates.
|
59 |
*/
|
60 |
public class AccountResource extends Resource { |
61 |
|
62 |
private long numberOfContainers = 0; |
63 |
|
64 |
private long numberOfObjects = 0; |
65 |
|
66 |
private long bytesUsed = 0; |
67 |
|
68 |
private long quota = 0; |
69 |
|
70 |
private Date lastLogin = null; |
71 |
|
72 |
private Date lastModified = null; |
73 |
|
74 |
private List<Folder> containers = new ArrayList<Folder>(); |
75 |
|
76 |
private Date currentLogin = null; |
77 |
|
78 |
private List<Group> groups = new ArrayList<Group>(); |
79 |
|
80 |
public long getQuota() { |
81 |
return quota;
|
82 |
} |
83 |
|
84 |
public void setQuota(long quota) { |
85 |
this.quota = quota;
|
86 |
} |
87 |
|
88 |
public long getBytesUsed() { |
89 |
return bytesUsed;
|
90 |
} |
91 |
|
92 |
public void setBytesUsed(long bytesUsed) { |
93 |
this.bytesUsed = bytesUsed;
|
94 |
} |
95 |
|
96 |
public Date getLastLogin() { |
97 |
return lastLogin;
|
98 |
} |
99 |
|
100 |
public void setLastLogin(Date lastLogin) { |
101 |
this.lastLogin = lastLogin;
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public Date getLastModified() { |
106 |
return lastModified;
|
107 |
} |
108 |
|
109 |
public void setLastModified(Date lastModified) { |
110 |
this.lastModified = lastModified;
|
111 |
} |
112 |
|
113 |
public long getNumberOfContainers() { |
114 |
return numberOfContainers;
|
115 |
} |
116 |
|
117 |
public void setNumberOfContainers(long numberOfContainers) { |
118 |
this.numberOfContainers = numberOfContainers;
|
119 |
} |
120 |
|
121 |
public long getNumberOfObjects() { |
122 |
return numberOfObjects;
|
123 |
} |
124 |
|
125 |
public void setNumberOfObjects(long numberOfObjects) { |
126 |
this.numberOfObjects = numberOfObjects;
|
127 |
} |
128 |
|
129 |
public List<Folder> getContainers() { |
130 |
return containers;
|
131 |
} |
132 |
|
133 |
public Date getCurrentLogin() { |
134 |
return currentLogin;
|
135 |
} |
136 |
|
137 |
public void setCurrentLogin(Date currentLogin) { |
138 |
this.currentLogin = currentLogin;
|
139 |
} |
140 |
|
141 |
public void populate(String owner, Response response) { |
142 |
DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822); |
143 |
for (Header h : response.getHeaders()) {
|
144 |
if (h != null) { |
145 |
String name = h.getName();
|
146 |
if (name.startsWith("X-Account-Group-")) { |
147 |
String groupName = URL.decodePathSegment(name.substring("X-Account-Group-".length())); |
148 |
Group g = new Group(groupName); |
149 |
String[] members = h.getValue().split(","); |
150 |
for (String s : members) |
151 |
g.addMember(URL.decodePathSegment(s).trim());
|
152 |
groups.add(g); |
153 |
} |
154 |
else if (name.equals("X-Account-Container-Count")) { |
155 |
numberOfContainers = Long.valueOf(h.getValue());
|
156 |
} |
157 |
else if (name.equals("X-Account-Object-Count")) { |
158 |
numberOfObjects = Long.valueOf(h.getValue());
|
159 |
} |
160 |
else if (name.equals("X-Account-Bytes-Used")) { |
161 |
bytesUsed = Long.valueOf(h.getValue());
|
162 |
} |
163 |
else if (name.equals("X-Account-Policy-Quota")) { |
164 |
quota = Long.valueOf(h.getValue());
|
165 |
} |
166 |
else if (name.equals("X-Account-Last-Login")) { |
167 |
lastLogin = df.parse(h.getValue()); |
168 |
} |
169 |
else if (name.equals("Last-Modified")) { |
170 |
lastModified = df.parse(h.getValue()); |
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
if (response.getText() != null && response.getText().length() > 0) { |
176 |
JSONValue json = JSONParser.parseStrict(response.getText()); |
177 |
JSONArray array = json.isArray(); |
178 |
if (array != null) { |
179 |
for (int i=0; i<array.size(); i++) { |
180 |
JSONObject o = array.get(i).isObject(); |
181 |
if (o != null) { |
182 |
Folder f = new Folder();
|
183 |
f.populate(null, o, owner, null); |
184 |
containers.add(f); |
185 |
} |
186 |
} |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
public static AccountResource createFromResponse(String owner, Response response, AccountResource result) { |
192 |
AccountResource a; |
193 |
if (result == null) |
194 |
a = new AccountResource();
|
195 |
else
|
196 |
a = result; |
197 |
a.populate(owner, response); |
198 |
return a;
|
199 |
} |
200 |
|
201 |
private String getSize(Long size, Double division){ |
202 |
Double res = Double.valueOf(size.toString())/division; |
203 |
NumberFormat nf = NumberFormat.getFormat("######.#"); |
204 |
return nf.format(res);
|
205 |
} |
206 |
|
207 |
public String getFileSizeAsString() { |
208 |
if (bytesUsed < 1024) |
209 |
return String.valueOf(bytesUsed) + "B"; |
210 |
else if (bytesUsed < 1024*1024) |
211 |
return getSize(bytesUsed, 1024D) + "KB"; |
212 |
else if (bytesUsed < 1024*1024*1024) |
213 |
return getSize(bytesUsed,(1024D*1024D)) + "MB"; |
214 |
return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB"; |
215 |
} |
216 |
|
217 |
public String getQuotaAsString() { |
218 |
if (quota < 1024) |
219 |
return String.valueOf(quota) + "B"; |
220 |
else if (quota < 1024 * 1024) |
221 |
return getSize(quota, 1024D) + "KB"; |
222 |
else if (quota < 1024 * 1024 * 1024) |
223 |
return getSize(quota,(1024D * 1024D)) + "MB"; |
224 |
return getSize(quota , (1024D * 1024D * 1024D)) + "GB"; |
225 |
} |
226 |
|
227 |
public List<Group> getGroups() { |
228 |
return groups;
|
229 |
} |
230 |
|
231 |
public boolean hasHomeContainer() { |
232 |
for (Folder f : containers)
|
233 |
if (f.getName().equals(Pithos.HOME_CONTAINER))
|
234 |
return true; |
235 |
return false; |
236 |
} |
237 |
|
238 |
public boolean hasTrashContainer() { |
239 |
for (Folder f : containers)
|
240 |
if (f.getName().equals(Pithos.TRASH_CONTAINER))
|
241 |
return true; |
242 |
return false; |
243 |
} |
244 |
|
245 |
public void addGroup(Group newGroup) { |
246 |
groups.add(newGroup); |
247 |
} |
248 |
|
249 |
public void removeGroup(Group group) { |
250 |
groups.remove(group); |
251 |
} |
252 |
|
253 |
public Folder getTrash() {
|
254 |
for (Folder c : containers) {
|
255 |
if (c.getName().equals(Pithos.TRASH_CONTAINER))
|
256 |
return c;
|
257 |
} |
258 |
return null; |
259 |
} |
260 |
|
261 |
public double getUsedPercentage() { |
262 |
if (quota == 0) |
263 |
return 0; |
264 |
return ((double) bytesUsed) / quota; |
265 |
} |
266 |
|
267 |
public Folder getPithos() {
|
268 |
for (Folder f : containers)
|
269 |
if (f.getName().equals(Pithos.HOME_CONTAINER))
|
270 |
return f;
|
271 |
return null; |
272 |
} |
273 |
} |