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