Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / commands / PropertiesCommand.java @ 287c3d8d

History | View | Annotate | Download (10 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client.commands;
36

    
37
import gr.grnet.pithos.web.client.FileMenu;
38
import gr.grnet.pithos.web.client.FilePropertiesDialog;
39
import gr.grnet.pithos.web.client.FilesPropertiesDialog;
40
import gr.grnet.pithos.web.client.FolderPropertiesDialog;
41
import gr.grnet.pithos.web.client.GSS;
42
import gr.grnet.pithos.web.client.FileMenu.Images;
43
import gr.grnet.pithos.web.client.rest.GetCommand;
44
import gr.grnet.pithos.web.client.rest.HeadCommand;
45
import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
46
import gr.grnet.pithos.web.client.rest.MultipleHeadCommand;
47
import gr.grnet.pithos.web.client.rest.RestException;
48
import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
49
import gr.grnet.pithos.web.client.rest.resource.FileResource;
50
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
51
import gr.grnet.pithos.web.client.rest.resource.GroupResource;
52
import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
53
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
54

    
55
import java.util.ArrayList;
56
import java.util.List;
57

    
58
import com.google.gwt.core.client.GWT;
59
import com.google.gwt.user.client.Command;
60
import com.google.gwt.user.client.DeferredCommand;
61
import com.google.gwt.user.client.IncrementalCommand;
62
import com.google.gwt.user.client.ui.PopupPanel;
63

    
64
/**
65
 * The command that displays the appropriate Properties dialog, according to the
66
 * selected object in the application.
67
 *
68
 */
69
public class PropertiesCommand implements Command {
70

    
71
        final FileMenu.Images newImages;
72

    
73
        private PopupPanel containerPanel;
74

    
75
        private List<GroupResource> groups = null;
76

    
77
        private List<FileResource> versions = null;
78

    
79
        private int tabToShow = 0;
80

    
81
        private String userName;
82

    
83
        /**
84
         * @param _containerPanel
85
         * @param _newImages the images of all the possible delete dialogs
86
         * @param _tab the tab to switch to
87
         */
88
        public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
89
                containerPanel = _containerPanel;
90
                newImages = _newImages;
91
                tabToShow = _tab;
92
        }
93

    
94
        @Override
95
        public void execute() {
96
                containerPanel.hide();
97
                if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
98
                        GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((RestResourceWrapper) GSS.get().getCurrentSelection()).getUri(),((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource()) {
99

    
100
                                @Override
101
                                public void onComplete() {
102
                                        ((RestResourceWrapper) GSS.get().getCurrentSelection()).setResource(getResult());
103
                                        initialize();
104
                                }
105

    
106
                                @Override
107
                                public void onError(Throwable t) {
108

    
109
                                }
110

    
111
                        };
112
                        DeferredCommand.addCommand(eg);
113
                }
114
                else if (GSS.get().getCurrentSelection() instanceof FileResource) {
115
                        final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
116
                        // Needed because firefox caches head requests.
117
                        HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random(), null ) {
118

    
119
                                @Override
120
                                public void onComplete() {
121
                                        FileResource res = getResult();
122
                                        GSS.get().setCurrentSelection(res);
123
                                        initialize();
124
                                }
125

    
126
                                @Override
127
                                public void onError(Throwable t) {
128
                                        if(t instanceof RestException)
129
                                                GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
130
                                }
131

    
132
                        };
133
                        DeferredCommand.addCommand(eg);
134
                }
135
                else if (GSS.get().getCurrentSelection() instanceof List) {
136
                        List<String> paths = new ArrayList<String>();
137
                        for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection())
138
                                paths.add(fr.getUri()+"?"+Math.random());
139
                        Cached[] cached = new Cached[paths.size()];
140
                        int i=0;
141
                        for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
142
                                Cached c = new Cached();
143
                                c.uri=fr.getUri()+"?"+Math.random();
144
                                c.cache=fr;
145
                                cached[i]=c;
146
                                i++;
147
                        }
148
                        MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
149

    
150
                                @Override
151
                                public void onComplete() {
152
                                        List<FileResource> res = getResult();
153
                                        GSS.get().setCurrentSelection(res);
154
                                        FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
155
                                        dlg.selectTab(tabToShow);
156
                                        dlg.center();
157
                                }
158

    
159
                                @Override
160
                                public void onError(Throwable t) {
161
                                        GWT.log("", t);
162
                                        GSS.get().displayError("Unable to fetch files details");
163
                                }
