Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / commands / PropertiesCommand.java @ 9ab5db6d

History | View | Annotate | Download (8.5 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 gr.ebs.gss.client.commands;
20

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

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

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

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

    
55
        final FileMenu.Images newImages;
56

    
57
        private PopupPanel containerPanel;
58

    
59
        private List<GroupResource> groups = null;
60

    
61
        private List<FileResource> versions = null;
62

    
63
        private int tabToShow = 0;
64

    
65
        private String userName;
66

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

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

    
84
                                @Override
85
                                public void onComplete() {
86
                                        GSS.get().setCurrentSelection(getResult());
87
                                        initialize();
88
                                }
89

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

    
93
                                }
94

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

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

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

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

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

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

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

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

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

    
174
                });
175

    
176
        }
177

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

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

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

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

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

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

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

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

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

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

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

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

    
276
        private void getOwnerFullName() {
277
                FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
278
                userName = fileResource.getOwner();
279
                GetUserCommand getUserCmd = new GetUserCommand(userName);
280
                getUserCmd.execute();
281

    
282
        }
283

    
284
}