Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (17.6 kB)

1
/*
2
 * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.rest.PostCommand;
22
import gr.ebs.gss.client.rest.RestException;
23
import gr.ebs.gss.client.rest.resource.FileResource;
24
import gr.ebs.gss.client.rest.resource.GroupResource;
25
import gr.ebs.gss.client.rest.resource.PermissionHolder;
26

    
27
import java.util.Iterator;
28
import java.util.List;
29
import java.util.Set;
30

    
31
import com.google.gwt.core.client.GWT;
32
import com.google.gwt.event.dom.client.ClickEvent;
33
import com.google.gwt.event.dom.client.ClickHandler;
34
import com.google.gwt.i18n.client.DateTimeFormat;
35
import com.google.gwt.json.client.JSONArray;
36
import com.google.gwt.json.client.JSONBoolean;
37
import com.google.gwt.json.client.JSONObject;
38
import com.google.gwt.json.client.JSONString;
39
import com.google.gwt.resources.client.ClientBundle;
40
import com.google.gwt.resources.client.ImageResource;
41
import com.google.gwt.user.client.Command;
42
import com.google.gwt.user.client.DeferredCommand;
43
import com.google.gwt.user.client.ui.AbstractImagePrototype;
44
import com.google.gwt.user.client.ui.Button;
45
import com.google.gwt.user.client.ui.CheckBox;
46
import com.google.gwt.user.client.ui.DecoratedTabPanel;
47
import com.google.gwt.user.client.ui.DisclosurePanel;
48
import com.google.gwt.user.client.ui.FlexTable;
49
import com.google.gwt.user.client.ui.FlowPanel;
50
import com.google.gwt.user.client.ui.FocusPanel;
51
import com.google.gwt.user.client.ui.HTML;
52
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
53
import com.google.gwt.user.client.ui.HorizontalPanel;
54
import com.google.gwt.user.client.ui.Label;
55
import com.google.gwt.user.client.ui.TextBox;
56
import com.google.gwt.user.client.ui.VerticalPanel;
57

    
58
/**
59
 * The 'File properties' dialog box implementation.
60
 *
61
 * @author past
62
 */
