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"));
158 else if(aclass.equals(GroupsResource.class)){
159 result1 = new GroupsResource(path);
160 result1.createFromJSON(response.getText());
162 else if(aclass.equals(TrashResource.class)){
163 result1 = new TrashResource(path);
164 result1.createFromJSON(response.getText());
166 else if(aclass.equals(SharedResource.class)){
167 result1 = new SharedResource(path);
168 result1.createFromJSON(response.getText());
170 else if(aclass.equals(OthersResource.class)){
171 result1 = new OthersResource(path);
172 result1.createFromJSON(response.getText());
174 else if(aclass.equals(OtherUserResource.class)){
175 result1 = new OtherUserResource(path);
176 result1.createFromJSON(response.getText());
178 else if(aclass.equals(GroupResource.class)){
179 result1 = new GroupResource(path);
180 result1.createFromJSON(response.getText());
182 else if(aclass.equals(GroupUserResource.class)){
183 result1 = new GroupUserResource(path);
184 result1.createFromJSON(response.getText());
186 else if(aclass.equals(UserResource.class)){
187 result1 = new UserResource(path);
188 result1.createFromJSON(response.getText());
193 public boolean hasErrors(){
194 return errors.size() >0;
198 * Retrieve the errors.
202 public Map<String, Throwable> getErrors() {
206 protected void debug(){
207 GWT.log("--->"+result.size(), null);
208 GWT.log("-ERRORS-->"+getErrors().size(), null);
209 for(String p : getErrors().keySet())
210 GWT.log("error:"+p, getErrors().get(p));