Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / client / rest / MultipleGetCommand.java
1 /*
2  * Copyright 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 package gr.ebs.gss.client.rest;
20
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;
34
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;
40 import java.util.Map;
41
42 import com.google.gwt.core.client.GWT;
43 import com.google.gwt.http.client.Request;
44 import com.google.gwt.http.client.Response;
45
46 /**
47  * @author kman
48  */
49 public abstract class MultipleGetCommand<T extends RestResource> extends RestCommand {
50
51         Class<T> aclass;
52         List<T> result = new ArrayList<T>();
53         Map<String, Throwable> errors = new HashMap<String, Throwable>();
54         String[] paths;
55
56         public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet){
57                 this(aNewClass, pathToGet, true);
58         }
59
60         public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, boolean showLoading){
61                 setShowLoadingIndicator(showLoading);
62                 if(isShowLoadingIndicator())
63                         GSS.get().showLoadingIndicator();
64                 aclass = aNewClass;
65                 paths = pathToGet;
66                 for (String pathg : pathToGet) {
67                         final String path = fixPath(pathg);
68                         RestRequestBuilder builder = new RestRequestBuilder("GET",  path);
69
70                         try {
71                                 handleHeaders(builder, path);
72                                 builder.sendRequest("", new RestCallback(path) {
73
74                                         @Override
75                                         public Object deserialize(Response response) {
76                                                 return deserializeResponse(path, response);
77                                         }
78
79                                         @Override
80                                         public void handleError(Request request, Throwable exception) {
81                                                 errors.put(path, exception);
82                                         }
83
84                                         @Override
85                                         public void handleSuccess(Object object) {
86                                                 if(object!= null)
87                                                         result.add((T)object);
88                                                 else
89                                                         errors.put(path, new ObjectNotFoundException("resource not found"));
90                                         }
91
92                                 });
93                         } catch (Exception ex) {
94                                 errors.put(path, ex);
95                         }
96                 }
97         }
98
99         public boolean isComplete() {
100                 return result.size()+errors.size() == paths.length;
101         }
102
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());
108                                 }
109
110                         });
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());
115                                 }
116
117                         });
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());
122                                 }
123
124                         });
125                 return result;
126         }
127
128         public boolean execute() {
129                 boolean com = isComplete();
130                 if (com) {
131                         if(isShowLoadingIndicator())
132                                 GSS.get().hideLoadingIndicator();
133                         if(hasErrors())
134                                 for(String p : errors.keySet())
135                                         onError(p, errors.get(p));
136                         onComplete();
137                         return false;
138                 }
139                 return true;
140         }
141
142         /**
143          * @param p
144          * @param throwable
145          */
146         public abstract void onError(String p, Throwable throwable);
147
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());
153                 }
154                 else if(aclass.equals(FileResource.class)){
155                         result1 = new FileResource(path);
156                         result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
157                 }
158                 else if(aclass.equals(GroupsResource.class)){
159                         result1 = new GroupsResource(path);
160                         result1.createFromJSON(response.getText());
161                 }
162                 else if(aclass.equals(TrashResource.class)){
163                         result1 = new TrashResource(path);
164                         result1.createFromJSON(response.getText());
165                 }
166                 else if(aclass.equals(SharedResource.class)){
167                         result1 = new SharedResource(path);
168                         result1.createFromJSON(response.getText());
169                 }
170                 else if(aclass.equals(OthersResource.class)){
171                         result1 = new OthersResource(path);
172                         result1.createFromJSON(response.getText());
173                 }
174                 else if(aclass.equals(OtherUserResource.class)){
175                         result1 = new OtherUserResource(path);
176                         result1.createFromJSON(response.getText());
177                 }
178                 else if(aclass.equals(GroupResource.class)){
179                         result1 = new GroupResource(path);
180                         result1.createFromJSON(response.getText());
181                 }
182                 else if(aclass.equals(GroupUserResource.class)){
183                         result1 = new GroupUserResource(path);
184                         result1.createFromJSON(response.getText());
185                 }
186                 else if(aclass.equals(UserResource.class)){
187                         result1 = new UserResource(path);
188                         result1.createFromJSON(response.getText());
189                 }
190                 return result1;
191         }
192
193         public boolean hasErrors(){
194                 return errors.size() >0;
195         }
196
197         /**
198          * Retrieve the errors.
199          *
200          * @return the errors
201          */
202         public Map<String, Throwable> getErrors() {
203                 return errors;
204         }
205
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));
211         }
212 }