Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / commands / PropertiesCommand.java @ 9ab5db6d

History | View | Annotate | Download (8.5 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.client.commands;
20 14ad7326 pastith
21 14ad7326 pastith
import gr.ebs.gss.client.FileMenu;
22 14ad7326 pastith
import gr.ebs.gss.client.FilePropertiesDialog;
23 2002c1dc Dimitris Routsis
import gr.ebs.gss.client.FilesPropertiesDialog;
24 14ad7326 pastith
import gr.ebs.gss.client.FolderPropertiesDialog;
25 14ad7326 pastith
import gr.ebs.gss.client.GSS;
26 9ab5db6d Natasa Kapravelou
import gr.ebs.gss.client.FileMenu.Images;
27 895035a2 pastith
import gr.ebs.gss.client.rest.GetCommand;
28 895035a2 pastith
import gr.ebs.gss.client.rest.HeadCommand;
29 895035a2 pastith
import gr.ebs.gss.client.rest.MultipleGetCommand;
30 895035a2 pastith
import gr.ebs.gss.client.rest.MultipleHeadCommand;
31 f98f18c2 koutsoub
import gr.ebs.gss.client.rest.RestException;
32 9ab5db6d Natasa Kapravelou
import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
33 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FileResource;
34 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FolderResource;
35 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupResource;
36 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.GroupsResource;
37 14ad7326 pastith
38 14ad7326 pastith
import java.util.ArrayList;
39 14ad7326 pastith
import java.util.List;
40 14ad7326 pastith
41 14ad7326 pastith
import com.google.gwt.core.client.GWT;
42 14ad7326 pastith
import com.google.gwt.user.client.Command;
43 14ad7326 pastith
import com.google.gwt.user.client.DeferredCommand;
44 14ad7326 pastith
import com.google.gwt.user.client.IncrementalCommand;
45 14ad7326 pastith
import com.google.gwt.user.client.ui.PopupPanel;
46 14ad7326 pastith
47 14ad7326 pastith
/**
48 14ad7326 pastith
 * The command that displays the appropriate Properties dialog, according to the
49 14ad7326 pastith
 * selected object in the application.
50 14ad7326 pastith
 *
51 14ad7326 pastith
 * @author kman
52 14ad7326 pastith
 */
53 14ad7326 pastith
public class PropertiesCommand implements Command {
54 14ad7326 pastith
55 14ad7326 pastith
        final FileMenu.Images newImages;
56 14ad7326 pastith
57 14ad7326 pastith
        private PopupPanel containerPanel;
58 14ad7326 pastith
59 a52ea5e4 pastith
        private List<GroupResource> groups = null;
60 14ad7326 pastith
61 a52ea5e4 pastith
        private List<FileResource> versions = null;
62 14ad7326 pastith
63 892a2836 fstamatelopoulos
        private int tabToShow = 0;
64 892a2836 fstamatelopoulos
65 9ab5db6d Natasa Kapravelou
        private String userName;
66 9ab5db6d Natasa Kapravelou
67 14ad7326 pastith
        /**
68 14ad7326 pastith
         * @param _containerPanel
69 14ad7326 pastith
         * @param _newImages the images of all the possible delete dialogs
70 892a2836 fstamatelopoulos
         * @param _tab the tab to switch to
71 14ad7326 pastith
         */
72 892a2836 fstamatelopoulos
        public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
73 14ad7326 pastith
                containerPanel = _containerPanel;
74 14ad7326 pastith
                newImages = _newImages;
75 892a2836 fstamatelopoulos
                tabToShow = _tab;
76 14ad7326 pastith
        }
77 14ad7326 pastith
78 023f6f1e Panagiotis Astithas
        @Override
79 14ad7326 pastith
        public void execute() {
80 14ad7326 pastith
                containerPanel.hide();
81 a52ea5e4 pastith
                if (GSS.get().getCurrentSelection() instanceof FolderResource) {
82 62f168b2 Giannis Koutsoubos
                        GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((FolderResource) GSS.get().getCurrentSelection()).getUri(),((FolderResource) GSS.get().getCurrentSelection())) {
83 a52ea5e4 pastith
84 a52ea5e4 pastith
                                @Override
85 a52ea5e4 pastith
                                public void onComplete() {
86 a52ea5e4 pastith
                                        GSS.get().setCurrentSelection(getResult());
87 a52ea5e4 pastith
                                        initialize();
88 a52ea5e4 pastith
                                }
89 a52ea5e4 pastith
90 a52ea5e4 pastith
                                @Override
91 a52ea5e4 pastith
                                public void onError(Throwable t) {
92 a52ea5e4 pastith
93 a52ea5e4 pastith
                                }
94 a52ea5e4 pastith
95 a52ea5e4 pastith
                        };
96 a52ea5e4 pastith
                        DeferredCommand.addCommand(eg);
97 a52ea5e4 pastith
                }
98 a52ea5e4 pastith
                else if (GSS.get().getCurrentSelection() instanceof FileResource) {
99 555e8e59 pastith
                        final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
100 555e8e59 pastith
                        // Needed because firefox caches head requests.
101 62f168b2 Giannis Koutsoubos
                        HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random(), null ) {
102 a52ea5e4 pastith
103 a52ea5e4 pastith
                                @Override
104 a52ea5e4 pastith
                                public void onComplete() {
105 515dbddb koutsoub
                                        FileResource res = getResult();
106 515dbddb koutsoub
                                        GSS.get().setCurrentSelection(res);
107 a52ea5e4 pastith
                                        initialize();
108 a52ea5e4 pastith
                                }
109 a52ea5e4 pastith
110 a52ea5e4 pastith
                                @Override
111 a52ea5e4 pastith
                                public void onError(Throwable t) {
112 f98f18c2 koutsoub
                                        if(t instanceof RestException)
113 f98f18c2 koutsoub
                                                GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
114 a52ea5e4 pastith
                                }
115 a52ea5e4 pastith
116 a52ea5e4 pastith
                        };
117 a52ea5e4 pastith
                        DeferredCommand.addCommand(eg);
118 a52ea5e4 pastith
                }
