root / web_client / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java @ 3ca8cd89
History | View | Annotate | Download (6.7 kB)
1 | 9e8e14e4 | Christos Stathis | /*
|
---|---|---|---|
2 | 58777026 | Christos Stathis | * Copyright 2011 GRNET S.A. All rights reserved.
|
3 | 58777026 | Christos Stathis | *
|
4 | 58777026 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 58777026 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 58777026 | Christos Stathis | * conditions are met:
|
7 | 58777026 | Christos Stathis | *
|
8 | 58777026 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 58777026 | Christos Stathis | * disclaimer.
|
11 | 58777026 | Christos Stathis | *
|
12 | 58777026 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 58777026 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 58777026 | Christos Stathis | * provided with the distribution.
|
16 | 58777026 | Christos Stathis | *
|
17 | 58777026 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 58777026 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 58777026 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 58777026 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 58777026 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 58777026 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 58777026 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 58777026 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 58777026 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 58777026 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 58777026 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 58777026 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 58777026 | Christos Stathis | *
|
30 | 58777026 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 58777026 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 58777026 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 58777026 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | 9e8e14e4 | Christos Stathis | */
|
35 | 9e8e14e4 | Christos Stathis | |
36 | 9e8e14e4 | Christos Stathis | package gr.grnet.pithos.web.client.foldertree; |
37 | 9e8e14e4 | Christos Stathis | |
38 | 9e8e14e4 | Christos Stathis | import com.google.gwt.http.client.Header; |
39 | 5cd18037 | Christos Stathis | import com.google.gwt.http.client.Response; |
40 | 5cd18037 | Christos Stathis | import com.google.gwt.i18n.client.DateTimeFormat; |
41 | 5cd18037 | Christos Stathis | import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; |
42 | 5cd18037 | Christos Stathis | import com.google.gwt.i18n.client.NumberFormat; |
43 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONArray; |
44 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONObject; |
45 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONParser; |
46 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONValue; |
47 | 9e8e14e4 | Christos Stathis | import gr.grnet.pithos.web.client.rest.resource.RestResource; |
48 | 9e8e14e4 | Christos Stathis | import java.util.ArrayList; |
49 | 5cd18037 | Christos Stathis | import java.util.Date; |
50 | 5cd18037 | Christos Stathis | import java.util.LinkedHashSet; |
51 | 9e8e14e4 | Christos Stathis | import java.util.List; |
52 | 5cd18037 | Christos Stathis | import java.util.Set; |
53 | 9e8e14e4 | Christos Stathis | |
54 | 9e8e14e4 | Christos Stathis | /**
|
55 | 9e8e14e4 | Christos Stathis | * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
|
56 | 9e8e14e4 | Christos Stathis | * File Templates.
|
57 | 9e8e14e4 | Christos Stathis | */
|
58 | 9e8e14e4 | Christos Stathis | public class AccountResource extends Resource { |
59 | 9e8e14e4 | Christos Stathis | |
60 | 5cd18037 | Christos Stathis | private long numberOfContainers = 0; |
61 | 5cd18037 | Christos Stathis | |
62 | 5cd18037 | Christos Stathis | private long numberOfObjects = 0; |
63 | 5cd18037 | Christos Stathis | |
64 | 5cd18037 | Christos Stathis | private long bytesUsed = 0; |
65 | 5cd18037 | Christos Stathis | |
66 | 5cd18037 | Christos Stathis | private long bytesRemaining = 0; |
67 | 5cd18037 | Christos Stathis | |
68 | 5cd18037 | Christos Stathis | private Date lastLogin = null; |
69 | 5cd18037 | Christos Stathis | |
70 | 5cd18037 | Christos Stathis | private Date lastModified = null; |
71 | 5cd18037 | Christos Stathis | |
72 | 5cd18037 | Christos Stathis | private Set<Folder> containers = new LinkedHashSet<Folder>(); |
73 | 5cd18037 | Christos Stathis | |
74 | 5cd18037 | Christos Stathis | private Date currentLogin = null; |
75 | 5cd18037 | Christos Stathis | |
76 | 5cd18037 | Christos Stathis | public long getBytesRemaining() { |
77 | 5cd18037 | Christos Stathis | return bytesRemaining;
|
78 | 5cd18037 | Christos Stathis | } |
79 | 5cd18037 | Christos Stathis | |
80 | 5cd18037 | Christos Stathis | public void setBytesRemaining(long bytesRemaining) { |
81 | 5cd18037 | Christos Stathis | this.bytesRemaining = bytesRemaining;
|
82 | 5cd18037 | Christos Stathis | } |
83 | 5cd18037 | Christos Stathis | |
84 | 5cd18037 | Christos Stathis | public long getBytesUsed() { |
85 | 5cd18037 | Christos Stathis | return bytesUsed;
|
86 | 5cd18037 | Christos Stathis | } |
87 | 5cd18037 | Christos Stathis | |
88 | 5cd18037 | Christos Stathis | public void setBytesUsed(long bytesUsed) { |
89 | 5cd18037 | Christos Stathis | this.bytesUsed = bytesUsed;
|
90 | 5cd18037 | Christos Stathis | } |
91 | 5cd18037 | Christos Stathis | |
92 | 5cd18037 | Christos Stathis | public Date getLastLogin() { |
93 | 5cd18037 | Christos Stathis | return lastLogin;
|
94 | 5cd18037 | Christos Stathis | } |
95 | 5cd18037 | Christos Stathis | |
96 | 5cd18037 | Christos Stathis | public void setLastLogin(Date lastLogin) { |
97 | 5cd18037 | Christos Stathis | this.lastLogin = lastLogin;
|
98 | 5cd18037 | Christos Stathis | } |
99 | 5cd18037 | Christos Stathis | |
100 | 5cd18037 | Christos Stathis | public Date getLastModified() { |
101 | 5cd18037 | Christos Stathis | return lastModified;
|
102 | 5cd18037 | Christos Stathis | } |
103 | 5cd18037 | Christos Stathis | |
104 | 5cd18037 | Christos Stathis | public void setLastModified(Date lastModified) { |
105 | 5cd18037 | Christos Stathis | this.lastModified = lastModified;
|
106 | 5cd18037 | Christos Stathis | } |
107 | 5cd18037 | Christos Stathis | |
108 | 5cd18037 | Christos Stathis | public long getNumberOfContainers() { |
109 | 5cd18037 | Christos Stathis | return numberOfContainers;
|
110 | 5cd18037 | Christos Stathis | } |
111 | 5cd18037 | Christos Stathis | |
112 | 5cd18037 | Christos Stathis | public void setNumberOfContainers(long numberOfContainers) { |
113 | 5cd18037 | Christos Stathis | this.numberOfContainers = numberOfContainers;
|
114 | 5cd18037 | Christos Stathis | } |
115 | 5cd18037 | Christos Stathis | |
116 | 5cd18037 | Christos Stathis | public long getNumberOfObjects() { |
117 | 5cd18037 | Christos Stathis | return numberOfObjects;
|
118 | 5cd18037 | Christos Stathis | } |
119 | 5cd18037 | Christos Stathis | |
120 | 5cd18037 | Christos Stathis | public void setNumberOfObjects(long numberOfObjects) { |
121 | 5cd18037 | Christos Stathis | this.numberOfObjects = numberOfObjects;
|
122 | 5cd18037 | Christos Stathis | } |
123 | 9e8e14e4 | Christos Stathis | |
124 | 9e8e14e4 | Christos Stathis | @Override
|
125 | 9e8e14e4 | Christos Stathis | public String getLastModifiedSince() { |
126 | 9e8e14e4 | Christos Stathis | return null; |
127 | 9e8e14e4 | Christos Stathis | } |
128 | 9e8e14e4 | Christos Stathis | |
129 | 5cd18037 | Christos Stathis | public Set<Folder> getContainers() { |
130 | 9e8e14e4 | Christos Stathis | return containers;
|
131 | 9e8e14e4 | Christos Stathis | } |
132 | 5cd18037 | Christos Stathis | |
133 | 5cd18037 | Christos Stathis | public Date getCurrentLogin() { |
134 | 5cd18037 | Christos Stathis | return currentLogin;
|
135 | 5cd18037 | Christos Stathis | } |
136 | 5cd18037 | Christos Stathis | |
137 | 5cd18037 | Christos Stathis | public void setCurrentLogin(Date currentLogin) { |
138 | 5cd18037 | Christos Stathis | this.currentLogin = currentLogin;
|
139 | 5cd18037 | Christos Stathis | } |
140 | 5cd18037 | Christos Stathis | |
141 | 5cd18037 | Christos Stathis | public void populate(Response response) { |
142 | 5cd18037 | Christos Stathis | String header = response.getHeader("X-Account-Container-Count"); |
143 | 5cd18037 | Christos Stathis | if (header != null) |
144 | 5cd18037 | Christos Stathis | numberOfContainers = Long.valueOf(header);
|
145 | 5cd18037 | Christos Stathis | |
146 | 5cd18037 | Christos Stathis | header = response.getHeader("X-Account-Object-Count");
|
147 | 5cd18037 | Christos Stathis | if (header != null) |
148 | 5cd18037 | Christos Stathis | numberOfObjects = Long.valueOf(header);
|
149 | 5cd18037 | Christos Stathis | |
150 | 5cd18037 | Christos Stathis | header = response.getHeader("X-Account-Bytes-Used");
|
151 | 5cd18037 | Christos Stathis | if (header != null) |
152 | 5cd18037 | Christos Stathis | bytesUsed = Long.valueOf(header);
|
153 | 5cd18037 | Christos Stathis | |
154 | 5cd18037 | Christos Stathis | header = response.getHeader("X-Account-Bytes-Remaining");
|
155 | 5cd18037 | Christos Stathis | if (header != null) |
156 | 5cd18037 | Christos Stathis | bytesRemaining = Long.valueOf(header);
|
157 | 5cd18037 | Christos Stathis | |
158 | 5cd18037 | Christos Stathis | DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822); |
159 | 5cd18037 | Christos Stathis | header = response.getHeader("X-Account-Last-Login");
|
160 | 5cd18037 | Christos Stathis | if (header != null) |
161 | 5cd18037 | Christos Stathis | lastLogin = df.parse(header); |
162 | 5cd18037 | Christos Stathis | |
163 | 5cd18037 | Christos Stathis | header = response.getHeader("Last-Modified");
|
164 | 5cd18037 | Christos Stathis | if (header != null) |
165 | 5cd18037 | Christos Stathis | lastModified = df.parse(header); |
166 | 5cd18037 | Christos Stathis | |
167 | 5cd18037 | Christos Stathis | JSONValue json = JSONParser.parseStrict(response.getText()); |
168 | 5cd18037 | Christos Stathis | JSONArray array = json.isArray(); |
169 | 5cd18037 | Christos Stathis | if (array != null) { |
170 | 5cd18037 | Christos Stathis | for (int i=0; i<array.size(); i++) { |
171 | 5cd18037 | Christos Stathis | JSONObject o = array.get(i).isObject(); |
172 | 5cd18037 | Christos Stathis | if (o != null) { |
173 | 5cd18037 | Christos Stathis | Folder f = new Folder();
|
174 | 3ca8cd89 | Christos Stathis | f.populate(null, o, null); |
175 | 5cd18037 | Christos Stathis | containers.add(f); |
176 | 5cd18037 | Christos Stathis | } |
177 | 5cd18037 | Christos Stathis | } |
178 | 5cd18037 | Christos Stathis | } |
179 | 5cd18037 | Christos Stathis | } |
180 | 5cd18037 | Christos Stathis | |
181 | 5cd18037 | Christos Stathis | public static AccountResource createFromResponse(Response response) { |
182 | 5cd18037 | Christos Stathis | AccountResource a = new AccountResource();
|
183 | 5cd18037 | Christos Stathis | a.populate(response); |
184 | 5cd18037 | Christos Stathis | return a;
|
185 | 5cd18037 | Christos Stathis | } |
186 | 5cd18037 | Christos Stathis | |
187 | 5cd18037 | Christos Stathis | private String getSize(Long size, Double division){ |
188 | 5cd18037 | Christos Stathis | Double res = Double.valueOf(size.toString())/division; |
189 | 5cd18037 | Christos Stathis | NumberFormat nf = NumberFormat.getFormat("######.#"); |
190 | 5cd18037 | Christos Stathis | return nf.format(res);
|
191 | 5cd18037 | Christos Stathis | } |
192 | 5cd18037 | Christos Stathis | |
193 | 5cd18037 | Christos Stathis | public String getFileSizeAsString() { |
194 | 5cd18037 | Christos Stathis | if (bytesUsed < 1024) |
195 | 5cd18037 | Christos Stathis | return String.valueOf(bytesUsed) + " B"; |
196 | 5cd18037 | Christos Stathis | else if (bytesUsed < 1024*1024) |
197 | 5cd18037 | Christos Stathis | return getSize(bytesUsed, 1024D) + " KB"; |
198 | 5cd18037 | Christos Stathis | else if (bytesUsed < 1024*1024*1024) |
199 | 5cd18037 | Christos Stathis | return getSize(bytesUsed,(1024D*1024D)) + " MB"; |
200 | 5cd18037 | Christos Stathis | return getSize(bytesUsed , (1024D*1024D*1024D)) + " GB"; |
201 | 5cd18037 | Christos Stathis | } |
202 | 5cd18037 | Christos Stathis | |
203 | 5cd18037 | Christos Stathis | public String getQuotaLeftAsString() { |
204 | 5cd18037 | Christos Stathis | if (bytesRemaining < 1024) |
205 | 5cd18037 | Christos Stathis | return String.valueOf(bytesRemaining) + " B"; |
206 | 5cd18037 | Christos Stathis | else if (bytesRemaining < 1024 * 1024) |
207 | 5cd18037 | Christos Stathis | return getSize(bytesRemaining, 1024D) + " KB"; |
208 | 5cd18037 | Christos Stathis | else if (bytesRemaining < 1024 * 1024 * 1024) |
209 | 5cd18037 | Christos Stathis | return getSize(bytesRemaining,(1024D * 1024D)) + " MB"; |
210 | 5cd18037 | Christos Stathis | return getSize(bytesRemaining , (1024D * 1024D * 1024D)) + " GB"; |
211 | 5cd18037 | Christos Stathis | } |
212 | 9e8e14e4 | Christos Stathis | } |