Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / rest / MultipleGetCommand.java @ 58777026

History | View | Annotate | Download (7.4 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client.rest;
36

    
37
import gr.grnet.pithos.web.client.GSS;
38
import gr.grnet.pithos.web.client.rest.resource.FileResource;
39
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
40
import gr.grnet.pithos.web.client.rest.resource.GroupResource;
41
import gr.grnet.pithos.web.client.rest.resource.GroupUserResource;
42
import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
43
import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
44
import gr.grnet.pithos.web.client.rest.resource.OthersResource;
45
import gr.grnet.pithos.web.client.rest.resource.RestResource;
46
import gr.grnet.pithos.web.client.rest.resource.SharedResource;
47
import gr.grnet.pithos.web.client.rest.resource.TrashResource;
48
import gr.grnet.pithos.web.client.rest.resource.UserResource;
49

    
50
import java.util.ArrayList;
51
import java.util.Collections;
52
import java.util.Comparator;
53
import java.util.HashMap;
54
import java.util.List;
55
import java.util.Map;
56

    
57
import com.google.gwt.core.client.GWT;
58
import com.google.gwt.http.client.Response;
59
import com.google.gwt.user.client.DeferredCommand;
60

    
61
public abstract class MultipleGetCommand<T extends RestResource> extends RestCommand {
62

    
63
        Class<T> aclass;
64
        List<T> result = new ArrayList<T>();
65
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
66
        Cached[] cached;
67
        String[] paths;
68
        private boolean requestSent=false;
69

    
70
        public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, Cached[] theCached) {
71
                this(aNewClass, pathToGet, true, theCached);
72
        }
73

    
74
        public MultipleGetCommand(Class<T> aNewClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
75
                setShowLoadingIndicator(showLoading);
76
                if (isShowLoadingIndicator())
77
                        GSS.get().showLoadingIndicator("Getting "+pathToGet.length+" items", null);
78
                aclass = aNewClass;
79
                paths = pathToGet;
80
                this.cached = theCached;
81
                //sendRequest();
82
        }
83

    
84
        private void sendRequest() {
85
                if (requestSent)
86
                        return;
87
                requestSent=true;
88
                if (cached!=null)
89
                        for (final Cached pathg : cached)
90
                                DeferredCommand.addCommand(new GetCommand<T>(aclass,pathg.uri,false,(T)pathg.cache) {
91

    
92
                                        @Override
93
                                        public void onComplete() {
94
                                                MultipleGetCommand.this.result.add(getResult());
95
                                        }
96

    
97
                                        @Override
98
                                        public void onError(Throwable t) {
99
                                                errors.put(pathg.uri, t);
100
                                        }
101

    
102
                                });
103
                else
104
                        for (final String pathg : paths)
105
                                DeferredCommand.addCommand(new GetCommand<T>(aclass,pathg,false,null) {
106

    
107
                                        @Override
108
                                        public void onComplete() {
109
                                                MultipleGetCommand.this.result.add(getResult());
110
                                        }
111

    
112
                                        @Override
113
                                        public void onError(Throwable t) {
114
                                                errors.put(pathg, t);
115
                                        }
116

    
117
                                });
118
        }
119

    
120
        public boolean isComplete() {
121
                return result.size()+errors.size() == paths.length;
122
        }
123

    
124
        public List<T> getResult() {
125
                if (aclass.equals(FolderResource.class))
126
                        Collections.sort(result, new Comparator() {
127
                                @Override
128
                                public int compare(Object o1, Object o2) {
129
                                        return ((FolderResource)o1).getName().compareTo(((FolderResource)o2).getName());
130
                                }
131

    
132
                        });
133
                else if(aclass.equals(GroupResource.class))
134
                        Collections.sort(result, new Comparator() {
135
                                @Override
136
                                public int compare(Object o1, Object o2) {
137
                                        return ((GroupResource)o1).getName().compareTo(((GroupResource)o2).getName());
138
                                }
139

    
140
                        });
141
                else if(aclass.equals(GroupUserResource.class))
142
                        Collections.sort(result, new Comparator() {
143
                                @Override
144
                                public int compare(Object o1, Object o2) {
145
                                        return ((GroupUserResource)o1).getName().compareTo(((GroupUserResource)o2).getName());
146
                                }
147

    
148
                        });
149
                return result;
150
        }
151

    
152
        @Override
153
        public boolean execute() {
154
                if (!requestSent)
155
                        sendRequest();
156
                boolean com = isComplete();
157
                if (com) {
158
                        if (isShowLoadingIndicator())
159
                                GSS.get().hideLoadingIndicator();
160
                        if (hasErrors())
161
                                for(String p : errors.keySet())
162
                                        onError(p, errors.get(p));
163
                        onComplete();
164
                        return false;
165
                }
166
                return true;
167
        }
168

    
169
        /**
170
         * @param p
171
         * @param throwable
172
         */
173
        public abstract void onError(String p, Throwable throwable);
174

    
175
        public Object deserializeResponse(String path, Response response) {
176
                RestResource result1 = null;
177
                if (aclass.equals(FolderResource.class)) {
178
                        result1 = new FolderResource(path);
179
                        result1.createFromJSON(response.getText());
180
                }
181
                else if (aclass.equals(FileResource.class)){
182
                        result1 = new FileResource(path);
183
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
184
                }
185
                else if (aclass.equals(GroupsResource.class)) {
186
                        result1 = new GroupsResource(path);
187
                        result1.createFromJSON(response.getText());
188
                }
189
                else if (aclass.equals(TrashResource.class)) {
190
                        result1 = new TrashResource(path);
191
                        result1.createFromJSON(response.getText());
192
                }
193
                else if (aclass.equals(SharedResource.class)) {
194
                        result1 = new SharedResource(path);
195
                        result1.createFromJSON(response.getText());
196
                }
197
                else if (aclass.equals(OthersResource.class)) {
198
                        result1 = new OthersResource(path);
199
                        result1.createFromJSON(response.getText());
200
                }
201
                else if (aclass.equals(OtherUserResource.class)) {
202
                        result1 = new OtherUserResource(path);
203
                        result1.createFromJSON(response.getText());
204
                }
205
                else if (aclass.equals(GroupResource.class)) {
206
                        result1 = new GroupResource(path);
207
                        result1.createFromJSON(response.getText());
208
                }
209
                else if (aclass.equals(GroupUserResource.class)) {
210
                        result1 = new GroupUserResource(path);
211
                        result1.createFromJSON(response.getText());
212
                }
213
                else if (aclass.equals(UserResource.class)) {
214
                        result1 = new UserResource(path);
215
                        result1.createFromJSON(response.getText());
216
                }
217
                return result1;
218
        }
219

    
220
        public boolean hasErrors() {
221
                return errors.size() >0;
222
        }
223

    
224
        /**
225
         * Retrieve the errors.
226
         *
227
         * @return the errors
228
         */
229
        public Map<String, Throwable> getErrors() {
230
                return errors;
231
        }
232

    
233
        protected void debug() {
234
                GWT.log("--->"+result.size(), null);
235
                GWT.log("-ERRORS-->"+getErrors().size(), null);
236
                for(String p : getErrors().keySet())
237
                        GWT.log("error:"+p, getErrors().get(p));
238
        }
239

    
240
        public static class Cached {
241
                public String uri;
242
                public RestResource cache;
243
        }
244
}