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.OtherUserResource;
29 import gr.ebs.gss.client.rest.resource.OthersResource;
30 import gr.ebs.gss.client.rest.resource.RestResource;
31 import gr.ebs.gss.client.rest.resource.SharedResource;
32 import gr.ebs.gss.client.rest.resource.TrashResource;
33 import gr.ebs.gss.client.rest.resource.UserResource;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Comparator;
38 import java.util.HashMap;
39 import java.util.List;
42 import com.google.gwt.core.client.GWT;
43 import com.google.gwt.http.client.Request;
44 import com.google.gwt.http.client.Response;
49 public abstract class MultipleGetCommand<T extends RestResource> extends RestCommand {
52 List<T> result = new ArrayList<T>();
53 Map<String, Throwable> errors = new HashMap<String, Throwable>();
56 public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet){
57 this(aNewClass, pathToGet, true);
60 public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, boolean showLoading){
61 setShowLoadingIndicator(showLoading);
62 if(isShowLoadingIndicator())
63 GSS.get().showLoadingIndicator();
66 for (String pathg : pathToGet) {
67 final String path = fixPath(pathg);
68 RestRequestBuilder builder = new RestRequestBuilder("GET", path);
71 handleHeaders(builder, path);
72 builder.sendRequest("", new RestCallback(path) {
75 public Object deserialize(Response response) {
76 return deserializeResponse(path, response);
80 public void handleError(Request request, Throwable exception) {
81 errors.put(path, exception);
85 public void handleSuccess(Object object) {
87 result.add((T)object);
89 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() {
104 if(aclass.equals(FolderResource.class))
105 Collections.sort(result, new Comparator(){
106 public int compare(Object o1, Object o2) {
107 return ((FolderResource)o1).getName().compareTo(((FolderResource)o2).getName());
111 else if(aclass.equals(GroupResource.class))
112 Collections.sort(result, new Comparator(){
113 public int compare(Object o1, Object o2) {
114 return ((GroupResource)o1).getName().compareTo(((GroupResource)o2).getName());
118 else if(aclass.equals(GroupUserResource.class))
119 Collections.sort(result, new Comparator(){
120 public int compare(Object o1, Object o2) {
121 return ((GroupUserResource)o1).getName().compareTo(((GroupUserResource)o2).getName());
128 public boolean execute() {
129 boolean com = isComplete();
131 if(isShowLoadingIndicator())
132 GSS.get().hideLoadingIndicator();
134 for(String p : errors.keySet())
135 onError(p, errors.get(p));
146 public abstract void onError(String p, Throwable throwable);
148 public Object deserializeResponse(String path, Response response) {
149 RestResource result1 = null;
150 if(aclass.equals(FolderResource.class)){
151 result1 = new FolderResource(path);
152 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 if(response.getHeader("Content-Type") != null )
158 ((FileResource)result1).setContentType(response.getHeader("Content-Type"));
160 else if(aclass.equals(GroupsResource.class)){
161 result1 = new GroupsResource(path);
162 result1.createFromJSON(response.getText());
164 else if(aclass.equals(TrashResource.class)){
165 result1 = new TrashResource(path);
166 result1.createFromJSON(response.getText());
168 else if(aclass.equals(SharedResource.class)){
169 result1 = new SharedResource(path);
170 result1.createFromJSON(response.getText());
172 else if(aclass.equals(OthersResource.class)){
173 result1 = new OthersResource(path);
174 result1.createFromJSON(response.getText());
176 else if(aclass.equals(OtherUserResource.class)){
177 result1 = new OtherUserResource(path);
178 result1.createFromJSON(response.getText());
180 else if(aclass.equals(GroupResource.class)){
181 result1 = new GroupResource(path);
182 result1.createFromJSON(response.getText());
184 else if(aclass.equals(GroupUserResource.class)){
185 result1 = new GroupUserResource(path);
186 result1.createFromJSON(response.getText());
188 else if(aclass.equals(UserResource.class)){
189 result1 = new UserResource(path);
190 result1.createFromJSON(response.getText());
195 public boolean hasErrors(){
196 return errors.size() >0;
200 * Retrieve the errors.
204 public Map<String, Throwable> getErrors() {
208 protected void debug(){
209 GWT.log("--->"+result.size(), null);
210 GWT.log("-ERRORS-->"+getErrors().size(), null);
211 for(String p : getErrors().keySet())
212 GWT.log("error:"+p, getErrors().get(p));