Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / foldertree / Resource.java @ 5cd18037

History | View | Annotate | Download (2.5 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.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
    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 unmarshallDate(JSONObject obj, String key){
59
        if(obj.get(key) != null) {
60
            JSONString s = obj.get(key).isString();
61
            if (s != null)
62
                return DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss").parse(s.stringValue());
63
        }
64
        return null;
65
    }
66

    
67
    public static native String getDate(Long ms)/*-{
68
        return (new Date(ms)).toUTCString();
69
    }-*/;
70

    
71
    public abstract String getLastModifiedSince();
72

    
73
    public static <T> T createFromResponse(Class<T> aClass, Response response, T result) {
74
        if (aClass.equals(AccountResource.class)) {
75
            result = (T) AccountResource.createFromResponse(response);
76
        }
77
        else if (aClass.equals(Folder.class)) {
78
            result = (T) Folder.createFromResponse(response, (Folder) result);
79
        }
80
        return result;
81
    }
82
}