Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / MultipleHeadCommand.java @ 44d84dc0

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

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

    
148
        public Object deserializeResponse(String path, Response response) {
149
                RestResource result1 = null;
150
                if (aclass.equals(FolderResource.class)) {
151
                        result1 = new FolderResource(path);
152
                        result1.createFromJSON(response.getText());
153
                } else if (aclass.equals(FileResource.class)) {
154
                        result1 = new FileResource(path);
155
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
156
                } else if (aclass.equals(GroupsResource.class)) {
157
                        result1 = new GroupsResource(path);
158
                        result1.createFromJSON(response.getText());
159
                } else if (aclass.equals(TrashResource.class)) {
160
                        result1 = new TrashResource(path);
161
                        result1.createFromJSON(response.getText());
162
                } else if (aclass.equals(SharedResource.class)) {
163
                        result1 = new SharedResource(path);
164
                        result1.createFromJSON(response.getText());
165
                } else if (aclass.equals(GroupResource.class)) {
166
                        result1 = new GroupResource(path);
167
                        result1.createFromJSON(response.getText());
168
                } else if (aclass.equals(GroupUserResource.class)) {
169
                        result1 = new GroupUserResource(path);
170
                        result1.createFromJSON(response.getText());
171
                } else if (aclass.equals(UserResource.class)) {
172
                        result1 = new UserResource(path);
173
                        result1.createFromJSON(response.getText());
174
                }
175
                return result1;
176
        }
177

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

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

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