Updated copyright notice
[pithos] / web_client / src / gr / grnet / pithos / web / client / commands / PropertiesCommand.java
1 /*
2  *  Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client.commands;
5
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;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
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;
32
33 /**
34  * The command that displays the appropriate Properties dialog, according to the
35  * selected object in the application.
36  *
37  */
38 public class PropertiesCommand implements Command {
39
40         final FileMenu.Images newImages;
41
42         private PopupPanel containerPanel;
43
44         private List<GroupResource> groups = null;
45
46         private List<FileResource> versions = null;
47
48         private int tabToShow = 0;
49
50         private String userName;
51
52         /**
53          * @param _containerPanel
54          * @param _newImages the images of all the possible delete dialogs
55          * @param _tab the tab to switch to
56          */
57         public PropertiesCommand(PopupPanel _containerPanel, final FileMenu.Images _newImages, int _tab) {
58                 containerPanel = _containerPanel;
59                 newImages = _newImages;
60                 tabToShow = _tab;
61         }
62
63         @Override
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()) {
68
69                                 @Override
70                                 public void onComplete() {
71                                         ((RestResourceWrapper) GSS.get().getCurrentSelection()).setResource(getResult());
72                                         initialize();
73                                 }
74
75                                 @Override
76                                 public void onError(Throwable t) {
77
78                                 }
79
80                         };
81                         DeferredCommand.addCommand(eg);
82                 }
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 ) {
87
88                                 @Override
89                                 public void onComplete() {
90                                         FileResource res = getResult();
91                                         GSS.get().setCurrentSelection(res);
92                                         initialize();
93                                 }
94
95                                 @Override
96                                 public void onError(Throwable t) {
97                                         if(t instanceof RestException)
98                                                 GSS.get().displayError("Unable to retrieve file details:"+((RestException)t).getHttpStatusText());
99                                 }
100
101                         };
102                         DeferredCommand.addCommand(eg);
103                 }
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()];
109                         int i=0;
110                         for (FileResource fr : (List<FileResource>) GSS.get().getCurrentSelection()){
111                                 Cached c = new Cached();
112                                 c.uri=fr.getUri()+"?"+Math.random();
113                                 c.cache=fr;
114                                 cached[i]=c;
115                                 i++;
116                         }
117                         MultipleHeadCommand<FileResource> gv = new MultipleHeadCommand<FileResource>(FileResource.class, paths.toArray(new String[] {}),cached) {
118
119                                 @Override
120                                 public void onComplete() {
121                                         List<FileResource> res = getResult();
122                                         GSS.get().setCurrentSelection(res);
123                                         FilesPropertiesDialog dlg = new FilesPropertiesDialog(res);
124                                         dlg.selectTab(tabToShow);
125                                         dlg.center();
126                                 }
127
128                                 @Override
129                                 public void onError(Throwable t) {
130                                         GWT.log("", t);
131                                         GSS.get().displayError("Unable to fetch files details");
132                                 }
133
134                                 @Override
135                                 public void onError(String p, Throwable throwable) {
136                                         GWT.log("Path:" + p, throwable);
137                                 }
138                         };
139                         DeferredCommand.addCommand(gv);
140                 }
141         }
142
143         private void initialize(){
144                 getGroups();
145                 getVersions();
146                 getOwnerFullName();
147                 DeferredCommand.addCommand(new IncrementalCommand() {
148
149                         @Override
150                         public boolean execute() {
151                                 boolean res = canContinue();
152                                 if (res) {
153                                         displayProperties(newImages, GSS.get().findUserFullName(userName));
154                                         return false;
155                                 }
156                                 return true;
157                         }
158
159                 });
160
161         }
162
163         private boolean canContinue() {
164                 String userFullNameFromMap = GSS.get().findUserFullName(userName);
165                 if(groups == null || versions == null || userFullNameFromMap == null)
166                         return false;
167                 return true;
168         }
169
170         /**
171          * Display the appropriate Properties dialog, according to the selected
172          * object in the application.
173          *
174          * @param propImages the images of all the possible properties dialogs
175          */
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);
180                         dlg.center();
181                 } else if (GSS.get().getCurrentSelection() instanceof FileResource) {
182                         FilePropertiesDialog dlg = new FilePropertiesDialog(propImages, groups, versions, _userName);
183                         dlg.selectTab(tabToShow);
184                         dlg.center();
185                 }
186         }
187
188         private void getGroups() {
189                 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null) {
190
191                         @Override
192                         public void onComplete() {
193                                 GroupsResource res = getResult();
194                                 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[] {}), null) {
195
196                                         @Override
197                                         public void onComplete() {
198                                                 List<GroupResource> groupList = getResult();
199                                                 groups = groupList;
200                                         }
201
202                                         @Override
203                                         public void onError(Throwable t) {
204                                                 GWT.log("", t);
205                                                 GSS.get().displayError("Unable to fetch groups");
206                                                 groups = new ArrayList<GroupResource>();
207                                         }
208
209                                         @Override
210                                         public void onError(String p, Throwable throwable) {
211                                                 GWT.log("Path:" + p, throwable);
212                                         }
213                                 };
214                                 DeferredCommand.addCommand(ga);
215                         }
216
217                         @Override
218                         public void onError(Throwable t) {
219                                 GWT.log("", t);
220                                 GSS.get().displayError("Unable to fetch groups");
221                                 groups = new ArrayList<GroupResource>();
222                         }
223                 };
224                 DeferredCommand.addCommand(gg);
225         }
226
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) {
236
237                                         @Override
238                                         public void onComplete() {
239                                                 versions = getResult();
240                                         }
241
242                                         @Override
243                                         public void onError(Throwable t) {
244                                                 GWT.log("", t);
245                                                 GSS.get().displayError("Unable to fetch versions");
246                                                 versions = new ArrayList<FileResource>();
247                                         }
248
249                                         @Override
250                                         public void onError(String p, Throwable throwable) {
251                                                 GWT.log("Path:" + p, throwable);
252                                         }
253                                 };
254                                 DeferredCommand.addCommand(gv);
255                         } else
256                                 versions = new ArrayList<FileResource>();
257                 } else
258                         versions = new ArrayList<FileResource>();
259         }
260
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);
267                                 gu.execute();
268                         }
269                 }else{                  
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);
274                                 gu.execute();
275                         }
276                 }
277         }
278
279
280 }