119 2002c1dc Dimitris Routsis
                else if (GSS.get().getCurrentSelection() instanceof List) {
120 2002c1dc Dimitris Routsis
                        List<String> paths = new ArrayList<String>();
121 2002c1dc Dimitris Routsis
                        for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection())
122 2002c1dc Dimitris Routsis
                                paths.add(fr.getUri()+"?"+Math.random());
123 62f168b2 Giannis Koutsoubos
                        Cached[] cached = new Cached[paths.size()];
124 62f168b2 Giannis Koutsoubos
                        int i=0;
125 62f168b2 Giannis Koutsoubos
                        for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
126 62f168b2 Giannis Koutsoubos
                                Cached c = new Cached();
127 62f168b2 Giannis Koutsoubos
                                c.uri=fr.getUri()+"?"+Math.random();
128 62f168b2 Giannis Koutsoubos
                                c.cache=fr;
129 62f168b2 Giannis Koutsoubos
                                cached[i]=c;
130 62f168b2 Giannis Koutsoubos
                                i++;
131 62f168b2 Giannis Koutsoubos
                        }
132 62f168b2 Giannis Koutsoubos
                        MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
133 2002c1dc Dimitris Routsis
134 2002c1dc Dimitris Routsis
                                @Override
135 2002c1dc Dimitris Routsis
                                public void onComplete() {
136 2002c1dc Dimitris Routsis
                                        List<FileResource> res = getResult();
137 2002c1dc Dimitris Routsis
                                        GSS.get().setCurrentSelection(res);
138 2002c1dc Dimitris Routsis
                                        FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
139 2002c1dc Dimitris Routsis
                                        dlg.selectTab(tabToShow);
140 2002c1dc Dimitris Routsis
                                        dlg.center();
141 2002c1dc Dimitris Routsis
                                }
142 2002c1dc Dimitris Routsis
143 2002c1dc Dimitris Routsis
                                @Override
144 2002c1dc Dimitris Routsis
                                public void onError(Throwable t) {
145 2002c1dc Dimitris Routsis
                                        GWT.log("", t);
146 2002c1dc Dimitris Routsis
                                        GSS.get().displayError("Unable to fetch files details");
147 2002c1dc Dimitris Routsis
                                }
