2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client.rest;
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.rest.resource.FileResource;
23 import gr.ebs.gss.client.rest.resource.FolderResource;
24 import gr.ebs.gss.client.rest.resource.GroupResource;
25 import gr.ebs.gss.client.rest.resource.GroupUserResource;
26 import gr.ebs.gss.client.rest.resource.GroupsResource;
27 import gr.ebs.gss.client.rest.resource.OtherUserResource;
28 import gr.ebs.gss.client.rest.resource.OthersResource;
29 import gr.ebs.gss.client.rest.resource.RestResource;
30 import gr.ebs.gss.client.rest.resource.SharedResource;
31 import gr.ebs.gss.client.rest.resource.TrashResource;
32 import gr.ebs.gss.client.rest.resource.UserResource;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.HashMap;
38 import java.util.List;
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.http.client.Response;
43 import com.google.gwt.user.client.DeferredCommand;
48 public abstract class MultipleGetCommand<T extends RestResource> extends RestCommand {
51 List<T> result = new ArrayList<T>();
52 Map<String, Throwable> errors = new HashMap<String, Throwable>();
55 private boolean requestSent=false;
57 public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, Cached[] theCached) {
58 this(aNewClass, pathToGet, true, theCached);
61 public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
62 setShowLoadingIndicator(showLoading);
63 if (isShowLoadingIndicator())
64 GSS.get().showLoadingIndicator();
67 this.cached = theCached;
71 private void sendRequest() {
76 for (final Cached pathg : cached)
77 DeferredCommand.addCommand(new GetCommand<T>(aclass,pathg.uri,false,(T)pathg.cache) {
80 public void onComplete() {
81 MultipleGetCommand.this.result.add(getResult());
85 public void onError(Throwable t) {
86 errors.put(pathg.uri, t);
91 for (final String pathg : paths)
92 DeferredCommand.addCommand(new GetCommand<T>(aclass,pathg,false,null) {
95 public void onComplete() {
96 MultipleGetCommand.this.result.add(getResult());
100 public void onError(Throwable t) {
101 errors.put(pathg, t);
107 public boolean isComplete() {
108 return result.size()+errors.size() == paths.length;
111 public List<T> getResult() {
112 if (aclass.equals(FolderResource.class))
113 Collections.sort(result, new Comparator() {
115 public int compare(Object o1, Object o2) {
116 return ((FolderResource)o1).getName().compareTo(((FolderResource)o2).getName());
120 else if(aclass.equals(GroupResource.class))
121 Collections.sort(result, new Comparator() {
123 public int compare(Object o1, Object o2) {
124 return ((GroupResource)o1).getName().compareTo(((GroupResource)o2).getName());
128 else if(aclass.equals(GroupUserResource.class))
129 Collections.sort(result, new Comparator() {
131 public int compare(Object o1, Object o2) {
132 return ((GroupUserResource)o1).getName().compareTo(((GroupUserResource)o2).getName());
140 public boolean execute() {
143 boolean com = isComplete();
145 if (isShowLoadingIndicator())
146 GSS.get().hideLoadingIndicator();
148 for(String p : errors.keySet())
149 onError(p, errors.get(p));
160 public abstract void onError(String p, Throwable throwable);
162 public Object deserializeResponse(String path, Response response) {
163 RestResource result1 = null;
164 if (aclass.equals(FolderResource.class)) {
165 result1 = new FolderResource(path);
166 result1.createFromJSON(response.getText());
168 else if (aclass.equals(FileResource.class)){
169 result1 = new FileResource(path);
170 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
172 else if (aclass.equals(GroupsResource.class)) {
173 result1 = new GroupsResource(path);
174 result1.createFromJSON(response.getText());
176 else if (aclass.equals(TrashResource.class)) {
177 result1 = new TrashResource(path);
178 result1.createFromJSON(response.getText());
180 else if (aclass.equals(SharedResource.class)) {
181 result1 = new SharedResource(path);
182 result1.createFromJSON(response.getText());
184 else if (aclass.equals(OthersResource.class)) {
185 result1 = new OthersResource(path);
186 result1.createFromJSON(response.getText());
188 else if (aclass.equals(OtherUserResource.class)) {
189 result1 = new OtherUserResource(path);
190 result1.createFromJSON(response.getText());
192 else if (aclass.equals(GroupResource.class)) {
193 result1 = new GroupResource(path);
194 result1.createFromJSON(response.getText());
196 else if (aclass.equals(GroupUserResource.class)) {
197 result1 = new GroupUserResource(path);
198 result1.createFromJSON(response.getText());
200 else if (aclass.equals(UserResource.class)) {
201 result1 = new UserResource(path);
202 result1.createFromJSON(response.getText());
207 public boolean hasErrors() {
208 return errors.size() >0;
212 * Retrieve the errors.
216 public Map<String, Throwable> getErrors() {
220 protected void debug() {
221 GWT.log("--->"+result.size(), null);
222 GWT.log("-ERRORS-->"+getErrors().size(), null);
223 for(String p : getErrors().keySet())
224 GWT.log("error:"+p, getErrors().get(p));
227 public static class Cached {
229 public RestResource cache;