root / web_client / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java @ 1ac430a1
History | View | Annotate | Download (5.2 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Greek Research and Technology Network
|
3 |
*/
|
4 |
|
5 |
package gr.grnet.pithos.web.client.foldertree; |
6 |
|
7 |
import com.google.gwt.http.client.Header; |
8 |
import com.google.gwt.http.client.Response; |
9 |
import com.google.gwt.i18n.client.DateTimeFormat; |
10 |
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; |
11 |
import com.google.gwt.i18n.client.NumberFormat; |
12 |
import com.google.gwt.json.client.JSONArray; |
13 |
import com.google.gwt.json.client.JSONObject; |
14 |
import com.google.gwt.json.client.JSONParser; |
15 |
import com.google.gwt.json.client.JSONValue; |
16 |
import gr.grnet.pithos.web.client.rest.resource.RestResource; |
17 |
import java.util.ArrayList; |
18 |
import java.util.Date; |
19 |
import java.util.LinkedHashSet; |
20 |
import java.util.List; |
21 |
import java.util.Set; |
22 |
|
23 |
/**
|
24 |
* Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
|
25 |
* File Templates.
|
26 |
*/
|
27 |
public class AccountResource extends Resource { |
28 |
|
29 |
private long numberOfContainers = 0; |
30 |
|
31 |
private long numberOfObjects = 0; |
32 |
|
33 |
private long bytesUsed = 0; |
34 |
|
35 |
private long bytesRemaining = 0; |
36 |
|
37 |
private Date lastLogin = null; |
38 |
|
39 |
private Date lastModified = null; |
40 |
|
41 |
private Set<Folder> containers = new LinkedHashSet<Folder>(); |
42 |
|
43 |
private Date currentLogin = null; |
44 |
|
45 |
public long getBytesRemaining() { |
46 |
return bytesRemaining;
|
47 |
} |
48 |
|
49 |
public void setBytesRemaining(long bytesRemaining) { |
50 |
this.bytesRemaining = bytesRemaining;
|
51 |
} |
52 |
|
53 |
public long getBytesUsed() { |
54 |
return bytesUsed;
|
55 |
} |
56 |
|
57 |
public void setBytesUsed(long bytesUsed) { |
58 |
this.bytesUsed = bytesUsed;
|
59 |
} |
60 |
|
61 |
public Date getLastLogin() { |
62 |
return lastLogin;
|
63 |
} |
64 |
|
65 |
public void setLastLogin(Date lastLogin) { |
66 |
this.lastLogin = lastLogin;
|
67 |
} |
68 |
|
69 |
public Date getLastModified() { |
70 |
return lastModified;
|
71 |
} |
72 |
|
73 |
public void setLastModified(Date lastModified) { |
74 |
this.lastModified = lastModified;
|
75 |
} |
76 |
|
77 |
public long getNumberOfContainers() { |
78 |
return numberOfContainers;
|
79 |
} |
80 |
|
81 |
public void setNumberOfContainers(long numberOfContainers) { |
82 |
this.numberOfContainers = numberOfContainers;
|
83 |
} |
84 |
|
85 |
public long getNumberOfObjects() { |
86 |
return numberOfObjects;
|
87 |
} |
88 |
|
89 |
public void setNumberOfObjects(long numberOfObjects) { |
90 |
this.numberOfObjects = numberOfObjects;
|
91 |
} |
92 |
|
93 |
@Override
|
94 |
public String getLastModifiedSince() { |
95 |
return null; |
96 |
} |
97 |
|
98 |
public Set<Folder> getContainers() { |
99 |
return containers;
|
100 |
} |
101 |
|
102 |
public Date getCurrentLogin() { |
103 |
return currentLogin;
|
104 |
} |
105 |
|
106 |
public void setCurrentLogin(Date currentLogin) { |
107 |
this.currentLogin = currentLogin;
|
108 |
} |
109 |
|
110 |
public void populate(Response response) { |
111 |
String header = response.getHeader("X-Account-Container-Count"); |
112 |
if (header != null) |
113 |
numberOfContainers = Long.valueOf(header);
|
114 |
|
115 |
header = response.getHeader("X-Account-Object-Count");
|
116 |
if (header != null) |
117 |
numberOfObjects = Long.valueOf(header);
|
118 |
|
119 |
header = response.getHeader("X-Account-Bytes-Used");
|
120 |
if (header != null) |
121 |
bytesUsed = Long.valueOf(header);
|
122 |
|
123 |
header = response.getHeader("X-Account-Bytes-Remaining");
|
124 |
if (header != null) |
125 |
bytesRemaining = Long.valueOf(header);
|
126 |
|
127 |
DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822); |
128 |
header = response.getHeader("X-Account-Last-Login");
|
129 |
if (header != null) |
130 |
lastLogin = df.parse(header); |
131 |
|
132 |
header = response.getHeader("Last-Modified");
|
133 |
if (header != null) |
134 |
lastModified = df.parse(header); |
135 |
|
136 |
JSONValue json = JSONParser.parseStrict(response.getText()); |
137 |
JSONArray array = json.isArray(); |
138 |
if (array != null) { |
139 |
for (int i=0; i<array.size(); i++) { |
140 |
JSONObject o = array.get(i).isObject(); |
141 |
if (o != null) { |
142 |
Folder f = new Folder();
|
143 |
f.populate(o, null);
|
144 |
containers.add(f); |
145 |
} |
146 |
} |
147 |
} |
148 |
} |
149 |
|
150 |
public static AccountResource createFromResponse(Response response) { |
151 |
AccountResource a = new AccountResource();
|
152 |
a.populate(response); |
153 |
return a;
|
154 |
} |
155 |
|
156 |
private String getSize(Long size, Double division){ |
157 |
Double res = Double.valueOf(size.toString())/division; |
158 |
NumberFormat nf = NumberFormat.getFormat("######.#"); |
159 |
return nf.format(res);
|
160 |
} |
161 |
|
162 |
public String getFileSizeAsString() { |
163 |
if (bytesUsed < 1024) |
164 |
return String.valueOf(bytesUsed) + " B"; |
165 |
else if (bytesUsed < 1024*1024) |
166 |
return getSize(bytesUsed, 1024D) + " KB"; |
167 |
else if (bytesUsed < 1024*1024*1024) |
168 |
return getSize(bytesUsed,(1024D*1024D)) + " MB"; |
169 |
return getSize(bytesUsed , (1024D*1024D*1024D)) + " GB"; |
170 |
} |
171 |
|
172 |
public String getQuotaLeftAsString() { |
173 |
if (bytesRemaining < 1024) |
174 |
return String.valueOf(bytesRemaining) + " B"; |
175 |
else if (bytesRemaining < 1024 * 1024) |
176 |
return getSize(bytesRemaining, 1024D) + " KB"; |
177 |
else if (bytesRemaining < 1024 * 1024 * 1024) |
178 |
return getSize(bytesRemaining,(1024D * 1024D)) + " MB"; |
179 |
return getSize(bytesRemaining , (1024D * 1024D * 1024D)) + " GB"; |
180 |
} |
181 |
} |