Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPermissionsDialog.java @ 2efd2a81

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 add = new Button("Add Group", new ClickHandler() {
127
            @Override
128
            public void onClick(ClickEvent event) {
129
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
130
                dlg.center();
131
            }
132
        });
133
        add.addStyleName("button");
134
        permButtons.add(add);
135
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
136

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

    
150
        inner.add(permPanel);
151

    
152
        outer.add(inner);
153

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

    
168
        setWidget(outer);
169
        }
170

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

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

    
190

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

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

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

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

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

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

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

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

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