148 2002c1dc Dimitris Routsis
149 2002c1dc Dimitris Routsis
                                @Override
150 2002c1dc Dimitris Routsis
                                public void onError(String p, Throwable throwable) {
151 2002c1dc Dimitris Routsis
                                        GWT.log("Path:" + p, throwable);
152 2002c1dc Dimitris Routsis
                                }
153 2002c1dc Dimitris Routsis
                        };
154 2002c1dc Dimitris Routsis
                        DeferredCommand.addCommand(gv);
155 2002c1dc Dimitris Routsis
                }
156 a52ea5e4 pastith
        }
157 a52ea5e4 pastith
158 a52ea5e4 pastith
        private void initialize(){
159 14ad7326 pastith
                getGroups();
160 14ad7326 pastith
                getVersions();
161 9ab5db6d Natasa Kapravelou
                getOwnerFullName();
162 14ad7326 pastith
                DeferredCommand.addCommand(new IncrementalCommand() {
163 14ad7326 pastith
164 023f6f1e Panagiotis Astithas
                        @Override
165 14ad7326 pastith
                        public boolean execute() {
166 14ad7326 pastith
                                boolean res = canContinue();
167 14ad7326 pastith
                                if (res) {
168 9ab5db6d Natasa Kapravelou
                                        displayProperties(newImages, GSS.get().findUserFullName(userName));
169 14ad7326 pastith
                                        return false;
170 14ad7326 pastith
                                }
171 14ad7326 pastith
                                return true;
172 14ad7326 pastith
                        }
173 14ad7326 pastith
174 14ad7326 pastith
                });
175 9ab5db6d Natasa Kapravelou
176 14ad7326 pastith
        }
177 14ad7326 pastith
178 14ad7326 pastith
        private boolean canContinue() {
179 9ab5db6d Natasa Kapravelou
                String userFullNameFromMap = GSS.get().findUserFullName(userName);
180 9ab5db6d Natasa Kapravelou
                if (groups == null || versions == null || userFullNameFromMap == null)
181 14ad7326 pastith
                        return false;
182 14ad7326 pastith
                return true;
183 14ad7326 pastith
        }
184 14ad7326 pastith
185 14ad7326 pastith
        /**
186 14ad7326 pastith
         * Display the appropriate Properties dialog, according to the selected
187 14ad7326 pastith
         * object in the application.
188 14ad7326 pastith
         *
189 14ad7326 pastith
         * @param propImages the images of all the possible properties dialogs
190 14ad7326 pastith
         */
191 9ab5db6d Natasa Kapravelou
        void displayProperties(final Images propImages, final String _userName) {
192 a52ea5e4 pastith
                if (GSS.get().getCurrentSelection() instanceof FolderResource) {
193 a52ea5e4 pastith
                        FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
194 892a2836 fstamatelopoulos
                        dlg.selectTab(tabToShow);
195 14ad7326 pastith
                        dlg.center();
196 a52ea5e4 pastith
                } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
197 9ab5db6d Natasa Kapravelou
                        FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
198 892a2836 fstamatelopoulos
                        dlg.selectTab(tabToShow);
199 14ad7326 pastith
                        dlg.center();
200 14ad7326 pastith
                }
201 14ad7326 pastith
        }
202 14ad7326 pastith
203 a52ea5e4 pastith
        private void getGroups() {
204 62f168b2 Giannis Koutsoubos
                GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
205 14ad7326 pastith
206 555e8e59 pastith
                        @Override
207 a52ea5e4 pastith
                        public void onComplete() {
208 a52ea5e4 pastith
                                GroupsResource res = getResult();
209 62f168b2 Giannis Koutsoubos
                                MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
210 14ad7326 pastith
211 555e8e59 pastith
                                        @Override
212 a52ea5e4 pastith
                                        public void onComplete() {
213 a52ea5e4 pastith
                                                List<GroupResource> groupList = getResult();
214 a52ea5e4 pastith
                                                groups = groupList;
215 14ad7326 pastith
                                        }
216 14ad7326 pastith
217 555e8e59 pastith
                                        @Override
218 a52ea5e4 pastith
                                        public void onError(Throwable t) {
219 a52ea5e4 pastith
                                                GWT.log("", t);
220 a52ea5e4 pastith
                                                GSS.get().displayError("Unable to fetch groups");
221 a52ea5e4 pastith
                                                groups = new ArrayList<GroupResource>();
222 14ad7326 pastith
                                        }
223 14ad7326 pastith
224 555e8e59 pastith
                                        @Override
225 a52ea5e4 pastith
                                        public void onError(String p, Throwable throwable) {
226 a52ea5e4 pastith
                                                GWT.log("Path:" + p, throwable);
227 a52ea5e4 pastith
                                        }
228 a52ea5e4 pastith
                                };
229 a52ea5e4 pastith
                                DeferredCommand.addCommand(ga);
230 14ad7326 pastith
                        }
