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 gr.grnet.pithos.web.client.Pithos;
39 import gr.grnet.pithos.web.client.grouptree.Group;
41 import java.util.ArrayList;
42 import java.util.Date;
43 import java.util.List;
45 import com.google.gwt.http.client.Header;
46 import com.google.gwt.http.client.Response;
47 import com.google.gwt.http.client.URL;
48 import com.google.gwt.i18n.client.DateTimeFormat;
49 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
50 import com.google.gwt.i18n.client.NumberFormat;
51 import com.google.gwt.json.client.JSONArray;
52 import com.google.gwt.json.client.JSONObject;
53 import com.google.gwt.json.client.JSONParser;
54 import com.google.gwt.json.client.JSONValue;
57 * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
60 public class AccountResource extends Resource {
62 private long numberOfContainers = 0;
64 private long numberOfObjects = 0;
66 private long bytesUsed = 0;
68 private long quota = 0;
70 private Date lastLogin = null;
72 private Date lastModified = null;
74 private List<Folder> containers = new ArrayList<Folder>();
76 private Date currentLogin = null;
78 private List<Group> groups = new ArrayList<Group>();
80 public long getQuota() {
84 public void setQuota(long quota) {
88 public long getBytesUsed() {
92 public void setBytesUsed(long bytesUsed) {
93 this.bytesUsed = bytesUsed;
96 public Date getLastLogin() {
100 public void setLastLogin(Date lastLogin) {
101 this.lastLogin = lastLogin;
105 public Date getLastModified() {
109 public void setLastModified(Date lastModified) {
110 this.lastModified = lastModified;
113 public long getNumberOfContainers() {
114 return numberOfContainers;
117 public void setNumberOfContainers(long numberOfContainers) {
118 this.numberOfContainers = numberOfContainers;
121 public long getNumberOfObjects() {
122 return numberOfObjects;
125 public void setNumberOfObjects(long numberOfObjects) {
126 this.numberOfObjects = numberOfObjects;
129 public List<Folder> getContainers() {
133 public Date getCurrentLogin() {
137 public void setCurrentLogin(Date currentLogin) {
138 this.currentLogin = currentLogin;
141 public void populate(String owner, Response response) {
142 DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
144 for (Header h : response.getHeaders()) {
146 String name = h.getName();
147 if (name.startsWith("X-Account-Group-")) {
148 String groupName = URL.decodePathSegment(name.substring("X-Account-Group-".length()));
149 Group g = new Group(groupName);
150 String[] members = h.getValue().split(",");
151 for (String s : members)
152 g.addMember(URL.decodePathSegment(s).trim());
155 else if (name.equals("X-Account-Container-Count")) {
156 numberOfContainers = Long.valueOf(h.getValue());
158 else if (name.equals("X-Account-Object-Count")) {
159 numberOfObjects = Long.valueOf(h.getValue());
161 else if (name.equals("X-Account-Bytes-Used")) {
162 bytesUsed = Long.valueOf(h.getValue());
164 else if (name.equals("X-Account-Policy-Quota")) {
165 quota = Long.valueOf(h.getValue());
167 else if (name.equals("X-Account-Last-Login")) {
168 lastLogin = df.parse(h.getValue());
170 else if (name.equals("Last-Modified")) {
171 lastModified = df.parse(h.getValue());
176 if (response.getText() != null && response.getText().length() > 0) {
178 JSONValue json = JSONParser.parseStrict(response.getText());
179 JSONArray array = json.isArray();
181 for (int i=0; i<array.size(); i++) {
182 JSONObject o = array.get(i).isObject();
184 Folder f = new Folder();
185 f.populate(null, o, owner, null);
193 public static AccountResource createFromResponse(String owner, Response response, AccountResource result) {
196 a = new AccountResource();
199 a.populate(owner, response);
203 private String getSize(Long size, Double division){
204 Double res = Double.valueOf(size.toString())/division;
205 NumberFormat nf = NumberFormat.getFormat("######.#");
206 return nf.format(res);
209 public String getFileSizeAsString() {
210 if (bytesUsed < 1024)
211 return String.valueOf(bytesUsed) + "B";
212 else if (bytesUsed < 1024*1024)
213 return getSize(bytesUsed, 1024D) + "KB";
214 else if (bytesUsed < 1024*1024*1024)
215 return getSize(bytesUsed,(1024D*1024D)) + "MB";
216 return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB";
219 public String getQuotaAsString() {
221 return String.valueOf(quota) + "B";
222 else if (quota < 1024 * 1024)
223 return getSize(quota, 1024D) + "KB";
224 else if (quota < 1024 * 1024 * 1024)
225 return getSize(quota,(1024D * 1024D)) + "MB";
226 return getSize(quota , (1024D * 1024D * 1024D)) + "GB";
229 public List<Group> getGroups() {
233 public boolean hasHomeContainer() {
234 for (Folder f : containers)
235 if (f.getName().equals(Pithos.HOME_CONTAINER))
240 public boolean hasTrashContainer() {
241 for (Folder f : containers)
242 if (f.getName().equals(Pithos.TRASH_CONTAINER))
247 public void addGroup(Group newGroup) {
248 groups.add(newGroup);
251 public void removeGroup(Group group) {
252 groups.remove(group);
255 public Folder getTrash() {
256 for (Folder c : containers) {
257 if (c.getName().equals(Pithos.TRASH_CONTAINER))
263 public double getUsedPercentage() {
266 return ((double) bytesUsed) / quota;
269 public Folder getPithos() {
270 for (Folder f : containers)
271 if (f.getName().equals(Pithos.HOME_CONTAINER))