Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / commands / PropertiesCommand.java @ 1206:292dec4eae08

History | View | Annotate | Download (9.3 kB)

1
/*
2
 * Copyright 2008, 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 org.gss_project.gss.web.client.commands;
20

    
21
import org.gss_project.gss.web.client.FileMenu;
22
import org.gss_project.gss.web.client.FilePropertiesDialog;
23
import org.gss_project.gss.web.client.FilesPropertiesDialog;
24
import org.gss_project.gss.web.client.FolderPropertiesDialog;
25
import org.gss_project.gss.web.client.GSS;
26
import org.gss_project.gss.web.client.FileMenu.Images;
27
import org.gss_project.gss.web.client.rest.GetCommand;
28
import org.gss_project.gss.web.client.rest.HeadCommand;
29
import org.gss_project.gss.web.client.rest.MultipleGetCommand;
30
import org.gss_project.gss.web.client.rest.MultipleHeadCommand;
31
import org.gss_project.gss.web.client.rest.RestException;
32
import org.gss_project.gss.web.client.rest.MultipleGetCommand.Cached;
33
import org.gss_project.gss.web.client.rest.resource.FileResource;
34
import org.gss_project.gss.web.client.rest.resource.FolderResource;
35
import org.gss_project.gss.web.client.rest.resource.GroupResource;
36
import org.gss_project.gss.web.client.rest.resource.GroupsResource;
37
import org.gss_project.gss.web.client.rest.resource.RestResourceWrapper;
38

    
39
import java.util.ArrayList;
40
import java.util.List;
41

    
42
import com.google.gwt.core.client.GWT;
43
import com.google.gwt.user.client.Command;
44
import com.google.gwt.user.client.DeferredCommand;
45
import com.google.gwt.user.client.IncrementalCommand;
46
import com.google.gwt.user.client.ui.PopupPanel;
47

    
48
/**
49
 * The command that displays the appropriate Properties dialog, according to the
50
 * selected object in the application.
51
 *
52
 * @author kman
53
 */
54
public class PropertiesCommand implements Command {
55

    
56
        final FileMenu.Images newImages;
57

    
58
        private PopupPanel containerPanel;
59

    
60
        private List<GroupResource> groups = null;
61

    
62
        private List<FileResource> versions = null;
63

    
64
        private int tabToShow = 0;
65

    
66
        private String userName;
67

    
68
        /**
69
         * @param _containerPanel
70
         * @param _newImages the images of all the possible delete dialogs
71
         * @param _tab the tab to switch to
72
         */
73
        public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
74
                containerPanel = _containerPanel;
75
                newImages = _newImages;
76
                tabToShow = _tab;
77
        }
78

    
79
        @Override
80
        public void execute() {
81
                containerPanel.hide();
82
                if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
83
                        GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((RestResourceWrapper) GSS.get().getCurrentSelection()).getUri(),((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource()) {
84

    
85
                                @Override
86
                                public void onComplete() {
87
                                        ((RestResourceWrapper) GSS.get().getCurrentSelection()).setResource(getResult());
88
                                        initialize();
89
                                }
90

    
91
                                @Override
92
                                public void onError(Throwable t) {
93

    
94
                                }
95

    
96
                        };
97
                        DeferredCommand.addCommand(eg);
98
                }
99
                else if (GSS.get().getCurrentSelection() instanceof FileResource) {
100
                        final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
101
                        // Needed because firefox caches head requests.
102
                        HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random(), null ) {
103

    
104
                                @Override
105
                                public void onComplete() {
106
                                        FileResource res = getResult();
107
                                        GSS.get().setCurrentSelection(res);
108
                                        initialize();
109
                                }
110

    
111
                                @Override
112
                                public void onError(Throwable t) {
113
                                        if(t instanceof RestException)
114
                                                GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
115
                                }
116

    
117
                        };
118
                        DeferredCommand.addCommand(eg);
119
                }
120
                else if (GSS.get().getCurrentSelection() instanceof List) {
121
                        List<String> paths = new ArrayList<String>();
122
                        for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection())
123
                                paths.add(fr.getUri()+"?"+Math.random());
124
                        Cached[] cached = new Cached[paths.size()];
125
                        int i=0;
126
                        for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
127
                                Cached c = new Cached();
128
                                c.uri=fr.getUri()+"?"+Math.random();
129
                                c.cache=fr;
130
                                cached[i]=c;
131
                                i++;
132
                        }
133
                        MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
134

    
135
                                @Override
136
                                public void onComplete() {
137
                                        List<FileResource> res = getResult();
138
                                        GSS.get().setCurrentSelection(res);
139
                                        FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
140
                                        dlg.selectTab(tabToShow);
141
                                        dlg.center();
142
                                }
143

    
144
                                @Override
