Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPermissionsDialog.java @ 57ab6e0b

History | View | Annotate | Download (13.7 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.foldertree.File;
38
import gr.grnet.pithos.web.client.foldertree.Folder;
39
import gr.grnet.pithos.web.client.foldertree.Resource;
40
import gr.grnet.pithos.web.client.rest.PostRequest;
41
import gr.grnet.pithos.web.client.rest.PutRequest;
42
import gr.grnet.pithos.web.client.rest.RestException;
43

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

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

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

    
71
    protected Pithos app;
72

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

    
78
        protected PermissionsList permList;
79

    
80
        final Folder folder;
81

    
82
        final VerticalPanel inner;
83

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

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

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

    
106
                folder = selected;
107

    
108
                setText("Folder permissions");
109

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

    
117

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

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

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

    
137
        Button add = new Button("Add Group", new ClickHandler() {
138
            @Override
139
            public void onClick(ClickEvent event) {
140
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
141
                dlg.center();
142
            }
143
        });
144
        add.addStyleName("button");
145
        permButtons.add(add);
146
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
147

    
148
        permButtons.setSpacing(8);
149
        permPanel.add(permButtons);
150

    
151
        inner.add(permPanel);
152

    
153
        outer.add(inner);
154

    
155
                // Create the 'Create/Update' button, along with a listener that hides the dialog
156
                // when the button is clicked and quits the application.
157
                String okLabel = "Update";
158
                final Button ok = new Button(okLabel, new ClickHandler() {
159
                        @Override
160
                        public void onClick(ClickEvent event) {
161
                                updateFolder();
162
                                closeDialog();
163
                        }
164
                });
165
                ok.addStyleName("button");
166
                outer.add(ok);
167
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
168

    
169
        setWidget(outer);
170
        }
171

    
172
        @Override
173
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
174
                super.onPreviewNativeEvent(preview);
175

    
176
                NativeEvent evt = preview.getNativeEvent();
177
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
178
                        // Use the popup's key preview hooks to close the dialog when either
179
                        // enter or escape is pressed.
180
                        switch (evt.getKeyCode()) {
181
                                case KeyCodes.KEY_ENTER:
182
                                        updateFolder();
183
                    closeDialog();
184
                                        break;
185
                                case KeyCodes.KEY_ESCAPE:
186
                                        closeDialog();
187
                                        break;
188
                        }
189
        }
190

    
191

    
192
        /**
193
         * Enables IE selection prevention and hides the dialog
194
         * (we disable the prevention on creation of the dialog)
195
         */
196
        public void closeDialog() {
197
                Pithos.preventIESelection();
198
                hide();
199
        }
200

    
201
        void updateFolder() {
202
        final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
203
        final String newName = folderName.getText().trim();
204
        if (!folder.isContainer() && !folder.getName().equals(newName)) {
205
            final String path = folder.getParent().getUri() + "/" + newName;
206
            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getParent().getOwner(), path) {
207
                @Override
208
                public void onSuccess(Resource result) {
209
                    Iterator<File> iter = folder.getFiles().iterator();
210
                    app.copyFiles(iter, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
211
                        @Override
212
                        public void execute() {
213
                            Iterator<Folder> iterf = folder.getSubfolders().iterator();
214
                            app.copySubfolders(iterf, folder.getParent().getOwner(), folder.getParent().getUri() + "/" + newName, new Command() {
215
                                @Override
216
                                public void execute() {
217
                                    app.deleteFolder(folder, null);
218
                                    updateMetadata(path + "?update=", perms);
219
                                }
220
                            });
221
                        }
222
                    });
223
                }
224

    
225
                @Override
226
                public void onError(Throwable t) {
227
                    GWT.log("", t);
228
                                        app.setError(t);
229
                    if(t instanceof RestException){
230
                        app.displayError("Unable to update folder: " + ((RestException) t).getHttpStatusText());
231
                    }
232
                    else
233
                        app.displayError("System error modifying folder: " + t.getMessage());
234
                }
235

    
236
                                @Override
237
                                protected void onUnauthorized(Response response) {
238
                                        app.sessionExpired();
239
                                }
240
            };