231 14ad7326 pastith
232 555e8e59 pastith
                        @Override
233 a52ea5e4 pastith
                        public void onError(Throwable t) {
234 a52ea5e4 pastith
                                GWT.log("", t);
235 a52ea5e4 pastith
                                GSS.get().displayError("Unable to fetch groups");
236 a52ea5e4 pastith
                                groups = new ArrayList<GroupResource>();
237 14ad7326 pastith
                        }
238 a52ea5e4 pastith
                };
239 a52ea5e4 pastith
                DeferredCommand.addCommand(gg);
240 14ad7326 pastith
        }
241 14ad7326 pastith
242 14ad7326 pastith
        private void getVersions() {
243 a52ea5e4 pastith
                if (GSS.get().getCurrentSelection() instanceof FileResource) {
244 a52ea5e4 pastith
                        FileResource afile = (FileResource) GSS.get().getCurrentSelection();
245 a52ea5e4 pastith
                        GWT.log("File is versioned:" + afile.isVersioned(), null);
246 a52ea5e4 pastith
                        if (afile.isVersioned()) {
247 a52ea5e4 pastith
                                List<String> paths = new ArrayList<String>();
248 f98f18c2 koutsoub
                                for (int i = 1; i <= afile.getVersion(); i++)
249 555e8e59 pastith
                                        paths.add(afile.getUri() + "?version=" + i);
250 62f168b2 Giannis Koutsoubos
                                MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}), null) {
251 a52ea5e4 pastith
252 555e8e59 pastith
                                        @Override
253 a52ea5e4 pastith
                                        public void onComplete() {
254 a52ea5e4 pastith
                                                versions = getResult();
255 a52ea5e4 pastith
                                        }
256 a52ea5e4 pastith
257 a52ea5e4 pastith
                                        @Override
258 a52ea5e4 pastith
                                        public void onError(Throwable t) {
259 a52ea5e4 pastith
                                                GWT.log("", t);
260 a52ea5e4 pastith
                                                GSS.get().displayError("Unable to fetch versions");
261 a52ea5e4 pastith
                                                versions = new ArrayList<FileResource>();
262 14ad7326 pastith
                                        }
263 14ad7326 pastith
264 555e8e59 pastith
                                        @Override
265 a52ea5e4 pastith
                                        public void onError(String p, Throwable throwable) {
266 a52ea5e4 pastith
                                                GWT.log("Path:" + p, throwable);
267 14ad7326 pastith
                                        }
268 a52ea5e4 pastith
                                };
269 a52ea5e4 pastith
                                DeferredCommand.addCommand(gv);
270 a52ea5e4 pastith
                        } else
271 a52ea5e4 pastith
                                versions = new ArrayList<FileResource>();
272 a52ea5e4 pastith
                } else
273 a52ea5e4 pastith
                        versions = new ArrayList<FileResource>();
274 14ad7326 pastith
        }
275 9ab5db6d Natasa Kapravelou
276 9ab5db6d Natasa Kapravelou
        private void getOwnerFullName() {
277 9ab5db6d Natasa Kapravelou
                FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
278 9ab5db6d Natasa Kapravelou
                userName = fileResource.getOwner();
279 9ab5db6d Natasa Kapravelou
                GetUserCommand getUserCmd = new GetUserCommand(userName);
280 9ab5db6d Natasa Kapravelou
                getUserCmd.execute();
281 9ab5db6d Natasa Kapravelou
282 9ab5db6d Natasa Kapravelou
        }
283 9ab5db6d Natasa Kapravelou
284 14ad7326 pastith
}