2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client.commands;
37 import gr.grnet.pithos.web.client.FileMenu;
38 import gr.grnet.pithos.web.client.FilePropertiesDialog;
39 import gr.grnet.pithos.web.client.FilesPropertiesDialog;
40 import gr.grnet.pithos.web.client.FolderPropertiesDialog;
41 import gr.grnet.pithos.web.client.GSS;
42 import gr.grnet.pithos.web.client.FileMenu.Images;
43 import gr.grnet.pithos.web.client.rest.GetCommand;
44 import gr.grnet.pithos.web.client.rest.HeadCommand;
45 import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
46 import gr.grnet.pithos.web.client.rest.MultipleHeadCommand;
47 import gr.grnet.pithos.web.client.rest.RestException;
48 import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
49 import gr.grnet.pithos.web.client.rest.resource.FileResource;
50 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
51 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
52 import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
53 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
55 import java.util.ArrayList;
56 import java.util.List;
58 import com.google.gwt.core.client.GWT;
59 import com.google.gwt.user.client.Command;
60 import com.google.gwt.user.client.DeferredCommand;
61 import com.google.gwt.user.client.IncrementalCommand;
62 import com.google.gwt.user.client.ui.PopupPanel;
65 * The command that displays the appropriate Properties dialog, according to the
66 * selected object in the application.
69 public class PropertiesCommand implements Command {
71 final FileMenu.Images newImages;
73 private PopupPanel containerPanel;
75 private List<GroupResource> groups = null;
77 private List<FileResource> versions = null;
79 private int tabToShow = 0;
81 private String userName;
84 * @param _containerPanel
85 * @param _newImages the images of all the possible delete dialogs
86 * @param _tab the tab to switch to
88 public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
89 containerPanel = _containerPanel;
90 newImages = _newImages;
95 public void execute() {
96 containerPanel.hide();
97 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
98 GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((RestResourceWrapper) GSS.get().getCurrentSelection()).getUri(),((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource()) {
101 public void onComplete() {
102 ((RestResourceWrapper) GSS.get().getCurrentSelection()).setResource(getResult());
107 public void onError(Throwable t) {
112 DeferredCommand.addCommand(eg);
114 else if (GSS.get().getCurrentSelection() instanceof FileResource) {
115 final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
116 // Needed because firefox caches head requests.
117 HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random(), null ) {
120 public void onComplete() {
121 FileResource res = getResult();
122 GSS.get().setCurrentSelection(res);
127 public void onError(Throwable t) {
128 if(t instanceof RestException)
129 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
133 DeferredCommand.addCommand(eg);
135 else if (GSS.get().getCurrentSelection() instanceof List) {
136 List<String> paths = new ArrayList<String>();
137 for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection())
138 paths.add(fr.getUri()+"?"+Math.random());
139 Cached[] cached = new Cached[paths.size()];
141 for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
142 Cached c = new Cached();
143 c.uri=fr.getUri()+"?"+Math.random();
148 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
151 public void onComplete() {
152 List<FileResource> res = getResult();
153 GSS.get().setCurrentSelection(res);
154 FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
155 dlg.selectTab(tabToShow);
160 public void onError(Throwable t) {
162 GSS.get().displayError("Unable to fetch files details");
166 public void onError(String p, Throwable throwable) {
167 GWT.log("Path:" + p, throwable);
170 DeferredCommand.addCommand(gv);
174 private void initialize(){
178 DeferredCommand.addCommand(new IncrementalCommand() {
181 public boolean execute() {
182 boolean res = canContinue();
184 displayProperties(newImages, GSS.get().findUserFullName(userName));
194 private boolean canContinue() {
195 String userFullNameFromMap = GSS.get().findUserFullName(userName);
196 if(groups == null || versions == null || userFullNameFromMap == null)
202 * Display the appropriate Properties dialog, according to the selected
203 * object in the application.
205 * @param propImages the images of all the possible properties dialogs
207 void displayProperties(final Images propImages, final String _userName) {
208 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
209 FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
210 dlg.selectTab(tabToShow);
212 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
213 FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
214 dlg.selectTab(tabToShow);
219 private void getGroups() {
220 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
223 public void onComplete() {
224 GroupsResource res = getResult();
225 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
228 public void onComplete() {
229 List<GroupResource> groupList = getResult();
234 public void onError(Throwable t) {
236 GSS.get().displayError("Unable to fetch groups");
237 groups = new ArrayList<GroupResource>();
241 public void onError(String p, Throwable throwable) {
242 GWT.log("Path:" + p, throwable);
245 DeferredCommand.addCommand(ga);
249 public void onError(Throwable t) {
251 GSS.get().displayError("Unable to fetch groups");
252 groups = new ArrayList<GroupResource>();
255 DeferredCommand.addCommand(gg);
258 private void getVersions() {
259 if (GSS.get().getCurrentSelection() instanceof FileResource) {
260 FileResource afile = (FileResource) GSS.get().getCurrentSelection();
261 GWT.log("File is versioned:" + afile.isVersioned(), null);
262 if (afile.isVersioned()) {
263 List<String> paths = new ArrayList<String>();
264 for (int i = 1; i <= afile.getVersion(); i++)
265 paths.add(afile.getUri() + "?version=" + i);
266 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}), null) {
269 public void onComplete() {
270 versions = getResult();
274 public void onError(Throwable t) {
276 GSS.get().displayError("Unable to fetch versions");
277 versions = new ArrayList<FileResource>();
281 public void onError(String p, Throwable throwable) {
282 GWT.log("Path:" + p, throwable);
285 DeferredCommand.addCommand(gv);
287 versions = new ArrayList<FileResource>();
289 versions = new ArrayList<FileResource>();
292 private void getOwnerFullName() {
293 if(GSS.get().getCurrentSelection() instanceof FileResource){
294 FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
295 userName = fileResource.getOwner();
296 if(GSS.get().findUserFullName(userName) == null){
297 GetUserCommand gu = new GetUserCommand(userName);
301 FolderResource resource = ((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource();
302 userName = resource.getOwner();
303 if(GSS.get().findUserFullName(userName) == null){
304 GetUserCommand gu = new GetUserCommand(userName);