2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.commands;
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;
40 import java.util.ArrayList;
41 import java.util.List;
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;
50 * The command that displays the appropriate Properties dialog, according to the
51 * selected object in the application.
55 public class PropertiesCommand implements Command {
57 final FileMenu.Images newImages;
59 private PopupPanel containerPanel;
61 private List<GroupResource> groups = null;
63 private List<FileResource> versions = null;
65 private int tabToShow = 0;
67 private String userName;
69 private boolean isFile = false;
72 * @param _containerPanel
73 * @param _newImages the images of all the possible delete dialogs
74 * @param _tab the tab to switch to
76 public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
77 containerPanel = _containerPanel;
78 newImages = _newImages;
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())) {
89 public void onComplete() {
90 GSS.get().setCurrentSelection(getResult());
95 public void onError(Throwable t) {
100 DeferredCommand.addCommand(eg);
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 ) {
108 public void onComplete() {
109 FileResource res = getResult();
110 GSS.get().setCurrentSelection(res);
115 public void onError(Throwable t) {
116 if(t instanceof RestException)
117 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
121 DeferredCommand.addCommand(eg);
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()];
129 for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
130 Cached c = new Cached();
131 c.uri=fr.getUri()+"?"+Math.random();
136 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
139 public void onComplete() {
140 List<FileResource> res = getResult();
141 GSS.get().setCurrentSelection(res);
142 FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
143 dlg.selectTab(tabToShow);
148 public void onError(Throwable t) {
150 GSS.get().displayError("Unable to fetch files details");
154 public void onError(String p, Throwable throwable) {
155 GWT.log("Path:" + p, throwable);
158 DeferredCommand.addCommand(gv);
162 private void initialize(){
166 DeferredCommand.addCommand(new IncrementalCommand() {
169 public boolean execute() {
170 boolean res = canContinue();
172 displayProperties(newImages, GSS.get().findUserFullName(userName));
182 private boolean canContinue() {
183 String userFullNameFromMap = GSS.get().findUserFullName(userName);
184 if(groups == null || versions == null || isFile && userFullNameFromMap == null)
190 * Display the appropriate Properties dialog, according to the selected
191 * object in the application.
193 * @param propImages the images of all the possible properties dialogs
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);
200 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
201 FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
202 dlg.selectTab(tabToShow);
207 private void getGroups() {
208 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
211 public void onComplete() {
212 GroupsResource res = getResult();
213 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
216 public void onComplete() {
217 List<GroupResource> groupList = getResult();
222 public void onError(Throwable t) {
224 GSS.get().displayError("Unable to fetch groups");
225 groups = new ArrayList<GroupResource>();
229 public void onError(String p, Throwable throwable) {
230 GWT.log("Path:" + p, throwable);
233 DeferredCommand.addCommand(ga);
237 public void onError(Throwable t) {
239 GSS.get().displayError("Unable to fetch groups");
240 groups = new ArrayList<GroupResource>();
243 DeferredCommand.addCommand(gg);
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) {
257 public void onComplete() {
258 versions = getResult();
262 public void onError(Throwable t) {
264 GSS.get().displayError("Unable to fetch versions");
265 versions = new ArrayList<FileResource>();
269 public void onError(String p, Throwable throwable) {
270 GWT.log("Path:" + p, throwable);
273 DeferredCommand.addCommand(gv);
275 versions = new ArrayList<FileResource>();
277 versions = new ArrayList<FileResource>();
280 private void getOwnerFullName() {
281 if(GSS.get().getCurrentSelection() instanceof FileResource){
283 FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
284 userName = fileResource.getOwner();
285 if(GSS.get().findUserFullName(userName) == null){
286 GetUserCommand gu = new GetUserCommand(userName);