Added 'versioned' field in JSON for My Shared
[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.rest.resource.FileResource;
23 import gr.ebs.gss.client.rest.resource.FolderResource;
24 import gr.ebs.gss.client.rest.resource.GroupResource;
25 import gr.ebs.gss.client.rest.resource.GroupUserResource;
26 import gr.ebs.gss.client.rest.resource.GroupsResource;
27 import gr.ebs.gss.client.rest.resource.OtherUserResource;
28 import gr.ebs.gss.client.rest.resource.OthersResource;
29 import gr.ebs.gss.client.rest.resource.RestResource;
30 import gr.ebs.gss.client.rest.resource.SharedResource;
31 import gr.ebs.gss.client.rest.resource.TrashResource;
32 import gr.ebs.gss.client.rest.resource.UserResource;
33
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.http.client.Response;
43 import com.google.gwt.user.client.DeferredCommand;
44
45 /**
46  * @author kman
47  */
48 public abstract class MultipleGetCommand<T extends RestResource> extends RestCommand {
49
50         Class<T> aclass;
51         List<T> result = new ArrayList<T>();
52         Map<String, Throwable> errors = new HashMap<String, Throwable>();
53         Cached[] cached;
54         String[] paths;
55         private boolean requestSent=false;
56
57         public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, Cached[] theCached) {
58                 this(aNewClass, pathToGet, true, theCached);
59         }
60
61         public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
62                 setShowLoadingIndicator(showLoading);
63                 if (isShowLoadingIndicator())
64                         GSS.get().showLoadingIndicator();
65                 aclass = aNewClass;
66                 paths = pathToGet;
67                 this.cached = theCached;
68                 //sendRequest();
69         }
70
71         private void sendRequest() {
72                 if (requestSent)
73                         return;
74                 requestSent=true;
75                 if (cached!=null)
76                         for (final Cached pathg : cached)
77                                 DeferredCommand.addCommand(new GetCommand<T>(aclass,pathg.uri,false,(T)pathg.cache) {
78
79                                         @Override
80                                         public void onComplete() {
81                                                 MultipleGetCommand.this.result.add(getResult());
82                                         }
83
84                                         @Override
85                                         public void onError(Throwable t) {
86                                                 errors.put(pathg.uri, t);
87                                         }
88
89                                 });
90                 else
91                         for (final String pathg : paths)
92                                 DeferredCommand.addCommand(new GetCommand<T>(aclass,pathg,false,null) {
93
94                                         @Override
95                                         public void onComplete() {
96                                                 MultipleGetCommand.this.result.add(getResult());
97                                         }
98
99                                         @Override
100                                         public void onError(Throwable t) {
101                                                 errors.put(pathg, t);
102                                         }
103
104                                 });
105         }
106
107         public boolean isComplete() {
108                 return result.size()+errors.size() == paths.length;
109         }
110
111         public List<T> getResult() {
112                 if (aclass.equals(FolderResource.class))
113                         Collections.sort(result, new Comparator() {
114                                 @Override
115                                 public int compare(Object o1, Object o2) {
116                                         return ((FolderResource)o1).getName().compareTo(((FolderResource)o2).getName());
117                                 }
118
119                         });
120                 else if(aclass.equals(GroupResource.class))
121                         Collections.sort(result, new Comparator() {
122                                 @Override
123                                 public int compare(Object o1, Object o2) {
124                                         return ((GroupResource)o1).getName().compareTo(((GroupResource)o2).getName());
125                                 }
126
127                         });
128                 else if(aclass.equals(GroupUserResource.class))
129                         Collections.sort(result, new Comparator() {
130                                 @Override
131                                 public int compare(Object o1, Object o2) {
132                                         return ((GroupUserResource)o1).getName().compareTo(((GroupUserResource)o2).getName());
133                                 }
134
135                         });
136                 return result;
137         }
138
139         @Override
140         public boolean execute() {
141                 if (!requestSent)
142                         sendRequest();
143                 boolean com = isComplete();
144                 if (com) {
145                         if (isShowLoadingIndicator())
146                                 GSS.get().hideLoadingIndicator();
147                         if (hasErrors())
148                                 for(String p : errors.keySet())
149                                         onError(p, errors.get(p));
150                         onComplete();
151                         return false;
152                 }
153                 return true;
154         }
155
156         /**
157          * @param p
158          * @param throwable
159          */
160         public abstract void onError(String p, Throwable throwable);
161
162         public Object deserializeResponse(String path, Response response) {
163                 RestResource result1 = null;
164                 if (aclass.equals(FolderResource.class)) {
165                         result1 = new FolderResource(path);
166                         result1.createFromJSON(response.getText());
167                 }
168                 else if (aclass.equals(FileResource.class)){
169                         result1 = new FileResource(path);
170                         result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
171                 }
172                 else if (aclass.equals(GroupsResource.class)) {
173                         result1 = new GroupsResource(path);
174                         result1.createFromJSON(response.getText());
175                 }
176                 else if (aclass.equals(TrashResource.class)) {
177                         result1 = new TrashResource(path);
178                         result1.createFromJSON(response.getText());
179                 }
180                 else if (aclass.equals(SharedResource.class)) {
181                         result1 = new SharedResource(path);
182                         result1.createFromJSON(response.getText());
183                 }
184                 else if (aclass.equals(OthersResource.class)) {
185                         result1 = new OthersResource(path);
186                         result1.createFromJSON(response.getText());
187                 }
188                 else if (aclass.equals(OtherUserResource.class)) {
189                         result1 = new OtherUserResource(path);
190                         result1.createFromJSON(response.getText());
191                 }
192                 else if (aclass.equals(GroupResource.class)) {
193                         result1 = new GroupResource(path);
194                         result1.createFromJSON(response.getText());
195                 }
196                 else if (aclass.equals(GroupUserResource.class)) {
197                         result1 = new GroupUserResource(path);
198                         result1.createFromJSON(response.getText());
199                 }
200                 else if (aclass.equals(UserResource.class)) {
201                         result1 = new UserResource(path);
202                         result1.createFromJSON(response.getText());
203                 }
204                 return result1;
205         }
206
207         public boolean hasErrors() {
208                 return errors.size() >0;
209         }
210
211         /**
212          * Retrieve the errors.
213          *
214          * @return the errors
215          */
216         public Map<String, Throwable> getErrors() {
217                 return errors;
218         }
219
220         protected void debug() {
221                 GWT.log("--->"+result.size(), null);
222                 GWT.log("-ERRORS-->"+getErrors().size(), null);
223                 for(String p : getErrors().keySet())
224                         GWT.log("error:"+p, getErrors().get(p));
225         }
226
227         public static class Cached {
228                 public String uri;
229                 public RestResource cache;
230         }
231 }