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.exceptions.ObjectNotFoundException;
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.Request;
40 import com.google.gwt.http.client.Response;
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>();
53 public MultipleHeadCommand(Class<T> aclass, String[] pathToGet){
54 this(aclass, pathToGet, true);
57 public MultipleHeadCommand(Class<T> aclass, String[] pathToGet, boolean showLoading){
58 setShowLoadingIndicator(showLoading);
59 if(isShowLoadingIndicator())
60 GSS.get().showLoadingIndicator();
63 for (String pathg : pathToGet) {
65 if(aclass.equals(FileResource.class))
68 path = fixPath(pathg);
69 RestRequestBuilder builder = new RestRequestBuilder("HEAD", path);
72 handleHeaders(builder, path);
73 builder.sendRequest("", new RestCallback(path) {
75 public Object deserialize(Response response) {
76 return deserializeResponse(path, response);
79 public void handleError(Request request, Throwable exception) {
80 errors.put(path, exception);
83 public void handleSuccess(Object object) {
85 result.add((T)object);
87 errors.put(path, new ObjectNotFoundException("resource not found"));
93 } catch (Exception ex) {
99 public boolean isComplete() {
100 return result.size()+errors.size() == paths.length;
103 public List<T> getResult() {
107 public boolean execute() {
108 boolean com = isComplete();
110 if(isShowLoadingIndicator())
111 GSS.get().hideLoadingIndicator();
113 for(String p : errors.keySet())
114 onError(p, errors.get(p));
125 public abstract void onError(String p, Throwable throwable);
127 public Object deserializeResponse(String path, Response response) {
128 RestResource result1 = null;
129 if (aclass.equals(FolderResource.class)) {
130 result1 = new FolderResource(path);
131 result1.createFromJSON(response.getText());
133 } else if (aclass.equals(FileResource.class)) {
134 result1 = new FileResource(path);
135 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
136 } else if (aclass.equals(GroupsResource.class)) {
137 result1 = new GroupsResource(path);
138 result1.createFromJSON(response.getText());
139 } else if (aclass.equals(TrashResource.class)) {
140 result1 = new TrashResource(path);
141 result1.createFromJSON(response.getText());
143 } else if (aclass.equals(SharedResource.class)) {
144 result1 = new SharedResource(path);
145 result1.createFromJSON(response.getText());
147 } else if (aclass.equals(GroupResource.class)) {
148 result1 = new GroupResource(path);
149 result1.createFromJSON(response.getText());
151 } else if (aclass.equals(GroupUserResource.class)) {
152 result1 = new GroupUserResource(path);
153 result1.createFromJSON(response.getText());
155 } else if (aclass.equals(UserResource.class)) {
156 result1 = new UserResource(path);
157 result1.createFromJSON(response.getText());
164 public boolean hasErrors(){
165 return errors.size() >0;
170 * Retrieve the errors.
174 public Map<String, Throwable> getErrors() {
179 GWT.log("--->"+result.size(), null);
180 GWT.log("-ERRORS-->"+getErrors().size(), null);
181 for(String p : getErrors().keySet())
182 GWT.log("error:"+p, getErrors().get(p));