root / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java @ f5c86c34
History | View | Annotate | Download (8.2 kB)
1 | 6084aa02 | Christos Stathis | /*
|
---|---|---|---|
2 | cae2a8db | Christos Stathis | * Copyright 2011-2012 GRNET S.A. All rights reserved.
|
3 | 63366925 | Christos Stathis | *
|
4 | 63366925 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 63366925 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 63366925 | Christos Stathis | * conditions are met:
|
7 | 63366925 | Christos Stathis | *
|
8 | 63366925 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 63366925 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 63366925 | Christos Stathis | * disclaimer.
|
11 | 63366925 | Christos Stathis | *
|
12 | 63366925 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 63366925 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 63366925 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 63366925 | Christos Stathis | * provided with the distribution.
|
16 | 63366925 | Christos Stathis | *
|
17 | 63366925 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 63366925 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 63366925 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 63366925 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 63366925 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 63366925 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 63366925 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 63366925 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 63366925 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 63366925 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 63366925 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 63366925 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 63366925 | Christos Stathis | *
|
30 | 63366925 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 63366925 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 63366925 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 63366925 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | 6084aa02 | Christos Stathis | */
|
35 | 6084aa02 | Christos Stathis | |
36 | 6084aa02 | Christos Stathis | package gr.grnet.pithos.web.client.foldertree; |
37 | 6084aa02 | Christos Stathis | |
38 | 7b28ae07 | Christos Stathis | import gr.grnet.pithos.web.client.Pithos; |
39 | 4baffab1 | Christos Stathis | import gr.grnet.pithos.web.client.grouptree.Group; |
40 | 7b28ae07 | Christos Stathis | |
41 | 7811b9d1 | Christos Stathis | import java.util.ArrayList; |
42 | 7811b9d1 | Christos Stathis | import java.util.Date; |
43 | 7811b9d1 | Christos Stathis | import java.util.List; |
44 | 7811b9d1 | Christos Stathis | |
45 | 6084aa02 | Christos Stathis | import com.google.gwt.http.client.Header; |
46 | 8e61880b | Christos Stathis | import com.google.gwt.http.client.Response; |
47 | ea94470a | Christos Stathis | import com.google.gwt.http.client.URL; |
48 | 8e61880b | Christos Stathis | import com.google.gwt.i18n.client.DateTimeFormat; |
49 | 8e61880b | Christos Stathis | import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; |
50 | 8e61880b | Christos Stathis | import com.google.gwt.i18n.client.NumberFormat; |
51 | 6084aa02 | Christos Stathis | import com.google.gwt.json.client.JSONArray; |
52 | 6084aa02 | Christos Stathis | import com.google.gwt.json.client.JSONObject; |
53 | 6084aa02 | Christos Stathis | import com.google.gwt.json.client.JSONParser; |
54 | 6084aa02 | Christos Stathis | import com.google.gwt.json.client.JSONValue; |
55 | 6084aa02 | Christos Stathis | |
56 | 6084aa02 | Christos Stathis | /**
|
57 | 6084aa02 | Christos Stathis | * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
|
58 | 6084aa02 | Christos Stathis | * File Templates.
|
59 | 6084aa02 | Christos Stathis | */
|
60 | 6084aa02 | Christos Stathis | public class AccountResource extends Resource { |
61 | 6084aa02 | Christos Stathis | |
62 | 8e61880b | Christos Stathis | private long numberOfContainers = 0; |
63 | 8e61880b | Christos Stathis | |
64 | 8e61880b | Christos Stathis | private long numberOfObjects = 0; |
65 | 8e61880b | Christos Stathis | |
66 | 8e61880b | Christos Stathis | private long bytesUsed = 0; |
67 | 8e61880b | Christos Stathis | |
68 | e92335eb | Christos Stathis | private long quota = 0; |
69 | 8e61880b | Christos Stathis | |
70 | 8e61880b | Christos Stathis | private Date lastLogin = null; |
71 | 8e61880b | Christos Stathis | |
72 | 8e61880b | Christos Stathis | private Date lastModified = null; |
73 | 8e61880b | Christos Stathis | |
74 | 105b4e49 | Christos Stathis | private List<Folder> containers = new ArrayList<Folder>(); |
75 | 8e61880b | Christos Stathis | |
76 | 8e61880b | Christos Stathis | private Date currentLogin = null; |
77 | 8e61880b | Christos Stathis | |
78 | d5c6298f | Christos Stathis | private List<Group> groups = new ArrayList<Group>(); |
79 | d5c6298f | Christos Stathis | |
80 | e92335eb | Christos Stathis | public long getQuota() { |
81 | e92335eb | Christos Stathis | return quota;
|
82 | 8e61880b | Christos Stathis | } |
83 | 8e61880b | Christos Stathis | |
84 | e92335eb | Christos Stathis | public void setQuota(long quota) { |
85 | e92335eb | Christos Stathis | this.quota = quota;
|
86 | 8e61880b | Christos Stathis | } |
87 | 8e61880b | Christos Stathis | |
88 | 8e61880b | Christos Stathis | public long getBytesUsed() { |
89 | 8e61880b | Christos Stathis | return bytesUsed;
|
90 | 8e61880b | Christos Stathis | } |
91 | 8e61880b | Christos Stathis | |
92 | 8e61880b | Christos Stathis | public void setBytesUsed(long bytesUsed) { |
93 | 8e61880b | Christos Stathis | this.bytesUsed = bytesUsed;
|
94 | 8e61880b | Christos Stathis | } |
95 | 8e61880b | Christos Stathis | |
96 | 8e61880b | Christos Stathis | public Date getLastLogin() { |
97 | 8e61880b | Christos Stathis | return lastLogin;
|
98 | 8e61880b | Christos Stathis | } |
99 | 8e61880b | Christos Stathis | |
100 | 8e61880b | Christos Stathis | public void setLastLogin(Date lastLogin) { |
101 | 8e61880b | Christos Stathis | this.lastLogin = lastLogin;
|
102 | 8e61880b | Christos Stathis | } |
103 | 8e61880b | Christos Stathis | |
104 | f5023f13 | Christos Stathis | @Override
|
105 | f5023f13 | Christos Stathis | public Date getLastModified() { |
106 | 8e61880b | Christos Stathis | return lastModified;
|
107 | 8e61880b | Christos Stathis | } |
108 | 8e61880b | Christos Stathis | |
109 | 8e61880b | Christos Stathis | public void setLastModified(Date lastModified) { |
110 | 8e61880b | Christos Stathis | this.lastModified = lastModified;
|
111 | 8e61880b | Christos Stathis | } |
112 | 8e61880b | Christos Stathis | |
113 | 8e61880b | Christos Stathis | public long getNumberOfContainers() { |
114 | 8e61880b | Christos Stathis | return numberOfContainers;
|
115 | 8e61880b | Christos Stathis | } |
116 | 8e61880b | Christos Stathis | |
117 | 8e61880b | Christos Stathis | public void setNumberOfContainers(long numberOfContainers) { |
118 | 8e61880b | Christos Stathis | this.numberOfContainers = numberOfContainers;
|
119 | 8e61880b | Christos Stathis | } |
120 | 8e61880b | Christos Stathis | |
121 | 8e61880b | Christos Stathis | public long getNumberOfObjects() { |
122 | 8e61880b | Christos Stathis | return numberOfObjects;
|
123 | 8e61880b | Christos Stathis | } |
124 | 8e61880b | Christos Stathis | |
125 | 8e61880b | Christos Stathis | public void setNumberOfObjects(long numberOfObjects) { |
126 | 8e61880b | Christos Stathis | this.numberOfObjects = numberOfObjects;
|
127 | 8e61880b | Christos Stathis | } |
128 | 6084aa02 | Christos Stathis | |
129 | 105b4e49 | Christos Stathis | public List<Folder> getContainers() { |
130 | 6084aa02 | Christos Stathis | return containers;
|
131 | 6084aa02 | Christos Stathis | } |
132 | 8e61880b | Christos Stathis | |
133 | 8e61880b | Christos Stathis | public Date getCurrentLogin() { |
134 | 8e61880b | Christos Stathis | return currentLogin;
|
135 | 8e61880b | Christos Stathis | } |
136 | 8e61880b | Christos Stathis | |
137 | 8e61880b | Christos Stathis | public void setCurrentLogin(Date currentLogin) { |
138 | 8e61880b | Christos Stathis | this.currentLogin = currentLogin;
|
139 | 8e61880b | Christos Stathis | } |
140 | 8e61880b | Christos Stathis | |
141 | b51c628b | Christos Stathis | public void populate(String owner, Response response) { |
142 | 8e61880b | Christos Stathis | DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822); |
143 | 6f79f96f | Christos Stathis | groups.clear(); |
144 | d5c6298f | Christos Stathis | for (Header h : response.getHeaders()) {
|
145 | e2403069 | Christos Stathis | if (h != null) { |
146 | e2403069 | Christos Stathis | String name = h.getName();
|
147 | e2403069 | Christos Stathis | if (name.startsWith("X-Account-Group-")) { |
148 | a2411896 | Christos Stathis | String groupName = URL.decodePathSegment(name.substring("X-Account-Group-".length())); |
149 | e2403069 | Christos Stathis | Group g = new Group(groupName); |
150 | e2403069 | Christos Stathis | String[] members = h.getValue().split(","); |
151 | e2403069 | Christos Stathis | for (String s : members) |
152 | a2411896 | Christos Stathis | g.addMember(URL.decodePathSegment(s).trim());
|
153 | e2403069 | Christos Stathis | groups.add(g); |
154 | e2403069 | Christos Stathis | } |
155 | e2403069 | Christos Stathis | else if (name.equals("X-Account-Container-Count")) { |
156 | e2403069 | Christos Stathis | numberOfContainers = Long.valueOf(h.getValue());
|
157 | e2403069 | Christos Stathis | } |
158 | e2403069 | Christos Stathis | else if (name.equals("X-Account-Object-Count")) { |
159 | e2403069 | Christos Stathis | numberOfObjects = Long.valueOf(h.getValue());
|
160 | e2403069 | Christos Stathis | } |
161 | e2403069 | Christos Stathis | else if (name.equals("X-Account-Bytes-Used")) { |
162 | e2403069 | Christos Stathis | bytesUsed = Long.valueOf(h.getValue());
|
163 | e2403069 | Christos Stathis | } |
164 | e92335eb | Christos Stathis | else if (name.equals("X-Account-Policy-Quota")) { |
165 | e92335eb | Christos Stathis | quota = Long.valueOf(h.getValue());
|
166 | e2403069 | Christos Stathis | } |
167 | e2403069 | Christos Stathis | else if (name.equals("X-Account-Last-Login")) { |
168 | e2403069 | Christos Stathis | lastLogin = df.parse(h.getValue()); |
169 | e2403069 | Christos Stathis | } |
170 | e2403069 | Christos Stathis | else if (name.equals("Last-Modified")) { |
171 | e2403069 | Christos Stathis | lastModified = df.parse(h.getValue()); |
172 | e2403069 | Christos Stathis | } |
173 | e2403069 | Christos Stathis | } |
174 | d5c6298f | Christos Stathis | } |
175 | 8e61880b | Christos Stathis | |
176 | 64f42c0b | Christos Stathis | if (response.getText() != null && response.getText().length() > 0) { |
177 | 6f79f96f | Christos Stathis | containers.clear(); |
178 | 64f42c0b | Christos Stathis | JSONValue json = JSONParser.parseStrict(response.getText()); |
179 | 64f42c0b | Christos Stathis | JSONArray array = json.isArray(); |
180 | 64f42c0b | Christos Stathis | if (array != null) { |
181 | 64f42c0b | Christos Stathis | for (int i=0; i<array.size(); i++) { |
182 | 64f42c0b | Christos Stathis | JSONObject o = array.get(i).isObject(); |
183 | 64f42c0b | Christos Stathis | if (o != null) { |
184 | 64f42c0b | Christos Stathis | Folder f = new Folder();
|
185 | 64f42c0b | Christos Stathis | f.populate(null, o, owner, null); |
186 | 64f42c0b | Christos Stathis | containers.add(f); |
187 | 64f42c0b | Christos Stathis | } |
188 | 64f42c0b | Christos Stathis | } |
189 | 64f42c0b | Christos Stathis | } |
190 | 8e61880b | Christos Stathis | } |
191 | 8e61880b | Christos Stathis | } |
192 | 8e61880b | Christos Stathis | |
193 | 64f42c0b | Christos Stathis | public static AccountResource createFromResponse(String owner, Response response, AccountResource result) { |
194 | 64f42c0b | Christos Stathis | AccountResource a; |
195 | 64f42c0b | Christos Stathis | if (result == null) |
196 | 64f42c0b | Christos Stathis | a = new AccountResource();
|
197 | 64f42c0b | Christos Stathis | else
|
198 | 64f42c0b | Christos Stathis | a = result; |
199 | b51c628b | Christos Stathis | a.populate(owner, response); |
200 | 8e61880b | Christos Stathis | return a;
|
201 | 8e61880b | Christos Stathis | } |
202 | 8e61880b | Christos Stathis | |
203 | 8e61880b | Christos Stathis | private String getSize(Long size, Double division){ |
204 | 8e61880b | Christos Stathis | Double res = Double.valueOf(size.toString())/division; |
205 | 8e61880b | Christos Stathis | NumberFormat nf = NumberFormat.getFormat("######.#"); |
206 | 8e61880b | Christos Stathis | return nf.format(res);
|
207 | 8e61880b | Christos Stathis | } |
208 | 8e61880b | Christos Stathis | |
209 | 8e61880b | Christos Stathis | public String getFileSizeAsString() { |
210 | 8e61880b | Christos Stathis | if (bytesUsed < 1024) |
211 | 140f0f6d | Christos Stathis | return String.valueOf(bytesUsed) + "B"; |
212 | 8e61880b | Christos Stathis | else if (bytesUsed < 1024*1024) |
213 | 140f0f6d | Christos Stathis | return getSize(bytesUsed, 1024D) + "KB"; |
214 | 8e61880b | Christos Stathis | else if (bytesUsed < 1024*1024*1024) |
215 | 140f0f6d | Christos Stathis | return getSize(bytesUsed,(1024D*1024D)) + "MB"; |
216 | 140f0f6d | Christos Stathis | return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB"; |
217 | 8e61880b | Christos Stathis | } |
218 | 8e61880b | Christos Stathis | |
219 | bf664298 | Christos Stathis | public String getQuotaAsString() { |
220 | bf664298 | Christos Stathis | if (quota < 1024) |
221 | 140f0f6d | Christos Stathis | return String.valueOf(quota) + "B"; |
222 | bf664298 | Christos Stathis | else if (quota < 1024 * 1024) |
223 | 140f0f6d | Christos Stathis | return getSize(quota, 1024D) + "KB"; |
224 | bf664298 | Christos Stathis | else if (quota < 1024 * 1024 * 1024) |
225 | 140f0f6d | Christos Stathis | return getSize(quota,(1024D * 1024D)) + "MB"; |
226 | 140f0f6d | Christos Stathis | return getSize(quota , (1024D * 1024D * 1024D)) + "GB"; |
227 | 8e61880b | Christos Stathis | } |
228 | d5c6298f | Christos Stathis | |
229 | d5c6298f | Christos Stathis | public List<Group> getGroups() { |
230 | d5c6298f | Christos Stathis | return groups;
|
231 | d5c6298f | Christos Stathis | } |
232 | 7b28ae07 | Christos Stathis | |
233 | 7b28ae07 | Christos Stathis | public boolean hasHomeContainer() { |
234 | 7b28ae07 | Christos Stathis | for (Folder f : containers)
|
235 | 7b28ae07 | Christos Stathis | if (f.getName().equals(Pithos.HOME_CONTAINER))
|
236 | 7b28ae07 | Christos Stathis | return true; |
237 | 7b28ae07 | Christos Stathis | return false; |
238 | 7b28ae07 | Christos Stathis | } |
239 | 7b28ae07 | Christos Stathis | |
240 | 7b28ae07 | Christos Stathis | public boolean hasTrashContainer() { |
241 | 7b28ae07 | Christos Stathis | for (Folder f : containers)
|
242 | 7b28ae07 | Christos Stathis | if (f.getName().equals(Pithos.TRASH_CONTAINER))
|
243 | 7b28ae07 | Christos Stathis | return true; |
244 | 7b28ae07 | Christos Stathis | return false; |
245 | 7b28ae07 | Christos Stathis | } |
246 | 4baffab1 | Christos Stathis | |
247 | 4baffab1 | Christos Stathis | public void addGroup(Group newGroup) { |
248 | 4baffab1 | Christos Stathis | groups.add(newGroup); |
249 | 4baffab1 | Christos Stathis | } |
250 | 61f8605d | Christos Stathis | |
251 | 61f8605d | Christos Stathis | public void removeGroup(Group group) { |
252 | 61f8605d | Christos Stathis | groups.remove(group); |
253 | 61f8605d | Christos Stathis | } |
254 | 49080dd5 | Christos Stathis | |
255 | 49080dd5 | Christos Stathis | public Folder getTrash() {
|
256 | 49080dd5 | Christos Stathis | for (Folder c : containers) {
|
257 | 49080dd5 | Christos Stathis | if (c.getName().equals(Pithos.TRASH_CONTAINER))
|
258 | 49080dd5 | Christos Stathis | return c;
|
259 | 49080dd5 | Christos Stathis | } |
260 | 49080dd5 | Christos Stathis | return null; |
261 | 49080dd5 | Christos Stathis | } |
262 | bf664298 | Christos Stathis | |
263 | bf664298 | Christos Stathis | public double getUsedPercentage() { |
264 | e92335eb | Christos Stathis | if (quota == 0) |
265 | e92335eb | Christos Stathis | return 0; |
266 | e92335eb | Christos Stathis | return ((double) bytesUsed) / quota; |
267 | bf664298 | Christos Stathis | } |
268 | 340bf0d6 | Christos Stathis | |
269 | 340bf0d6 | Christos Stathis | public Folder getPithos() {
|
270 | 340bf0d6 | Christos Stathis | for (Folder f : containers)
|
271 | 340bf0d6 | Christos Stathis | if (f.getName().equals(Pithos.HOME_CONTAINER))
|
272 | 340bf0d6 | Christos Stathis | return f;
|
273 | 340bf0d6 | Christos Stathis | return null; |
274 | 340bf0d6 | Christos Stathis | } |
275 | 6084aa02 | Christos Stathis | } |