164

    
165
                                @Override
166
                                public void onError(String p, Throwable throwable) {
167
                                        GWT.log("Path:" + p, throwable);
168
                                }
169
                        };
170
                        DeferredCommand.addCommand(gv);
171
                }
172
        }
173

    
174
        private void initialize(){
175
                getGroups();
176
                getVersions();
177
                getOwnerFullName();
178
                DeferredCommand.addCommand(new IncrementalCommand() {
179

    
180
                        @Override
181
                        public boolean execute() {
182
                                boolean res = canContinue();
183
                                if (res) {
184
                                        displayProperties(newImages, GSS.get().findUserFullName(userName));
185
                                        return false;
186
                                }
187
                                return true;
188
                        }
189

    
190
                });
191

    
192
        }
193

    
194
        private boolean canContinue() {
195
                String userFullNameFromMap = GSS.get().findUserFullName(userName);
196
                if(groups == null || versions == null || userFullNameFromMap == null)
197
                        return false;
198
                return true;
199
        }
200

    
201
        /**
202
         * Display the appropriate Properties dialog, according to the selected
203
         * object in the application.
204
         *
205
         * @param propImages the images of all the possible properties dialogs
206
         */
207
        void displayProperties(final Images propImages, final String _userName) {
208
                if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
209
//                        FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
210
//                        dlg.selectTab(tabToShow);
211
//                        dlg.center();
212
                } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
213
                        FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
214
                        dlg.selectTab(tabToShow);
215
                        dlg.center();
216
                }
217
        }
218

    
219
        private void getGroups() {
220
                GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
221

    
222
                        @Override
223
                        public void onComplete() {
224
                                GroupsResource res = getResult();
225
                                MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
226

    
227
                                        @Override
228
                                        public void onComplete() {
229
                                                List<GroupResource> groupList = getResult();
230
                                                groups = groupList;
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
                                        @Override
241
                                        public void onError(String p, Throwable throwable) {
242
                                                GWT.log("Path:" + p, throwable);
243
                                        }
244
                                };
245
                                DeferredCommand.addCommand(ga);
246
                        }
247

    
248
                        @Override
249
                        public void onError(Throwable t) {
250
                                GWT.log("", t);
251
                                GSS.get().displayError("Unable to fetch groups");
252
                                groups = new ArrayList<GroupResource>();
253
                        }
254
                };
255
                DeferredCommand.addCommand(gg);
256
        }
257

    
258
        private void getVersions() {
259
                if (GSS.get().getCurrentSelection() instanceof FileResource) {
260
                        FileResource afile = (FileResource) GSS.get().getCurrentSelection();
261
                        GWT.log("File is versioned:" + afile.isVersioned(), null);
262
                        if (afile.isVersioned()) {
263
                                List<String> paths = new ArrayList<String>();
264
                                for (int i = 1; i <= afile.getVersion(); i++)
265
                                        paths.add(afile.getUri() + "?version=" + i);
266
                                MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}), null) {
267

    
268
                                        @Override
269
                                        public void onComplete() {
270
                                                versions = getResult();
271
                                        }
272

    
273
                                        @Override
274
                                        public void onError(Throwable t) {
275
                                                GWT.log("", t);
276
                                                GSS.get().displayError("Unable to fetch versions");
277
                                                versions = new ArrayList<FileResource>();
278
                                        }
279

    
280
                                        @Override
281
                                        public void onError(String p, Throwable throwable) {
282
                                                GWT.log("Path:" + p, throwable);
283
                                        }
284
                                };
285
                                DeferredCommand.addCommand(gv);
286
                        } else
287
                                versions = new ArrayList<FileResource>();
288
                } else
289
                        versions = new ArrayList<FileResource>();
290
        }
291

    
292
        private void getOwnerFullName() {
293
                if(GSS.get().getCurrentSelection() instanceof FileResource){                        
294
                        FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
295
                        userName = fileResource.getOwner();
296
                        if(GSS.get().findUserFullName(userName) == null){
297
                                GetUserCommand gu = new GetUserCommand(userName);
298
                                gu.execute();
299
                        }
300
                }else{                        
301
                        FolderResource resource = ((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource();
302
                        userName = resource.getOwner();
303
                        if(GSS.get().findUserFullName(userName) == null){
304
                                GetUserCommand gu = new GetUserCommand(userName);
305
                                gu.execute();
306
                        }
307
                }
308
        }
309

    
310

    
311
}