63
public class FilePropertiesDialog extends AbstractPropertiesDialog {
64

    
65
        final PermissionsList permList;
66

    
67
        private CheckBox readForAll;
68

    
69
        /**
70
         * An image bundle for this widgets images.
71
         */
72
        public interface Images extends ClientBundle,MessagePanel.Images {
73

    
74
                @Source("gr/ebs/gss/resources/edit_user.png")
75
                ImageResource permUser();
76

    
77
                @Source("gr/ebs/gss/resources/groupevent.png")
78
                ImageResource permGroup();
79

    
80
                @Source("gr/ebs/gss/resources/editdelete.png")
81
                ImageResource delete();
82

    
83
                @Source("gr/ebs/gss/resources/db_update.png")
84
                ImageResource restore();
85

    
86
                @Source("gr/ebs/gss/resources/folder_inbox.png")
87
                ImageResource download();
88
        }
89

    
90
        /**
91
         * The widget that holds the name of the file.
92
         */
93
        private TextBox name = new TextBox();
94

    
95
        private final CheckBox versioned = new CheckBox();
96

    
97
        final FileResource file;
98

    
99
        private String userFullName;
100

    
101
        /**
102
         * The widget's constructor.
103
         *
104
         * @param images the dialog's ImageBundle
105
         * @param groups
106
         * @param bodies
107
         */
108
        public FilePropertiesDialog(final Images images, final List<GroupResource> groups, List<FileResource> bodies, String _userFullName) {
109

    
110
                // Set the dialog's caption.
111
                setText("File properties");
112

    
113
                file = (FileResource) GSS.get().getCurrentSelection();
114
                permList = new PermissionsList(images, file.getPermissions(), file.getOwner());
115
                userFullName = _userFullName;
116

    
117
                // Outer contains inner and buttons.
118
                final VerticalPanel outer = new VerticalPanel();
119
                final FocusPanel focusPanel = new FocusPanel(outer);
120
                // Inner contains generalPanel and permPanel.
121
                inner = new DecoratedTabPanel();
122
                inner.setAnimationEnabled(true);
123
                final VerticalPanel generalPanel = new VerticalPanel();
124
                final VerticalPanel permPanel = new VerticalPanel();
125
                final HorizontalPanel buttons = new HorizontalPanel();
126
                final HorizontalPanel permButtons = new HorizontalPanel();
127
                final HorizontalPanel permForAll = new HorizontalPanel();
128
                final HorizontalPanel pathPanel = new HorizontalPanel();
129
                final VerticalPanel verPanel = new VerticalPanel();
130
                final HorizontalPanel vPanel = new HorizontalPanel();
131
                final HorizontalPanel vPanel2 = new HorizontalPanel();
132

    
133
                versioned.setValue(file.isVersioned());
134
                inner.add(generalPanel, "General");
135
                inner.add(permPanel, "Sharing");
136
                inner.add(verPanel, "Versions");
137
                inner.selectTab(0);
138

    
139
                final FlexTable generalTable = new FlexTable();
140
                generalTable.setText(0, 0, "Name");
141
                generalTable.setText(1, 0, "Folder");
142
                generalTable.setText(2, 0, "Owner");
143
                generalTable.setText(3, 0, "Last modified");
144
                generalTable.setText(4, 0, "Tags");
145
                name.setWidth("100%");
146
                name.setText(file.getName());
147
                generalTable.setWidget(0, 1, name);
148
                if(file.getFolderName() != null)
149
                        generalTable.setText(1, 1, file.getFolderName());
150
                else
151
                        generalTable.setText(1, 1, "-");
152
                generalTable.setText(2, 1,userFullName);
153

    
154
                final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
155
                generalTable.setText(3, 1, formatter.format(file.getModificationDate()));
156
                // Get the tags.
157
                StringBuffer tagsBuffer = new StringBuffer();
158
                Iterator i = file.getTags().iterator();
159
                while (i.hasNext()) {
160
                        String tag = (String) i.next();
161
                        tagsBuffer.append(tag).append(", ");
162
                }
163
                if (tagsBuffer.length() > 1)
164
                        tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);
165
                initialTagText = tagsBuffer.toString();
166
                tags.setWidth("100%");
167
                tags.setText(initialTagText);
168
                generalTable.setWidget(4, 1, tags);
169
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
170
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
171
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
172
                generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
173
                generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
174
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
175
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
176
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
177
                generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
178
                generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");
179
                generalTable.setCellSpacing(4);
180

    
181
                // Create the 'OK' button, along with a listener that hides the dialog
182
                // when the button is clicked.
183
                final Button ok = new Button("OK", new ClickHandler() {
184
                        @Override
185
                        public void onClick(ClickEvent event) {
186
                                accept();
187
                                closeDialog();
188
                        }
189
                });
190
                buttons.add(ok);
191
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
192
                // Create the 'Cancel' button, along with a listener that hides the
193
                // dialog when the button is clicked.
194
                final Button cancel = new Button("Cancel", new ClickHandler() {
195
                        @Override
196
                        public void onClick(ClickEvent event) {
197
                                closeDialog();
198
                        }
199
                });
200
                buttons.add(cancel);
201
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
202
                buttons.setSpacing(8);
203
                buttons.addStyleName("gss-TabPanelBottom");
204

    
205
                generalPanel.add(generalTable);
206

    
207
                // Asynchronously retrieve the tags defined by this user.
208
                DeferredCommand.addCommand(new Command() {
209

    
210
                        @Override
211
                        public void execute() {
212
                                updateTags();
213
                        }
214
                });
215

    
216
                DisclosurePanel allTags = new DisclosurePanel("All tags");
217
                allTagsContent = new FlowPanel();
218
                allTagsContent.setWidth("100%");
219
                allTags.setContent(allTagsContent);
220
                generalPanel.add(allTags);
221
                generalPanel.setSpacing(4);
222

    
223
                final Button add = new Button("Add Group", new ClickHandler() {
224
                        @Override
225
                        public void onClick(ClickEvent event) {
226
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
227
                                dlg.center();
228
                        }
229
                });
230
                permButtons.add(add);
231
                permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
232

    
233
                final Button addUser = new Button("Add User", new ClickHandler() {
234
                        @Override
235
                        public void onClick(ClickEvent event) {
236
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
237
                                dlg.center();
238
                        }
239
                });
240
                permButtons.add(addUser);
241
                permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
242

    
243
                permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
244
                permButtons.setSpacing(8);
245
                permButtons.addStyleName("gss-TabPanelBottom");
246

    
247
                final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
248
                                        " by everyone. By checking this option, you are certifying that you have the right to " +
249
                                        "distribute this file and that it does not violate the Terms of Use.", true);
250
                readForAllNote.setVisible(false);
251
                readForAllNote.setStylePrimaryName("gss-readForAllNote");
252

    
253
                readForAll = new CheckBox();
254
                readForAll.setValue(file.isReadForAll());
255
                readForAll.addClickHandler(new ClickHandler() {
256
                        @Override
257
                        public void onClick(ClickEvent event) {
258
                                readForAllNote.setVisible(readForAll.getValue());
259
                        }
260

    
261
                });
