CSS fun
[pithos-web-client] / src / gr / grnet / pithos / web / client / foldertree / AccountResource.java
index 18d781c..770c791 100644 (file)
@@ -100,7 +100,8 @@ public class AccountResource extends Resource {
         this.lastLogin = lastLogin;
     }
 
-    public Date getLastModified() {
+    @Override
+       public Date getLastModified() {
         return lastModified;
     }
 
@@ -139,51 +140,59 @@ public class AccountResource extends Resource {
     public void populate(String owner, Response response) {
         DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
         for (Header h : response.getHeaders()) {
-            String name = h.getName();
-            if (name.startsWith("X-Account-Group-")) {
-                String groupName = name.substring("X-Account-Group-".length()).trim().toLowerCase();
-                Group g = new Group(groupName);
-                String[] members = h.getValue().split(",");
-                for (String s : members)
-                    g.addMember(s.trim());
-                groups.add(g);
-            }
-            else if (name.equals("X-Account-Container-Count")) {
-                numberOfContainers = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Object-Count")) {
-                numberOfObjects = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Bytes-Used")) {
-                bytesUsed = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Bytes-Remaining")) {
-                bytesRemaining = Long.valueOf(h.getValue());
-            }
-            else if (name.equals("X-Account-Last-Login")) {
-                lastLogin = df.parse(h.getValue());
-            }
-            else if (name.equals("Last-Modified")) {
-                lastModified = df.parse(h.getValue());
-            }
+               if (h != null) {
+                       String name = h.getName();
+                       if (name.startsWith("X-Account-Group-")) {
+                           String groupName = name.substring("X-Account-Group-".length()).trim().toLowerCase();
+                           Group g = new Group(groupName);
+                           String[] members = h.getValue().split(",");
+                           for (String s : members)
+                               g.addMember(s.trim());
+                           groups.add(g);
+                       }
+                       else if (name.equals("X-Account-Container-Count")) {
+                           numberOfContainers = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Object-Count")) {
+                           numberOfObjects = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Bytes-Used")) {
+                           bytesUsed = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Bytes-Remaining")) {
+                           bytesRemaining = Long.valueOf(h.getValue());
+                       }
+                       else if (name.equals("X-Account-Last-Login")) {
+                           lastLogin = df.parse(h.getValue());
+                       }
+                       else if (name.equals("Last-Modified")) {
+                           lastModified = df.parse(h.getValue());
+                       }
+               }
         }
 
-        JSONValue json = JSONParser.parseStrict(response.getText());
-        JSONArray array = json.isArray();
-        if (array != null) {
-            for (int i=0; i<array.size(); i++) {
-                JSONObject o = array.get(i).isObject();
-                if (o != null) {
-                    Folder f = new Folder();
-                    f.populate(null, o, owner, null);
-                    containers.add(f);
-                }
-            }
+        if (response.getText() != null && response.getText().length() > 0) {
+               JSONValue json = JSONParser.parseStrict(response.getText());
+               JSONArray array = json.isArray();
+               if (array != null) {
+                   for (int i=0; i<array.size(); i++) {
+                       JSONObject o = array.get(i).isObject();
+                       if (o != null) {
+                           Folder f = new Folder();
+                           f.populate(null, o, owner, null);
+                           containers.add(f);
+                       }
+                   }
+               }
         }
     }
 
-    public static AccountResource createFromResponse(String owner, Response response) {
-        AccountResource a = new AccountResource();
+    public static AccountResource createFromResponse(String owner, Response response, AccountResource result) {
+       AccountResource a;
+       if (result == null)
+               a = new AccountResource();
+       else
+               a = result;
         a.populate(owner, response);
         return a;
     }
@@ -204,14 +213,15 @@ public class AccountResource extends Resource {
         return getSize(bytesUsed , (1024D*1024D*1024D)) + " GB";
     }
 
-    public String getQuotaLeftAsString() {
-        if (bytesRemaining < 1024)
-            return String.valueOf(bytesRemaining) + " B";
-        else if (bytesRemaining < 1024 * 1024)
-            return getSize(bytesRemaining, 1024D) + " KB";
-        else if (bytesRemaining < 1024 * 1024 * 1024)
-            return getSize(bytesRemaining,(1024D * 1024D)) + " MB";
-        return getSize(bytesRemaining , (1024D * 1024D * 1024D)) + " GB";
+    public String getQuotaAsString() {
+       long quota = bytesUsed + bytesRemaining;
+        if (quota < 1024)
+            return String.valueOf(quota) + " B";
+        else if (quota < 1024 * 1024)
+            return getSize(quota, 1024D) + " KB";
+        else if (quota < 1024 * 1024 * 1024)
+            return getSize(quota,(1024D * 1024D)) + " MB";
+        return getSize(quota , (1024D * 1024D * 1024D)) + " GB";
     }
 
     public List<Group> getGroups() {
@@ -247,4 +257,15 @@ public class AccountResource extends Resource {
                }
                return null;
        }
+
+       public double getUsedPercentage() {
+               return ((double) bytesUsed) / (bytesUsed + bytesRemaining);
+       }
+
+       public Folder getPithos() {
+               for (Folder f : containers)
+                       if (f.getName().equals(Pithos.HOME_CONTAINER))
+                               return f;
+               return null;
+       }
 }