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