Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / Resource.java @ aad9808c

History | View | Annotate | Download (5.3 kB)

1
/*
2
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35

    
36
package gr.grnet.pithos.web.client;
37

    
38
import com.google.gwt.core.client.GWT;
39
import com.google.gwt.http.client.Response;
40
import com.google.gwt.i18n.client.DateTimeFormat;
41
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
42
import com.google.gwt.json.client.*;
43
import gr.grnet.pithos.web.client.Invitations;
44
import gr.grnet.pithos.web.client.SharingUsers;
45
import gr.grnet.pithos.web.client.foldertree.AccountResource;
46
import gr.grnet.pithos.web.client.foldertree.File;
47
import gr.grnet.pithos.web.client.foldertree.FileVersions;
48
import gr.grnet.pithos.web.client.foldertree.Folder;
49

    
50
import java.util.Date;
51

    
52
public abstract class Resource {
53

    
54
    protected static String unmarshallString(JSONObject obj, String key) {
55
        final JSONValue jsonValue = obj.get(key);
56
        if(jsonValue != null) {
57
            JSONString s = jsonValue.isString();
58
            if(s != null) {
59
                return s.stringValue();
60
            }
61
        }
62
        return null;
63
    }
64

    
65
    protected static int unmarshallInt(JSONObject obj, String key) {
66
        final JSONValue jsonValue = obj.get(key);
67
        if(jsonValue != null) {
68
            final JSONNumber jsonNumber = jsonValue.isNumber();
69
            if(jsonNumber != null) {
70
                return (int) jsonNumber.doubleValue();
71
            }
72
        }
73
        return -1;
74
    }
75

    
76
    protected static long unmarshallLong(JSONObject obj, String key) {
77
        final JSONValue jsonValue = obj.get(key);
78
        if(jsonValue != null) {
79
            final JSONNumber jsonNumber = jsonValue.isNumber();
80
            if(jsonNumber != null) {
81
                return (long) jsonNumber.doubleValue();
82
            }
83
        }
84
        return -1;
85
    }
86

    
87
    protected static boolean unmarshallBoolean(JSONObject obj, String key) {
88
        final JSONValue jsonValue = obj.get(key);
89
        if(jsonValue != null) {
90
            final JSONBoolean aBoolean = jsonValue.isBoolean();
91
            if(aBoolean != null) {
92
                return aBoolean.booleanValue();
93
            }
94
        }
95
        return false;
96
    }
97

    
98
    protected static Date unmarshallDate(JSONObject obj, String key) {
99
        final JSONValue jsonValue = obj.get(key);
100
        if(jsonValue != null) {
101
            JSONString jsonString = jsonValue.isString();
102
            if(jsonString != null) {
103
                try {
104
                    return DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).parse(jsonString.stringValue());
105
                }
106
                catch(IllegalArgumentException e) {
107
                    GWT.log("", e);
108
                }
109
            }
110
        }
111
        return null;
112
    }
113

    
114
    public static native String getDate(Long ms)/*-{
115
      return (new Date(ms)).toUTCString();
116
    }-*/;
117

    
118
    @SuppressWarnings("unchecked")
119
    public static <T> T createFromResponse(Class<T> aClass, String owner, Response response, T result) {
120
        T result1 = null;
121
        if(aClass.equals(AccountResource.class)) {
122
            result1 = (T) AccountResource.createFromResponse(owner, response, (AccountResource) result);
123
        }
124
        else if(aClass.equals(Folder.class)) {
125
            result1 = (T) Folder.createFromResponse(owner, response, (Folder) result);
126
        }
127
        else if(aClass.equals(File.class)) {
128
            result1 = (T) File.createFromResponse(owner, response, (File) result);
129
        }
130
        else if(aClass.equals(SharingUsers.class)) {
131
            result1 = (T) SharingUsers.createFromResponse(response, (SharingUsers) result);
132
        }
133
        else if(aClass.equals(FileVersions.class)) {
134
            result1 = (T) FileVersions.createFromResponse(response);
135
        }
136
        else if(aClass.equals(Invitations.class)) {
137
            result1 = (T) Invitations.createFromResponse(response);
138
        }
139
        return result1;
140
    }
141

    
142
    public abstract Date getLastModified();
143
}