Fix the response uri when making a GET request by username to /pithos/rest/users/
[pithos] / src / gr / ebs / gss / client / FilePropertiesDialog.java
1 /*\r
2  * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.rest.PostCommand;\r
22 import gr.ebs.gss.client.rest.RestException;\r
23 import gr.ebs.gss.client.rest.resource.FileResource;\r
24 import gr.ebs.gss.client.rest.resource.GroupResource;\r
25 import gr.ebs.gss.client.rest.resource.PermissionHolder;\r
26 \r
27 import java.util.Iterator;\r
28 import java.util.List;\r
29 import java.util.Set;\r
30 \r
31 import com.google.gwt.core.client.GWT;\r
32 import com.google.gwt.event.dom.client.ClickEvent;\r
33 import com.google.gwt.event.dom.client.ClickHandler;\r
34 import com.google.gwt.i18n.client.DateTimeFormat;\r
35 import com.google.gwt.json.client.JSONArray;\r
36 import com.google.gwt.json.client.JSONBoolean;\r
37 import com.google.gwt.json.client.JSONObject;\r
38 import com.google.gwt.json.client.JSONString;\r
39 import com.google.gwt.resources.client.ClientBundle;\r
40 import com.google.gwt.resources.client.ImageResource;\r
41 import com.google.gwt.user.client.Command;\r
42 import com.google.gwt.user.client.DeferredCommand;\r
43 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
44 import com.google.gwt.user.client.ui.Button;\r
45 import com.google.gwt.user.client.ui.CheckBox;\r
46 import com.google.gwt.user.client.ui.DecoratedTabPanel;\r
47 import com.google.gwt.user.client.ui.DisclosurePanel;\r
48 import com.google.gwt.user.client.ui.FlexTable;\r
49 import com.google.gwt.user.client.ui.FlowPanel;\r
50 import com.google.gwt.user.client.ui.FocusPanel;\r
51 import com.google.gwt.user.client.ui.HTML;\r
52 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
53 import com.google.gwt.user.client.ui.HorizontalPanel;\r
54 import com.google.gwt.user.client.ui.Label;\r
55 import com.google.gwt.user.client.ui.TextBox;\r
56 import com.google.gwt.user.client.ui.VerticalPanel;\r
57 \r
58 /**\r
59  * The 'File properties' dialog box implementation.\r
60  *\r
61  * @author past\r
62  */\r
63 public class FilePropertiesDialog extends AbstractPropertiesDialog {\r
64 \r
65         final PermissionsList permList;\r
66 \r
67         private CheckBox readForAll;\r
68 \r
69         /**\r
70          * An image bundle for this widgets images.\r
71          */\r
72         public interface Images extends ClientBundle,MessagePanel.Images {\r
73 \r
74                 @Source("gr/ebs/gss/resources/edit_user.png")\r
75                 ImageResource permUser();\r
76 \r
77                 @Source("gr/ebs/gss/resources/groupevent.png")\r
78                 ImageResource permGroup();\r
79 \r
80                 @Source("gr/ebs/gss/resources/editdelete.png")\r
81                 ImageResource delete();\r
82 \r
83                 @Source("gr/ebs/gss/resources/db_update.png")\r
84                 ImageResource restore();\r
85 \r
86                 @Source("gr/ebs/gss/resources/folder_inbox.png")\r
87                 ImageResource download();\r
88         }\r
89 \r
90         /**\r
91          * The widget that holds the name of the file.\r
92          */\r
93         private TextBox name = new TextBox();\r
94 \r
95         private final CheckBox versioned = new CheckBox();\r
96 \r
97         final FileResource file;\r
98 \r
99         private String userFullName;\r
100 \r
101         /**\r
102          * The widget's constructor.\r
103          *\r
104          * @param images the dialog's ImageBundle\r
105          * @param groups\r
106          * @param bodies\r
107          */\r
108         public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies, String _userFullName) {\r
109 \r
110                 // Set the dialog's caption.\r
111                 setText("File properties");\r
112 \r
113                 file = (FileResource) GSS.get().getCurrentSelection();\r
114                 userFullName = _userFullName;\r
115                 permList = new PermissionsList(images, file.getPermissions(), file.getOwner());\r
116 \r
117 \r
118                 // Outer contains inner and buttons.\r
119                 final VerticalPanel outer = new VerticalPanel();\r
120                 final FocusPanel focusPanel = new FocusPanel(outer);\r
121                 // Inner contains generalPanel and permPanel.\r
122                 inner = new DecoratedTabPanel();\r
123                 inner.setAnimationEnabled(true);\r
124                 final VerticalPanel generalPanel = new VerticalPanel();\r
125                 final VerticalPanel permPanel = new VerticalPanel();\r
126                 final HorizontalPanel buttons = new HorizontalPanel();\r
127                 final HorizontalPanel permButtons = new HorizontalPanel();\r
128                 final HorizontalPanel permForAll = new HorizontalPanel();\r
129                 final HorizontalPanel pathPanel = new HorizontalPanel();\r
130                 final VerticalPanel verPanel = new VerticalPanel();\r
131                 final HorizontalPanel vPanel = new HorizontalPanel();\r
132                 final HorizontalPanel vPanel2 = new HorizontalPanel();\r
133 \r
134                 versioned.setValue(file.isVersioned());\r
135                 inner.add(generalPanel, "General");\r
136                 inner.add(permPanel, "Sharing");\r
137                 inner.add(verPanel, "Versions");\r
138                 inner.selectTab(0);\r
139 \r
140                 final FlexTable generalTable = new FlexTable();\r
141                 generalTable.setText(0, 0, "Name");\r
142                 generalTable.setText(1, 0, "Folder");\r
143                 generalTable.setText(2, 0, "Owner");\r
144                 generalTable.setText(3, 0, "Last modified");\r
145                 generalTable.setText(4, 0, "Tags");\r
146                 name.setWidth("100%");\r
147                 name.setText(file.getName());\r
148                 generalTable.setWidget(0, 1, name);\r
149                 if(file.getFolderName() != null)\r
150                         generalTable.setText(1, 1, file.getFolderName());\r
151                 else\r
152                         generalTable.setText(1, 1, "-");\r
153                 generalTable.setText(2, 1,userFullName);\r
154 \r
155                 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");\r
156                 generalTable.setText(3, 1, formatter.format(file.getModificationDate()));\r
157                 // Get the tags.\r
158                 StringBuffer tagsBuffer = new StringBuffer();\r
159                 Iterator i = file.getTags().iterator();\r
160                 while (i.hasNext()) {\r
161                         String tag = (String) i.next();\r
162                         tagsBuffer.append(tag).append(", ");\r
163                 }\r
164                 if (tagsBuffer.length() > 1)\r
165                         tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);\r
166                 initialTagText = tagsBuffer.toString();\r
167                 tags.setWidth("100%");\r
168                 tags.setText(initialTagText);\r
169                 generalTable.setWidget(4, 1, tags);\r
170                 generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");\r
171                 generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");\r
172                 generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");\r
173                 generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");\r
174                 generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");\r
175                 generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");\r
176                 generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");\r
177                 generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");\r
178                 generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");\r
179                 generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");\r
180                 generalTable.setCellSpacing(4);\r
181 \r
182                 // Create the 'OK' button, along with a listener that hides the dialog\r
183                 // when the button is clicked.\r
184                 final Button ok = new Button("OK", new ClickHandler() {\r
185                         @Override\r
186                         public void onClick(ClickEvent event) {\r
187                                 accept();\r
188                                 closeDialog();\r
189                         }\r
190                 });\r
191                 buttons.add(ok);\r
192                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
193                 // Create the 'Cancel' button, along with a listener that hides the\r
194                 // dialog when the button is clicked.\r
195                 final Button cancel = new Button("Cancel", new ClickHandler() {\r
196                         @Override\r
197                         public void onClick(ClickEvent event) {\r
198                                 closeDialog();\r
199                         }\r
200                 });\r
201                 buttons.add(cancel);\r
202                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
203                 buttons.setSpacing(8);\r
204                 buttons.addStyleName("gss-TabPanelBottom");\r
205 \r
206                 generalPanel.add(generalTable);\r
207 \r
208                 // Asynchronously retrieve the tags defined by this user.\r
209                 DeferredCommand.addCommand(new Command() {\r
210 \r
211                         @Override\r
212                         public void execute() {\r
213                                 updateTags();\r
214                         }\r
215                 });\r
216 \r
217                 DisclosurePanel allTags = new DisclosurePanel("All tags");\r
218                 allTagsContent = new FlowPanel();\r
219                 allTagsContent.setWidth("100%");\r
220                 allTags.setContent(allTagsContent);\r
221                 generalPanel.add(allTags);\r
222                 generalPanel.setSpacing(4);\r
223 \r
224                 final Button add = new Button("Add Group", new ClickHandler() {\r
225                         @Override\r
226                         public void onClick(ClickEvent event) {\r
227                                 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);\r
228                                 dlg.center();\r
229                         }\r
230                 });\r
231                 permButtons.add(add);\r
232                 permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);\r
233 \r
234                 final Button addUser = new Button("Add User", new ClickHandler() {\r
235                         @Override\r
236                         public void onClick(ClickEvent event) {\r
237                                 PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);\r
238                                 dlg.center();\r
239                         }\r
240                 });\r
241                 permButtons.add(addUser);\r
242                 permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);\r
243 \r
244                 permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
245                 permButtons.setSpacing(8);\r
246                 permButtons.addStyleName("gss-TabPanelBottom");\r
247 \r
248                 final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +\r
249                                         " by everyone. By checking this option, you are certifying that you have the right to " +\r
250                                         "distribute this file and that it does not violate the Terms of Use.", true);\r
251                 readForAllNote.setVisible(false);\r
252                 readForAllNote.setStylePrimaryName("gss-readForAllNote");\r
253 \r
254                 readForAll = new CheckBox();\r
255                 readForAll.setValue(file.isReadForAll());\r
256                 readForAll.addClickHandler(new ClickHandler() {\r
257                         @Override\r
258                         public void onClick(ClickEvent event) {\r
259                                 readForAllNote.setVisible(readForAll.getValue());\r
260                         }\r
261 \r
262                 });\r
263 \r
264                 permPanel.add(permList);\r
265                 permPanel.add(permButtons);\r
266                 // Only show the read for all permission if the user is the owner.\r
267                 if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {\r
268                         permForAll.add(new Label("Public"));\r
269                         permForAll.add(readForAll);\r
270                         permForAll.setSpacing(8);\r
271                         permForAll.addStyleName("gss-TabPanelBottom");\r
272                         permForAll.add(readForAllNote);\r
273                         permPanel.add(permForAll);\r
274                 }\r
275 \r
276                 TextBox path = new TextBox();\r
277                 path.setWidth("100%");\r
278                 path.addClickHandler(new ClickHandler() {\r
279                         @Override\r
280                         public void onClick(ClickEvent event) {\r
281                                 GSS.enableIESelection();\r
282                                 ((TextBox) event.getSource()).selectAll();\r
283                                 GSS.preventIESelection();\r
284                         }\r
285 \r
286                 });\r
287                 path.setText(file.getUri());\r
288                 path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");\r
289                 path.setWidth("100%");\r
290                 path.setReadOnly(true);\r
291                 pathPanel.setWidth("100%");\r
292                 pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);\r
293                 pathPanel.add(new Label("Link"));\r
294                 pathPanel.setSpacing(8);\r
295                 pathPanel.addStyleName("gss-TabPanelBottom");\r
296                 pathPanel.add(path);\r
297                 permPanel.add(pathPanel);\r
298 \r
299                 VersionsList verList = new VersionsList(this, images, bodies);\r
300                 verPanel.add(verList);\r
301 \r
302                 vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
303                 vPanel.setSpacing(8);\r
304                 vPanel.addStyleName("gss-TabPanelBottom");\r
305                 vPanel.add(new Label("Versioned"));\r
306 \r
307                 vPanel.add(versioned);\r
308                 verPanel.add(vPanel);\r
309                 vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
310                 vPanel2.setSpacing(8);\r
311                 vPanel2.addStyleName("gss-TabPanelBottom");\r
312                 Button removeVersionsButton = new Button(AbstractImagePrototype.create(images.delete()).getHTML(), new ClickHandler() {\r
313                         @Override\r
314                         public void onClick(ClickEvent event) {\r
315                                 ConfirmationDialog confirm = new ConfirmationDialog("Really " +\r
316                                                 "remove all previous versions?", "Remove") {\r
317 \r
318                                         @Override\r
319                                         public void cancel() {\r
320                                         }\r
321 \r
322                                         @Override\r
323                                         public void confirm() {\r
324                                                 FilePropertiesDialog.this.closeDialog();\r
325                                                 removeAllOldVersions();\r
326                                         }\r
327 \r
328                                 };\r
329                                 confirm.center();\r
330                         }\r
331 \r
332                 });\r
333                 HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");\r
334                 vPanel2.add(removeAllVersion);\r
335                 vPanel2.add(removeVersionsButton);\r
336                 verPanel.add(vPanel2);\r
337                 if(!file.isVersioned())\r
338                         vPanel2.setVisible(false);\r
339                 outer.add(inner);\r
340                 outer.add(buttons);\r
341                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
342                 outer.addStyleName("gss-TabPanelBottom");\r
343 \r
344                 focusPanel.setFocus(true);\r
345                 setWidget(outer);\r
346         }\r
347 \r
348 \r
349         /**\r
350          * Accepts any change and updates the file\r
351          *\r
352          */\r
353         @Override\r
354         protected void accept() {\r
355                 String newFilename = null;\r
356                 permList.updatePermissionsAccordingToInput();\r
357                 Set<PermissionHolder> perms = permList.getPermissions();\r
358                 JSONObject json = new JSONObject();\r
359                 if (!name.getText().equals(file.getName())) {\r
360                         newFilename = name.getText();\r
361                         json.put("name", new JSONString(newFilename));\r
362                 }\r
363                 if (versioned.getValue() != file.isVersioned())\r
364                         json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));\r
365                 //only update the read for all perm if the user is the owner\r
366                 if (readForAll.getValue() != file.isReadForAll())\r
367                         if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))\r
368                                 json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));\r
369                 int i = 0;\r
370                 if (permList.hasChanges()) {\r
371                         GWT.log("Permissions change", null);\r
372                         JSONArray perma = new JSONArray();\r
373 \r
374                         for (PermissionHolder p : perms) {\r
375                                 JSONObject po = new JSONObject();\r
376                                 if (p.getUser() != null)\r
377                                         po.put("user", new JSONString(p.getUser()));\r
378                                 if (p.getGroup() != null)\r
379                                         po.put("group", new JSONString(p.getGroup()));\r
380                                 po.put("read", JSONBoolean.getInstance(p.isRead()));\r
381                                 po.put("write", JSONBoolean.getInstance(p.isWrite()));\r
382                                 po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));\r
383                                 perma.set(i, po);\r
384                                 i++;\r
385                         }\r
386                         json.put("permissions", perma);\r
387                 }\r
388                 JSONArray taga = new JSONArray();\r
389                 i = 0;\r
390                 if (!tags.getText().equals(initialTagText)) {\r
391                         String[] tagset = tags.getText().split(",");\r
392                         for (String t : tagset) {\r
393                                 JSONString to = new JSONString(t);\r
394                                 taga.set(i, to);\r
395                                 i++;\r
396                         }\r
397                         json.put("tags", taga);\r
398                 }\r
399                 String jsonString = json.toString();\r
400                 if(jsonString.equals("{}")){\r
401                         GWT.log("NO CHANGES", null);\r
402                         return;\r
403                 }\r
404                 final String newFilenameFinal = newFilename;\r
405                 PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {\r
406 \r
407                         @Override\r
408                         public void onComplete() {\r
409                                 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/, newFilenameFinal);\r
410                         }\r
411 \r
412                         @Override\r
413                         public void onError(Throwable t) {\r
414                                 GWT.log("", t);\r
415                                 if (t instanceof RestException) {\r
416                                         int statusCode = ((RestException) t).getHttpStatusCode();\r
417                                         if (statusCode == 405)\r
418                                                 GSS.get().displayError("You don't have the necessary permissions");\r
419                                         else if (statusCode == 404)\r
420                                                 GSS.get().displayError("User in permissions does not exist");\r
421                                         else if (statusCode == 409)\r
422                                                 GSS.get().displayError("A file with the same name already exists");\r
423                                         else if (statusCode == 413)\r
424                                                 GSS.get().displayError("Your quota has been exceeded");\r
425                                         else\r
426                                                 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
427                                 } else\r
428                                         GSS.get().displayError("System error modifying file:" + t.getMessage());\r
429                         }\r
430 \r
431                 };\r
432                 DeferredCommand.addCommand(cf);\r
433 \r
434         }\r
435 \r
436         private void removeAllOldVersions() {\r
437                 JSONObject json = new JSONObject();\r
438                 json.put("versioned", JSONBoolean.getInstance(false));\r
439                 GWT.log(json.toString(), null);\r
440                 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {\r
441 \r
442                         @Override\r
443                         public void onComplete() {\r
444                                 toggleVersioned(true);\r
445                         }\r
446 \r
447                         @Override\r
448                         public void onError(Throwable t) {\r
449                                 GWT.log("", t);\r
450                                 if (t instanceof RestException) {\r
451                                         int statusCode = ((RestException) t).getHttpStatusCode();\r
452                                         if (statusCode == 405)\r
453                                                 GSS.get().displayError("You don't have the necessary permissions");\r
454                                         else if (statusCode == 404)\r
455                                                 GSS.get().displayError("User in permissions does not exist");\r
456                                         else if (statusCode == 409)\r
457                                                 GSS.get().displayError("A folder with the same name already exists");\r
458                                         else if (statusCode == 413)\r
459                                                 GSS.get().displayError("Your quota has been exceeded");\r
460                                         else\r
461                                                 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
462                                 } else\r
463                                         GSS.get().displayError("System error moifying file:" + t.getMessage());\r
464                         }\r
465                 };\r
466                 DeferredCommand.addCommand(cf);\r
467         }\r
468 \r
469         private void toggleVersioned(boolean versionedValue) {\r
470                 JSONObject json = new JSONObject();\r
471                 json.put("versioned", JSONBoolean.getInstance(versionedValue));\r
472                 GWT.log(json.toString(), null);\r
473                 PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {\r
474 \r
475                         @Override\r
476                         public void onComplete() {\r
477                                 GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);\r
478                         }\r
479 \r
480                         @Override\r
481                         public void onError(Throwable t) {\r
482                                 GWT.log("", t);\r
483                                 if (t instanceof RestException) {\r
484                                         int statusCode = ((RestException) t).getHttpStatusCode();\r
485                                         if (statusCode == 405)\r
486                                                 GSS.get().displayError("You don't have the necessary permissions");\r
487                                         else if (statusCode == 404)\r
488                                                 GSS.get().displayError("User in permissions does not exist");\r
489                                         else if (statusCode == 409)\r
490                                                 GSS.get().displayError("A folder with the same name already exists");\r
491                                         else if (statusCode == 413)\r
492                                                 GSS.get().displayError("Your quota has been exceeded");\r
493                                         else\r
494                                                 GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
495                                 } else\r
496                                         GSS.get().displayError("System error moifying file:" + t.getMessage());\r
497                         }\r
498                 };\r
499                 DeferredCommand.addCommand(cf);\r
500         }\r
501 \r
502 }\r