Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java @ fa68c84d

History | View | Annotate | Download (8.4 kB)

1 6084aa02 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 6084aa02 Christos Stathis
 */
35 6084aa02 Christos Stathis
36 6084aa02 Christos Stathis
package gr.grnet.pithos.web.client.foldertree;
37 6084aa02 Christos Stathis
38 fa68c84d Christos KK Loverdos
import gr.grnet.pithos.web.client.Const;
39 cc0120ab Christos KK Loverdos
import gr.grnet.pithos.web.client.Resource;
40 4baffab1 Christos Stathis
import gr.grnet.pithos.web.client.grouptree.Group;
41 7b28ae07 Christos Stathis
42 7811b9d1 Christos Stathis
import java.util.ArrayList;
43 7811b9d1 Christos Stathis
import java.util.Date;
44 7811b9d1 Christos Stathis
import java.util.List;
45 7811b9d1 Christos Stathis
46 6084aa02 Christos Stathis
import com.google.gwt.http.client.Header;
47 8e61880b Christos Stathis
import com.google.gwt.http.client.Response;
48 ea94470a Christos Stathis
import com.google.gwt.http.client.URL;
49 8e61880b Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat;
50 8e61880b Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
51 8e61880b Christos Stathis
import com.google.gwt.i18n.client.NumberFormat;
52 6084aa02 Christos Stathis
import com.google.gwt.json.client.JSONArray;
53 6084aa02 Christos Stathis
import com.google.gwt.json.client.JSONObject;
54 6084aa02 Christos Stathis
import com.google.gwt.json.client.JSONParser;
55 6084aa02 Christos Stathis
import com.google.gwt.json.client.JSONValue;
56 6084aa02 Christos Stathis
57 6084aa02 Christos Stathis
/**
58 6084aa02 Christos Stathis
 * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
59 6084aa02 Christos Stathis
 * File Templates.
60 6084aa02 Christos Stathis
 */
