Properly cancel the upload for both gears-enabled and regular file uploads. This...
[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.FolderPropertiesDialog;
24 import gr.ebs.gss.client.GSS;
25 import gr.ebs.gss.client.FileMenu.Images;
26 import gr.ebs.gss.client.rest.GetCommand;
27 import gr.ebs.gss.client.rest.HeadCommand;
28 import gr.ebs.gss.client.rest.MultipleGetCommand;
29 import gr.ebs.gss.client.rest.MultipleHeadCommand;
30 import gr.ebs.gss.client.rest.RestException;
31 import gr.ebs.gss.client.rest.resource.FileResource;
32 import gr.ebs.gss.client.rest.resource.FolderResource;
33 import gr.ebs.gss.client.rest.resource.GroupResource;
34 import gr.ebs.gss.client.rest.resource.GroupsResource;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import com.google.gwt.core.client.GWT;
40 import com.google.gwt.user.client.Command;
41 import com.google.gwt.user.client.DeferredCommand;
42 import com.google.gwt.user.client.IncrementalCommand;
43 import com.google.gwt.user.client.ui.PopupPanel;
44
45 /**
46  * The command that displays the appropriate Properties dialog, according to the
47  * selected object in the application.
48  *
49  * @author kman
50  */
51 public class PropertiesCommand implements Command {
52
53         final FileMenu.Images newImages;
54
55         private PopupPanel containerPanel;
56
57         private List<GroupResource> groups = null;
58
59         private List<FileResource> versions = null;
60
61         private int tabToShow = 0;
62
63         /**
64          * @param _containerPanel
65          * @param _newImages the images of all the possible delete dialogs
66          * @param _tab the tab to switch to
67          */
68         public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
69                 containerPanel = _containerPanel;
70                 newImages = _newImages;
71                 tabToShow = _tab;
72         }
73
74         public void execute() {
75                 containerPanel.hide();
76                 if (GSS.get().getCurrentSelection() instanceof FolderResource) {
77                         GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((FolderResource) GSS.get().getCurrentSelection()).getUri()) {
78
79                                 @Override
80                                 public void onComplete() {
81                                         GSS.get().setCurrentSelection(getResult());
82                                         initialize();
83                                 }
84
85                                 @Override
86                                 public void onError(Throwable t) {
87
88                                 }
89
90                         };
91                         DeferredCommand.addCommand(eg);
92                 }
93                 else if (GSS.get().getCurrentSelection() instanceof FileResource) {
94                         final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
95                         // Needed because firefox caches head requests.
96                         HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random() ) {
97
98                                 @Override
99                                 public void onComplete() {
100                                         FileResource res = getResult();
101                                         res.setUri(path);
102                                         GSS.get().setCurrentSelection(res);
103                                         initialize();
104                                 }
105
106                                 @Override
107                                 public void onError(Throwable t) {
108                                         if(t instanceof RestException)
109                                                 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
110                                 }
111
112                         };
113                         DeferredCommand.addCommand(eg);
114                 }
115         }
116
117         private void initialize(){
118                 getGroups();
119                 getVersions();
120                 DeferredCommand.addCommand(new IncrementalCommand() {
121
122                         public boolean execute() {
123                                 boolean res = canContinue();
124                                 if (res) {
125                                         displayProperties(newImages);
126                                         return false;
127                                 }
128                                 return true;
129
130                         }
131
132                 });
133         }
134
135         private boolean canContinue() {
136                 if (groups == null || versions == null)
137                         return false;
138                 return true;
139         }
140
141         /**
142          * Display the appropriate Properties dialog, according to the selected
143          * object in the application.
144          *
145          * @param propImages the images of all the possible properties dialogs
146          */
147         void displayProperties(final Images propImages) {
148                 if (GSS.get().getCurrentSelection() instanceof FolderResource) {
149                         FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
150                         dlg.selectTab(tabToShow);
151                         dlg.center();
152                 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
153                         FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions);
154                         dlg.selectTab(tabToShow);
155                         dlg.center();
156                 }
157         }
158
159         private void getGroups() {
160                 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath()) {
161
162                         @Override
163                         public void onComplete() {
164                                 GroupsResource res = getResult();
165                                 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {})) {
166
167                                         @Override
168                                         public void onComplete() {
169                                                 List<GroupResource> groupList = getResult();
170                                                 groups = groupList;
171                                         }
172
173                                         @Override
174                                         public void onError(Throwable t) {
175                                                 GWT.log("", t);
176                                                 GSS.get().displayError("Unable to fetch groups");
177                                                 groups = new ArrayList<GroupResource>();
178                                         }
179
180                                         @Override
181                                         public void onError(String p, Throwable throwable) {
182                                                 GWT.log("Path:" + p, throwable);
183                                         }
184                                 };
185                                 DeferredCommand.addCommand(ga);
186                         }
187
188                         @Override
189                         public void onError(Throwable t) {
190                                 GWT.log("", t);
191                                 GSS.get().displayError("Unable to fetch groups");
192                                 groups = new ArrayList<GroupResource>();
193                         }
194                 };
195                 DeferredCommand.addCommand(gg);
196         }
197
198         private void getVersions() {
199                 if (GSS.get().getCurrentSelection() instanceof FileResource) {
200                         FileResource afile = (FileResource) GSS.get().getCurrentSelection();
201                         GWT.log("File is versioned:" + afile.isVersioned(), null);
202                         if (afile.isVersioned()) {
203                                 List<String> paths = new ArrayList<String>();
204                                 for (int i = 1; i <= afile.getVersion(); i++)
205                                         paths.add(afile.getUri() + "?version=" + i);
206                                 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {})) {
207
208                                         @Override
209                                         public void onComplete() {
210                                                 versions = getResult();
211                                         }
212
213                                         @Override
214                                         public void onError(Throwable t) {
215                                                 GWT.log("", t);
216                                                 GSS.get().displayError("Unable to fetch versions");
217                                                 versions = new ArrayList<FileResource>();
218                                         }
219
220                                         @Override
221                                         public void onError(String p, Throwable throwable) {
222                                                 GWT.log("Path:" + p, throwable);
223                                         }
224                                 };
225                                 DeferredCommand.addCommand(gv);
226                         } else
227                                 versions = new ArrayList<FileResource>();
228                 } else
229                         versions = new ArrayList<FileResource>();
230         }
231 }