Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / GetCommand.java @ d0d4d0f5

History | View | Annotate | Download (6.2 kB)

1 a52ea5e4 pastith
/*
2 a52ea5e4 pastith
 * Copyright 2009 Electronic Business Systems Ltd.
3 a52ea5e4 pastith
 *
4 a52ea5e4 pastith
 * This file is part of GSS.
5 a52ea5e4 pastith
 *
6 a52ea5e4 pastith
 * GSS is free software: you can redistribute it and/or modify
7 a52ea5e4 pastith
 * it under the terms of the GNU General Public License as published by
8 a52ea5e4 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 a52ea5e4 pastith
 * (at your option) any later version.
10 a52ea5e4 pastith
 *
11 a52ea5e4 pastith
 * GSS is distributed in the hope that it will be useful,
12 a52ea5e4 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 a52ea5e4 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 a52ea5e4 pastith
 * GNU General Public License for more details.
15 a52ea5e4 pastith
 *
16 a52ea5e4 pastith
 * You should have received a copy of the GNU General Public License
17 a52ea5e4 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 a52ea5e4 pastith
 */
19 a52ea5e4 pastith
package gr.ebs.gss.client.rest;
20 0e4865ee pastith
21 a52ea5e4 pastith
import gr.ebs.gss.client.GSS;
22 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FileResource;
23 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FolderResource;
24 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupResource;
25 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupUserResource;
26 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupsResource;
27 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.OtherUserResource;
28 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.OthersResource;
29 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.RestResource;
30 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.SearchResource;
31 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.SharedResource;
32 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.TagsResource;
33 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.TrashResource;
34 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.UploadStatusResource;
35 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.UserResource;
36 41d05cc8 pastith
import gr.ebs.gss.client.rest.resource.UserSearchResource;
37 a52ea5e4 pastith
38 a52ea5e4 pastith
import com.google.gwt.http.client.Request;
39 a52ea5e4 pastith
import com.google.gwt.http.client.Response;
40 a52ea5e4 pastith
41 a52ea5e4 pastith
/**
42 a52ea5e4 pastith
 * @author kman
43 a52ea5e4 pastith
 */
