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.Const;
39 import gr.grnet.pithos.web.client.Resource;
40 import gr.grnet.pithos.web.client.grouptree.Group;
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.List;
46 import com.google.gwt.http.client.Header;
47 import com.google.gwt.http.client.Response;
48 import com.google.gwt.http.client.URL;
49 import com.google.gwt.i18n.client.DateTimeFormat;
50 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
51 import com.google.gwt.i18n.client.NumberFormat;
52 import com.google.gwt.json.client.JSONArray;
53 import com.google.gwt.json.client.JSONObject;
54 import com.google.gwt.json.client.JSONParser;
55 import com.google.gwt.json.client.JSONValue;
58 * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
61 public class AccountResource extends Resource {
63 private long numberOfContainers = 0;
65 private long numberOfObjects = 0;
67 private long bytesUsed = 0;
69 private long quota = 0;
71 private Date lastLogin = null;
73 private Date lastModified = null;
75 private List<Folder> containers = new ArrayList<Folder>();
77 private Date currentLogin = null;
79 private List<Group> groups = new ArrayList<Group>();
81 public long getQuota() {
85 public void setQuota(long quota) {
89 public long getBytesUsed() {
93 public void setBytesUsed(long bytesUsed) {
94 this.bytesUsed = bytesUsed;
97 public Date getLastLogin() {
101 public void setLastLogin(Date lastLogin) {
102 this.lastLogin = lastLogin;
106 public Date getLastModified() {
110 public void setLastModified(Date lastModified) {
111 this.lastModified = lastModified;
114 public long getNumberOfContainers() {
115 return numberOfContainers;
118 public void setNumberOfContainers(long numberOfContainers) {
119 this.numberOfContainers = numberOfContainers;
122 public long getNumberOfObjects() {
123 return numberOfObjects;
126 public void setNumberOfObjects(long numberOfObjects) {
127 this.numberOfObjects = numberOfObjects;
130 public List<Folder> getContainers() {
134 public Date getCurrentLogin() {
138 public void setCurrentLogin(Date currentLogin) {
139 this.currentLogin = currentLogin;
142 public void populate(String owner, Response response) {
143 DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
145 for (Header h : response.getHeaders()) {
147 String name = h.getName();
148 if (name.startsWith("X-Account-Group-")) {
149 String groupName = URL.decodePathSegment(name.substring("X-Account-Group-".length()));
150 Group g = new Group(groupName);
151 String[] members = h.getValue().split(",");
152 for (String s : members)
153 g.addMember(URL.decodePathSegment(s).trim());
156 else if (name.equals("X-Account-Container-Count")) {
157 numberOfContainers = Long.valueOf(h.getValue());
159 else if (name.equals("X-Account-Object-Count")) {
160 numberOfObjects = Long.valueOf(h.getValue());
162 else if (name.equals("X-Account-Bytes-Used")) {
163 bytesUsed = Long.valueOf(h.getValue());
165 else if (name.equals("X-Account-Policy-Quota")) {
166 quota = Long.valueOf(h.getValue());
168 else if (name.equals("X-Account-Last-Login")) {
169 lastLogin = df.parse(h.getValue());
171 else if (name.equals("Last-Modified")) {
172 lastModified = df.parse(h.getValue());
177 if (response.getText() != null && response.getText().length() > 0) {
179 JSONValue json = JSONParser.parseStrict(response.getText());
180 JSONArray array = json.isArray();
182 for (int i=0; i<array.size(); i++) {
183 JSONObject o = array.get(i).isObject();
185 Folder f = new Folder();
186 f.populate(null, o, owner, null);
194 public static AccountResource createFromResponse(String owner, Response response, AccountResource result) {
197 a = new AccountResource();
200 a.populate(owner, response);
204 private String getSize(Long size, Double division){
205 Double res = Double.valueOf(size.toString())/division;
206 NumberFormat nf = NumberFormat.getFormat("######.#");
207 return nf.format(res);
210 public String getFileSizeAsString() {
211 if (bytesUsed < 1024)
212 return String.valueOf(bytesUsed) + "B";
213 else if (bytesUsed < 1024*1024)
214 return getSize(bytesUsed, 1024D) + "KB";
215 else if (bytesUsed < 1024*1024*1024)
216 return getSize(bytesUsed,(1024D*1024D)) + "MB";
217 return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB";
220 public String getQuotaAsString() {
222 return String.valueOf(quota) + "B";
223 else if (quota < 1024 * 1024)
224 return getSize(quota, 1024D) + "KB";
225 else if (quota < 1024 * 1024 * 1024)
226 return getSize(quota,(1024D * 1024D)) + "MB";
227 return getSize(quota , (1024D * 1024D * 1024D)) + "GB";
230 public List<Group> getGroups() {
234 public boolean hasHomeContainer() {
235 for (Folder f : containers)
236 if (f.getName().equals(Const.HOME_CONTAINER))
241 public boolean hasTrashContainer() {
242 for (Folder f : containers)
243 if (f.getName().equals(Const.TRASH_CONTAINER))
248 public void addGroup(Group newGroup) {
249 groups.add(newGroup);
252 public void removeGroup(Group group) {
253 groups.remove(group);
256 public Folder getTrash() {
257 for (Folder c : containers) {
258 if (c.getName().equals(Const.TRASH_CONTAINER))
264 public double getUsedPercentage() {
267 return ((double) bytesUsed) / quota;
270 public Folder getPithos() {
271 for (Folder f : containers)
272 if (f.getName().equals(Const.HOME_CONTAINER))
277 public Group getGroup(String groupName) {
278 for (Group g : groups)
279 if (g.getName().equalsIgnoreCase(groupName))