Statistics
| Branch: | Tag: | Revision:

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

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.Const;
39
import gr.grnet.pithos.web.client.Resource;
40
import gr.grnet.pithos.web.client.grouptree.Group;
41

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

    
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;
56

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

    
63
    private long numberOfContainers = 0;
64

    
65
    private long numberOfObjects = 0;
66

    
67
    private long bytesUsed = 0;
68

    
69
    private long quota = 0;
70

    
71
    private Date lastLogin = null;
72

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

    
77
    private Date currentLogin = null;
78

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
142
    public void populate(String owner, Response response) {
143
        DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
144
        groups.clear();
145
        for (Header h : response.getHeaders()) {
146
                if (h != null) {
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.addMemberID(URL.decodePathSegment(s).trim());
154
                            groups.add(g);
155
                        }
156
                        else if (name.equals("X-Account-Container-Count")) {
157
                            numberOfContainers = Long.valueOf(h.getValue());
158
                        }
159
                        else if (name.equals("X-Account-Object-Count")) {
160
                            numberOfObjects = Long.valueOf(h.getValue());
161
                        }
162
                        else if (name.equals("X-Account-Bytes-Used")) {
163
                            bytesUsed = Long.valueOf(h.getValue());
164
                        }
165
                        else if (name.equals("X-Account-Policy-Quota")) {
166
                            quota = Long.valueOf(h.getValue());
167
                        }
168
                        else if (name.equals("X-Account-Last-Login")) {
169
                            lastLogin = df.parse(h.getValue());
170
                        }
171
                        else if (name.equals("Last-Modified")) {
172
                            lastModified = df.parse(h.getValue());
173
                        }
174
                }
175
        }
176

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

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

    
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);
208
    }
209

    
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";
218
    }
219

    
220
    public String getQuotaAsString() {
221
        if (quota < 1024)
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";
228
    }
229

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

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

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

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

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

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

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

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