First version of client that displays first level containers
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / Resource.java
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.json.client.JSONArray;
11 import com.google.gwt.json.client.JSONNumber;
12 import com.google.gwt.json.client.JSONObject;
13 import com.google.gwt.json.client.JSONParser;
14 import com.google.gwt.json.client.JSONString;
15 import com.google.gwt.json.client.JSONValue;
16 import java.util.Date;
17
18 public abstract class Resource {
19
20     String uri;
21
22     public String getUri() {
23         return uri;
24     }
25
26     protected static String unmarshallString(JSONObject obj, String key){
27         if(obj.get(key) != null) {
28             JSONString s = obj.get(key).isString();
29             if(s != null)
30                 return s.stringValue();
31         }
32         return null;
33     }
34
35     protected static int unmarshallInt(JSONObject obj, String key){
36         if(obj.get(key) != null)
37             if(obj.get(key).isNumber() != null)
38                 return (int) obj.get(key).isNumber().getValue();
39         return -1;
40     }
41
42     protected static long unmarshallLong(JSONObject obj, String key){
43         if(obj.get(key) != null) {
44             JSONNumber value = obj.get(key).isNumber();
45             if(value != null)
46                 return (long) value.doubleValue();
47         }
48         return -1;
49     }
50
51     protected static boolean unmarshallBoolean(JSONObject obj, String key){
52         if(obj.get(key) != null)
53             if(obj.get(key).isBoolean() != null)
54                 return obj.get(key).isBoolean().booleanValue();
55         return false;
56     }
57
58     protected static Date unmarshallTimestamp(JSONObject obj, String key){
59         if(obj.get(key) != null) {
60             JSONString s = obj.get(key).isString();
61             if (s != null)
62                 return new Date(Long.valueOf(s.stringValue()).longValue());
63         }
64         return null;
65     }
66
67     protected static Date unmarshallDate(JSONObject obj, String key){
68         if(obj.get(key) != null) {
69             JSONString s = obj.get(key).isString();
70             if (s != null)
71                 return DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss").parse(s.stringValue());
72         }
73         return null;
74     }
75
76     public static native String getDate(Long ms)/*-{
77         return (new Date(ms)).toUTCString();
78     }-*/;
79
80     public abstract String getLastModifiedSince();
81
82     public static <T> T createFromResponse(Class<T> aClass, Response response) {
83         JSONValue json = JSONParser.parseStrict(response.getText());
84         T result = null;
85         if (aClass.equals(AccountResource.class)) {
86             AccountResource a = new AccountResource();
87             JSONArray array = json.isArray();
88             if (array != null) {
89                 for (int i=0; i<array.size(); i++) {
90                     JSONObject o = array.get(i).isObject();
91                     if (o != null) {
92                         ContainerResource container = new ContainerResource();
93                         container.setName(unmarshallString(o, "name"));
94                         container.setCount(unmarshallLong(o, "count"));
95                         container.setBytes(unmarshallLong(o, "bytes"));
96                         container.setCreated(unmarshallTimestamp(o, "created"));
97                         container.setLastModified(unmarshallDate(o, "last_modified"));
98                         a.getContainers().add(container);
99                     }
100                 }
101                 result = (T) a;
102             }
103         }
104         return result;
105     }
106 }