44 895035a2 pastith
public abstract class GetCommand<T extends RestResource> extends RestCommand{
45 a52ea5e4 pastith
46 a52ea5e4 pastith
        boolean complete = false;
47 a52ea5e4 pastith
        T result = null;
48 a52ea5e4 pastith
        Class<T> aclass;
49 a52ea5e4 pastith
50 895035a2 pastith
        public GetCommand(Class<T> theclass, String pathToGet){
51 0e4865ee pastith
                this(theclass,pathToGet,true);
52 aa8608d3 koutsoub
        }
53 aa8608d3 koutsoub
54 895035a2 pastith
        public GetCommand(Class<T> theclass, String pathToGet, boolean showLoading){
55 aa8608d3 koutsoub
                setShowLoadingIndicator(showLoading);
56 aa8608d3 koutsoub
                if(isShowLoadingIndicator())
57 aa8608d3 koutsoub
                        GSS.get().showLoadingIndicator();
58 0e4865ee pastith
                this.aclass = theclass;
59 a52ea5e4 pastith
                final String path;
60 a52ea5e4 pastith
                if(pathToGet.indexOf("?") != -1)
61 a52ea5e4 pastith
                        path = pathToGet;
62 a52ea5e4 pastith
                else
63 a52ea5e4 pastith
                        path =fixPath(pathToGet);
64 614140f2 pastith
                RestRequestBuilder builder = new RestRequestBuilder("GET", path);
65 a52ea5e4 pastith
66 a52ea5e4 pastith
                try {
67 a52ea5e4 pastith
                        handleHeaders(builder, path);
68 a52ea5e4 pastith
                        builder.sendRequest("", new RestCallback(path) {
69 a52ea5e4 pastith
70 0e4865ee pastith
                                @Override
71 a52ea5e4 pastith
                                public Object deserialize(Response response) {
72 a52ea5e4 pastith
                                        return deserializeResponse(path, response);
73 a52ea5e4 pastith
                                }
74 a52ea5e4 pastith
75 0e4865ee pastith
                                @Override
76 a52ea5e4 pastith
                                public void handleError(Request request, Throwable exception) {
77 a52ea5e4 pastith
                                        complete = true;
78 895035a2 pastith
                                        GetCommand.this.onError(exception);
79 a52ea5e4 pastith
                                }
80 a52ea5e4 pastith
81 0e4865ee pastith
                                @Override
82 a52ea5e4 pastith
                                public void handleSuccess(Object object) {
83 a52ea5e4 pastith
                                        result = (T) object;
84 a52ea5e4 pastith
                                        complete = true;
85 a52ea5e4 pastith
                                }
86 a52ea5e4 pastith
87 a52ea5e4 pastith
                        });
88 a52ea5e4 pastith
                } catch (Exception ex) {
89 a52ea5e4 pastith
                        complete = true;
90 a52ea5e4 pastith
                        onError(ex);
91 a52ea5e4 pastith
                }
92 a52ea5e4 pastith
        }
93 a52ea5e4 pastith
94 895035a2 pastith
        public GetCommand(Class<T> theclass, String username , String pathToGet){
95 0e4865ee pastith
                this(theclass,username, pathToGet, true);
96 aa8608d3 koutsoub
        }
97 aa8608d3 koutsoub
98 895035a2 pastith
        public GetCommand(Class<T> theclass, String username , String pathToGet, boolean showLoading){
99 aa8608d3 koutsoub
                setShowLoadingIndicator(showLoading);
100 aa8608d3 koutsoub
                if(isShowLoadingIndicator())
101 aa8608d3 koutsoub
                        GSS.get().showLoadingIndicator();
102 0e4865ee pastith
                this.aclass = theclass;
103 a52ea5e4 pastith
                final String path = fixPath(pathToGet);
104 a52ea5e4 pastith
                RestRequestBuilder builder = new RestRequestBuilder("GET", path);
105 a52ea5e4 pastith
106 a52ea5e4 pastith
                try {
107 a52ea5e4 pastith
                        handleHeaders(username, builder, path);
108 a52ea5e4 pastith
                        builder.sendRequest("", new RestCallback(path) {
109 a52ea5e4 pastith
110 0e4865ee pastith
                                @Override
111 a52ea5e4 pastith
                                public Object deserialize(Response response) {
112 a52ea5e4 pastith
                                        return deserializeResponse(path, response);
113 a52ea5e4 pastith
                                }
114 a52ea5e4 pastith
115 0e4865ee pastith
                                @Override
116 a52ea5e4 pastith
                                public void handleError(Request request, Throwable exception) {
117 a52ea5e4 pastith
                                        complete = true;
118 895035a2 pastith
                                        GetCommand.this.onError(exception);
119 a52ea5e4 pastith
                                }
120 a52ea5e4 pastith
121 0e4865ee pastith
                                @Override
122 a52ea5e4 pastith
                                public void handleSuccess(Object object) {
123 a52ea5e4 pastith
                                        result = (T) object;
124 a52ea5e4 pastith
                                        complete = true;
125 a52ea5e4 pastith
                                }
126 a52ea5e4 pastith
127 a52ea5e4 pastith
                        });
128 a52ea5e4 pastith
                } catch (Exception ex) {
129 a52ea5e4 pastith
                        complete = true;
130 a52ea5e4 pastith
                        onError(ex);
131 a52ea5e4 pastith
                }
132 a52ea5e4 pastith
        }
133 a52ea5e4 pastith
134 a52ea5e4 pastith
        public boolean isComplete() {
135 a52ea5e4 pastith
                return complete;
136 a52ea5e4 pastith
        }
137 a52ea5e4 pastith
138 a52ea5e4 pastith
        public T getResult(){
139 a52ea5e4 pastith
                return result;
140 a52ea5e4 pastith
        }
141 a52ea5e4 pastith
142 a52ea5e4 pastith
        public boolean execute() {
143 a52ea5e4 pastith
                boolean com = isComplete();
144 a52ea5e4 pastith
                if(com){
145 aa8608d3 koutsoub
                        if(isShowLoadingIndicator())
146 aa8608d3 koutsoub
                                GSS.get().hideLoadingIndicator();
147 a52ea5e4 pastith
                        if(getResult() != null)
148 a52ea5e4 pastith
                                onComplete();
149 a52ea5e4 pastith
                        return false;
150 a52ea5e4 pastith
                }
151 a52ea5e4 pastith
                return true;
152 a52ea5e4 pastith
        }
153 a52ea5e4 pastith
154 0e4865ee pastith
        public Object deserializeResponse(String path, Response response) {
155 a52ea5e4 pastith
                RestResource result1 = null;
156 a52ea5e4 pastith
                if(aclass.equals(FolderResource.class)){
157 a52ea5e4 pastith
                        result1 = new FolderResource(path);
158 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
159 a52ea5e4 pastith
                }
160 a52ea5e4 pastith
                else if(aclass.equals(FileResource.class)){
161 a52ea5e4 pastith
                        result1 = new FileResource(path);
162 a52ea5e4 pastith
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
163 a52ea5e4 pastith
                }
164 a52ea5e4 pastith
                else if(aclass.equals(GroupsResource.class)){
165 a52ea5e4 pastith
                        result1 = new GroupsResource(path);
166 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
167 a52ea5e4 pastith
                }
168 a52ea5e4 pastith
                else if(aclass.equals(TrashResource.class)){
169 a52ea5e4 pastith
                        result1 = new TrashResource(path);
170 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
171 a52ea5e4 pastith
                }
172 a52ea5e4 pastith
                else if(aclass.equals(SharedResource.class)){
173 a52ea5e4 pastith
                        result1 = new SharedResource(path);
174 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
175 a52ea5e4 pastith
                }
176 a52ea5e4 pastith
                else if(aclass.equals(OthersResource.class)){
177 a52ea5e4 pastith
                        result1 = new OthersResource(path);
178 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
179 a52ea5e4 pastith
                }
180 a52ea5e4 pastith
                else if(aclass.equals(OtherUserResource.class)){
181 a52ea5e4 pastith
                        result1 = new OtherUserResource(path);
182 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
183 a52ea5e4 pastith
                }
184 a52ea5e4 pastith
                else if(aclass.equals(GroupResource.class)){
185 a52ea5e4 pastith
                        result1 = new GroupResource(path);
186 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
187 a52ea5e4 pastith
                }
188 a52ea5e4 pastith
                else if(aclass.equals(GroupUserResource.class)){
189 a52ea5e4 pastith
                        result1 = new GroupUserResource(path);
190 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
191 a52ea5e4 pastith
                }
192 a52ea5e4 pastith
                else if(aclass.equals(UserResource.class)){
193 a52ea5e4 pastith
                        result1 = new UserResource(path);
194 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
195 a52ea5e4 pastith
                }
196 a52ea5e4 pastith
                else if(aclass.equals(TagsResource.class)){
197 a52ea5e4 pastith
                        result1 = new TagsResource(path);
198 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
199 a52ea5e4 pastith
                }
200 a52ea5e4 pastith
                else if(aclass.equals(SearchResource.class)){
201 a52ea5e4 pastith
                        result1 = new SearchResource(path);
202 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
203 a52ea5e4 pastith
                }
204 41d05cc8 pastith
                else if(aclass.equals(UserSearchResource.class)){
205 41d05cc8 pastith
                        result1 = new UserSearchResource(path);
206 41d05cc8 pastith
                        result1.createFromJSON(response.getText());
207 41d05cc8 pastith
                }
208 a52ea5e4 pastith
                else if(aclass.equals(UploadStatusResource.class)){
209 a52ea5e4 pastith
                        result1 = new UploadStatusResource(path);
210 a52ea5e4 pastith
                        result1.createFromJSON(response.getText());
211 a52ea5e4 pastith
                }
212 a52ea5e4 pastith
                return result1;
213 a52ea5e4 pastith
        }
214 a52ea5e4 pastith
215 a52ea5e4 pastith
}