show pager both on top and bottom, make all columns draggable (bug: ctrl+click does...
[pithos] / src / gr / ebs / gss / client / commands / PropertiesCommand.java
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 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
38 import gr.ebs.gss.client.rest.resource.UserResource;
39 import gr.ebs.gss.client.rest.resource.UserSearchResource;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import com.google.gwt.core.client.GWT;
45 import com.google.gwt.user.client.Command;
46 import com.google.gwt.user.client.DeferredCommand;
47 import com.google.gwt.user.client.IncrementalCommand;
48 import com.google.gwt.user.client.ui.PopupPanel;
49
50 /**
51  * The command that displays the appropriate Properties dialog, according to the
52  * selected object in the application.
53  *
54  * @author kman
55  */
56 public class PropertiesCommand implements Command {
57
58         final FileMenu.Images newImages;
59
60         private PopupPanel containerPanel;
61
62         private List<GroupResource> groups = null;
63
64         private List<FileResource> versions = null;
65
66         private int tabToShow = 0;
67
68         private String userName;
69
70         /**
71          * @param _containerPanel
72          * @param _newImages the images of all the possible delete dialogs
73          * @param _tab the tab to switch to
74          */
75         public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
76                 containerPanel = _containerPanel;
77                 newImages = _newImages;
78                 tabToShow = _tab;
79         }
80
81         @Override
82         public void execute() {
83                 containerPanel.hide();
84                 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
85                         GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((RestResourceWrapper) GSS.get().getCurrentSelection()).getUri(),((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource()) {
86
87                                 @Override
88                                 public void onComplete() {
89                                         ((RestResourceWrapper) GSS.get().getCurrentSelection()).setResource(getResult());
90                                         initialize();
91                                 }
92
93                                 @Override
94                                 public void onError(Throwable t) {
95
96                                 }
97
98                         };
99                         DeferredCommand.addCommand(eg);
100                 }
101                 else if (GSS.get().getCurrentSelection() instanceof FileResource) {
102                         final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
103                         // Needed because firefox caches head requests.
104                         HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random(), null ) {
105
106                                 @Override
107                                 public void onComplete() {
108                                         FileResource res = getResult();
109                                         GSS.get().setCurrentSelection(res);
110                                         initialize();
111                                 }
112
113                                 @Override
114                                 public void onError(Throwable t) {
115                                         if(t instanceof RestException)
116                                                 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
117                                 }
118
119                         };
120                         DeferredCommand.addCommand(eg);
121                 }
122                 else if (GSS.get().getCurrentSelection() instanceof List) {
123                         List<String> paths = new ArrayList<String>();
124                         for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection())
125                                 paths.add(fr.getUri()+"?"+Math.random());
126                         Cached[] cached = new Cached[paths.size()];
127                         int i=0;
128                         for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
129                                 Cached c = new Cached();
130                                 c.uri=fr.getUri()+"?"+Math.random();
131                                 c.cache=fr;
132                                 cached[i]=c;
133                                 i++;
134                         }
135                         MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
136
137                                 @Override
138                                 public void onComplete() {
139                                         List<FileResource> res = getResult();
140                                         GSS.get().setCurrentSelection(res);
141                                         FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
142                                         dlg.selectTab(tabToShow);
143                                         dlg.center();
144                                 }
145
146                                 @Override
147                                 public void onError(Throwable t) {
148                                         GWT.log("", t);
149                                         GSS.get().displayError("Unable to fetch files details");
150                                 }
151
152                                 @Override
153                                 public void onError(String p, Throwable throwable) {
154                                         GWT.log("Path:" + p, throwable);
155                                 }
156                         };
157                         DeferredCommand.addCommand(gv);
158                 }
159         }
160
161         private void initialize(){
162                 getGroups();
163                 getVersions();
164                 getOwnerFullName();
165                 DeferredCommand.addCommand(new IncrementalCommand() {
166
167                         @Override
168                         public boolean execute() {
169                                 boolean res = canContinue();
170                                 if (res) {
171                                         displayProperties(newImages, GSS.get().findUserFullName(userName));
172                                         return false;
173                                 }
174                                 return true;
175                         }
176
177                 });
178
179         }
180
181         private boolean canContinue() {
182                 String userFullNameFromMap = GSS.get().findUserFullName(userName);
183                 if(groups == null || versions == null || userFullNameFromMap == null)
184                         return false;
185                 return true;
186         }
187
188         /**
189          * Display the appropriate Properties dialog, according to the selected
190          * object in the application.
191          *
192          * @param propImages the images of all the possible properties dialogs
193          */
194         void displayProperties(final Images propImages, final String _userName) {
195                 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
196                         FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
197                         dlg.selectTab(tabToShow);
198                         dlg.center();
199                 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
200                         FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
201                         dlg.selectTab(tabToShow);
202                         dlg.center();
203                 }
204         }
205
206         private void getGroups() {
207                 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
208
209                         @Override
210                         public void onComplete() {
211                                 GroupsResource res = getResult();
212                                 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
213
214                                         @Override
215                                         public void onComplete() {
216                                                 List<GroupResource> groupList = getResult();
217                                                 groups = groupList;
218                                         }
219
220                                         @Override
221                                         public void onError(Throwable t) {
222                                                 GWT.log("", t);
223                                                 GSS.get().displayError("Unable to fetch groups");
224                                                 groups = new ArrayList<GroupResource>();
225                                         }
226
227                                         @Override
228                                         public void onError(String p, Throwable throwable) {
229                                                 GWT.log("Path:" + p, throwable);
230                                         }
231                                 };
232                                 DeferredCommand.addCommand(ga);
233                         }
234
235                         @Override
236                         public void onError(Throwable t) {
237                                 GWT.log("", t);
238                                 GSS.get().displayError("Unable to fetch groups");
239                                 groups = new ArrayList<GroupResource>();
240                         }
241                 };
242                 DeferredCommand.addCommand(gg);
243         }
244
245         private void getVersions() {
246                 if (GSS.get().getCurrentSelection() instanceof FileResource) {
247                         FileResource afile = (FileResource) GSS.get().getCurrentSelection();
248                         GWT.log("File is versioned:" + afile.isVersioned(), null);
249                         if (afile.isVersioned()) {
250                                 List<String> paths = new ArrayList<String>();
251                                 for (int i = 1; i <= afile.getVersion(); i++)
252                                         paths.add(afile.getUri() + "?version=" + i);
253                                 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}), null) {
254
255                                         @Override
256                                         public void onComplete() {
257                                                 versions = getResult();
258                                         }
259
260                                         @Override
261                                         public void onError(Throwable t) {
262                                                 GWT.log("", t);
263                                                 GSS.get().displayError("Unable to fetch versions");
264                                                 versions = new ArrayList<FileResource>();
265                                         }
266
267                                         @Override
268                                         public void onError(String p, Throwable throwable) {
269                                                 GWT.log("Path:" + p, throwable);
270                                         }
271                                 };
272                                 DeferredCommand.addCommand(gv);
273                         } else
274                                 versions = new ArrayList<FileResource>();
275                 } else
276                         versions = new ArrayList<FileResource>();
277         }
278
279         private void getOwnerFullName() {
280                 if(GSS.get().getCurrentSelection() instanceof FileResource){                    
281                         FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
282                         userName = fileResource.getOwner();
283                         if(GSS.get().findUserFullName(userName) == null){
284                                 GetUserCommand gu = new GetUserCommand(userName);
285                                 gu.execute();
286                         }
287                 }else{                  
288                         FolderResource resource = ((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource();
289                         userName = resource.getOwner();
290                         if(GSS.get().findUserFullName(userName) == null){
291                                 GetUserCommand gu = new GetUserCommand(userName);
292                                 gu.execute();
293                         }
294                 }
295         }
296
297
298 }