Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.1 kB)

1 6b7d024f Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 6b7d024f Christos Stathis
 *
4 6b7d024f Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 6b7d024f Christos Stathis
 * without modification, are permitted provided that the following
6 6b7d024f Christos Stathis
 * conditions are met:
7 6b7d024f Christos Stathis
 *
8 6b7d024f Christos Stathis
 *   1. Redistributions of source code must retain the above
9 6b7d024f Christos Stathis
 *      copyright notice, this list of conditions and the following
10 6b7d024f Christos Stathis
 *      disclaimer.
11 6b7d024f Christos Stathis
 *
12 6b7d024f Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 6b7d024f Christos Stathis
 *      copyright notice, this list of conditions and the following
14 6b7d024f Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 6b7d024f Christos Stathis
 *      provided with the distribution.
16 6b7d024f Christos Stathis
 *
17 6b7d024f Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 6b7d024f Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 6b7d024f Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 6b7d024f Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 6b7d024f Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 6b7d024f Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 6b7d024f Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 6b7d024f Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 6b7d024f Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 6b7d024f Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 6b7d024f Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 6b7d024f Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 6b7d024f Christos Stathis
 *
30 6b7d024f Christos Stathis
 * The views and conclusions contained in the software and
31 6b7d024f Christos Stathis
 * documentation are those of the authors and should not be
32 6b7d024f Christos Stathis
 * interpreted as representing official policies, either expressed
33 6b7d024f Christos Stathis
 * or implied, of GRNET S.A.
34 6b7d024f Christos Stathis
 */
35 6b7d024f Christos Stathis
package gr.grnet.pithos.web.client;
36 6b7d024f Christos Stathis
37 5ec7b091 Christos Stathis
import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
38 6b7d024f Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
39 6b7d024f Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
40 6b7d024f Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
41 6b7d024f Christos Stathis
import gr.grnet.pithos.web.client.rest.PostRequest;
42 6b7d024f Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
43 6b7d024f Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
44 6b7d024f Christos Stathis
45 6b7d024f Christos Stathis
import java.util.Iterator;
46 6b7d024f Christos Stathis
import java.util.Map;
47 6b7d024f Christos Stathis
48 6b7d024f Christos Stathis
import com.google.gwt.core.client.GWT;
49 6b7d024f Christos Stathis
import com.google.gwt.core.client.Scheduler;
50 6b7d024f Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
51 6b7d024f Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
52 6b7d024f Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
53 6b7d024f Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
54 6b7d024f Christos Stathis
import com.google.gwt.event.dom.client.KeyDownEvent;
55 6b7d024f Christos Stathis
import com.google.gwt.http.client.Response;
56 6b7d024f Christos Stathis
import com.google.gwt.http.client.URL;
57 6b7d024f Christos Stathis
import com.google.gwt.user.client.Command;
58 6b7d024f Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
59 6b7d024f Christos Stathis
import com.google.gwt.user.client.ui.Anchor;
60 6b7d024f Christos Stathis
import com.google.gwt.user.client.ui.Button;
61 6b7d024f Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
62 6b7d024f Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
63 6b7d024f Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
64 6b7d024f Christos Stathis
import com.google.gwt.user.client.ui.Label;
65 6b7d024f Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
66 6b7d024f Christos Stathis
67 6b7d024f Christos Stathis
/**
68 6b7d024f Christos Stathis
 * The 'Folder properties' dialog box implementation.
69 6b7d024f Christos Stathis
 */
