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.RestResourceWrapper;
38 import gr.ebs.gss.client.rest.resource.UserResource;
39 import gr.ebs.gss.client.rest.resource.UserSearchResource;
41 import java.util.ArrayList;
42 import java.util.List;
44 import com.google.gwt.core.client.GWT;
45 import com.google.gwt.user.client.Command;
46 import com.google.gwt.user.client.DeferredCommand;
47 import com.google.gwt.user.client.IncrementalCommand;
48 import com.google.gwt.user.client.ui.PopupPanel;
51 * The command that displays the appropriate Properties dialog, according to the
52 * selected object in the application.
56 public class PropertiesCommand implements Command {
58 final FileMenu.Images newImages;
60 private PopupPanel containerPanel;
62 private List<GroupResource> groups = null;
64 private List<FileResource> versions = null;
66 private int tabToShow = 0;
68 private String userName;
71 * @param _containerPanel
72 * @param _newImages the images of all the possible delete dialogs
73 * @param _tab the tab to switch to
75 public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
76 containerPanel = _containerPanel;
77 newImages = _newImages;
82 public void execute() {
83 containerPanel.hide();
84 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
85 GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((RestResourceWrapper) GSS.get().getCurrentSelection()).getUri(),((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource()) {
88 public void onComplete() {
89 ((RestResourceWrapper) GSS.get().getCurrentSelection()).setResource(getResult());
94 public void onError(Throwable t) {
99 DeferredCommand.addCommand(eg);
101 else if (GSS.get().getCurrentSelection() instanceof FileResource) {
102 final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
103 // Needed because firefox caches head requests.
104 HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random(), null ) {
107 public void onComplete() {
108 FileResource res = getResult();
109 GSS.get().setCurrentSelection(res);
114 public void onError(Throwable t) {
115 if(t instanceof RestException)
116 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
120 DeferredCommand.addCommand(eg);
122 else if (GSS.get().getCurrentSelection() instanceof List) {
123 List<String> paths = new ArrayList<String>();
124 for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection())
125 paths.add(fr.getUri()+"?"+Math.random());
126 Cached[] cached = new Cached[paths.size()];
128 for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
129 Cached c = new Cached();
130 c.uri=fr.getUri()+"?"+Math.random();
135 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
138 public void onComplete() {
139 List<FileResource> res = getResult();
140 GSS.get().setCurrentSelection(res);
141 FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
142 dlg.selectTab(tabToShow);
147 public void onError(Throwable t) {
149 GSS.get().displayError("Unable to fetch files details");
153 public void onError(String p, Throwable throwable) {
154 GWT.log("Path:" + p, throwable);
157 DeferredCommand.addCommand(gv);
161 private void initialize(){
165 DeferredCommand.addCommand(new IncrementalCommand() {
168 public boolean execute() {
169 boolean res = canContinue();
171 displayProperties(newImages, GSS.get().findUserFullName(userName));
181 private boolean canContinue() {
182 String userFullNameFromMap = GSS.get().findUserFullName(userName);
183 if(groups == null || versions == null || userFullNameFromMap == null)
189 * Display the appropriate Properties dialog, according to the selected
190 * object in the application.
192 * @param propImages the images of all the possible properties dialogs
194 void displayProperties(final Images propImages, final String _userName) {
195 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
196 FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
197 dlg.selectTab(tabToShow);
199 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
200 FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
201 dlg.selectTab(tabToShow);
206 private void getGroups() {
207 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
210 public void onComplete() {
211 GroupsResource res = getResult();
212 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
215 public void onComplete() {
216 List<GroupResource> groupList = getResult();
221 public void onError(Throwable t) {
223 GSS.get().displayError("Unable to fetch groups");
224 groups = new ArrayList<GroupResource>();
228 public void onError(String p, Throwable throwable) {
229 GWT.log("Path:" + p, throwable);
232 DeferredCommand.addCommand(ga);
236 public void onError(Throwable t) {
238 GSS.get().displayError("Unable to fetch groups");
239 groups = new ArrayList<GroupResource>();
242 DeferredCommand.addCommand(gg);
245 private void getVersions() {
246 if (GSS.get().getCurrentSelection() instanceof FileResource) {
247 FileResource afile = (FileResource) GSS.get().getCurrentSelection();
248 GWT.log("File is versioned:" + afile.isVersioned(), null);
249 if (afile.isVersioned()) {
250 List<String> paths = new ArrayList<String>();
251 for (int i = 1; i <= afile.getVersion(); i++)
252 paths.add(afile.getUri() + "?version=" + i);
253 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}), null) {
256 public void onComplete() {
257 versions = getResult();
261 public void onError(Throwable t) {
263 GSS.get().displayError("Unable to fetch versions");
264 versions = new ArrayList<FileResource>();
268 public void onError(String p, Throwable throwable) {
269 GWT.log("Path:" + p, throwable);
272 DeferredCommand.addCommand(gv);
274 versions = new ArrayList<FileResource>();
276 versions = new ArrayList<FileResource>();
279 private void getOwnerFullName() {
280 if(GSS.get().getCurrentSelection() instanceof FileResource){
281 FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
282 userName = fileResource.getOwner();
283 if(GSS.get().findUserFullName(userName) == null){
284 GetUserCommand gu = new GetUserCommand(userName);
288 FolderResource resource = ((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource();
289 userName = resource.getOwner();
290 if(GSS.get().findUserFullName(userName) == null){
291 GetUserCommand gu = new GetUserCommand(userName);