2 * Copyright 2011 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;
40 import java.util.ArrayList;
41 import java.util.Date;
42 import java.util.List;
44 import com.google.gwt.http.client.Header;
45 import com.google.gwt.http.client.Response;
46 import com.google.gwt.i18n.client.DateTimeFormat;
47 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
48 import com.google.gwt.i18n.client.NumberFormat;
49 import com.google.gwt.json.client.JSONArray;
50 import com.google.gwt.json.client.JSONObject;
51 import com.google.gwt.json.client.JSONParser;
52 import com.google.gwt.json.client.JSONValue;
55 * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
58 public class AccountResource extends Resource {
60 private long numberOfContainers = 0;
62 private long numberOfObjects = 0;
64 private long bytesUsed = 0;
66 private long bytesRemaining = 0;
68 private Date lastLogin = null;
70 private Date lastModified = null;
72 private List<Folder> containers = new ArrayList<Folder>();
74 private Date currentLogin = null;
76 private List<Group> groups = new ArrayList<Group>();
78 public long getBytesRemaining() {
79 return bytesRemaining;
82 public void setBytesRemaining(long bytesRemaining) {
83 this.bytesRemaining = bytesRemaining;
86 public long getBytesUsed() {
90 public void setBytesUsed(long bytesUsed) {
91 this.bytesUsed = bytesUsed;
94 public Date getLastLogin() {
98 public void setLastLogin(Date lastLogin) {
99 this.lastLogin = lastLogin;
102 public Date getLastModified() {
106 public void setLastModified(Date lastModified) {
107 this.lastModified = lastModified;
110 public long getNumberOfContainers() {
111 return numberOfContainers;
114 public void setNumberOfContainers(long numberOfContainers) {
115 this.numberOfContainers = numberOfContainers;
118 public long getNumberOfObjects() {
119 return numberOfObjects;
122 public void setNumberOfObjects(long numberOfObjects) {
123 this.numberOfObjects = numberOfObjects;
126 public List<Folder> getContainers() {
130 public Date getCurrentLogin() {
134 public void setCurrentLogin(Date currentLogin) {
135 this.currentLogin = currentLogin;
138 public void populate(String owner, Response response) {
139 DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
140 for (Header h : response.getHeaders()) {
141 String name = h.getName();
142 if (name.startsWith("X-Account-Group-")) {
143 String groupName = name.substring("X-Account-Group-".length()).trim().toLowerCase();
144 Group g = new Group(groupName);
145 String[] members = h.getValue().split(",");
146 for (String s : members)
147 g.addMember(s.trim());
150 else if (name.equals("X-Account-Container-Count")) {
151 numberOfContainers = Long.valueOf(h.getValue());
153 else if (name.equals("X-Account-Object-Count")) {
154 numberOfObjects = Long.valueOf(h.getValue());
156 else if (name.equals("X-Account-Bytes-Used")) {
157 bytesUsed = Long.valueOf(h.getValue());
159 else if (name.equals("X-Account-Bytes-Remaining")) {
160 bytesRemaining = Long.valueOf(h.getValue());
162 else if (name.equals("X-Account-Last-Login")) {
163 lastLogin = df.parse(h.getValue());
165 else if (name.equals("Last-Modified")) {
166 lastModified = df.parse(h.getValue());
170 JSONValue json = JSONParser.parseStrict(response.getText());
171 JSONArray array = json.isArray();
173 for (int i=0; i<array.size(); i++) {
174 JSONObject o = array.get(i).isObject();
176 Folder f = new Folder();
177 f.populate(null, o, owner, null);
184 public static AccountResource createFromResponse(String owner, Response response) {
185 AccountResource a = new AccountResource();
186 a.populate(owner, response);
190 private String getSize(Long size, Double division){
191 Double res = Double.valueOf(size.toString())/division;
192 NumberFormat nf = NumberFormat.getFormat("######.#");
193 return nf.format(res);
196 public String getFileSizeAsString() {
197 if (bytesUsed < 1024)
198 return String.valueOf(bytesUsed) + " B";
199 else if (bytesUsed < 1024*1024)
200 return getSize(bytesUsed, 1024D) + " KB";
201 else if (bytesUsed < 1024*1024*1024)
202 return getSize(bytesUsed,(1024D*1024D)) + " MB";
203 return getSize(bytesUsed , (1024D*1024D*1024D)) + " GB";
206 public String getQuotaLeftAsString() {
207 if (bytesRemaining < 1024)
208 return String.valueOf(bytesRemaining) + " B";
209 else if (bytesRemaining < 1024 * 1024)
210 return getSize(bytesRemaining, 1024D) + " KB";
211 else if (bytesRemaining < 1024 * 1024 * 1024)
212 return getSize(bytesRemaining,(1024D * 1024D)) + " MB";
213 return getSize(bytesRemaining , (1024D * 1024D * 1024D)) + " GB";
216 public List<Group> getGroups() {
220 public boolean hasHomeContainer() {
221 for (Folder f : containers)
222 if (f.getName().equals(Pithos.HOME_CONTAINER))
227 public boolean hasTrashContainer() {
228 for (Folder f : containers)
229 if (f.getName().equals(Pithos.TRASH_CONTAINER))