Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / MultipleHeadCommand.java @ 62f168b2

History | View | Annotate | Download (5.5 kB)

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.MultipleGetCommand.Cached;
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.RestResource;
29
import gr.ebs.gss.client.rest.resource.SharedResource;
30
import gr.ebs.gss.client.rest.resource.TrashResource;
31
import gr.ebs.gss.client.rest.resource.UserResource;
32

    
33
import java.util.ArrayList;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37

    
38
import com.google.gwt.core.client.GWT;
39
import com.google.gwt.http.client.Response;
40
import com.google.gwt.user.client.DeferredCommand;
41

    
42

    
43
/**
44
 * @author kman
45
 *
46
 */
47
public abstract class MultipleHeadCommand <T extends RestResource> extends RestCommand {
48
        String[] paths;
49
        Class<T> aclass;
50
        List<T> result = new ArrayList<T>();
51
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
52
        private boolean requestSent=false;
53
        Cached[] cached;
54

    
55
        public MultipleHeadCommand(Class<T> aclass, String[] pathToGet, Cached[] cached){
56
                this(aclass, pathToGet, true, cached);
57
        }
58

    
59
        public MultipleHeadCommand(Class<T> aclass, String[] pathToGet, boolean showLoading, Cached[] cached){
60
                setShowLoadingIndicator(showLoading);
61
                if(isShowLoadingIndicator())
62
                        GSS.get().showLoadingIndicator();
63
                paths = pathToGet;
64
                this.aclass = aclass;
65
                this.cached = cached;
66
                //sendRequest();
67
        }
68
        private void sendRequest(){
69
                if(requestSent)
70
                        return;
71
                requestSent=true;
72
                if(cached!=null)
73
                        for (final Cached c : cached){
74
                                final String path;
75
                                if(aclass.equals(FileResource.class)){
76
                                        if(c.uri.indexOf("?") == -1)
77
                                                path=c.uri+"?"+Math.random();
78
                                        else
79
                                                path=c.uri;
80
                                }
81
                                else
82
                                        path = fixPath(c.uri);
83
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, (T)c.cache) {
84

    
85

    
86
                                        @Override
87
                                        public void onComplete() {
88
                                                MultipleHeadCommand.this.result.add(getResult());
89
                                        }
90

    
91
                                        @Override
92
                                        public void onError(Throwable t) {
93
                                                errors.put(path, t);
94

    
95
                                        }
96

    
97
                                });
98
                        }
99
                else
100
                        for (String pathg : paths) {
101
                                final String path;
102
                                if(aclass.equals(FileResource.class))
103
                                        path = pathg;
104
                                else
105
                                        path = fixPath(pathg);
106
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, null) {
107
                                        @Override
108
                                        public void onComplete() {
109
                                                MultipleHeadCommand.this.result.add(getResult());
110
                                        }
111

    
112
                                        @Override
113
                                        public void onError(Throwable t) {
114
                                                errors.put(path, t);
115
                                        }
116
                                });
117
                        }
118
        }
119
        public boolean isComplete() {
120
                return result.size()+errors.size() == paths.length;
121
        }
122

    
123
        public List<T> getResult() {
124
                return result;
125
        }
126

    
127
        public boolean execute() {
128
                if(!requestSent)
129
                        sendRequest();
130
                boolean com = isComplete();
131
                if (com) {
132
                        if(isShowLoadingIndicator())
133
                                GSS.get().hideLoadingIndicator();
134
                        if(hasErrors())
135
                                for(String p : errors.keySet())
136
                                        onError(p, errors.get(p));
137
                        onComplete();
138
                        return false;
139
                }
140
                return true;
141
        }
142

    
143
        /**
144
         * @param p
145
         * @param throwable
146
         */
147
        public abstract void onError(String p, Throwable throwable);
148

    
149
        public Object deserializeResponse(String path, Response response) {
150
                RestResource result1 = null;
151
                if (aclass.equals(FolderResource.class)) {
152
                        result1 = new FolderResource(path);
153
                        result1.createFromJSON(response.getText());
154

    
155
                } else if (aclass.equals(FileResource.class)) {
156
                        result1 = new FileResource(path);
157
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
158
                } else if (aclass.equals(GroupsResource.class)) {
159
                        result1 = new GroupsResource(path);
160
                        result1.createFromJSON(response.getText());
161
                } else if (aclass.equals(TrashResource.class)) {
162
                        result1 = new TrashResource(path);
163
                        result1.createFromJSON(response.getText());
164

    
165
                } else if (aclass.equals(SharedResource.class)) {
166
                        result1 = new SharedResource(path);
167
                        result1.createFromJSON(response.getText());
168

    
169
                } else if (aclass.equals(GroupResource.class)) {
170
                        result1 = new GroupResource(path);
171
                        result1.createFromJSON(response.getText());
172

    
173
                } else if (aclass.equals(GroupUserResource.class)) {
174
                        result1 = new GroupUserResource(path);
175
                        result1.createFromJSON(response.getText());
176

    
177
                } else if (aclass.equals(UserResource.class)) {
178
                        result1 = new UserResource(path);
179
                        result1.createFromJSON(response.getText());
180

    
181
                }
182
                return result1;
183

    
184
        }
185

    
186
        public boolean hasErrors(){
187
                return errors.size() >0;
188
        }
189

    
190

    
191
        /**
192
         * Retrieve the errors.
193
         *
194
         * @return the errors
195
         */
196
        public Map<String, Throwable> getErrors() {
197
                return errors;
198
        }
199

    
200
        public void debug(){
201
                GWT.log("--->"+result.size(), null);
202
                GWT.log("-ERRORS-->"+getErrors().size(), null);
203
                for(String p : getErrors().keySet())
204
                        GWT.log("error:"+p, getErrors().get(p));
205
        }
206
}