Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FolderPermissionsDialog.java @ 6b7d024f

History | View | Annotate | Download (14.2 kB)

1
/*
2
 * Copyright 2011 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(@SuppressWarnings("unused") 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
        boolean permsReadonly = folder.getInheritedPermissionsFrom() != null || folder.existChildrenPermissions();
123
        permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner(), permsReadonly);
124
        permPanel.add(permList);
125

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

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

    
153
        inner.add(permPanel);
154

    
155
        outer.add(inner);
156

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

    
171
        setWidget(outer);
172
        }
173

    
174
        @Override
175
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
176
                super.onPreviewNativeEvent(preview);
177

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

    
193

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

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

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

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

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

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

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

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

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