262

    
263
                permPanel.add(permList);
264
                permPanel.add(permButtons);
265
                // Only show the read for all permission if the user is the owner.
266
                if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
267
                        permForAll.add(new Label("Public"));
268
                        permForAll.add(readForAll);
269
                        permForAll.setSpacing(8);
270
                        permForAll.addStyleName("gss-TabPanelBottom");
271
                        permForAll.add(readForAllNote);
272
                        permPanel.add(permForAll);
273
                }
274

    
275
                TextBox path = new TextBox();
276
                path.setWidth("100%");
277
                path.addClickHandler(new ClickHandler() {
278
                        @Override
279
                        public void onClick(ClickEvent event) {
280
                                GSS.enableIESelection();
281
                                ((TextBox) event.getSource()).selectAll();
282
                                GSS.preventIESelection();
283
                        }
284

    
285
                });
286
                path.setText(file.getUri());
287
                path.setTitle("Use this link for sharing the file via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
288
                path.setWidth("100%");
289
                path.setReadOnly(true);
290
                pathPanel.setWidth("100%");
291
                pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
292
                pathPanel.add(new Label("Link"));
293
                pathPanel.setSpacing(8);
294
                pathPanel.addStyleName("gss-TabPanelBottom");
295
                pathPanel.add(path);
296
                permPanel.add(pathPanel);
297

    
298
                VersionsList verList = new VersionsList(this, images, bodies);
299
                verPanel.add(verList);
300

    
301
                vPanel.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
302
                vPanel.setSpacing(8);
303
                vPanel.addStyleName("gss-TabPanelBottom");
304
                vPanel.add(new Label("Versioned"));
305

    
306
                vPanel.add(versioned);
307
                verPanel.add(vPanel);
308
                vPanel2.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
309
                vPanel2.setSpacing(8);
310
                vPanel2.addStyleName("gss-TabPanelBottom");
311
                Button removeVersionsButton = new Button(AbstractImagePrototype.create(images.delete()).getHTML(), new ClickHandler() {
312
                        @Override
313
                        public void onClick(ClickEvent event) {
314
                                ConfirmationDialog confirm = new ConfirmationDialog("Really " +
315
                                                "remove all previous versions?", "Remove") {
316

    
317
                                        @Override
318
                                        public void cancel() {
319
                                        }
320

    
321
                                        @Override
322
                                        public void confirm() {
323
                                                FilePropertiesDialog.this.closeDialog();
324
                                                removeAllOldVersions();
325
                                        }
326

    
327
                                };
328
                                confirm.center();
329
                        }
330

    
331
                });
332
                HTML removeAllVersion = new HTML("<span>Remove all previous versions?</span>");
333
                vPanel2.add(removeAllVersion);
334
                vPanel2.add(removeVersionsButton);
335
                verPanel.add(vPanel2);
336
                if(!file.isVersioned())
337
                        vPanel2.setVisible(false);
338
                outer.add(inner);
339
                outer.add(buttons);
340
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
341
                outer.addStyleName("gss-TabPanelBottom");
342

    
343
                focusPanel.setFocus(true);
344
                setWidget(outer);
345
        }
346

    
347

    
348
        /**
349
         * Accepts any change and updates the file
350
         *
351
         */
352
        @Override
353
        protected void accept() {
354
                String newFilename = null;
355
                permList.updatePermissionsAccordingToInput();
356
                Set<PermissionHolder> perms = permList.getPermissions();
357
                JSONObject json = new JSONObject();
358
                if (!name.getText().equals(file.getName())) {
359
                        newFilename = name.getText();
360
                        json.put("name", new JSONString(newFilename));
361
                }
362
                if (versioned.getValue() != file.isVersioned())
363
                        json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));
364
                //only update the read for all perm if the user is the owner
365
                if (readForAll.getValue() != file.isReadForAll())
