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;
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.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;
56 * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
59 public class AccountResource extends Resource {
61 private long numberOfContainers = 0;
63 private long numberOfObjects = 0;
65 private long bytesUsed = 0;
67 private long bytesRemaining = 0;
69 private Date lastLogin = null;
71 private Date lastModified = null;
73 private List<Folder> containers = new ArrayList<Folder>();
75 private Date currentLogin = null;
77 private List<Group> groups = new ArrayList<Group>();
79 public long getBytesRemaining() {
80 return bytesRemaining;
83 public void setBytesRemaining(long bytesRemaining) {
84 this.bytesRemaining = bytesRemaining;
87 public long getBytesUsed() {
91 public void setBytesUsed(long bytesUsed) {
92 this.bytesUsed = bytesUsed;
95 public Date getLastLogin() {
99 public void setLastLogin(Date lastLogin) {
100 this.lastLogin = lastLogin;
104 public Date getLastModified() {
108 public void setLastModified(Date lastModified) {
109 this.lastModified = lastModified;
112 public long getNumberOfContainers() {
113 return numberOfContainers;
116 public void setNumberOfContainers(long numberOfContainers) {
117 this.numberOfContainers = numberOfContainers;
120 public long getNumberOfObjects() {
121 return numberOfObjects;
124 public void setNumberOfObjects(long numberOfObjects) {
125 this.numberOfObjects = numberOfObjects;
128 public List<Folder> getContainers() {
132 public Date getCurrentLogin() {
136 public void setCurrentLogin(Date currentLogin) {
137 this.currentLogin = currentLogin;
140 public void populate(String owner, Response response) {
141 DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
142 for (Header h : response.getHeaders()) {
144 String name = h.getName();
145 if (name.startsWith("X-Account-Group-")) {
146 String groupName = name.substring("X-Account-Group-".length()).trim().toLowerCase();
147 Group g = new Group(groupName);
148 String[] members = h.getValue().split(",");
149 for (String s : members)
150 g.addMember(s.trim());
153 else if (name.equals("X-Account-Container-Count")) {
154 numberOfContainers = Long.valueOf(h.getValue());
156 else if (name.equals("X-Account-Object-Count")) {
157 numberOfObjects = Long.valueOf(h.getValue());
159 else if (name.equals("X-Account-Bytes-Used")) {
160 bytesUsed = Long.valueOf(h.getValue());
162 else if (name.equals("X-Account-Bytes-Remaining")) {
163 bytesRemaining = Long.valueOf(h.getValue());
165 else if (name.equals("X-Account-Last-Login")) {
166 lastLogin = df.parse(h.getValue());
168 else if (name.equals("Last-Modified")) {
169 lastModified = df.parse(h.getValue());
174 if (response.getText() != null && response.getText().length() > 0) {
175 JSONValue json = JSONParser.parseStrict(response.getText());
176 JSONArray array = json.isArray();
178 for (int i=0; i<array.size(); i++) {
179 JSONObject o = array.get(i).isObject();
181 Folder f = new Folder();
182 f.populate(null, o, owner, null);
190 public static AccountResource createFromResponse(String owner, Response response, AccountResource result) {
193 a = new AccountResource();
196 a.populate(owner, response);
200 private String getSize(Long size, Double division){
201 Double res = Double.valueOf(size.toString())/division;
202 NumberFormat nf = NumberFormat.getFormat("######.#");
203 return nf.format(res);
206 public String getFileSizeAsString() {
207 if (bytesUsed < 1024)
208 return String.valueOf(bytesUsed) + "B";
209 else if (bytesUsed < 1024*1024)
210 return getSize(bytesUsed, 1024D) + "KB";
211 else if (bytesUsed < 1024*1024*1024)
212 return getSize(bytesUsed,(1024D*1024D)) + "MB";
213 return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB";
216 public String getQuotaAsString() {
217 long quota = bytesUsed + bytesRemaining;
219 return String.valueOf(quota) + "B";
220 else if (quota < 1024 * 1024)
221 return getSize(quota, 1024D) + "KB";
222 else if (quota < 1024 * 1024 * 1024)
223 return getSize(quota,(1024D * 1024D)) + "MB";
224 return getSize(quota , (1024D * 1024D * 1024D)) + "GB";
227 public List<Group> getGroups() {
231 public boolean hasHomeContainer() {
232 for (Folder f : containers)
233 if (f.getName().equals(Pithos.HOME_CONTAINER))
238 public boolean hasTrashContainer() {
239 for (Folder f : containers)
240 if (f.getName().equals(Pithos.TRASH_CONTAINER))
245 public void addGroup(Group newGroup) {
246 groups.add(newGroup);
249 public void removeGroup(Group group) {
250 groups.remove(group);
253 public Folder getTrash() {
254 for (Folder c : containers) {
255 if (c.getName().equals(Pithos.TRASH_CONTAINER))
261 public double getUsedPercentage() {
262 return ((double) bytesUsed) / (bytesUsed + bytesRemaining);
265 public Folder getPithos() {
266 for (Folder f : containers)
267 if (f.getName().equals(Pithos.HOME_CONTAINER))