2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client.commands;
6 import gr.grnet.pithos.web.client.FileMenu;
7 import gr.grnet.pithos.web.client.FilePropertiesDialog;
8 import gr.grnet.pithos.web.client.FilesPropertiesDialog;
9 import gr.grnet.pithos.web.client.FolderPropertiesDialog;
10 import gr.grnet.pithos.web.client.GSS;
11 import gr.grnet.pithos.web.client.FileMenu.Images;
12 import gr.grnet.pithos.web.client.rest.GetCommand;
13 import gr.grnet.pithos.web.client.rest.HeadCommand;
14 import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
15 import gr.grnet.pithos.web.client.rest.MultipleHeadCommand;
16 import gr.grnet.pithos.web.client.rest.RestException;
17 import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
18 import gr.grnet.pithos.web.client.rest.resource.FileResource;
19 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
20 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
21 import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
22 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
24 import java.util.ArrayList;
25 import java.util.List;
27 import com.google.gwt.core.client.GWT;
28 import com.google.gwt.user.client.Command;
29 import com.google.gwt.user.client.DeferredCommand;
30 import com.google.gwt.user.client.IncrementalCommand;
31 import com.google.gwt.user.client.ui.PopupPanel;
34 * The command that displays the appropriate Properties dialog, according to the
35 * selected object in the application.
38 public class PropertiesCommand implements Command {
40 final FileMenu.Images newImages;
42 private PopupPanel containerPanel;
44 private List<GroupResource> groups = null;
46 private List<FileResource> versions = null;
48 private int tabToShow = 0;
50 private String userName;
53 * @param _containerPanel
54 * @param _newImages the images of all the possible delete dialogs
55 * @param _tab the tab to switch to
57 public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
58 containerPanel = _containerPanel;
59 newImages = _newImages;
64 public void execute() {
65 containerPanel.hide();
66 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
67 GetCommand<FolderResource> eg = new GetCommand<FolderResource>(FolderResource.class, ((RestResourceWrapper) GSS.get().getCurrentSelection()).getUri(),((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource()) {
70 public void onComplete() {
71 ((RestResourceWrapper) GSS.get().getCurrentSelection()).setResource(getResult());
76 public void onError(Throwable t) {
81 DeferredCommand.addCommand(eg);
83 else if (GSS.get().getCurrentSelection() instanceof FileResource) {
84 final String path = ((FileResource) GSS.get().getCurrentSelection()).getUri();
85 // Needed because firefox caches head requests.
86 HeadCommand<FileResource> eg = new HeadCommand<FileResource>(FileResource.class, path+"?"+Math.random(), null ) {
89 public void onComplete() {
90 FileResource res = getResult();
91 GSS.get().setCurrentSelection(res);
96 public void onError(Throwable t) {
97 if(t instanceof RestException)
98 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
102 DeferredCommand.addCommand(eg);
104 else if (GSS.get().getCurrentSelection() instanceof List) {
105 List<String> paths = new ArrayList<String>();
106 for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection())
107 paths.add(fr.getUri()+"?"+Math.random());
108 Cached[] cached = new Cached[paths.size()];
110 for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
111 Cached c = new Cached();
112 c.uri=fr.getUri()+"?"+Math.random();
117 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
120 public void onComplete() {
121 List<FileResource> res = getResult();
122 GSS.get().setCurrentSelection(res);
123 FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
124 dlg.selectTab(tabToShow);
129 public void onError(Throwable t) {
131 GSS.get().displayError("Unable to fetch files details");
135 public void onError(String p, Throwable throwable) {
136 GWT.log("Path:" + p, throwable);
139 DeferredCommand.addCommand(gv);
143 private void initialize(){
147 DeferredCommand.addCommand(new IncrementalCommand() {
150 public boolean execute() {
151 boolean res = canContinue();
153 displayProperties(newImages, GSS.get().findUserFullName(userName));
163 private boolean canContinue() {
164 String userFullNameFromMap = GSS.get().findUserFullName(userName);
165 if(groups == null || versions == null || userFullNameFromMap == null)
171 * Display the appropriate Properties dialog, according to the selected
172 * object in the application.
174 * @param propImages the images of all the possible properties dialogs
176 void displayProperties(final Images propImages, final String _userName) {
177 if (GSS.get().getCurrentSelection() instanceof RestResourceWrapper) {
178 FolderPropertiesDialog dlg = new FolderPropertiesDialog(propImages, false, groups);
179 dlg.selectTab(tabToShow);
181 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
182 FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
183 dlg.selectTab(tabToShow);
188 private void getGroups() {
189 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
192 public void onComplete() {
193 GroupsResource res = getResult();
194 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
197 public void onComplete() {
198 List<GroupResource> groupList = getResult();
203 public void onError(Throwable t) {
205 GSS.get().displayError("Unable to fetch groups");
206 groups = new ArrayList<GroupResource>();
210 public void onError(String p, Throwable throwable) {
211 GWT.log("Path:" + p, throwable);
214 DeferredCommand.addCommand(ga);
218 public void onError(Throwable t) {
220 GSS.get().displayError("Unable to fetch groups");
221 groups = new ArrayList<GroupResource>();
224 DeferredCommand.addCommand(gg);
227 private void getVersions() {
228 if (GSS.get().getCurrentSelection() instanceof FileResource) {
229 FileResource afile = (FileResource) GSS.get().getCurrentSelection();
230 GWT.log("File is versioned:" + afile.isVersioned(), null);
231 if (afile.isVersioned()) {
232 List<String> paths = new ArrayList<String>();
233 for (int i = 1; i <= afile.getVersion(); i++)
234 paths.add(afile.getUri() + "?version=" + i);
235 MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}), null) {
238 public void onComplete() {
239 versions = getResult();
243 public void onError(Throwable t) {
245 GSS.get().displayError("Unable to fetch versions");
246 versions = new ArrayList<FileResource>();
250 public void onError(String p, Throwable throwable) {
251 GWT.log("Path:" + p, throwable);
254 DeferredCommand.addCommand(gv);
256 versions = new ArrayList<FileResource>();
258 versions = new ArrayList<FileResource>();
261 private void getOwnerFullName() {
262 if(GSS.get().getCurrentSelection() instanceof FileResource){
263 FileResource fileResource = (FileResource) GSS.get().getCurrentSelection();
264 userName = fileResource.getOwner();
265 if(GSS.get().findUserFullName(userName) == null){
266 GetUserCommand gu = new GetUserCommand(userName);
270 FolderResource resource = ((RestResourceWrapper) GSS.get().getCurrentSelection()).getResource();
271 userName = resource.getOwner();
272 if(GSS.get().findUserFullName(userName) == null){
273 GetUserCommand gu = new GetUserCommand(userName);