70 6b7d024f Christos Stathis
public class FolderPermissionsDialog extends DialogBox {
71 6b7d024f Christos Stathis
72 6b7d024f Christos Stathis
    protected Pithos app;
73 6b7d024f Christos Stathis
74 6b7d024f Christos Stathis
        /**
75 6b7d024f Christos Stathis
         * The widget that holds the folderName of the folder.
76 6b7d024f Christos Stathis
         */
77 6b7d024f Christos Stathis
        Label folderName = new Label();
78 6b7d024f Christos Stathis
79 6b7d024f Christos Stathis
        protected PermissionsList permList;
80 6b7d024f Christos Stathis
81 6b7d024f Christos Stathis
        final Folder folder;
82 6b7d024f Christos Stathis
83 6b7d024f Christos Stathis
        final VerticalPanel inner;
84 6b7d024f Christos Stathis
85 6b7d024f Christos Stathis
        /**
86 6b7d024f Christos Stathis
         * The widget's constructor.
87 6b7d024f Christos Stathis
         */
88 6b7d024f Christos Stathis
        public FolderPermissionsDialog(final Pithos app, Folder selected) {
89 6b7d024f Christos Stathis
        this.app = app;
90 948a9b34 Christos Stathis
                Anchor close = new Anchor("close");
91 6b7d024f Christos Stathis
                close.addStyleName("close");
92 6b7d024f Christos Stathis
                close.addClickHandler(new ClickHandler() {
93 6b7d024f Christos Stathis
                        
94 6b7d024f Christos Stathis
                        @Override
95 ebead1b5 Christos Stathis
                        public void onClick(ClickEvent event) {
96 6b7d024f Christos Stathis
                                hide();
97 6b7d024f Christos Stathis
                        }
98 6b7d024f Christos Stathis
                });
99 6b7d024f Christos Stathis
100 6b7d024f Christos Stathis
                setAnimationEnabled(true);
101 6b7d024f Christos Stathis
                setGlassEnabled(true);
102 6b7d024f Christos Stathis
                setStyleName("pithos-DialogBox");
103 6b7d024f Christos Stathis
104 6b7d024f Christos Stathis
                // Enable IE selection for the dialog (must disable it upon closing it)
105 6b7d024f Christos Stathis
                Pithos.enableIESelection();
106 6b7d024f Christos Stathis
107 6b7d024f Christos Stathis
                folder = selected;
108 6b7d024f Christos Stathis
109 6b7d024f Christos Stathis
                setText("Folder permissions");
110 6b7d024f Christos Stathis
111 6b7d024f Christos Stathis
                // Outer contains inner and buttons
112 6b7d024f Christos Stathis
                VerticalPanel outer = new VerticalPanel();
113 6b7d024f Christos Stathis
                outer.add(close);
114 6b7d024f Christos Stathis
                // Inner contains generalPanel and permPanel
115 6b7d024f Christos Stathis
                inner = new VerticalPanel();
116 6b7d024f Christos Stathis
                inner.addStyleName("inner");
117 6b7d024f Christos Stathis
118 6b7d024f Christos Stathis
119 6b7d024f Christos Stathis
                folderName.setText(folder.getName());
120 6b7d024f Christos Stathis
121 6b7d024f Christos Stathis
        VerticalPanel permPanel = new VerticalPanel();
122 6b7d024f Christos Stathis
        FilePermissionsDialog.Images images = GWT.create(FilePermissionsDialog.Images.class);
123 4d869bf1 Christos Stathis
        permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner(), false, null);
124 6b7d024f Christos Stathis
        permPanel.add(permList);
125 6b7d024f Christos Stathis
126 b3c3954b Christos Stathis
        HorizontalPanel permButtons = new HorizontalPanel();
127 57ab6e0b Christos Stathis
        Button addUser = new Button("Add User", new ClickHandler() {
128 b3c3954b Christos Stathis
            @Override
129 b3c3954b Christos Stathis
            public void onClick(ClickEvent event) {
130 57ab6e0b Christos Stathis
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
131 b3c3954b Christos Stathis
                dlg.center();
132 b3c3954b Christos Stathis
            }
133 b3c3954b Christos Stathis
        });
134 57ab6e0b Christos Stathis
        addUser.addStyleName("button");
135 57ab6e0b Christos Stathis
        permButtons.add(addUser);
136 57ab6e0b Christos Stathis
        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
137 b3c3954b Christos Stathis
138 57ab6e0b Christos Stathis
        Button add = new Button("Add Group", new ClickHandler() {
139 b3c3954b Christos Stathis
            @Override
140 b3c3954b Christos Stathis
            public void onClick(ClickEvent event) {
141 5ec7b091 Christos Stathis
                    if (app.getAccount().getGroups().isEmpty()) {
142 5ec7b091 Christos Stathis
                    new GroupCreateDialog(app, new Command() {
143 5ec7b091 Christos Stathis
                                                
144 5ec7b091 Christos Stathis
                                                @Override
145 5ec7b091 Christos Stathis
                                                public void execute() {
146 5ec7b091 Christos Stathis
                                            if (app.getAccount().getGroups().isEmpty())
147 5ec7b091 Christos Stathis
                                                    return;
148 5ec7b091 Christos Stathis
                                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
149 5ec7b091 Christos Stathis
                                        dlg.center();
150 5ec7b091 Christos Stathis
                                                }
151 5ec7b091 Christos Stathis
                                        }).center();
152 5ec7b091 Christos Stathis
                    }
153 5ec7b091 Christos Stathis
                    else {
154 5ec7b091 Christos Stathis
                        PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
155 5ec7b091 Christos Stathis
                        dlg.center();
156 5ec7b091 Christos Stathis
                    }
157 b3c3954b Christos Stathis
            }
158 b3c3954b Christos Stathis
        });
159 57ab6e0b Christos Stathis
        add.addStyleName("button");
160 57ab6e0b Christos Stathis
        permButtons.add(add);
161 57ab6e0b Christos Stathis
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
162 57ab6e0b Christos Stathis
163 b3c3954b Christos Stathis
        permButtons.setSpacing(8);
164 b3c3954b Christos Stathis
        permPanel.add(permButtons);
165 6b7d024f Christos Stathis
166 6b7d024f Christos Stathis
        inner.add(permPanel);
167 6b7d024f Christos Stathis
168 6b7d024f Christos Stathis
        outer.add(inner);
169 6b7d024f Christos Stathis
170 6b7d024f Christos Stathis
                // Create the 'Create/Update' button, along with a listener that hides the dialog
171 6b7d024f Christos Stathis
                // when the button is clicked and quits the application.
172 6b7d024f Christos Stathis
                String okLabel = "Update";
173 6b7d024f Christos Stathis
                final Button ok = new Button(okLabel, new ClickHandler() {
174 6b7d024f Christos Stathis
                        @Override
175 ebead1b5 Christos Stathis
                        public void onClick(ClickEvent event) {
176 6b7d024f Christos Stathis
                                updateFolder();
177 6b7d024f Christos Stathis
                                closeDialog();
178 6b7d024f Christos Stathis
                        }
179 6b7d024f Christos Stathis
                });
180 6b7d024f Christos Stathis
                ok.addStyleName("button");
181 6b7d024f Christos Stathis
                outer.add(ok);
182 6b7d024f Christos Stathis
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
183 6b7d024f Christos Stathis
184 6b7d024f Christos Stathis
        setWidget(outer);
185 6b7d024f Christos Stathis
        }