366
                        if (file.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
367
                                json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
368
                int i = 0;
369
                if (permList.hasChanges()) {
370
                        GWT.log("Permissions change", null);
371
                        JSONArray perma = new JSONArray();
372

    
373
                        for (PermissionHolder p : perms) {
374
                                JSONObject po = new JSONObject();
375
                                if (p.getUser() != null)
376
                                        po.put("user", new JSONString(p.getUser()));
377
                                if (p.getGroup() != null)
378
                                        po.put("group", new JSONString(p.getGroup()));
379
                                po.put("read", JSONBoolean.getInstance(p.isRead()));
380
                                po.put("write", JSONBoolean.getInstance(p.isWrite()));
381
                                po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
382
                                perma.set(i, po);
383
                                i++;
384
                        }
385
                        json.put("permissions", perma);
386
                }
387
                JSONArray taga = new JSONArray();
388
                i = 0;
389
                if (!tags.getText().equals(initialTagText)) {
390
                        String[] tagset = tags.getText().split(",");
391
                        for (String t : tagset) {
392
                                JSONString to = new JSONString(t);
393
                                taga.set(i, to);
394
                                i++;
395
                        }
396
                        json.put("tags", taga);
397
                }
398
                String jsonString = json.toString();
399
                if(jsonString.equals("{}")){
400
                        GWT.log("NO CHANGES", null);
401
                        return;
402
                }
403
                final String newFilenameFinal = newFilename;
404
                PostCommand cf = new PostCommand(file.getUri() + "?update=", jsonString, 200) {
405

    
406
                        @Override
407
                        public void onComplete() {
408
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/, newFilenameFinal);
409
                        }
410

    
411
                        @Override
412
                        public void onError(Throwable t) {
413
                                GWT.log("", t);
414
                                if (t instanceof RestException) {
415
                                        int statusCode = ((RestException) t).getHttpStatusCode();
416
                                        if (statusCode == 405)
417
                                                GSS.get().displayError("You don't have the necessary permissions");
418
                                        else if (statusCode == 404)
419
                                                GSS.get().displayError("User in permissions does not exist");
420
                                        else if (statusCode == 409)
421
                                                GSS.get().displayError("A file with the same name already exists");
422
                                        else if (statusCode == 413)
423
                                                GSS.get().displayError("Your quota has been exceeded");
424
                                        else
425
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
426
                                } else
427
                                        GSS.get().displayError("System error modifying file:" + t.getMessage());
428
                        }
429

    
430
                };
431
                DeferredCommand.addCommand(cf);
432

    
433
        }
434

    
435
        private void removeAllOldVersions() {
436
                JSONObject json = new JSONObject();
437
                json.put("versioned", JSONBoolean.getInstance(false));
438
                GWT.log(json.toString(), null);
439
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
440

    
441
                        @Override
442
                        public void onComplete() {
443
                                toggleVersioned(true);
444
                        }
445

    
446
                        @Override
447
                        public void onError(Throwable t) {
448
                                GWT.log("", t);
449
                                if (t instanceof RestException) {
450
                                        int statusCode = ((RestException) t).getHttpStatusCode();
451
                                        if (statusCode == 405)
452
                                                GSS.get().displayError("You don't have the necessary permissions");
453
                                        else if (statusCode == 404)
454
                                                GSS.get().displayError("User in permissions does not exist");
455
                                        else if (statusCode == 409)
456
                                                GSS.get().displayError("A folder with the same name already exists");
457
                                        else if (statusCode == 413)
458
                                                GSS.get().displayError("Your quota has been exceeded");
459
                                        else
460
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
461
                                } else
462
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
463
                        }
464
                };
465
                DeferredCommand.addCommand(cf);
466
        }
467

    
468
        private void toggleVersioned(boolean versionedValue) {
469
                JSONObject json = new JSONObject();
470
                json.put("versioned", JSONBoolean.getInstance(versionedValue));
471
                GWT.log(json.toString(), null);
472
                PostCommand cf = new PostCommand(file.getUri() + "?update=", json.toString(), 200) {
473

    
474
                        @Override
475
                        public void onComplete() {
476
                                GSS.get().getFileList().updateFileCache(true, false /* do not clear selected file*/);
477
                        }
478

    
479
                        @Override
480
                        public void onError(Throwable t) {
481
                                GWT.log("", t);
482
                                if (t instanceof RestException) {
483
                                        int statusCode = ((RestException) t).getHttpStatusCode();
484
                                        if (statusCode == 405)
485
                                                GSS.get().displayError("You don't have the necessary permissions");
486
                                        else if (statusCode == 404)
487
                                                GSS.get().displayError("User in permissions does not exist");
488
                                        else if (statusCode == 409)
489
                                                GSS.get().displayError("A folder with the same name already exists");
490
                                        else if (statusCode == 413)
491
                                                GSS.get().displayError("Your quota has been exceeded");
492
                                        else
493
                                                GSS.get().displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
494
                                } else
495
                                        GSS.get().displayError("System error moifying file:" + t.getMessage());
496
                        }
497
                };
498
                DeferredCommand.addCommand(cf);
499
        }
500
        private boolean hasOwner(String _userName) {
501
                if (GSS.get().findUserFullName(_userName) == null)
502
                        return false;
503
                return true;
504
        }
505

    
506
}