2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.pithos.web.client.foldertree;
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;
59 * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
62 public class AccountResource extends Resource {
63 private final Pithos app;
65 private long numberOfContainers = 0;
67 private long numberOfObjects = 0;
69 private long bytesUsed = 0;
71 private long quota = 0;
73 private Date lastLogin = null;
75 private Date lastModified = null;
77 private List<Folder> containers = new ArrayList<Folder>();
79 private Date currentLogin = null;
81 private List<Group> groups = new ArrayList<Group>();
83 public AccountResource(Pithos app) {
87 public long getQuota() {
91 public void setQuota(long quota) {
95 public long getBytesUsed() {
99 public void setBytesUsed(long bytesUsed) {
100 this.bytesUsed = bytesUsed;
103 public Date getLastLogin() {
107 public void setLastLogin(Date lastLogin) {
108 this.lastLogin = lastLogin;
112 public Date getLastModified() {
116 public void setLastModified(Date lastModified) {
117 this.lastModified = lastModified;
120 public long getNumberOfContainers() {
121 return numberOfContainers;
124 public void setNumberOfContainers(long numberOfContainers) {
125 this.numberOfContainers = numberOfContainers;
128 public long getNumberOfObjects() {
129 return numberOfObjects;
132 public void setNumberOfObjects(long numberOfObjects) {
133 this.numberOfObjects = numberOfObjects;
136 public List<Folder> getContainers() {
140 public Date getCurrentLogin() {
144 public void setCurrentLogin(Date currentLogin) {
145 this.currentLogin = currentLogin;
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);
155 app.LOG("AccountResource::doAddUsersByIDs(), user = (", userID, ", ", userDisplayName, ")");
162 public void populate(String owner, Response response) {
163 DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
166 final Map<String, List<String>> parsedGroups = new HashMap<String, List<String>>();
168 for(Header h : response.getHeaders()) {
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();
179 parsedGroups.put(groupName, userIDs);
181 else if(name.equals(Const.X_ACCOUNT_CONTAINER_COUNT)) {
182 numberOfContainers = Long.valueOf(h.getValue());
184 else if(name.equals(Const.X_ACCOUNT_OBJECT_COUNT)) {
185 numberOfObjects = Long.valueOf(h.getValue());
187 else if(name.equals(Const.X_ACCOUNT_BYTES_USED)) {
188 bytesUsed = Long.valueOf(h.getValue());
190 else if(name.equals(Const.X_ACCOUNT_POLICY_QUOTA)) {
191 quota = Long.valueOf(h.getValue());
193 else if(name.equals(Const.X_ACCOUNT_LAST_LOGIN)) {
194 lastLogin = df.parse(h.getValue());
196 else if(name.equals(Const.LAST_MODIFIED)) {
197 lastModified = df.parse(h.getValue());
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));
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);
216 new UpdateUserCatalogs(app, userIDsWithUnknownDisplayName) {
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);
228 if(response.getText() != null && response.getText().length() > 0) {
230 JSONValue json = JSONParser.parseStrict(response.getText());
231 JSONArray array = json.isArray();
233 for(int i = 0; i < array.size(); i++) {
234 JSONObject o = array.get(i).isObject();
236 Folder f = new Folder();
237 f.populate(null, o, owner, null);
245 public static AccountResource createFromResponse(Pithos app, String owner, Response response, AccountResource result) {
248 a = new AccountResource(app);
253 a.populate(owner, response);
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);
263 public String getFileSizeAsString() {
264 if(bytesUsed < 1024) {
265 return String.valueOf(bytesUsed) + "B";
267 else if(bytesUsed < 1024 * 1024) {
268 return getSize(bytesUsed, 1024D) + "KB";
270 else if(bytesUsed < 1024 * 1024 * 1024) {
271 return getSize(bytesUsed, (1024D * 1024D)) + "MB";
273 return getSize(bytesUsed, (1024D * 1024D * 1024D)) + "GB";
276 public String getQuotaAsString() {
278 return String.valueOf(quota) + "B";
280 else if(quota < 1024 * 1024) {
281 return getSize(quota, 1024D) + "KB";
283 else if(quota < 1024 * 1024 * 1024) {
284 return getSize(quota, (1024D * 1024D)) + "MB";
286 return getSize(quota, (1024D * 1024D * 1024D)) + "GB";
289 public List<Group> getGroups() {
293 public boolean hasHomeContainer() {
294 for(Folder f : containers) {
295 if(f.getName().equals(Const.HOME_CONTAINER)) {
302 public boolean hasTrashContainer() {
303 for(Folder f : containers) {
304 if(f.getName().equals(Const.TRASH_CONTAINER)) {
311 public void addGroup(Group group) {
315 public void removeGroup(Group group) {
316 groups.remove(group);
319 public Folder getTrash() {
320 for(Folder c : containers) {
321 if(c.getName().equals(Const.TRASH_CONTAINER)) {
328 public double getUsedPercentage() {
332 return ((double) bytesUsed) / quota;
335 public Folder getPithos() {
336 for(Folder f : containers) {
337 if(f.getName().equals(Const.HOME_CONTAINER)) {
344 public Group getGroup(String groupName) {
345 for(Group g : groups) {
346 if(g.getName().equalsIgnoreCase(groupName)) {