186 6b7d024f Christos Stathis
187 6b7d024f Christos Stathis
        @Override
188 6b7d024f Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
189 6b7d024f Christos Stathis
                super.onPreviewNativeEvent(preview);
190 6b7d024f Christos Stathis
191 6b7d024f Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
192 6b7d024f Christos Stathis
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
193 6b7d024f Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
194 6b7d024f Christos Stathis
                        // enter or escape is pressed.
195 6b7d024f Christos Stathis
                        switch (evt.getKeyCode()) {
196 6b7d024f Christos Stathis
                                case KeyCodes.KEY_ENTER:
197 6b7d024f Christos Stathis
                                        updateFolder();
198 6b7d024f Christos Stathis
                    closeDialog();
199 6b7d024f Christos Stathis
                                        break;
200 6b7d024f Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
201 6b7d024f Christos Stathis
                                        closeDialog();
202 6b7d024f Christos Stathis
                                        break;
203 6b7d024f Christos Stathis
                        }
204 6b7d024f Christos Stathis
        }
205 6b7d024f Christos Stathis
206 6b7d024f Christos Stathis
207 6b7d024f Christos Stathis
        /**
208 6b7d024f Christos Stathis
         * Enables IE selection prevention and hides the dialog
209 6b7d024f Christos Stathis
         * (we disable the prevention on creation of the dialog)
210 6b7d024f Christos Stathis
         */
