Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / commands / AddUserCommand.java @ d94eb987

History | View | Annotate | Download (3.7 kB)

1 cd1a56d8 Christos Stathis
/*
2 cd1a56d8 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 cd1a56d8 Christos Stathis
 *
4 cd1a56d8 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 cd1a56d8 Christos Stathis
 * without modification, are permitted provided that the following
6 cd1a56d8 Christos Stathis
 * conditions are met:
7 cd1a56d8 Christos Stathis
 *
8 cd1a56d8 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 cd1a56d8 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 cd1a56d8 Christos Stathis
 *      disclaimer.
11 cd1a56d8 Christos Stathis
 *
12 cd1a56d8 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 cd1a56d8 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 cd1a56d8 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 cd1a56d8 Christos Stathis
 *      provided with the distribution.
16 cd1a56d8 Christos Stathis
 *
17 cd1a56d8 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 cd1a56d8 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 cd1a56d8 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 cd1a56d8 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 cd1a56d8 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 cd1a56d8 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 cd1a56d8 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 cd1a56d8 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 cd1a56d8 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 cd1a56d8 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 cd1a56d8 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 cd1a56d8 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 cd1a56d8 Christos Stathis
 *
30 cd1a56d8 Christos Stathis
 * The views and conclusions contained in the software and
31 cd1a56d8 Christos Stathis
 * documentation are those of the authors and should not be
32 cd1a56d8 Christos Stathis
 * interpreted as representing official policies, either expressed
33 cd1a56d8 Christos Stathis
 * or implied, of GRNET S.A.
34 cd1a56d8 Christos Stathis
 */
35 cd1a56d8 Christos Stathis
package gr.grnet.pithos.web.client.commands;
36 cd1a56d8 Christos Stathis
37 cd1a56d8 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
38 cd1a56d8 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
39 cd1a56d8 Christos Stathis
import gr.grnet.pithos.web.client.grouptree.Group;
40 cd1a56d8 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostRequest;
41 cd1a56d8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
42 cd1a56d8 Christos Stathis
43 cd1a56d8 Christos Stathis
import com.google.gwt.core.client.GWT;
44 cd1a56d8 Christos Stathis
import com.google.gwt.core.client.Scheduler;
45 045234b6 Christos Stathis
import com.google.gwt.http.client.Response;
46 cd1a56d8 Christos Stathis
import com.google.gwt.user.client.Command;
47 cd1a56d8 Christos Stathis
import com.google.gwt.user.client.Window;
48 cd1a56d8 Christos Stathis
import com.google.gwt.user.client.ui.PopupPanel;
49 cd1a56d8 Christos Stathis
50 cd1a56d8 Christos Stathis
/**
51 cd1a56d8 Christos Stathis
 * Display the 'new folder' dialog for creating a new folder.
52 cd1a56d8 Christos Stathis
 *
53 cd1a56d8 Christos Stathis
 */
54 cd1a56d8 Christos Stathis
public class AddUserCommand implements Command {
55 cd1a56d8 Christos Stathis
        private PopupPanel containerPanel;
56 cd1a56d8 Christos Stathis
57 cd1a56d8 Christos Stathis
    Group group;
58 cd1a56d8 Christos Stathis
59 cd1a56d8 Christos Stathis
    Pithos app;
60 cd1a56d8 Christos Stathis
61 cd1a56d8 Christos Stathis
        /**
62 cd1a56d8 Christos Stathis
         * @param aContainerPanel
63 cd1a56d8 Christos Stathis
         */
64 cd1a56d8 Christos Stathis
        public AddUserCommand(Pithos _app, PopupPanel aContainerPanel, Group _group){
65 cd1a56d8 Christos Stathis
        app = _app;
66 cd1a56d8 Christos Stathis
                containerPanel = aContainerPanel;
67 cd1a56d8 Christos Stathis
            group = _group;
68 cd1a56d8 Christos Stathis
        }
69 cd1a56d8 Christos Stathis
70 cd1a56d8 Christos Stathis
        @Override
71 cd1a56d8 Christos Stathis
        public void execute() {
72 cd1a56d8 Christos Stathis
        if (containerPanel != null)
73 cd1a56d8 Christos Stathis
                    containerPanel.hide();
74 cd1a56d8 Christos Stathis
        String username = Window.prompt("Enter username:", "");
75 cd1a56d8 Christos Stathis
        if (username != null && username.length() > 0) {
76 cd1a56d8 Christos Stathis
                group.addMember(username);
77 cd1a56d8 Christos Stathis
                String path = "?update=";
78 cd1a56d8 Christos Stathis
                PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUsername(), path) {
79 cd1a56d8 Christos Stathis
                                
80 cd1a56d8 Christos Stathis
                                @Override
81 0ac99e41 Christos Stathis
                                public void onSuccess(@SuppressWarnings("unused") Resource result) {
82 cd1a56d8 Christos Stathis
                                        app.updateGroupNode(group);
83 cd1a56d8 Christos Stathis
                                }
84 cd1a56d8 Christos Stathis
                                
85 cd1a56d8 Christos Stathis
                                @Override
86 cd1a56d8 Christos Stathis
                                public void onError(Throwable t) {
87 cd1a56d8 Christos Stathis
                                        GWT.log("", t);
88 cd1a56d8 Christos Stathis
                                        if (t instanceof RestException) {
89 cd1a56d8 Christos Stathis
                                                app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
90 cd1a56d8 Christos Stathis
                                        }
91 cd1a56d8 Christos Stathis
                                        else
92 cd1a56d8 Christos Stathis
                                                app.displayError("System error updating group:" + t.getMessage());
93 cd1a56d8 Christos Stathis
                                }
94 045234b6 Christos Stathis
95 045234b6 Christos Stathis
                                @Override
96 045234b6 Christos Stathis
                                protected void onUnauthorized(Response response) {
97 045234b6 Christos Stathis
                                        app.sessionExpired();
98 045234b6 Christos Stathis
                                }
99 cd1a56d8 Christos Stathis
                        };
100 cd1a56d8 Christos Stathis
                        updateGroup.setHeader("X-Auth-Token", app.getToken());
101 cd1a56d8 Christos Stathis
                        String groupMembers = "";
102 cd1a56d8 Christos Stathis
                        for (String u : group.getMembers())
103 cd1a56d8 Christos Stathis
                                groupMembers += (u + ",");
104 cd1a56d8 Christos Stathis
                        updateGroup.setHeader("X-Account-Group-" + group.getName(), groupMembers);
105 cd1a56d8 Christos Stathis
                        Scheduler.get().scheduleDeferred(updateGroup);
106 cd1a56d8 Christos Stathis
        }
107 cd1a56d8 Christos Stathis
        }
108 cd1a56d8 Christos Stathis
}