Got file info from headers
[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.Response;
8 import com.google.gwt.i18n.client.DateTimeFormat;
9 import com.google.gwt.json.client.JSONArray;
10 import com.google.gwt.json.client.JSONNumber;
11 import com.google.gwt.json.client.JSONObject;
12 import com.google.gwt.json.client.JSONParser;
13 import com.google.gwt.json.client.JSONString;
14 import com.google.gwt.json.client.JSONValue;
15 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
16 import java.util.Date;
17
18 public abstract class Resource {
19
20     protected static String unmarshallString(JSONObject obj, String key){
21         if(obj.get(key) != null) {
22             JSONString s = obj.get(key).isString();
23             if(s != null)
24                 return s.stringValue();
25         }
26         return null;
27     }
28
29     protected static int unmarshallInt(JSONObject obj, String key){
30         if(obj.get(key) != null)
31             if(obj.get(key).isNumber() != null)
32                 return (int) obj.get(key).isNumber().getValue();
33         return -1;
34     }
35
36     protected static long unmarshallLong(JSONObject obj, String key){
37         if(obj.get(key) != null) {
38             JSONNumber value = obj.get(key).isNumber();
39             if(value != null)
40                 return (long) value.doubleValue();
41         }
42         return -1;
43     }
44
45     protected static boolean unmarshallBoolean(JSONObject obj, String key){
46         if(obj.get(key) != null)
47             if(obj.get(key).isBoolean() != null)
48                 return obj.get(key).isBoolean().booleanValue();
49         return false;
50     }
51
52     protected static Date unmarshallDate(JSONObject obj, String key){
53         if(obj.get(key) != null) {
54             JSONString s = obj.get(key).isString();
55             if (s != null)
56                 return DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss").parse(s.stringValue());
57         }
58         return null;
59     }
60
61     public static native String getDate(Long ms)/*-{
62         return (new Date(ms)).toUTCString();
63     }-*/;
64
65     public abstract String getLastModifiedSince();
66
67     public static <T> T createFromResponse(Class<T> aClass, Response response, T result) {
68         if (aClass.equals(AccountResource.class)) {
69             result = (T) AccountResource.createFromResponse(response);
70         }
71         else if (aClass.equals(Folder.class)) {
72             result = (T) Folder.createFromResponse(response, (Folder) result);
73         }
74         else if (aClass.equals(File.class)) {
75             result = (T) File.createFromResponse(response, (File) result);
76         }
77         return result;
78     }
79 }