Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.4 kB)

1
/*
2
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35

    
36
package gr.grnet.pithos.web.client.foldertree;
37

    
38
import gr.grnet.pithos.web.client.Pithos;
39
import gr.grnet.pithos.web.client.grouptree.Group;
40

    
41
import java.util.ArrayList;
42
import java.util.Date;
43
import java.util.List;
44

    
45
import com.google.gwt.http.client.Header;
46
import com.google.gwt.http.client.Response;
47
import com.google.gwt.http.client.URL;
48
import com.google.gwt.i18n.client.DateTimeFormat;
49
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
50
import com.google.gwt.i18n.client.NumberFormat;
51
import com.google.gwt.json.client.JSONArray;
52
import com.google.gwt.json.client.JSONObject;
53
import com.google.gwt.json.client.JSONParser;
54
import com.google.gwt.json.client.JSONValue;
55

    
56
/**
57
 * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
58
 * File Templates.
59
 */
60
public class AccountResource extends Resource {
61

    
62
    private long numberOfContainers = 0;
63

    
64
    private long numberOfObjects = 0;
65

    
66
    private long bytesUsed = 0;
67

    
68
    private long quota = 0;
69

    
70
    private Date lastLogin = null;
71

    
72
    private Date lastModified = null;
73
    
74
    private List<Folder> containers = new ArrayList<Folder>();
75

    
76
    private Date currentLogin = null;
77

    
78
    private List<Group> groups = new ArrayList<Group>();
79

    
80
    public long getQuota() {
81
        return quota;
82
    }
83

    
84
    public void setQuota(long quota) {
85
        this.quota = quota;
86
    }
87

    
88
    public long getBytesUsed() {
89
        return bytesUsed;
90
    }
91

    
92
    public void setBytesUsed(long bytesUsed) {
93
        this.bytesUsed = bytesUsed;
94
    }
95

    
96
    public Date getLastLogin() {
97
        return lastLogin;
98
    }
99

    
100
    public void setLastLogin(Date lastLogin) {
101
        this.lastLogin = lastLogin;
102
    }
103

    
104
    @Override
105
        public Date getLastModified() {
106
        return lastModified;
107
    }
108

    
109
    public void setLastModified(Date lastModified) {
110
        this.lastModified = lastModified;
111
    }
112

    
113
    public long getNumberOfContainers() {
114
        return numberOfContainers;
115
    }
116

    
117
    public void setNumberOfContainers(long numberOfContainers) {
118
        this.numberOfContainers = numberOfContainers;
119
    }
120

    
121
    public long getNumberOfObjects() {
122
        return numberOfObjects;
123
    }
124

    
125
    public void setNumberOfObjects(long numberOfObjects) {
126
        this.numberOfObjects = numberOfObjects;
127
    }
128

    
129
    public List<Folder> getContainers() {
130
        return containers;
131
    }
132

    
133
    public Date getCurrentLogin() {
134
        return currentLogin;
135
    }
136

    
137
    public void setCurrentLogin(Date currentLogin) {
138
        this.currentLogin = currentLogin;
139
    }
140

    
141
    public void populate(String owner, Response response) {
142
        DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
143
        groups.clear();
144
        for (Header h : response.getHeaders()) {
145
                if (h != null) {
146
                        String name = h.getName();
147
                        if (name.startsWith("X-Account-Group-")) {
148
                            String groupName = URL.decodePathSegment(name.substring("X-Account-Group-".length()));
149
                            Group g = new Group(groupName);
150
                            String[] members = h.getValue().split(",");
151
                            for (String s : members)
152
                                g.addMember(URL.decodePathSegment(s).trim());
153
                            groups.add(g);
154
                        }
155
                        else if (name.equals("X-Account-Container-Count")) {
156
                            numberOfContainers = Long.valueOf(h.getValue());
157
                        }
158
                        else if (name.equals("X-Account-Object-Count")) {
159
                            numberOfObjects = Long.valueOf(h.getValue());
160
                        }
161
                        else if (name.equals("X-Account-Bytes-Used")) {
162
                            bytesUsed = Long.valueOf(h.getValue());
163
                        }
164
                        else if (name.equals("X-Account-Policy-Quota")) {
165
                            quota = Long.valueOf(h.getValue());
166
                        }
167
                        else if (name.equals("X-Account-Last-Login")) {
168
                            lastLogin = df.parse(h.getValue());
169
                        }
170
                        else if (name.equals("Last-Modified")) {
171
                            lastModified = df.parse(h.getValue());
172
                        }
173
                }
174
        }
175

    
176
        if (response.getText() != null && response.getText().length() > 0) {
177
                containers.clear();
178
                JSONValue json = JSONParser.parseStrict(response.getText());
179
                JSONArray array = json.isArray();
180
                if (array != null) {
181
                    for (int i=0; i<array.size(); i++) {
182
                        JSONObject o = array.get(i).isObject();
183
                        if (o != null) {
184
                            Folder f = new Folder();
185
                            f.populate(null, o, owner, null);
186
                            containers.add(f);
187
                        }
188
                    }
189
                }
190
        }
191
    }
192

    
193
    public static AccountResource createFromResponse(String owner, Response response, AccountResource result) {
194
            AccountResource a;
195
            if (result == null)
196
                    a = new AccountResource();
197
            else
198
                    a = result;
199
        a.populate(owner, response);
200
        return a;
201
    }
202

    
203
    private String getSize(Long size, Double division){
204
        Double res = Double.valueOf(size.toString())/division;
205
        NumberFormat nf = NumberFormat.getFormat("######.#");
206
        return nf.format(res);
207
    }
208

    
209
    public String getFileSizeAsString() {
210
        if (bytesUsed < 1024)
211
            return String.valueOf(bytesUsed) + "B";
212
        else if (bytesUsed < 1024*1024)
213
            return getSize(bytesUsed, 1024D) + "KB";
214
        else if (bytesUsed < 1024*1024*1024)
215
            return getSize(bytesUsed,(1024D*1024D)) + "MB";
216
        return getSize(bytesUsed , (1024D*1024D*1024D)) + "GB";
217
    }
218

    
219
    public String getQuotaAsString() {
220
        if (quota < 1024)
221
            return String.valueOf(quota) + "B";
222
        else if (quota < 1024 * 1024)
223
            return getSize(quota, 1024D) + "KB";
224
        else if (quota < 1024 * 1024 * 1024)
225
            return getSize(quota,(1024D * 1024D)) + "MB";
226
        return getSize(quota , (1024D * 1024D * 1024D)) + "GB";
227
    }
228

    
229
    public List<Group> getGroups() {
230
        return groups;
231
    }
232
    
233
    public boolean hasHomeContainer() {
234
            for (Folder f : containers)
235
                    if (f.getName().equals(Pithos.HOME_CONTAINER))
236
                            return true;
237
            return false;
238
    }
239

    
240
    public boolean hasTrashContainer() {
241
            for (Folder f : containers)
242
                    if (f.getName().equals(Pithos.TRASH_CONTAINER))
243
                            return true;
244
            return false;
245
    }
246

    
247
        public void addGroup(Group newGroup) {
248
                groups.add(newGroup);
249
        }
250

    
251
        public void removeGroup(Group group) {
252
                groups.remove(group);
253
        }
254

    
255
        public Folder getTrash() {
256
                for (Folder c : containers) {
257
                        if (c.getName().equals(Pithos.TRASH_CONTAINER))
258
                                return c;
259
                }
260
                return null;
261
        }
262

    
263
        public double getUsedPercentage() {
264
                if (quota == 0)
265
                        return 0;
266
                return ((double) bytesUsed) / quota;
267
        }
268

    
269
        public Folder getPithos() {
270
                for (Folder f : containers)
271
                        if (f.getName().equals(Pithos.HOME_CONTAINER))
272
                                return f;
273
                return null;
274
        }
275

    
276
        public Group getGroup(String groupName) {
277
                for (Group g : groups)
278
                        if (g.getName().equalsIgnoreCase(groupName))
279
                                return g;
280
                return null;
281
        }
282
}