211 6b7d024f Christos Stathis
        public void closeDialog() {
212 6b7d024f Christos Stathis
                Pithos.preventIESelection();
213 6b7d024f Christos Stathis
                hide();
214 6b7d024f Christos Stathis
        }
215 6b7d024f Christos Stathis
216 6b7d024f Christos Stathis
        void updateFolder() {
217 6b7d024f Christos Stathis
        final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
218 f6c9957e Christos Stathis
        updateMetadata(folder.getUri() + "?update=", perms);
219 6b7d024f Christos Stathis
        }
220 6b7d024f Christos Stathis
221 6b7d024f Christos Stathis
        protected void updateMetadata(final String path, final Map<String, Boolean[]> newPermissions) {
222 6b7d024f Christos Stathis
        if (newPermissions != null) {
223 6b7d024f Christos Stathis
            PostRequest updateFolder = new PostRequest(app.getApiPath(), folder.getOwner(), path) {
224 6b7d024f Christos Stathis
                @Override
225 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
226 6b7d024f Christos Stathis
                    app.updateFolder(folder.getParent(), false, new Command() {
227 6b7d024f Christos Stathis
                                                
228 6b7d024f Christos Stathis
                                                @Override
229 6b7d024f Christos Stathis
                                                public void execute() {
230 6b7d024f Christos Stathis
                                                        app.updateMySharedRoot();
231 6b7d024f Christos Stathis
                                                }
232 6acd4df3 Christos Stathis
                                        }, true);
233 6b7d024f Christos Stathis
                }
234 6b7d024f Christos Stathis
235 6b7d024f Christos Stathis
                @Override
236 6b7d024f Christos Stathis
                public void onError(Throwable t) {
237 6b7d024f Christos Stathis
                    GWT.log("", t);
238 6b7d024f Christos Stathis
                                        app.setError(t);
239 6b7d024f Christos Stathis
                    if (t instanceof RestException) {
240 6b7d024f Christos Stathis
                            if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) { //Probably a virtual folder
241 6b7d024f Christos Stathis
                            final String path1 = folder.getUri();
242 6b7d024f Christos Stathis
                            PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path1) {
243 6b7d024f Christos Stathis
                                @Override
244 ebead1b5 Christos Stathis
                                public void onSuccess(Resource result) {
245 6b7d024f Christos Stathis
                                        updateMetadata(path, newPermissions);
246 6b7d024f Christos Stathis
                                }
247 6b7d024f Christos Stathis
248 6b7d024f Christos Stathis
                                @Override
249 6b7d024f Christos Stathis
                                public void onError(Throwable _t) {
250 6b7d024f Christos Stathis
                                    GWT.log("", _t);
251 6b7d024f Christos Stathis
                                                            app.setError(_t);
252 6b7d024f Christos Stathis
                                    if(_t instanceof RestException){
253 6b7d024f Christos Stathis
                                        app.displayError("Unable to update folder: " + ((RestException) _t).getHttpStatusText());
254 6b7d024f Christos Stathis
                                    }
255 6b7d024f Christos Stathis
                                    else
256 6b7d024f Christos Stathis
                                        app.displayError("System error modifying folder: " + _t.getMessage());
257 6b7d024f Christos Stathis
                                }
258 6b7d024f Christos Stathis
259 6b7d024f Christos Stathis
                                                @Override
260 ebead1b5 Christos Stathis
                                                protected void onUnauthorized(Response response) {
261 6b7d024f Christos Stathis
                                                        app.sessionExpired();
262 6b7d024f Christos Stathis
                                                }
263 6b7d024f Christos Stathis
                            };
264 6b7d024f Christos Stathis
                            newFolder.setHeader("X-Auth-Token", app.getToken());
265 6b7d024f Christos Stathis
                            newFolder.setHeader("Content-Type", "application/folder");
266 6b7d024f Christos Stathis
                            newFolder.setHeader("Accept", "*/*");
267 6b7d024f Christos Stathis
                            newFolder.setHeader("Content-Length", "0");
268 6b7d024f Christos Stathis
                            Scheduler.get().scheduleDeferred(newFolder);
269 6b7d024f Christos Stathis
                            }
270 6b7d024f Christos Stathis
                            else if (((RestException) t).getHttpStatusCode() == Response.SC_CONFLICT) {
271 6b7d024f Christos Stathis
                                    app.displayError("Cannot set permissions. Probably subfolders or files already have permissions set");
272 6b7d024f Christos Stathis
                            }
273 6b7d024f Christos Stathis
                            else
274 15dce1cd Christos Stathis
                                    app.displayError("Εrror modifying folder: " + t.getMessage());
275 6b7d024f Christos Stathis
                    }
276 6b7d024f Christos Stathis
                    else
277 6b7d024f Christos Stathis
                            app.displayError("System error modifying folder: " + t.getMessage());
278 6b7d024f Christos Stathis
                }
