root / src / gr / ebs / gss / client / rest / MultipleHeadCommand.java @ 44d84dc0
History | View | Annotate | Download (5.5 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 | a52ea5e4 | pastith | |
21 | a52ea5e4 | pastith | import gr.ebs.gss.client.GSS; |
22 | 62f168b2 | Giannis Koutsoubos | import gr.ebs.gss.client.rest.MultipleGetCommand.Cached; |
23 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.FileResource; |
24 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.FolderResource; |
25 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.GroupResource; |
26 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.GroupUserResource; |
27 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.GroupsResource; |
28 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.RestResource; |
29 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.SharedResource; |
30 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.TrashResource; |
31 | a52ea5e4 | pastith | import gr.ebs.gss.client.rest.resource.UserResource; |
32 | a52ea5e4 | pastith | |
33 | a52ea5e4 | pastith | import java.util.ArrayList; |
34 | a52ea5e4 | pastith | import java.util.HashMap; |
35 | a52ea5e4 | pastith | import java.util.List; |
36 | a52ea5e4 | pastith | import java.util.Map; |
37 | a52ea5e4 | pastith | |
38 | a52ea5e4 | pastith | import com.google.gwt.core.client.GWT; |
39 | a52ea5e4 | pastith | import com.google.gwt.http.client.Response; |
40 | 62f168b2 | Giannis Koutsoubos | import com.google.gwt.user.client.DeferredCommand; |
41 | a52ea5e4 | pastith | |
42 | a52ea5e4 | pastith | |
43 | a52ea5e4 | pastith | /**
|
44 | a52ea5e4 | pastith | * @author kman
|
45 | a52ea5e4 | pastith | *
|
46 | a52ea5e4 | pastith | */
|
47 | 895035a2 | pastith | public abstract class MultipleHeadCommand <T extends RestResource> extends RestCommand { |
48 | a52ea5e4 | pastith | String[] paths; |
49 | a52ea5e4 | pastith | Class<T> aclass;
|
50 | a52ea5e4 | pastith | List<T> result = new ArrayList<T>(); |
51 | a52ea5e4 | pastith | Map<String, Throwable> errors = new HashMap<String, Throwable>(); |
52 | 62f168b2 | Giannis Koutsoubos | private boolean requestSent=false; |
53 | 62f168b2 | Giannis Koutsoubos | Cached[] cached;
|
54 | a52ea5e4 | pastith | |
55 | 86c951b2 | Panagiotis Astithas | public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, Cached[] theCached) { |
56 | 86c951b2 | Panagiotis Astithas | this(theClass, pathToGet, true, theCached); |
57 | aa8608d3 | koutsoub | } |
58 | aa8608d3 | koutsoub | |
59 | 86c951b2 | Panagiotis Astithas | public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, boolean showLoading, Cached[] theCached) { |
60 | aa8608d3 | koutsoub | setShowLoadingIndicator(showLoading); |
61 | aa8608d3 | koutsoub | if(isShowLoadingIndicator())
|
62 | aa8608d3 | koutsoub | GSS.get().showLoadingIndicator(); |
63 | a52ea5e4 | pastith | paths = pathToGet; |
64 | 86c951b2 | Panagiotis Astithas | this.aclass = theClass;
|
65 | 86c951b2 | Panagiotis Astithas | this.cached = theCached;
|
66 | 62f168b2 | Giannis Koutsoubos | //sendRequest();
|
67 | 62f168b2 | Giannis Koutsoubos | } |
68 | 86c951b2 | Panagiotis Astithas | |
69 | 86c951b2 | Panagiotis Astithas | private void sendRequest() { |
70 | 62f168b2 | Giannis Koutsoubos | if(requestSent)
|
71 | 62f168b2 | Giannis Koutsoubos | return;
|
72 | 62f168b2 | Giannis Koutsoubos | requestSent=true;
|
73 | 62f168b2 | Giannis Koutsoubos | if(cached!=null) |
74 | 62f168b2 | Giannis Koutsoubos | for (final Cached c : cached){ |
75 | 62f168b2 | Giannis Koutsoubos | final String path; |
76 | 62f168b2 | Giannis Koutsoubos | if(aclass.equals(FileResource.class)){
|
77 | 62f168b2 | Giannis Koutsoubos | if(c.uri.indexOf("?") == -1) |
78 | 62f168b2 | Giannis Koutsoubos | path=c.uri+"?"+Math.random(); |
79 | 62f168b2 | Giannis Koutsoubos | else
|
80 | 62f168b2 | Giannis Koutsoubos | path=c.uri; |
81 | 62f168b2 | Giannis Koutsoubos | } |
82 | 62f168b2 | Giannis Koutsoubos | else
|
83 | 62f168b2 | Giannis Koutsoubos | path = fixPath(c.uri); |
84 | 62f168b2 | Giannis Koutsoubos | DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, (T)c.cache) { |
85 | 62f168b2 | Giannis Koutsoubos | |
86 | 62f168b2 | Giannis Koutsoubos | @Override
|
87 | 62f168b2 | Giannis Koutsoubos | public void onComplete() { |
88 | 62f168b2 | Giannis Koutsoubos | MultipleHeadCommand.this.result.add(getResult()); |
89 | a52ea5e4 | pastith | } |
90 | a52ea5e4 | pastith | |
91 | 62f168b2 | Giannis Koutsoubos | @Override
|
92 | 62f168b2 | Giannis Koutsoubos | public void onError(Throwable t) { |
93 | 62f168b2 | Giannis Koutsoubos | errors.put(path, t); |
94 | 62f168b2 | Giannis Koutsoubos | } |
95 | a52ea5e4 | pastith | |
96 | 62f168b2 | Giannis Koutsoubos | }); |
97 | 62f168b2 | Giannis Koutsoubos | } |
98 | 62f168b2 | Giannis Koutsoubos | else
|
99 | 62f168b2 | Giannis Koutsoubos | for (String pathg : paths) { |
100 | 62f168b2 | Giannis Koutsoubos | final String path; |
101 | 62f168b2 | Giannis Koutsoubos | if(aclass.equals(FileResource.class))
|
102 | 62f168b2 | Giannis Koutsoubos | path = pathg; |
103 | 62f168b2 | Giannis Koutsoubos | else
|
104 | 62f168b2 | Giannis Koutsoubos | path = fixPath(pathg); |
105 | 62f168b2 | Giannis Koutsoubos | DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, null) { |
106 | 62f168b2 | Giannis Koutsoubos | @Override
|
107 | 62f168b2 | Giannis Koutsoubos | public void onComplete() { |
108 | 62f168b2 | Giannis Koutsoubos | MultipleHeadCommand.this.result.add(getResult()); |
109 | a52ea5e4 | pastith | } |
110 | a52ea5e4 | pastith | |
111 | 62f168b2 | Giannis Koutsoubos | @Override
|
112 | 62f168b2 | Giannis Koutsoubos | public void onError(Throwable t) { |
113 | 62f168b2 | Giannis Koutsoubos | errors.put(path, t); |
114 | 62f168b2 | Giannis Koutsoubos | } |
115 | a52ea5e4 | pastith | }); |
116 | a52ea5e4 | pastith | } |
117 | a52ea5e4 | pastith | } |
118 | a52ea5e4 | pastith | public boolean isComplete() { |
119 | a52ea5e4 | pastith | return result.size()+errors.size() == paths.length;
|
120 | a52ea5e4 | pastith | } |
121 | a52ea5e4 | pastith | |
122 | a52ea5e4 | pastith | public List<T> getResult() { |
123 | a52ea5e4 | pastith | return result;
|
124 | a52ea5e4 | pastith | } |
125 | a52ea5e4 | pastith | |
126 | a52ea5e4 | pastith | public boolean execute() { |
127 | 62f168b2 | Giannis Koutsoubos | if(!requestSent)
|
128 | 62f168b2 | Giannis Koutsoubos | sendRequest(); |
129 | a52ea5e4 | pastith | boolean com = isComplete();
|
130 | a52ea5e4 | pastith | if (com) {
|
131 | aa8608d3 | koutsoub | if(isShowLoadingIndicator())
|
132 | aa8608d3 | koutsoub | GSS.get().hideLoadingIndicator(); |
133 | a52ea5e4 | pastith | if(hasErrors())
|
134 | a52ea5e4 | pastith | for(String p : errors.keySet()) |
135 | a52ea5e4 | pastith | onError(p, errors.get(p)); |
136 | a52ea5e4 | pastith | onComplete(); |
137 | a52ea5e4 | pastith | return false; |
138 | a52ea5e4 | pastith | } |
139 | a52ea5e4 | pastith | return true; |
140 | a52ea5e4 | pastith | } |
141 | a52ea5e4 | pastith | |
142 | a52ea5e4 | pastith | /**
|
143 | a52ea5e4 | pastith | * @param p
|
144 | a52ea5e4 | pastith | * @param throwable
|
145 | a52ea5e4 | pastith | */
|
146 | a52ea5e4 | pastith | public abstract void onError(String p, Throwable throwable); |
147 | a52ea5e4 | pastith | |
148 | a52ea5e4 | pastith | public Object deserializeResponse(String path, Response response) { |
149 | a52ea5e4 | pastith | RestResource result1 = null;
|
150 | a52ea5e4 | pastith | if (aclass.equals(FolderResource.class)) {
|
151 | a52ea5e4 | pastith | result1 = new FolderResource(path);
|
152 | a52ea5e4 | pastith | result1.createFromJSON(response.getText()); |
153 | a52ea5e4 | pastith | } else if (aclass.equals(FileResource.class)) { |
154 | a52ea5e4 | pastith | result1 = new FileResource(path);
|
155 | a52ea5e4 | pastith | result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
|
156 | a52ea5e4 | pastith | } else if (aclass.equals(GroupsResource.class)) { |
157 | a52ea5e4 | pastith | result1 = new GroupsResource(path);
|
158 | a52ea5e4 | pastith | result1.createFromJSON(response.getText()); |
159 | a52ea5e4 | pastith | } else if (aclass.equals(TrashResource.class)) { |
160 | a52ea5e4 | pastith | result1 = new TrashResource(path);
|
161 | a52ea5e4 | pastith | result1.createFromJSON(response.getText()); |
162 | a52ea5e4 | pastith | } else if (aclass.equals(SharedResource.class)) { |
163 | a52ea5e4 | pastith | result1 = new SharedResource(path);
|
164 | a52ea5e4 | pastith | result1.createFromJSON(response.getText()); |
165 | a52ea5e4 | pastith | } else if (aclass.equals(GroupResource.class)) { |
166 | a52ea5e4 | pastith | result1 = new GroupResource(path);
|
167 | a52ea5e4 | pastith | result1.createFromJSON(response.getText()); |
168 | a52ea5e4 | pastith | } else if (aclass.equals(GroupUserResource.class)) { |
169 | a52ea5e4 | pastith | result1 = new GroupUserResource(path);
|
170 | a52ea5e4 | pastith | result1.createFromJSON(response.getText()); |
171 | a52ea5e4 | pastith | } else if (aclass.equals(UserResource.class)) { |
172 | a52ea5e4 | pastith | result1 = new UserResource(path);
|
173 | a52ea5e4 | pastith | result1.createFromJSON(response.getText()); |
174 | a52ea5e4 | pastith | } |
175 | a52ea5e4 | pastith | return result1;
|
176 | a52ea5e4 | pastith | } |
177 | a52ea5e4 | pastith | |
178 | a52ea5e4 | pastith | public boolean hasErrors(){ |
179 | a52ea5e4 | pastith | return errors.size() >0; |
180 | a52ea5e4 | pastith | } |
181 | a52ea5e4 | pastith | |
182 | a52ea5e4 | pastith | /**
|
183 | a52ea5e4 | pastith | * Retrieve the errors.
|
184 | a52ea5e4 | pastith | *
|
185 | a52ea5e4 | pastith | * @return the errors
|
186 | a52ea5e4 | pastith | */
|
187 | a52ea5e4 | pastith | public Map<String, Throwable> getErrors() { |
188 | a52ea5e4 | pastith | return errors;
|
189 | a52ea5e4 | pastith | } |
190 | a52ea5e4 | pastith | |
191 | a52ea5e4 | pastith | public void debug(){ |
192 | a52ea5e4 | pastith | GWT.log("--->"+result.size(), null); |
193 | a52ea5e4 | pastith | GWT.log("-ERRORS-->"+getErrors().size(), null); |
194 | a52ea5e4 | pastith | for(String p : getErrors().keySet()) |
195 | a52ea5e4 | pastith | GWT.log("error:"+p, getErrors().get(p));
|
196 | a52ea5e4 | pastith | } |
197 | a52ea5e4 | pastith | } |