241
            newFolder.setHeader("X-Auth-Token", app.getToken());
242
            newFolder.setHeader("Content-Type", "application/folder");
243
            newFolder.setHeader("Accept", "*/*");
244
            newFolder.setHeader("Content-Length", "0");
245
            Scheduler.get().scheduleDeferred(newFolder);
246
        }
247
        else
248
            updateMetadata(folder.getUri() + "?update=", perms);
249
        }
250

    
251
        protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) {
252
        if (newPermissions != null) {
253
            PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwner(), path) {
254
                @Override
255
                public void onSuccess(Resource result) {
256
                    app.updateFolder(folder.getParent(), false, new Command() {
257
                                                
258
                                                @Override
259
                                                public void execute() {
260
                                                        app.updateMySharedRoot();
261
                                                }
262
                                        });
263
                }
264

    
265
                @Override
266
                public void onError(Throwable t) {
267
                    GWT.log("", t);
268
                                        app.setError(t);
269
                    if (t instanceof RestException) {
270
                            if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder
271
                            final String path1 = folder.getUri();
272
                            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path1) {
273
                                @Override
274
                                public void onSuccess(Resource result) {
275
                                        updateMetadata(path, newPermissions);
276
                                }
277

    
278
                                @Override
279
                                public void onError(Throwable _t) {
280
                                    GWT.log("", _t);
281
                                                            app.setError(_t);
282
                                    if(_t instanceof RestException){
283
                                        app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());
284
                                    }
285
                                    else
286
                                        app.displayError("System error modifying folder: " + _t.getMessage());
287
                                }
288

    
289
                                                @Override
290
                                                protected void onUnauthorized(Response response) {
291
                                                        app.sessionExpired();
292
                                                }
293
                            };
294
                            newFolder.setHeader("X-Auth-Token", app.getToken());
295
                            newFolder.setHeader("Content-Type", "application/folder");
296
                            newFolder.setHeader("Accept", "*/*");
297
                            newFolder.setHeader("Content-Length", "0");
298
                            Scheduler.get().scheduleDeferred(newFolder);
299
                            }
300
                            else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) {
301
                                    app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");
302
                            }
303
                            else
304
                                    app.displayError("Εrror modifying folder: " + t.getMessage());
305
                    }
306
                    else
307
                            app.displayError("System error modifying folder: " + t.getMessage());
308
                }
309

    
310
                                @Override
311
                                protected void onUnauthorized(Response response) {
312
                                        app.sessionExpired();
313
                                }
314
            };
315
            updateFolder.setHeader("X-Auth-Token", app.getToken());
316
            String readPermHeader = "read=";
317
            String writePermHeader = "write=";
318
            for (String u : newPermissions.keySet()) {
319
                Boolean[] p = newPermissions.get(u);
320
                if (p[0] != null && p[0])
321
                    readPermHeader += u + ",";
322
                if (p[1] != null && p[1])
323
                    writePermHeader += u + ",";
324
            }
325
            if (readPermHeader.endsWith("="))
326
                readPermHeader = "";
327
            else if (readPermHeader.endsWith(","))
328
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
329
            if (writePermHeader.endsWith("="))
330
                writePermHeader = "";
331
            else if (writePermHeader.endsWith(","))
332
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
333
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
334
            if (permHeader.length() == 0)
335
                permHeader = "~";
336
            else
337
                    permHeader = URL.encodePathSegment(permHeader);
338
            updateFolder.setHeader("X-Object-Sharing", permHeader);
339
            Scheduler.get().scheduleDeferred(updateFolder);
340
        }
341
        else
342
            app.updateFolder(folder.getParent(), false, new Command() {
343
                                
344
                                @Override
345
                                public void execute() {
346
                                        app.updateMySharedRoot();
347
                                }
348
                        });
349
    }
350
}