61 6084aa02 Christos Stathis
public class AccountResource extends Resource {
62 6084aa02 Christos Stathis
63 8e61880b Christos Stathis
    private long numberOfContainers = 0;
64 8e61880b Christos Stathis
65 8e61880b Christos Stathis
    private long numberOfObjects = 0;
66 8e61880b Christos Stathis
67 8e61880b Christos Stathis
    private long bytesUsed = 0;
68 8e61880b Christos Stathis
69 e92335eb Christos Stathis
    private long quota = 0;
70 8e61880b Christos Stathis
71 8e61880b Christos Stathis
    private Date lastLogin = null;
72 8e61880b Christos Stathis
73 8e61880b Christos Stathis
    private Date lastModified = null;
74 8e61880b Christos Stathis
    
75 105b4e49 Christos Stathis
    private List<Folder> containers = new ArrayList<Folder>();
76 8e61880b Christos Stathis
77 8e61880b Christos Stathis
    private Date currentLogin = null;
78 8e61880b Christos Stathis
79 d5c6298f Christos Stathis
    private List<Group> groups = new ArrayList<Group>();
80 d5c6298f Christos Stathis
81 e92335eb Christos Stathis
    public long getQuota() {
82 e92335eb Christos Stathis
        return quota;
83 8e61880b Christos Stathis
    }
84 8e61880b Christos Stathis
85 e92335eb Christos Stathis
    public void setQuota(long quota) {
86 e92335eb Christos Stathis
        this.quota = quota;
87 8e61880b Christos Stathis
    }
88 8e61880b Christos Stathis
89 8e61880b Christos Stathis
    public long getBytesUsed() {
90 8e61880b Christos Stathis
        return bytesUsed;
91 8e61880b Christos Stathis
    }
92 8e61880b Christos Stathis
93 8e61880b Christos Stathis
    public void setBytesUsed(long bytesUsed) {
94 8e61880b Christos Stathis
        this.bytesUsed = bytesUsed;
95 8e61880b Christos Stathis
    }
96 8e61880b Christos Stathis
97 8e61880b Christos Stathis
    public Date getLastLogin() {
98 8e61880b Christos Stathis
        return lastLogin;
99 8e61880b Christos Stathis
    }
100 8e61880b Christos Stathis
101 8e61880b Christos Stathis
    public void setLastLogin(Date lastLogin) {
102 8e61880b Christos Stathis
        this.lastLogin = lastLogin;
103 8e61880b Christos Stathis
    }
104 8e61880b Christos Stathis
105 f5023f13 Christos Stathis
    @Override
106 f5023f13 Christos Stathis
        public Date getLastModified() {
107 8e61880b Christos Stathis
        return lastModified;
108 8e61880b Christos Stathis
    }
109 8e61880b Christos Stathis
110 8e61880b Christos Stathis
    public void setLastModified(Date lastModified) {
111 8e61880b Christos Stathis
        this.lastModified = lastModified;
112 8e61880b Christos Stathis
    }
113 8e61880b Christos Stathis
114 8e61880b Christos Stathis
    public long getNumberOfContainers() {
115 8e61880b Christos Stathis
        return numberOfContainers;
116 8e61880b Christos Stathis
    }
117 8e61880b Christos Stathis
118 8e61880b Christos Stathis
    public void setNumberOfContainers(long numberOfContainers) {
119 8e61880b Christos Stathis
        this.numberOfContainers = numberOfContainers;
120 8e61880b Christos Stathis
    }
121 8e61880b Christos Stathis
122 8e61880b Christos Stathis
    public long getNumberOfObjects() {
123 8e61880b Christos Stathis
        return numberOfObjects;
124 8e61880b Christos Stathis
    }
125 8e61880b Christos Stathis
126 8e61880b Christos Stathis
    public void setNumberOfObjects(long numberOfObjects) {
127 8e61880b Christos Stathis
        this.numberOfObjects = numberOfObjects;
128 8e61880b Christos Stathis
    }
129 6084aa02 Christos Stathis
130 105b4e49 Christos Stathis
    public List<Folder> getContainers() {
131 6084aa02 Christos Stathis
        return containers;
132 6084aa02 Christos Stathis
    }
133 8e61880b Christos Stathis
134 8e61880b Christos Stathis
    public Date getCurrentLogin() {
135 8e61880b Christos Stathis
        return currentLogin;
136 8e61880b Christos Stathis
    }
137 8e61880b Christos Stathis
138 8e61880b Christos Stathis
    public void setCurrentLogin(Date currentLogin) {
139 8e61880b Christos Stathis
        this.currentLogin = currentLogin;
140 8e61880b Christos Stathis
    }
141 8e61880b Christos Stathis
142 b51c628b Christos Stathis
    public void populate(String owner, Response response) {
143 8e61880b Christos Stathis
        DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
144 6f79f96f Christos Stathis
        groups.clear();
145 d5c6298f Christos Stathis
        for (Header h : response.getHeaders()) {
146 e2403069 Christos Stathis
                if (h != null) {
147 e2403069 Christos Stathis
                        String name = h.getName();
148 e2403069 Christos Stathis
                        if (name.startsWith("X-Account-Group-")) {
149 a2411896 Christos Stathis
                            String groupName = URL.decodePathSegment(name.substring("X-Account-Group-".length()));
150 e2403069 Christos Stathis
                            Group g = new Group(groupName);
151 e2403069 Christos Stathis
                            String[] members = h.getValue().split(",");
152 e2403069 Christos Stathis
                            for (String s : members)
153 a2411896 Christos Stathis
                                g.addMember(URL.decodePathSegment(s).trim());
154 e2403069 Christos Stathis
                            groups.add(g);
155 e2403069 Christos Stathis
                        }
156 e2403069 Christos Stathis
                        else if (name.equals("X-Account-Container-Count")) {
157 e2403069 Christos Stathis
                            numberOfContainers = Long.valueOf(h.getValue());
158 e2403069 Christos Stathis
                        }
159 e2403069 Christos Stathis
                        else if (name.equals("X-Account-Object-Count")) {
160 e2403069 Christos Stathis
                            numberOfObjects = Long.valueOf(h.getValue());
161 e2403069 Christos Stathis
                        }
162 e2403069 Christos Stathis
                        else if (name.equals("X-Account-Bytes-Used")) {
163 e2403069 Christos Stathis
                            bytesUsed = Long.valueOf(h.getValue());
164 e2403069 Christos Stathis
                        }
165 e92335eb Christos Stathis
                        else if (name.equals("X-Account-Policy-Quota")) {
166 e92335eb Christos Stathis
                            quota = Long.valueOf(h.getValue());
167 e2403069 Christos Stathis
                        }
168 e2403069 Christos Stathis
                        else if (name.equals("X-Account-Last-Login")) {
169 e2403069 Christos Stathis
                            lastLogin = df.parse(h.getValue());
170 e2403069 Christos Stathis
                        }
171 e2403069 Christos Stathis
                        else if (name.equals("Last-Modified")) {
172 e2403069 Christos Stathis
                            lastModified = df.parse(h.getValue());
173 e2403069 Christos Stathis
                        }
174 e2403069 Christos Stathis
                }
175 d5c6298f Christos Stathis
        }
176 8e61880b Christos Stathis
177 64f42c0b Christos Stathis
        if (response.getText() != null && response.getText().length() > 0) {
178 6f79f96f Christos Stathis
                containers.clear();
179 64f42c0b Christos Stathis
                JSONValue json = JSONParser.parseStrict(response.getText());
180 64f42c0b Christos Stathis
                JSONArray array = json.isArray();
181 64f42c0b Christos Stathis
                if (array != null) {
182 64f42c0b Christos Stathis
                    for (int i=0; i<array.size(); i++) {
183 64f42c0b Christos Stathis
                        JSONObject o = array.get(i).isObject();
184 64f42c0b Christos Stathis
                        if (o != null) {
185 64f42c0b Christos Stathis
                            Folder f = new Folder();
186 64f42c0b Christos Stathis
                            f.populate(null, o, owner, null);
187 64f42c0b Christos Stathis
                            containers.add(f);
188 64f42c0b Christos Stathis
                        }
189 64f42c0b Christos Stathis
                    }
190 64f42c0b Christos Stathis
                }
191 8e61880b Christos Stathis
        }
192 8e61880b Christos Stathis
    }
193 8e61880b Christos Stathis
194 64f42c0b Christos Stathis
    public static AccountResource createFromResponse(String owner, Response response, AccountResource result) {
195 64f42c0b Christos Stathis
            AccountResource a;
196 64f42c0b Christos Stathis
            if (result == null)
197 64f42c0b Christos Stathis
                    a = new AccountResource();
198 64f42c0b Christos Stathis
            else
199 64f42c0b Christos Stathis
                    a = result;
200 b51c628b Christos Stathis
        a.populate(owner, response);
201 8e61880b Christos Stathis
        return a;
202 8e61880b Christos Stathis
    }
203 8e61880b Christos Stathis
204 8e61880b Christos Stathis
    private String getSize(Long size, Double division){
205 8e61880b Christos Stathis
        Double res = Double.valueOf(size.toString())/division;
206 8e61880b Christos Stathis
        NumberFormat nf = NumberFormat.getFormat("######.#");
207 8e61880b Christos Stathis
        return nf.format(res);
208 8e61880b Christos Stathis
    }
209 8e61880b Christos Stathis
210 8e61880b Christos Stathis
    public String getFileSizeAsString() {
211 8e61880b Christos Stathis
        if (bytesUsed < 1024)
212 140f0f6d Christos Stathis
            return String.valueOf(bytesUsed) + "B";
213 8e61880b Christos Stathis
        else if (bytesUsed < 1024*1024)
214 140f0f6d Christos Stathis
            return getSize(bytesUsed, 1024D) + "KB";
215 8e61880b Christos Stathis
        else if (bytesUsed < 1024*1024*1024)
216 140f0f6d Christos Stathis
            return getSize(bytesUsed,(1024D*1024D)) + "MB";
217 140f0f6d Christos Stathis
        return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB";
218 8e61880b Christos Stathis
    }
219 8e61880b Christos Stathis
220 bf664298 Christos Stathis
    public String getQuotaAsString() {
221 bf664298 Christos Stathis
        if (quota < 1024)
222 140f0f6d Christos Stathis
            return String.valueOf(quota) + "B";
223 bf664298 Christos Stathis
        else if (quota < 1024 * 1024)
224 140f0f6d Christos Stathis
            return getSize(quota, 1024D) + "KB";
225 bf664298 Christos Stathis
        else if (quota < 1024 * 1024 * 1024)
226 140f0f6d Christos Stathis
            return getSize(quota,(1024D * 1024D)) + "MB";
227 140f0f6d Christos Stathis
        return getSize(quota , (1024D * 1024D * 1024D)) + "GB";
228 8e61880b Christos Stathis
    }
229 d5c6298f Christos Stathis
230 d5c6298f Christos Stathis
    public List<Group> getGroups() {
231 d5c6298f Christos Stathis
        return groups;
232 d5c6298f Christos Stathis
    }
233 7b28ae07 Christos Stathis
    
234 7b28ae07 Christos Stathis
    public boolean hasHomeContainer() {
235 7b28ae07 Christos Stathis
            for (Folder f : containers)
236 fa68c84d Christos KK Loverdos
                    if (f.getName().equals(Const.HOME_CONTAINER))
237 7b28ae07 Christos Stathis
                            return true;
238 7b28ae07 Christos Stathis
            return false;
239 7b28ae07 Christos Stathis
    }
240 7b28ae07 Christos Stathis
241 7b28ae07 Christos Stathis
    public boolean hasTrashContainer() {
242 7b28ae07 Christos Stathis
            for (Folder f : containers)
243 fa68c84d Christos KK Loverdos
                    if (f.getName().equals(Const.TRASH_CONTAINER))
244 7b28ae07 Christos Stathis
                            return true;
245 7b28ae07 Christos Stathis
            return false;
246 7b28ae07 Christos Stathis
    }
247 4baffab1 Christos Stathis
248 4baffab1 Christos Stathis
        public void addGroup(Group newGroup) {
249 4baffab1 Christos Stathis
                groups.add(newGroup);
250 4baffab1 Christos Stathis
        }
251 61f8605d Christos Stathis
252 61f8605d Christos Stathis
        public void removeGroup(Group group) {
253 61f8605d Christos Stathis
                groups.remove(group);
254 61f8605d Christos Stathis
        }
255 49080dd5 Christos Stathis
256 49080dd5 Christos Stathis
        public Folder getTrash() {
257 49080dd5 Christos Stathis
                for (Folder c : containers) {
258 fa68c84d Christos KK Loverdos
                        if (c.getName().equals(Const.TRASH_CONTAINER))
259 49080dd5 Christos Stathis
                                return c;
260 49080dd5 Christos Stathis
                }
261 49080dd5 Christos Stathis
                return null;
262 49080dd5 Christos Stathis
        }
263 bf664298 Christos Stathis
264 bf664298 Christos Stathis
        public double getUsedPercentage() {
265 e92335eb Christos Stathis
                if (quota == 0)
266 e92335eb Christos Stathis
                        return 0;
267 e92335eb Christos Stathis
                return ((double) bytesUsed) / quota;
268 bf664298 Christos Stathis
        }
269 340bf0d6 Christos Stathis
270 340bf0d6 Christos Stathis
        public Folder getPithos() {
271 340bf0d6 Christos Stathis
                for (Folder f : containers)
272 fa68c84d Christos KK Loverdos
                        if (f.getName().equals(Const.HOME_CONTAINER))
273 340bf0d6 Christos Stathis
                                return f;
274 340bf0d6 Christos Stathis
                return null;
275 340bf0d6 Christos Stathis
        }
276 39a92f5c Christos Stathis
277 39a92f5c Christos Stathis
        public Group getGroup(String groupName) {
278 39a92f5c Christos Stathis
                for (Group g : groups)
279 39a92f5c Christos Stathis
                        if (g.getName().equalsIgnoreCase(groupName))
280 39a92f5c Christos Stathis
                                return g;
281 39a92f5c Christos Stathis
                return null;
282 39a92f5c Christos Stathis
        }
283 6084aa02 Christos Stathis
}