279 6b7d024f Christos Stathis
280 6b7d024f Christos Stathis
                                @Override
281 ebead1b5 Christos Stathis
                                protected void onUnauthorized(Response response) {
282 6b7d024f Christos Stathis
                                        app.sessionExpired();
283 6b7d024f Christos Stathis
                                }
284 6b7d024f Christos Stathis
            };
285 6b7d024f Christos Stathis
            updateFolder.setHeader("X-Auth-Token", app.getToken());
286 6b7d024f Christos Stathis
            String readPermHeader = "read=";
287 6b7d024f Christos Stathis
            String writePermHeader = "write=";
288 6b7d024f Christos Stathis
            for (String u : newPermissions.keySet()) {
289 6b7d024f Christos Stathis
                Boolean[] p = newPermissions.get(u);
290 6b7d024f Christos Stathis
                if (p[0] != null && p[0])
291 6b7d024f Christos Stathis
                    readPermHeader += u + ",";
292 6b7d024f Christos Stathis
                if (p[1] != null && p[1])
293 6b7d024f Christos Stathis
                    writePermHeader += u + ",";
294 6b7d024f Christos Stathis
            }
295 6b7d024f Christos Stathis
            if (readPermHeader.endsWith("="))
296 6b7d024f Christos Stathis
                readPermHeader = "";
297 6b7d024f Christos Stathis
            else if (readPermHeader.endsWith(","))
298 6b7d024f Christos Stathis
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
299 6b7d024f Christos Stathis
            if (writePermHeader.endsWith("="))
300 6b7d024f Christos Stathis
                writePermHeader = "";
301 6b7d024f Christos Stathis
            else if (writePermHeader.endsWith(","))
302 6b7d024f Christos Stathis
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
303 6b7d024f Christos Stathis
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
304 6b7d024f Christos Stathis
            if (permHeader.length() == 0)
305 6b7d024f Christos Stathis
                permHeader = "~";
306 6b7d024f Christos Stathis
            else
307 6b7d024f Christos Stathis
                    permHeader = URL.encodePathSegment(permHeader);
308 6b7d024f Christos Stathis
            updateFolder.setHeader("X-Object-Sharing", permHeader);
309 6b7d024f Christos Stathis
            Scheduler.get().scheduleDeferred(updateFolder);
310 6b7d024f Christos Stathis
        }
311 6b7d024f Christos Stathis
        else
312 6b7d024f Christos Stathis
            app.updateFolder(folder.getParent(), false, new Command() {
313 6b7d024f Christos Stathis
                                
314 6b7d024f Christos Stathis
                                @Override
315 6b7d024f Christos Stathis
                                public void execute() {
316 6b7d024f Christos Stathis
                                        app.updateMySharedRoot();
317 6b7d024f Christos Stathis
                                }
318 6acd4df3 Christos Stathis
                        }, true);
319 6b7d024f Christos Stathis
    }
320 6b7d024f Christos Stathis
}