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