Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPermissionsDialog.java @ 948a9b34

History | View | Annotate | Download (14.3 kB)

1
/*
2
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
38
import gr.grnet.pithos.web.client.foldertree.File;
39
import gr.grnet.pithos.web.client.foldertree.Folder;
40
import gr.grnet.pithos.web.client.foldertree.Resource;
41
import gr.grnet.pithos.web.client.rest.PostRequest;
42
import gr.grnet.pithos.web.client.rest.PutRequest;
43
import gr.grnet.pithos.web.client.rest.RestException;
44

    
45
import java.util.Iterator;
46
import java.util.Map;
47

    
48
import com.google.gwt.core.client.GWT;
49
import com.google.gwt.core.client.Scheduler;
50
import com.google.gwt.dom.client.NativeEvent;
51
import com.google.gwt.event.dom.client.ClickEvent;
52
import com.google.gwt.event.dom.client.ClickHandler;
53
import com.google.gwt.event.dom.client.KeyCodes;
54
import com.google.gwt.event.dom.client.KeyDownEvent;
55
import com.google.gwt.http.client.Response;
56
import com.google.gwt.http.client.URL;
57
import com.google.gwt.user.client.Command;
58
import com.google.gwt.user.client.Event.NativePreviewEvent;
59
import com.google.gwt.user.client.ui.Anchor;
60
import com.google.gwt.user.client.ui.Button;
61
import com.google.gwt.user.client.ui.DialogBox;
62
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
63
import com.google.gwt.user.client.ui.HorizontalPanel;
64
import com.google.gwt.user.client.ui.Label;
65
import com.google.gwt.user.client.ui.VerticalPanel;
66

    
67
/**
68
 * The 'Folder properties' dialog box implementation.
69
 */
70
public class FolderPermissionsDialog extends DialogBox {
71

    
72
    protected Pithos app;
73

    
74
        /**
75
         * The widget that holds the folderName of the folder.
76
         */
77
        Label folderName = new Label();
78

    
79
        protected PermissionsList permList;
80

    
81
        final Folder folder;
82

    
83
        final VerticalPanel inner;
84

    
85
        /**
86
         * The widget's constructor.
87
         */
88
        public FolderPermissionsDialog(final Pithos app, Folder selected) {
89
        this.app = app;
90
                Anchor close = new Anchor("close");
91
                close.addStyleName("close");
92
                close.addClickHandler(new ClickHandler() {
93
                        
94
                        @Override
95
                        public void onClick(ClickEvent event) {
96
                                hide();
97
                        }
98
                });
99

    
100
                setAnimationEnabled(true);
101
                setGlassEnabled(true);
102
                setStyleName("pithos-DialogBox");
103

    
104
                // Enable IE selection for the dialog (must disable it upon closing it)
105
                Pithos.enableIESelection();
106

    
107
                folder = selected;
108

    
109
                setText("Folder permissions");
110

    
111
                // Outer contains inner and buttons
112
                VerticalPanel outer = new VerticalPanel();
113
                outer.add(close);
114
                // Inner contains generalPanel and permPanel
115
                inner = new VerticalPanel();
116
                inner.addStyleName("inner");
117

    
118

    
119
                folderName.setText(folder.getName());
120

    
121
        VerticalPanel permPanel = new VerticalPanel();
122
        FilePermissionsDialog.Images images = GWT.create(FilePermissionsDialog.Images.class);
123
        permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner(), false, null);
124
        permPanel.add(permList);
125

    
126
        HorizontalPanel permButtons = new HorizontalPanel();
127
        Button addUser = new Button("Add User", new ClickHandler() {
128
            @Override
129
            public void onClick(ClickEvent event) {
130
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
131
                dlg.center();
132
            }
133
        });
134
        addUser.addStyleName("button");
135
        permButtons.add(addUser);
136
        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
137

    
138
        Button add = new Button("Add Group", new ClickHandler() {
139
            @Override
140
            public void onClick(ClickEvent event) {
141
                    if (app.getAccount().getGroups().isEmpty()) {
142
                    new GroupCreateDialog(app, new Command() {
143
                                                
144
                                                @Override
145
                                                public void execute() {
146
                                            if (app.getAccount().getGroups().isEmpty())
147
                                                    return;
148
                                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
149
                                        dlg.center();
150
                                                }
151
                                        }).center();
152
                    }
153
                    else {
154
                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
155
                        dlg.center();
156
                    }
157
            }
158
        });
159
        add.addStyleName("button");
160
        permButtons.add(add);
161
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
162

    
163
        permButtons.setSpacing(8);
164
        permPanel.add(permButtons);
165

    
166
        inner.add(permPanel);
167

    
168
        outer.add(inner);
169

    
170
                // Create the 'Create/Update' button, along with a listener that hides the dialog
171
                // when the button is clicked and quits the application.
172
                String okLabel = "Update";
173
                final Button ok = new Button(okLabel, new ClickHandler() {
174
                        @Override
175
                        public void onClick(ClickEvent event) {
176
                                updateFolder();
177
                                closeDialog();
178
                        }
179
                });
180
                ok.addStyleName("button");
181
                outer.add(ok);
182
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
183

    
184
        setWidget(outer);
185
        }
186

    
187
        @Override
188
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
189
                super.onPreviewNativeEvent(preview);
190

    
191
                NativeEvent evt = preview.getNativeEvent();
192
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
193
                        // Use the popup's key preview hooks to close the dialog when either
194
                        // enter or escape is pressed.
