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