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