145
                                public void onError(Throwable t) {
146
                                        GWT.log("", t);
147
                                        GSS.get().displayError("Unable to fetch files details");
148
                                }
149

    
150
                                @Override
151
                                public void onError(String p, Throwable throwable) {
152
                                        GWT.log("Path:" + p, throwable);
153
                                }
154
                        };
155
                        DeferredCommand.addCommand(gv);
156
                }
157
        }
158

    
159
        private void initialize(){
160
                getGroups();
161
                getVersions();
162
                getOwnerFullName();
163
                DeferredCommand.addCommand(new IncrementalCommand() {
164

    
165
                        @Override
166
                        public boolean execute() {
167
                                boolean res = canContinue();
168
                                if (res) {
169
                                        displayProperties(newImages, GSS.get().findUserFullName(userName));
170
                                        return false;
171
                                }
172
                                return true;
173
                        }
174

    
175
                });
176

    
177
        }
178

    
179
        private boolean canContinue() {
180
                String userFullNameFromMap = GSS.get().findUserFullName(userName);
181
                if(groups == null || versions == null || userFullNameFromMap == null)
182
                        return false;
183
                return true;
184
        }
185

    
186
        /**
187
         * Display the appropriate Properties dialog, according to the selected
188
         * object in the application.
189
         *
190
         * @param propImages the images of all the possible properties dialogs
191
         */
192
        void displayProperties(final Images propImages, final String _userName) {
193
                if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
194
                        FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
195
                        dlg.selectTab(tabToShow);
196
                        dlg.center();
197
                } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
198
                        FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
199
                        dlg.selectTab(tabToShow);
200
                        dlg.center();
201
                }
202
        }
203

    
204
        private void getGroups() {
205
                GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
206

    
207
                        @Override
208
                        public void onComplete() {
209
                                GroupsResource res = getResult();
210
                                MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
211

    
212
                                        @Override
213
                                        public void onComplete() {
214
                                                List<GroupResource> groupList = getResult();
215
                                                groups = groupList;
216
                                        }
217

    
218
                                        @Override
219
                                        public void onError(Throwable t) {
220
                                                GWT.log("", t);
221
                                                GSS.get().displayError("Unable to fetch groups");
222
                                                groups = new ArrayList<GroupResource>();
223
                                        }
224

    
225
                                        @Override
226
                                        public void onError(String p, Throwable throwable) {
227
                                                GWT.log("Path:" + p, throwable);
228
                                        }
229
                                };
230
                                DeferredCommand.addCommand(ga);
231
                        }
232

    
233
                        @Override
234
                        public void onError(Throwable t) {
235
                                GWT.log("", t);
236
                                GSS.get().displayError("Unable to fetch groups");
237
                                groups = new ArrayList<GroupResource>();
238
                        }
239
                };
240
                DeferredCommand.addCommand(gg);
241
        }
242

    
243
        private void getVersions() {
244
                if (GSS.get().getCurrentSelection() instanceof FileResource) {
245
                        FileResource afile = (FileResource) GSS.get().getCurrentSelection();
246
                        GWT.log("File is versioned:" + afile.isVersioned(), null);
247
                        if (afile.isVersioned()) {
248
                                List<String> paths = new ArrayList<String>();
249
                                for (int i = 1; i <= afile.getVersion(); i++)
250
                                        paths.add(afile.getUri() + "?version=" + i);
251
                                MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}), null) {
252

    
253
                                        @Override
254
                                        public void onComplete() {
255
                                                versions = getResult();
256
                                        }
257

    
258
                                        @Override
259
                                        public void onError(Throwable t) {
260
                                                GWT.log("", t);
261
                                                GSS.get().displayError("Unable to fetch versions");
262
                                                versions = new ArrayList<FileResource>();
263
                                        }
264

    
265
                                        @Override
266
                                        public void onError(String p, Throwable throwable) {
267
                                                GWT.log("Path:" + p, throwable);
268
                                        }
269
                                };
270
                                DeferredCommand.addCommand(gv);
271
                        } else
272
                                versions = new ArrayList<FileResource>();
273
                } else
274
                        versions = new ArrayList<FileResource>();
275
        }
276

    
277
        private void getOwnerFullName() {
278
                if(GSS.get().getCurrentSelection() instanceof FileResource){                        
279
                        FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
280
                        userName = fileResource.getOwner();
281
                        if(GSS.get().findUserFullName(userName) == null){
282
                                GetUserCommand gu = new GetUserCommand(userName);
283
                                gu.execute();
284
                        }
285
                }else{                        
286
                        FolderResource resource = ((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource();
287
                        userName = resource.getOwner();
288
                        if(GSS.get().findUserFullName(userName) == null){
289
                                GetUserCommand gu = new GetUserCommand(userName);
290
                                gu.execute();
291
                        }
292
                }
293
        }
294

    
295

    
296
}