Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / MultipleHeadCommand.java @ 023f6f1e

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> theClass, String[] pathToGet, Cached[] theCached) {
56
                this(theClass, pathToGet, true, theCached);
57
        }
58

    
59
        public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
60
                setShowLoadingIndicator(showLoading);
61
                if(isShowLoadingIndicator())
62
                        GSS.get().showLoadingIndicator();
63
                paths = pathToGet;
64
                this.aclass = theClass;
65
                this.cached = theCached;
66
                //sendRequest();
67
        }
68

    
69
        private void sendRequest() {
70
                if(requestSent)
71
                        return;
72
                requestSent=true;
73
                if(cached!=null)
74
                        for (final Cached c : cached){
75
                                final String path;
76
                                if(aclass.equals(FileResource.class)){
77
                                        if(c.uri.indexOf("?") == -1)
78
                                                path=c.uri+"?"+Math.random();
79
                                        else
80
                                                path=c.uri;
81
                                }
82
                                else
83
                                        path = fixPath(c.uri);
84
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, (T)c.cache) {
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
                else
99
                        for (String pathg : paths) {
100
                                final String path;
101
                                if(aclass.equals(FileResource.class))
102
                                        path = pathg;
103
                                else
104
                                        path = fixPath(pathg);
105
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, null) {
106
                                        @Override
107
                                        public void onComplete() {
108
                                                MultipleHeadCommand.this.result.add(getResult());
109
                                        }
110

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

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

    
126
        @Override
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
                } else if (aclass.equals(FileResource.class)) {
155
                        result1 = new FileResource(path);
156
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
157
                } else if (aclass.equals(GroupsResource.class)) {
158
                        result1 = new GroupsResource(path);
159
                        result1.createFromJSON(response.getText());
160
                } else if (aclass.equals(TrashResource.class)) {
161
                        result1 = new TrashResource(path);
162
                        result1.createFromJSON(response.getText());
163
                } else if (aclass.equals(SharedResource.class)) {
164
                        result1 = new SharedResource(path);
165
                        result1.createFromJSON(response.getText());
166
                } else if (aclass.equals(GroupResource.class)) {
167
                        result1 = new GroupResource(path);
168
                        result1.createFromJSON(response.getText());
169
                } else if (aclass.equals(GroupUserResource.class)) {
170
                        result1 = new GroupUserResource(path);
171
                        result1.createFromJSON(response.getText());
172
                } else if (aclass.equals(UserResource.class)) {
173
                        result1 = new UserResource(path);
174
                        result1.createFromJSON(response.getText());
175
                }
176
                return result1;
177
        }
178

    
179
        public boolean hasErrors(){
180
                return errors.size() >0;
181
        }
182

    
183
        /**
184
         * Retrieve the errors.
185
         *
186
         * @return the errors
187
         */
188
        public Map<String, Throwable> getErrors() {
189
                return errors;
190
        }
191

    
192
        public void debug(){
193
                GWT.log("--->"+result.size(), null);
194
                GWT.log("-ERRORS-->"+getErrors().size(), null);
195
                for(String p : getErrors().keySet())
196
                        GWT.log("error:"+p, getErrors().get(p));
197
        }
198
}