Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / rest / MultipleHeadCommand.java @ f55cf326

History | View | Annotate | Download (6.5 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.Pithos;
38
import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
39
import gr.grnet.pithos.web.client.rest.resource.FileResource;
40
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
41
import gr.grnet.pithos.web.client.rest.resource.GroupResource;
42
import gr.grnet.pithos.web.client.rest.resource.GroupUserResource;
43
import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
44
import gr.grnet.pithos.web.client.rest.resource.RestResource;
45
import gr.grnet.pithos.web.client.rest.resource.SharedResource;
46
import gr.grnet.pithos.web.client.rest.resource.TrashResource;
47
import gr.grnet.pithos.web.client.rest.resource.UserResource;
48

    
49
import java.util.ArrayList;
50
import java.util.HashMap;
51
import java.util.List;
52
import java.util.Map;
53

    
54
import com.google.gwt.core.client.GWT;
55
import com.google.gwt.http.client.Response;
56
import com.google.gwt.user.client.DeferredCommand;
57

    
58

    
59
public abstract class MultipleHeadCommand <T extends RestResource> extends RestCommand {
60
        String[] paths;
61
        Class<T> aclass;
62
        List<T> result = new ArrayList<T>();
63
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
64
        private boolean requestSent=false;
65
        Cached[] cached;
66

    
67
        public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, Cached[] theCached) {
68
                this(theClass, pathToGet, true, theCached);
69
        }
70

    
71
        public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
72
                setShowLoadingIndicator(showLoading);
73
                if(isShowLoadingIndicator())
74
                        Pithos.get().showLoadingIndicator("Getting "+pathToGet.length+" items", null);
75
                paths = pathToGet;
76
                this.aclass = theClass;
77
                this.cached = theCached;
78
                //sendRequest();
79
        }
80

    
81
        private void sendRequest() {
82
                if(requestSent)
83
                        return;
84
                requestSent=true;
85
                if(cached!=null)
86
                        for (final Cached c : cached){
87
                                final String path;
88
                                if(aclass.equals(FileResource.class)){
89
                                        if(c.uri.indexOf("?") == -1)
90
                                                path=c.uri+"?"+Math.random();
91
                                        else
92
                                                path=c.uri;
93
                                }
94
                                else
95
                                        path = fixPath(c.uri);
96
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, (T)c.cache) {
97

    
98
                                        @Override
99
                                        public void onComplete() {
100
                                                MultipleHeadCommand.this.result.add(getResult());
101
                                        }
102

    
103
                                        @Override
104
                                        public void onError(Throwable t) {
105
                                                errors.put(path, t);
106
                                        }
107

    
108
                                });
109
                        }
110
                else
111
                        for (String pathg : paths) {
112
                                final String path;
113
                                if(aclass.equals(FileResource.class))
114
                                        path = pathg;
115
                                else
116
                                        path = fixPath(pathg);
117
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, null) {
118
                                        @Override
119
                                        public void onComplete() {
120
                                                MultipleHeadCommand.this.result.add(getResult());
121
                                        }
122

    
123
                                        @Override
124
                                        public void onError(Throwable t) {
125
                                                errors.put(path, t);
126
                                        }
127
                                });
128
                        }
129
        }
130
        public boolean isComplete() {
131
                return result.size()+errors.size() == paths.length;
132
        }
133

    
134
        public List<T> getResult() {
135
                return result;
136
        }
137

    
138
        @Override
139
        public boolean execute() {
140
                if(!requestSent)
141
                        sendRequest();
142
                boolean com = isComplete();
143
                if (com) {
144
                        if(isShowLoadingIndicator())
145
                                Pithos.get().hideLoadingIndicator();
146
                        if(hasErrors())
147
                                for(String p : errors.keySet())
148
                                        onError(p, errors.get(p));
149
                        onComplete();
150
                        return false;
151
                }
152
                return true;
153
        }
154

    
155
        /**
156
         * @param p
157
         * @param throwable
158
         */
159
        public abstract void onError(String p, Throwable throwable);
160

    
161
        public Object deserializeResponse(String path, Response response) {
162
                RestResource result1 = null;
163
                if (aclass.equals(FolderResource.class)) {
164
                        result1 = new FolderResource(path);
165
                        result1.createFromJSON(response.getText());
166
                } else if (aclass.equals(FileResource.class)) {
167
                        result1 = new FileResource(path);
168
                        result1.createFromJSON(response.getHeader("X-Pithos-Metadata"));
169
                } else if (aclass.equals(GroupsResource.class)) {
170
                        result1 = new GroupsResource(path);
171
                        result1.createFromJSON(response.getText());
172
                } else if (aclass.equals(TrashResource.class)) {
173
                        result1 = new TrashResource(path);
174
                        result1.createFromJSON(response.getText());
175
                } else if (aclass.equals(SharedResource.class)) {
176
                        result1 = new SharedResource(path);
177
                        result1.createFromJSON(response.getText());
178
                } else if (aclass.equals(GroupResource.class)) {
179
                        result1 = new GroupResource(path);
180
                        result1.createFromJSON(response.getText());
181
                } else if (aclass.equals(GroupUserResource.class)) {
182
                        result1 = new GroupUserResource(path);
183
                        result1.createFromJSON(response.getText());
184
                } else if (aclass.equals(UserResource.class)) {
185
                        result1 = new UserResource(path);
186
                        result1.createFromJSON(response.getText());
187
                }
188
                return result1;
189
        }
190

    
191
        public boolean hasErrors(){
192
                return errors.size() >0;
193
        }
194

    
195
        /**
196
         * Retrieve the errors.
197
         *
198
         * @return the errors
199
         */
200
        public Map<String, Throwable> getErrors() {
201
                return errors;
202
        }
203

    
204
        public void debug(){
205
                GWT.log("--->"+result.size(), null);
206
                GWT.log("-ERRORS-->"+getErrors().size(), null);
207
                for(String p : getErrors().keySet())
208
                        GWT.log("error:"+p, getErrors().get(p));
209
        }
210
}