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.MultipleGetCommand.Cached;
23 import gr.ebs.gss.client.rest.resource.FileResource;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25 import gr.ebs.gss.client.rest.resource.GroupResource;
26 import gr.ebs.gss.client.rest.resource.GroupUserResource;
27 import gr.ebs.gss.client.rest.resource.GroupsResource;
28 import gr.ebs.gss.client.rest.resource.RestResource;
29 import gr.ebs.gss.client.rest.resource.SharedResource;
30 import gr.ebs.gss.client.rest.resource.TrashResource;
31 import gr.ebs.gss.client.rest.resource.UserResource;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
38 import com.google.gwt.core.client.GWT;
39 import com.google.gwt.http.client.Response;
40 import com.google.gwt.user.client.DeferredCommand;
47 public abstract class MultipleHeadCommand <T extends RestResource> extends RestCommand {
50 List<T> result = new ArrayList<T>();
51 Map<String, Throwable> errors = new HashMap<String, Throwable>();
52 private boolean requestSent=false;
55 public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, Cached[] theCached) {
56 this(theClass, pathToGet, true, theCached);
59 public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
60 setShowLoadingIndicator(showLoading);
61 if(isShowLoadingIndicator())
62 GSS.get().showLoadingIndicator();
64 this.aclass = theClass;
65 this.cached = theCached;
69 private void sendRequest() {
74 for (final Cached c : cached){
76 if(aclass.equals(FileResource.class)){
77 if(c.uri.indexOf("?") == -1)
78 path=c.uri+"?"+Math.random();
83 path = fixPath(c.uri);
84 DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, (T)c.cache) {
87 public void onComplete() {
88 MultipleHeadCommand.this.result.add(getResult());
92 public void onError(Throwable t) {
99 for (String pathg : paths) {
101 if(aclass.equals(FileResource.class))
104 path = fixPath(pathg);
105 DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, null) {
107 public void onComplete() {
108 MultipleHeadCommand.this.result.add(getResult());
112 public void onError(Throwable t) {
118 public boolean isComplete() {
119 return result.size()+errors.size() == paths.length;
122 public List<T> getResult() {
127 public boolean execute() {
130 boolean com = isComplete();
132 if(isShowLoadingIndicator())
133 GSS.get().hideLoadingIndicator();
135 for(String p : errors.keySet())
136 onError(p, errors.get(p));
147 public abstract void onError(String p, Throwable throwable);
149 public Object deserializeResponse(String path, Response response) {
150 RestResource result1 = null;
151 if (aclass.equals(FolderResource.class)) {
152 result1 = new FolderResource(path);
153 result1.createFromJSON(response.getText());
154 } else if (aclass.equals(FileResource.class)) {
155 result1 = new FileResource(path);
156 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
157 } else if (aclass.equals(GroupsResource.class)) {
158 result1 = new GroupsResource(path);
159 result1.createFromJSON(response.getText());
160 } else if (aclass.equals(TrashResource.class)) {
161 result1 = new TrashResource(path);
162 result1.createFromJSON(response.getText());
163 } else if (aclass.equals(SharedResource.class)) {
164 result1 = new SharedResource(path);
165 result1.createFromJSON(response.getText());
166 } else if (aclass.equals(GroupResource.class)) {
167 result1 = new GroupResource(path);
168 result1.createFromJSON(response.getText());
169 } else if (aclass.equals(GroupUserResource.class)) {
170 result1 = new GroupUserResource(path);
171 result1.createFromJSON(response.getText());
172 } else if (aclass.equals(UserResource.class)) {
173 result1 = new UserResource(path);
174 result1.createFromJSON(response.getText());
179 public boolean hasErrors(){
180 return errors.size() >0;
184 * Retrieve the errors.
188 public Map<String, Throwable> getErrors() {
193 GWT.log("--->"+result.size(), null);
194 GWT.log("-ERRORS-->"+getErrors().size(), null);
195 for(String p : getErrors().keySet())
196 GWT.log("error:"+p, getErrors().get(p));