Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.2 kB)

1
/*
2
 * Copyright 2011 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.i18n.client.DateTimeFormat;
48
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
49
import com.google.gwt.i18n.client.NumberFormat;
50
import com.google.gwt.json.client.JSONArray;
51
import com.google.gwt.json.client.JSONObject;
52
import com.google.gwt.json.client.JSONParser;
53
import com.google.gwt.json.client.JSONValue;
54

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

    
61
    private long numberOfContainers = 0;
62

    
63
    private long numberOfObjects = 0;
64

    
65
    private long bytesUsed = 0;
66

    
67
    private long bytesRemaining = 0;
68

    
69
    private Date lastLogin = null;
70

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

    
75
    private Date currentLogin = null;
76

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

    
79
    public long getBytesRemaining() {
80
        return bytesRemaining;
81
    }
82

    
83
    public void setBytesRemaining(long bytesRemaining) {
84
        this.bytesRemaining = bytesRemaining;
85
    }
86

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
188
    public static AccountResource createFromResponse(String owner, Response response, AccountResource result) {
189
            AccountResource a;
190
            if (result == null)
191
                    a = new AccountResource();
192
            else
193
                    a = result;
194
        a.populate(owner, response);
195
        return a;
196
    }
197

    
198
    private String getSize(Long size, Double division){
199
        Double res = Double.valueOf(size.toString())/division;
200
        NumberFormat nf = NumberFormat.getFormat("######.#");
201
        return nf.format(res);
202
    }
203

    
204
    public String getFileSizeAsString() {
205
        if (bytesUsed < 1024)
206
            return String.valueOf(bytesUsed) + " B";
207
        else if (bytesUsed < 1024*1024)
208
            return getSize(bytesUsed, 1024D) + " KB";
209
        else if (bytesUsed < 1024*1024*1024)
210
            return getSize(bytesUsed,(1024D*1024D)) + " MB";
211
        return getSize(bytesUsed , (1024D*1024D*1024D)) + " GB";
212
    }
213

    
214
    public String getQuotaAsString() {
215
            long quota = bytesUsed + bytesRemaining;
216
        if (quota < 1024)
217
            return String.valueOf(quota) + " B";
218
        else if (quota < 1024 * 1024)
219
            return getSize(quota, 1024D) + " KB";
220
        else if (quota < 1024 * 1024 * 1024)
221
            return getSize(quota,(1024D * 1024D)) + " MB";
222
        return getSize(quota , (1024D * 1024D * 1024D)) + " GB";
223
    }
224

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

    
236
    public boolean hasTrashContainer() {
237
            for (Folder f : containers)
238
                    if (f.getName().equals(Pithos.TRASH_CONTAINER))
239
                            return true;
240
            return false;
241
    }
242

    
243
        public void addGroup(Group newGroup) {
244
                groups.add(newGroup);
245
        }
246

    
247
        public void removeGroup(Group group) {
248
                groups.remove(group);
249
        }
250

    
251
        public Folder getTrash() {
252
                for (Folder c : containers) {
253
                        if (c.getName().equals(Pithos.TRASH_CONTAINER))
254
                                return c;
255
                }
256
                return null;
257
        }
258

    
259
        public double getUsedPercentage() {
260
                return 100.0 * bytesUsed / (bytesUsed + bytesRemaining);
261
        }
262

    
263
        public Folder getPithos() {
264
                for (Folder f : containers)
265
                        if (f.getName().equals(Pithos.HOME_CONTAINER))
266
                                return f;
267
                return null;
268
        }
269
}