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