e4011a0b55d5fa7eebf8817d3ed2d2c5bf357722
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java
1 /*
2  * Copyright 2011-2012 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.Header;
39 import com.google.gwt.http.client.Response;
40 import com.google.gwt.http.client.URL;
41 import com.google.gwt.i18n.client.DateTimeFormat;
42 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
43 import com.google.gwt.i18n.client.NumberFormat;
44 import com.google.gwt.json.client.JSONArray;
45 import com.google.gwt.json.client.JSONObject;
46 import com.google.gwt.json.client.JSONParser;
47 import com.google.gwt.json.client.JSONValue;
48 import gr.grnet.pithos.web.client.Const;
49 import gr.grnet.pithos.web.client.Pithos;
50 import gr.grnet.pithos.web.client.Resource;
51 import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
52 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
53 import gr.grnet.pithos.web.client.grouptree.Group;
54 import gr.grnet.pithos.web.client.grouptree.User;
55
56 import java.util.*;
57
58 /**
59  * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
60  * File Templates.
61  */
62 public class AccountResource extends Resource {
63     private final Pithos app;
64
65     private long numberOfContainers = 0;
66
67     private long numberOfObjects = 0;
68
69     private long bytesUsed = 0;
70
71     private long quota = 0;
72
73     private Date lastLogin = null;
74
75     private Date lastModified = null;
76
77     private List<Folder> containers = new ArrayList<Folder>();
78
79     private Date currentLogin = null;
80
81     private List<Group> groups = new ArrayList<Group>();
82
83     public AccountResource(Pithos app) {
84         this.app = app;
85     }
86
87     public long getQuota() {
88         return quota;
89     }
90
91     public void setQuota(long quota) {
92         this.quota = quota;
93     }
94
95     public long getBytesUsed() {
96         return bytesUsed;
97     }
98
99     public void setBytesUsed(long bytesUsed) {
100         this.bytesUsed = bytesUsed;
101     }
102
103     public Date getLastLogin() {
104         return lastLogin;
105     }
106
107     public void setLastLogin(Date lastLogin) {
108         this.lastLogin = lastLogin;
109     }
110
111     @Override
112     public Date getLastModified() {
113         return lastModified;
114     }
115
116     public void setLastModified(Date lastModified) {
117         this.lastModified = lastModified;
118     }
119
120     public long getNumberOfContainers() {
121         return numberOfContainers;
122     }
123
124     public void setNumberOfContainers(long numberOfContainers) {
125         this.numberOfContainers = numberOfContainers;
126     }
127
128     public long getNumberOfObjects() {
129         return numberOfObjects;
130     }
131
132     public void setNumberOfObjects(long numberOfObjects) {
133         this.numberOfObjects = numberOfObjects;
134     }
135
136     public List<Folder> getContainers() {
137         return containers;
138     }
139
140     public Date getCurrentLogin() {
141         return currentLogin;
142     }
143
144     public void setCurrentLogin(Date currentLogin) {
145         this.currentLogin = currentLogin;
146     }
147
148     private void doAddUsersByIDs(final List<String> userIDs, final String groupName) {
149         app.LOG("AccountResource::doAddUsersByIDs(), group = ", groupName);
150         final Group group = new Group(groupName);
151         for(String userID : userIDs) {
152             final String userDisplayName = app.getUserDisplayNameForID(userID);
153             final User user = new User(userID, userDisplayName, groupName);
154             group.addUser(user);
155             app.LOG("AccountResource::doAddUsersByIDs(),   user = (", userID, ", ", userDisplayName, ")");
156
157         }
158         groups.add(group);
159
160     }
161
162     public void populate(String owner, Response response) {
163         DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
164         groups.clear();
165
166         final Map<String, List<String>> parsedGroups = new HashMap<String, List<String>>();
167
168         for(Header h : response.getHeaders()) {
169             if(h != null) {
170                 String name = h.getName();
171                 if(name.startsWith(Const.X_ACCOUNT_GROUP_)) {
172                     final String groupName = URL.decodePathSegment(name.substring(Const.X_ACCOUNT_GROUP_.length()));
173                     final String[] users = h.getValue().split(",");
174                     final List<String> userIDs = new ArrayList<String>();
175                     for(String user : users) {
176                         final String userID = URL.decodePathSegment(user).trim();
177                         userIDs.add(userID);
178                     }
179                     parsedGroups.put(groupName, userIDs);
180                 }
181                 else if(name.equals(Const.X_ACCOUNT_CONTAINER_COUNT)) {
182                     numberOfContainers = Long.valueOf(h.getValue());
183                 }
184                 else if(name.equals(Const.X_ACCOUNT_OBJECT_COUNT)) {
185                     numberOfObjects = Long.valueOf(h.getValue());
186                 }
187                 else if(name.equals(Const.X_ACCOUNT_BYTES_USED)) {
188                     bytesUsed = Long.valueOf(h.getValue());
189                 }
190                 else if(name.equals(Const.X_ACCOUNT_POLICY_QUOTA)) {
191                     quota = Long.valueOf(h.getValue());
192                 }
193                 else if(name.equals(Const.X_ACCOUNT_LAST_LOGIN)) {
194                     lastLogin = df.parse(h.getValue());
195                 }
196                 else if(name.equals(Const.LAST_MODIFIED)) {
197                     lastModified = df.parse(h.getValue());
198                 }
199             }
200         }
201         
202         // Gather all unknown users for the groups
203         final List<String> userIDsWithUnknownDisplayName = new ArrayList<String>();
204         for(Map.Entry<String, List<String>> parsedGroupEntry : parsedGroups.entrySet()) {
205             final List<String> groupUserIDs = parsedGroupEntry.getValue();
206             userIDsWithUnknownDisplayName.addAll(app.filterUserIDsWithUnknownDisplayName(groupUserIDs));
207         }
208         if(userIDsWithUnknownDisplayName.size() == 0) {
209             for(Map.Entry<String, List<String>> parsedGroupEntry : parsedGroups.entrySet()) {
210                 final String groupName = parsedGroupEntry.getKey();
211                 final List<String> groupUserIDs = parsedGroupEntry.getValue();
212                 doAddUsersByIDs(groupUserIDs, groupName);
213             }
214         }
215         else {
216             new UpdateUserCatalogs(app, userIDsWithUnknownDisplayName) {
217                 @Override
218                 public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
219                     for(Map.Entry<String, List<String>> parsedGroupEntry : parsedGroups.entrySet()) {
220                         final String groupName = parsedGroupEntry.getKey();
221                         final List<String> groupUserIDs = parsedGroupEntry.getValue();
222                         doAddUsersByIDs(groupUserIDs, groupName);
223                     }
224                 }
225             }.scheduleEntry();
226         }
227
228         if(response.getText() != null && response.getText().length() > 0) {
229             containers.clear();
230             JSONValue json = JSONParser.parseStrict(response.getText());
231             JSONArray array = json.isArray();
232             if(array != null) {
233                 for(int i = 0; i < array.size(); i++) {
234                     JSONObject o = array.get(i).isObject();
235                     if(o != null) {
236                         Folder f = new Folder();
237                         f.populate(null, o, owner, null);
238                         containers.add(f);
239                     }
240                 }
241             }
242         }
243     }
244
245     public static AccountResource createFromResponse(Pithos app, String owner, Response response, AccountResource result) {
246         AccountResource a;
247         if(result == null) {
248             a = new AccountResource(app);
249         }
250         else {
251             a = result;
252         }
253         a.populate(owner, response);
254         return a;
255     }
256
257     private String getSize(Long size, Double division) {
258         Double res = Double.valueOf(size.toString()) / division;
259         NumberFormat nf = NumberFormat.getFormat(Const.NUMBER_FORMAT_1);
260         return nf.format(res);
261     }
262
263     public String getFileSizeAsString() {
264         if(bytesUsed < 1024) {
265             return String.valueOf(bytesUsed) + "B";
266         }
267         else if(bytesUsed < 1024 * 1024) {
268             return getSize(bytesUsed, 1024D) + "KB";
269         }
270         else if(bytesUsed < 1024 * 1024 * 1024) {
271             return getSize(bytesUsed, (1024D * 1024D)) + "MB";
272         }
273         return getSize(bytesUsed, (1024D * 1024D * 1024D)) + "GB";
274     }
275
276     public String getQuotaAsString() {
277         if(quota < 1024) {
278             return String.valueOf(quota) + "B";
279         }
280         else if(quota < 1024 * 1024) {
281             return getSize(quota, 1024D) + "KB";
282         }
283         else if(quota < 1024 * 1024 * 1024) {
284             return getSize(quota, (1024D * 1024D)) + "MB";
285         }
286         return getSize(quota, (1024D * 1024D * 1024D)) + "GB";
287     }
288
289     public List<Group> getGroups() {
290         return groups;
291     }
292
293     public boolean hasHomeContainer() {
294         for(Folder f : containers) {
295             if(f.getName().equals(Const.HOME_CONTAINER)) {
296                 return true;
297             }
298         }
299         return false;
300     }
301
302     public boolean hasTrashContainer() {
303         for(Folder f : containers) {
304             if(f.getName().equals(Const.TRASH_CONTAINER)) {
305                 return true;
306             }
307         }
308         return false;
309     }
310
311     public void addGroup(Group group) {
312         groups.add(group);
313     }
314
315     public void removeGroup(Group group) {
316         groups.remove(group);
317     }
318
319     public Folder getTrash() {
320         for(Folder c : containers) {
321             if(c.getName().equals(Const.TRASH_CONTAINER)) {
322                 return c;
323             }
324         }
325         return null;
326     }
327
328     public double getUsedPercentage() {
329         if(quota == 0) {
330             return 0;
331         }
332         return ((double) bytesUsed) / quota;
333     }
334
335     public Folder getPithos() {
336         for(Folder f : containers) {
337             if(f.getName().equals(Const.HOME_CONTAINER)) {
338                 return f;
339             }
340         }
341         return null;
342     }
343
344     public Group getGroup(String groupName) {
345         for(Group g : groups) {
346             if(g.getName().equalsIgnoreCase(groupName)) {
347                 return g;
348             }
349         }
350         return null;
351     }
352 }