195
                        switch (evt.getKeyCode()) {
196
                                case KeyCodes.KEY_ENTER:
197
                                        updateFolder();
198
                    closeDialog();
199
                                        break;
200
                                case KeyCodes.KEY_ESCAPE:
201
                                        closeDialog();
202
                                        break;
203
                        }
204
        }
205

    
206

    
207
        /**
208
         * Enables IE selection prevention and hides the dialog
209
         * (we disable the prevention on creation of the dialog)
210
         */
211
        public void closeDialog() {
212
                Pithos.preventIESelection();
213
                hide();
214
        }
215

    
216
        void updateFolder() {
217
        final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
218
        final String newName = folderName.getText().trim();
219
        if (!folder.isContainer() && !folder.getName().equals(newName)) {
220
            final String path = folder.getParent().getUri() + "/" + newName;
221
            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getParent().getOwner(), path) {
222
                @Override
223
                public void onSuccess(Resource result) {
224
                    Iterator<File> iter = folder.getFiles().iterator();
225
                    app.copyFiles(iter, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
226
                        @Override
227
                        public void execute() {
228
                            Iterator<Folder> iterf = folder.getSubfolders().iterator();
229
                            app.copySubfolders(iterf, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
230
                                @Override
231
                                public void execute() {
232
                                    app.deleteFolder(folder, null);
233
                                    updateMetadata(path + "?update=", perms);
234
                                }
235
                            });
236
                        }
237
                    });
238
                }
239

    
240
                @Override
241
                public void onError(Throwable t) {
242
                    GWT.log("", t);
243
                                        app.setError(t);
244
                    if(t instanceof RestException){
245
                        app.displayError("Unable to update folder: " + ((RestException) t).getHttpStatusText());
246
                    }
247
                    else
248
                        app.displayError("System error modifying folder: " + t.getMessage());
249
                }
250

    
251
                                @Override
252
                                protected void onUnauthorized(Response response) {
253
                                        app.sessionExpired();
254
                                }
255
            };
256
            newFolder.setHeader("X-Auth-Token", app.getToken());
257
            newFolder.setHeader("Content-Type", "application/folder");
258
            newFolder.setHeader("Accept", "*/*");
259
            newFolder.setHeader("Content-Length", "0");
260
            Scheduler.get().scheduleDeferred(newFolder);
261
        }
262
        else
263
            updateMetadata(folder.getUri() + "?update=", perms);
264
        }
265

    
266
        protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) {
267
        if (newPermissions != null) {
268
            PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwner(), path) {
269
                @Override
270
                public void onSuccess(Resource result) {
271
                    app.updateFolder(folder.getParent(), false, new Command() {
272
                                                
273
                                                @Override
274
                                                public void execute() {
275
                                                        app.updateMySharedRoot();
276
                                                }
277
                                        }, true);
278
                }
279

    
280
                @Override
281
                public void onError(Throwable t) {
282
                    GWT.log("", t);
283
                                        app.setError(t);
284
                    if (t instanceof RestException) {
285
                            if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder
286
                            final String path1 = folder.getUri();
287
                            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path1) {
288
                                @Override
289
                                public void onSuccess(Resource result) {
290
                                        updateMetadata(path, newPermissions);
291
                                }
292

    
293
                                @Override
294
                                public void onError(Throwable _t) {
295
                                    GWT.log("", _t);
296
                                                            app.setError(_t);
297
                                    if(_t instanceof RestException){
298
                                        app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());
299
                                    }
300
                                    else
301
                                        app.displayError("System error modifying folder: " + _t.getMessage());
302
                                }
303

    
304
                                                @Override
305
                                                protected void onUnauthorized(Response response) {
306
                                                        app.sessionExpired();
307
                                                }
308
                            };
309
                            newFolder.setHeader("X-Auth-Token", app.getToken());
310
                            newFolder.setHeader("Content-Type", "application/folder");
311
                            newFolder.setHeader("Accept", "*/*");
312
                            newFolder.setHeader("Content-Length", "0");
313
                            Scheduler.get().scheduleDeferred(newFolder);
314
                            }
315
                            else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) {
316
                                    app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");
317
                            }
318
                            else
319
                                    app.displayError("Εrror modifying folder: " + t.getMessage());
320
                    }
321
                    else
322
                            app.displayError("System error modifying folder: " + t.getMessage());
323
                }
324

    
325
                                @Override
326
                                protected void onUnauthorized(Response response) {
327
                                        app.sessionExpired();
328
                                }
329
            };
330
            updateFolder.setHeader("X-Auth-Token", app.getToken());
331
            String readPermHeader = "read=";
332
            String writePermHeader = "write=";
333
            for (String u : newPermissions.keySet()) {
334
                Boolean[] p = newPermissions.get(u);
335
                if (p[0] != null && p[0])
336
                    readPermHeader += u + ",";
337
                if (p[1] != null && p[1])
338
                    writePermHeader += u + ",";
339
            }
340
            if (readPermHeader.endsWith("="))
341
                readPermHeader = "";
342
            else if (readPermHeader.endsWith(","))
343
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
344
            if (writePermHeader.endsWith("="))
345
                writePermHeader = "";
346
            else if (writePermHeader.endsWith(","))
347
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
348
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
349
            if (permHeader.length() == 0)
350
                permHeader = "~";
351
            else
352
                    permHeader = URL.encodePathSegment(permHeader);
353
            updateFolder.setHeader("X-Object-Sharing", permHeader);
354
            Scheduler.get().scheduleDeferred(updateFolder);
355
        }
356
        else
357
            app.updateFolder(folder.getParent(), false, new Command() {
358
                                
359
                                @Override
360
                                public void execute() {
361
                                        app.updateMySharedRoot();
362
                                }
363
                        }, true);
364
    }
365
}