root / web_client / src / gr / grnet / pithos / web / client / foldertree / Resource.java @ 58777026
History | View | Annotate | Download (4.1 kB)
1 | 9e8e14e4 | Christos Stathis | /*
|
---|---|---|---|
2 | 58777026 | Christos Stathis | * Copyright 2011 GRNET S.A. All rights reserved.
|
3 | 58777026 | Christos Stathis | *
|
4 | 58777026 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 58777026 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 58777026 | Christos Stathis | * conditions are met:
|
7 | 58777026 | Christos Stathis | *
|
8 | 58777026 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 58777026 | Christos Stathis | * disclaimer.
|
11 | 58777026 | Christos Stathis | *
|
12 | 58777026 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 58777026 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 58777026 | Christos Stathis | * provided with the distribution.
|
16 | 58777026 | Christos Stathis | *
|
17 | 58777026 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 58777026 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 58777026 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 58777026 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 58777026 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 58777026 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 58777026 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 58777026 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 58777026 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 58777026 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 58777026 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 58777026 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 58777026 | Christos Stathis | *
|
30 | 58777026 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 58777026 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 58777026 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 58777026 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | 9e8e14e4 | Christos Stathis | */
|
35 | 9e8e14e4 | Christos Stathis | |
36 | 9e8e14e4 | Christos Stathis | package gr.grnet.pithos.web.client.foldertree; |
37 | 9e8e14e4 | Christos Stathis | |
38 | 9e8e14e4 | Christos Stathis | import com.google.gwt.http.client.Response; |
39 | 9e8e14e4 | Christos Stathis | import com.google.gwt.i18n.client.DateTimeFormat; |
40 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONArray; |
41 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONNumber; |
42 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONObject; |
43 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONParser; |
44 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONString; |
45 | 9e8e14e4 | Christos Stathis | import com.google.gwt.json.client.JSONValue; |
46 | 5cd18037 | Christos Stathis | import gr.grnet.pithos.web.client.rest.resource.FolderResource; |
47 | 9e8e14e4 | Christos Stathis | import java.util.Date; |
48 | 9e8e14e4 | Christos Stathis | |
49 | 9e8e14e4 | Christos Stathis | public abstract class Resource { |
50 | 9e8e14e4 | Christos Stathis | |
51 | 9e8e14e4 | Christos Stathis | protected static String unmarshallString(JSONObject obj, String key){ |
52 | 9e8e14e4 | Christos Stathis | if(obj.get(key) != null) { |
53 | 9e8e14e4 | Christos Stathis | JSONString s = obj.get(key).isString(); |
54 | 9e8e14e4 | Christos Stathis | if(s != null) |
55 | 9e8e14e4 | Christos Stathis | return s.stringValue();
|
56 | 9e8e14e4 | Christos Stathis | } |
57 | 9e8e14e4 | Christos Stathis | return null; |
58 | 9e8e14e4 | Christos Stathis | } |
59 | 9e8e14e4 | Christos Stathis | |
60 | 9e8e14e4 | Christos Stathis | protected static int unmarshallInt(JSONObject obj, String key){ |
61 | 9e8e14e4 | Christos Stathis | if(obj.get(key) != null) |
62 | 9e8e14e4 | Christos Stathis | if(obj.get(key).isNumber() != null) |
63 | 9e8e14e4 | Christos Stathis | return (int) obj.get(key).isNumber().getValue(); |
64 | 9e8e14e4 | Christos Stathis | return -1; |
65 | 9e8e14e4 | Christos Stathis | } |
66 | 9e8e14e4 | Christos Stathis | |
67 | 9e8e14e4 | Christos Stathis | protected static long unmarshallLong(JSONObject obj, String key){ |
68 | 9e8e14e4 | Christos Stathis | if(obj.get(key) != null) { |
69 | 9e8e14e4 | Christos Stathis | JSONNumber value = obj.get(key).isNumber(); |
70 | 9e8e14e4 | Christos Stathis | if(value != null) |
71 | 9e8e14e4 | Christos Stathis | return (long) value.doubleValue(); |
72 | 9e8e14e4 | Christos Stathis | } |
73 | 9e8e14e4 | Christos Stathis | return -1; |
74 | 9e8e14e4 | Christos Stathis | } |
75 | 9e8e14e4 | Christos Stathis | |
76 | 9e8e14e4 | Christos Stathis | protected static boolean unmarshallBoolean(JSONObject obj, String key){ |
77 | 9e8e14e4 | Christos Stathis | if(obj.get(key) != null) |
78 | 9e8e14e4 | Christos Stathis | if(obj.get(key).isBoolean() != null) |
79 | 9e8e14e4 | Christos Stathis | return obj.get(key).isBoolean().booleanValue();
|
80 | 9e8e14e4 | Christos Stathis | return false; |
81 | 9e8e14e4 | Christos Stathis | } |
82 | 9e8e14e4 | Christos Stathis | |
83 | 9e8e14e4 | Christos Stathis | protected static Date unmarshallDate(JSONObject obj, String key){ |
84 | 9e8e14e4 | Christos Stathis | if(obj.get(key) != null) { |
85 | 9e8e14e4 | Christos Stathis | JSONString s = obj.get(key).isString(); |
86 | 9e8e14e4 | Christos Stathis | if (s != null) |
87 | 9e8e14e4 | Christos Stathis | return DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ss").parse(s.stringValue()); |
88 | 9e8e14e4 | Christos Stathis | } |
89 | 9e8e14e4 | Christos Stathis | return null; |
90 | 9e8e14e4 | Christos Stathis | } |
91 | 9e8e14e4 | Christos Stathis | |
92 | 9e8e14e4 | Christos Stathis | public static native String getDate(Long ms)/*-{ |
93 | 9e8e14e4 | Christos Stathis | return (new Date(ms)).toUTCString();
|
94 | 9e8e14e4 | Christos Stathis | }-*/;
|
95 | 9e8e14e4 | Christos Stathis | |
96 | 9e8e14e4 | Christos Stathis | public abstract String getLastModifiedSince(); |
97 | 9e8e14e4 | Christos Stathis | |
98 | 5cd18037 | Christos Stathis | public static <T> T createFromResponse(Class<T> aClass, Response response, T result) { |
99 | 9e8e14e4 | Christos Stathis | if (aClass.equals(AccountResource.class)) {
|
100 | 5cd18037 | Christos Stathis | result = (T) AccountResource.createFromResponse(response); |
101 | 5cd18037 | Christos Stathis | } |
102 | 5cd18037 | Christos Stathis | else if (aClass.equals(Folder.class)) { |
103 | 5cd18037 | Christos Stathis | result = (T) Folder.createFromResponse(response, (Folder) result); |
104 | 9e8e14e4 | Christos Stathis | } |
105 | 0bc032bf | Christos Stathis | else if (aClass.equals(File.class)) { |
106 | 0bc032bf | Christos Stathis | result = (T) File.createFromResponse(response, (File) result); |
107 | 0bc032bf | Christos Stathis | } |
108 | 9e8e14e4 | Christos Stathis | return result;
|
109 | 9e8e14e4 | Christos Stathis | } |
110 | 9e8e14e4 | Christos Stathis | } |