Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / AddUserDialog.java @ e6e9f6e6

History | View | Annotate | Download (8.2 kB)

1 53f8abed Christos Stathis
/*
2 e6e9f6e6 Christos KK Loverdos
 * Copyright 2011-2013 GRNET S.A. All rights reserved.
3 53f8abed Christos Stathis
 *
4 53f8abed Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 53f8abed Christos Stathis
 * without modification, are permitted provided that the following
6 53f8abed Christos Stathis
 * conditions are met:
7 53f8abed Christos Stathis
 *
8 53f8abed Christos Stathis
 *   1. Redistributions of source code must retain the above
9 53f8abed Christos Stathis
 *      copyright notice, this list of conditions and the following
10 53f8abed Christos Stathis
 *      disclaimer.
11 53f8abed Christos Stathis
 *
12 53f8abed Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 53f8abed Christos Stathis
 *      copyright notice, this list of conditions and the following
14 53f8abed Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 53f8abed Christos Stathis
 *      provided with the distribution.
16 53f8abed Christos Stathis
 *
17 53f8abed Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 53f8abed Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 53f8abed Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 53f8abed Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 53f8abed Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 53f8abed Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 53f8abed Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 53f8abed Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 53f8abed Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 53f8abed Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 53f8abed Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 53f8abed Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 53f8abed Christos Stathis
 *
30 53f8abed Christos Stathis
 * The views and conclusions contained in the software and
31 53f8abed Christos Stathis
 * documentation are those of the authors and should not be
32 53f8abed Christos Stathis
 * interpreted as representing official policies, either expressed
33 53f8abed Christos Stathis
 * or implied, of GRNET S.A.
34 53f8abed Christos Stathis
 */
35 53f8abed Christos Stathis
package gr.grnet.pithos.web.client;
36 53f8abed Christos Stathis
37 53f8abed Christos Stathis
import com.google.gwt.core.client.GWT;
38 53f8abed Christos Stathis
import com.google.gwt.core.client.Scheduler;
39 53f8abed Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
40 53f8abed Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
41 53f8abed Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
42 53f8abed Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
43 53f8abed Christos Stathis
import com.google.gwt.event.dom.client.KeyDownEvent;
44 53f8abed Christos Stathis
import com.google.gwt.http.client.Response;
45 53f8abed Christos Stathis
import com.google.gwt.http.client.URL;
46 53f8abed Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
47 cd2dd1fe Christos KK Loverdos
import com.google.gwt.user.client.ui.*;
48 c8f8690d Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
49 c8f8690d Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UserCatalogs;
50 cd2dd1fe Christos KK Loverdos
import gr.grnet.pithos.web.client.grouptree.Group;
51 cd2dd1fe Christos KK Loverdos
import gr.grnet.pithos.web.client.rest.PostRequest;
52 cd2dd1fe Christos KK Loverdos
import gr.grnet.pithos.web.client.rest.RestException;
53 53f8abed Christos Stathis
54 c8f8690d Christos KK Loverdos
import java.util.Arrays;
55 c8f8690d Christos KK Loverdos
56 53f8abed Christos Stathis
/**
57 53f8abed Christos Stathis
 * The 'Folder properties' dialog box implementation.
58 53f8abed Christos Stathis
 */
59 53f8abed Christos Stathis
public class AddUserDialog extends DialogBox {
60 53f8abed Christos Stathis
61 53f8abed Christos Stathis
    protected Pithos app;
62 53f8abed Christos Stathis
63 53f8abed Christos Stathis
    Group group;
64 cd2dd1fe Christos KK Loverdos
    /**
65 cd2dd1fe Christos KK Loverdos
     * The widget that holds the folderName of the folder.
66 cd2dd1fe Christos KK Loverdos
     */
67 c8f8690d Christos KK Loverdos
    TextBox userNameInput = new TextBox();
68 53f8abed Christos Stathis
69 cd2dd1fe Christos KK Loverdos
    final VerticalPanel inner;
70 53f8abed Christos Stathis
71 cd2dd1fe Christos KK Loverdos
    /**
72 cd2dd1fe Christos KK Loverdos
     * The widget's constructor.
73 cd2dd1fe Christos KK Loverdos
     */
74 cd2dd1fe Christos KK Loverdos
    public AddUserDialog(final Pithos app, Group _group) {
75 53f8abed Christos Stathis
        this.app = app;
76 53f8abed Christos Stathis
        this.group = _group;
77 cd2dd1fe Christos KK Loverdos
78 cd2dd1fe Christos KK Loverdos
        Anchor close = new Anchor("close");
79 cd2dd1fe Christos KK Loverdos
        close.addStyleName("close");
80 cd2dd1fe Christos KK Loverdos
        close.addClickHandler(new ClickHandler() {
81 cd2dd1fe Christos KK Loverdos
82 cd2dd1fe Christos KK Loverdos
            @Override
83 cd2dd1fe Christos KK Loverdos
            public void onClick(ClickEvent event) {
84 cd2dd1fe Christos KK Loverdos
                hide();
85 cd2dd1fe Christos KK Loverdos
            }
86 cd2dd1fe Christos KK Loverdos
        });
87 cd2dd1fe Christos KK Loverdos
88 cd2dd1fe Christos KK Loverdos
        setGlassEnabled(true);
89 cd2dd1fe Christos KK Loverdos
        setStyleName("pithos-DialogBox");
90 cd2dd1fe Christos KK Loverdos
91 cd2dd1fe Christos KK Loverdos
        // Enable IE selection for the dialog (must disable it upon closing it)
92 cd2dd1fe Christos KK Loverdos
        Pithos.enableIESelection();
93 cd2dd1fe Christos KK Loverdos
94 cd2dd1fe Christos KK Loverdos
        // Use this opportunity to set the dialog's caption.
95 cd2dd1fe Christos KK Loverdos
        setText("Add user");
96 cd2dd1fe Christos KK Loverdos
97 cd2dd1fe Christos KK Loverdos
        // Outer contains inner and buttons
98 cd2dd1fe Christos KK Loverdos
        VerticalPanel outer = new VerticalPanel();
99 cd2dd1fe Christos KK Loverdos
        outer.add(close);
100 cd2dd1fe Christos KK Loverdos
        // Inner contains generalPanel and permPanel
101 cd2dd1fe Christos KK Loverdos
        inner = new VerticalPanel();
102 cd2dd1fe Christos KK Loverdos
        inner.addStyleName("inner");
103 cd2dd1fe Christos KK Loverdos
104 cd2dd1fe Christos KK Loverdos
        VerticalPanel generalPanel = new VerticalPanel();
105 53f8abed Christos Stathis
        FlexTable generalTable = new FlexTable();
106 53f8abed Christos Stathis
        generalTable.setText(0, 0, "Username");
107 53f8abed Christos Stathis
108 c8f8690d Christos KK Loverdos
        generalTable.setWidget(0, 1, userNameInput);
109 53f8abed Christos Stathis
110 53f8abed Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
111 53f8abed Christos Stathis
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
112 53f8abed Christos Stathis
        generalTable.setCellSpacing(4);
113 53f8abed Christos Stathis
        generalPanel.add(generalTable);
114 53f8abed Christos Stathis
        inner.add(generalPanel);
115 53f8abed Christos Stathis
116 53f8abed Christos Stathis
        outer.add(inner);
117 53f8abed Christos Stathis
118 cd2dd1fe Christos KK Loverdos
        // Create the 'Create/Update' button, along with a listener that hides the dialog
119 cd2dd1fe Christos KK Loverdos
        // when the button is clicked and quits the application.
120 cd2dd1fe Christos KK Loverdos
        String okLabel = "Create";
121 cd2dd1fe Christos KK Loverdos
        final Button ok = new Button(okLabel, new ClickHandler() {
122 cd2dd1fe Christos KK Loverdos
            @Override
123 cd2dd1fe Christos KK Loverdos
            public void onClick(ClickEvent event) {
124 cd2dd1fe Christos KK Loverdos
                addUser();
125 cd2dd1fe Christos KK Loverdos
                closeDialog();
126 cd2dd1fe Christos KK Loverdos
            }
127 cd2dd1fe Christos KK Loverdos
        });
128 cd2dd1fe Christos KK Loverdos
        ok.addStyleName("button");
129 cd2dd1fe Christos KK Loverdos
        outer.add(ok);
130 53f8abed Christos Stathis
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
131 53f8abed Christos Stathis
132 53f8abed Christos Stathis
        setWidget(outer);
133 cd2dd1fe Christos KK Loverdos
    }
134 cd2dd1fe Christos KK Loverdos
135 cd2dd1fe Christos KK Loverdos
    @Override
136 cd2dd1fe Christos KK Loverdos
    public void center() {
137 cd2dd1fe Christos KK Loverdos
        super.center();
138 c8f8690d Christos KK Loverdos
        userNameInput.setFocus(true);
139 cd2dd1fe Christos KK Loverdos
    }
140 cd2dd1fe Christos KK Loverdos
141 cd2dd1fe Christos KK Loverdos
    @Override
142 cd2dd1fe Christos KK Loverdos
    protected void onPreviewNativeEvent(NativePreviewEvent preview) {
143 cd2dd1fe Christos KK Loverdos
        super.onPreviewNativeEvent(preview);
144 cd2dd1fe Christos KK Loverdos
145 cd2dd1fe Christos KK Loverdos
        NativeEvent evt = preview.getNativeEvent();
146 cd2dd1fe Christos KK Loverdos
        if(evt.getType().equals(KeyDownEvent.getType().getName()))
147 cd2dd1fe Christos KK Loverdos
        // Use the popup's key preview hooks to close the dialog when either
148 cd2dd1fe Christos KK Loverdos
        // enter or escape is pressed.
149 cd2dd1fe Christos KK Loverdos
        {
150 cd2dd1fe Christos KK Loverdos
            switch(evt.getKeyCode()) {
151 cd2dd1fe Christos KK Loverdos
                case KeyCodes.KEY_ENTER:
152 cd2dd1fe Christos KK Loverdos
                    addUser();
153 cd2dd1fe Christos KK Loverdos
                    closeDialog();
154 cd2dd1fe Christos KK Loverdos
                    break;
155 cd2dd1fe Christos KK Loverdos
                case KeyCodes.KEY_ESCAPE:
156 53f8abed Christos Stathis
                    closeDialog();
157 cd2dd1fe Christos KK Loverdos
                    break;
158 cd2dd1fe Christos KK Loverdos
            }
159 cd2dd1fe Christos KK Loverdos
        }
160 cd2dd1fe Christos KK Loverdos
    }
161 cd2dd1fe Christos KK Loverdos
162 cd2dd1fe Christos KK Loverdos
163 cd2dd1fe Christos KK Loverdos
    /**
164 cd2dd1fe Christos KK Loverdos
     * Enables IE selection prevention and hides the dialog
165 cd2dd1fe Christos KK Loverdos
     * (we disable the prevention on creation of the dialog)
166 cd2dd1fe Christos KK Loverdos
     */
167 cd2dd1fe Christos KK Loverdos
    public void closeDialog() {
168 cd2dd1fe Christos KK Loverdos
        Pithos.preventIESelection();
169 cd2dd1fe Christos KK Loverdos
        hide();
170 cd2dd1fe Christos KK Loverdos
    }
171 cd2dd1fe Christos KK Loverdos
172 0459d2ef Christos KK Loverdos
    private void doAddUserByName(final String userDisplayName) {
173 0459d2ef Christos KK Loverdos
        final String userID = app.getIDForUserDisplayName(userDisplayName);
174 0459d2ef Christos KK Loverdos
        group.addMemberID(userID);
175 dc8cc18a Christos KK Loverdos
        String path = "?update=";
176 dc8cc18a Christos KK Loverdos
        PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUserID(), path) {
177 cd2dd1fe Christos KK Loverdos
178 cd2dd1fe Christos KK Loverdos
            @Override
179 cd2dd1fe Christos KK Loverdos
            public void onSuccess(Resource result) {
180 cd2dd1fe Christos KK Loverdos
                app.updateGroupNode(group);
181 cd2dd1fe Christos KK Loverdos
            }
182 cd2dd1fe Christos KK Loverdos
183 cd2dd1fe Christos KK Loverdos
            @Override
184 cd2dd1fe Christos KK Loverdos
            public void onError(Throwable t) {
185 cd2dd1fe Christos KK Loverdos
                GWT.log("", t);
186 cd2dd1fe Christos KK Loverdos
                app.setError(t);
187 cd2dd1fe Christos KK Loverdos
                if(t instanceof RestException) {
188 cd2dd1fe Christos KK Loverdos
                    app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
189 cd2dd1fe Christos KK Loverdos
                }
190 cd2dd1fe Christos KK Loverdos
                else {
191 cd2dd1fe Christos KK Loverdos
                    app.displayError("System error updating group:" + t.getMessage());
192 cd2dd1fe Christos KK Loverdos
                }
193 cd2dd1fe Christos KK Loverdos
            }
194 cd2dd1fe Christos KK Loverdos
195 cd2dd1fe Christos KK Loverdos
            @Override
196 cd2dd1fe Christos KK Loverdos
            protected void onUnauthorized(Response response) {
197 cd2dd1fe Christos KK Loverdos
                app.sessionExpired();
198 cd2dd1fe Christos KK Loverdos
            }
199 cd2dd1fe Christos KK Loverdos
        };
200 cd2dd1fe Christos KK Loverdos
        updateGroup.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
201 dc8cc18a Christos KK Loverdos
        String groupMembers = "";
202 0459d2ef Christos KK Loverdos
        for(String u : group.getMemberIDs()) {
203 dc8cc18a Christos KK Loverdos
            groupMembers += (URL.encodePathSegment(u) + ",");
204 dc8cc18a Christos KK Loverdos
        }
205 cd2dd1fe Christos KK Loverdos
        updateGroup.setHeader(Const.X_ACCOUNT_GROUP_ + URL.encodePathSegment(group.getName()), groupMembers);
206 cd2dd1fe Christos KK Loverdos
        Scheduler.get().scheduleDeferred(updateGroup);
207 cd2dd1fe Christos KK Loverdos
    }
208 c8f8690d Christos KK Loverdos
    /**
209 c8f8690d Christos KK Loverdos
     * Generate an RPC request to create a new folder.
210 c8f8690d Christos KK Loverdos
     */
211 c8f8690d Christos KK Loverdos
    void addUser() {
212 c8f8690d Christos KK Loverdos
        final String userDisplayName = userNameInput.getText().trim();
213 c8f8690d Christos KK Loverdos
        if(userDisplayName.length() == 0) {
214 c8f8690d Christos KK Loverdos
            return;
215 c8f8690d Christos KK Loverdos
        }
216 c8f8690d Christos KK Loverdos
        if(!Const.EMAIL_REGEX.test(userDisplayName)) {
217 c8f8690d Christos KK Loverdos
            app.displayWarning("Username must be a valid email address");
218 c8f8690d Christos KK Loverdos
            return;
219 c8f8690d Christos KK Loverdos
        }
220 c8f8690d Christos KK Loverdos
221 c8f8690d Christos KK Loverdos
        if(app.hasIDForUserDisplayName(userDisplayName)) {
222 0459d2ef Christos KK Loverdos
            doAddUserByName(userDisplayName);
223 c8f8690d Christos KK Loverdos
        }
224 c8f8690d Christos KK Loverdos
        else {
225 c8f8690d Christos KK Loverdos
            new UpdateUserCatalogs(app, null, Arrays.asList(userDisplayName)) {
226 c8f8690d Christos KK Loverdos
                @Override
227 c8f8690d Christos KK Loverdos
                public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
228 0459d2ef Christos KK Loverdos
                    doAddUserByName(userDisplayName);
229 c8f8690d Christos KK Loverdos
                }
230 c8f8690d Christos KK Loverdos
            }.scheduleDeferred();
231 c8f8690d Christos KK Loverdos
        }
232 c8f8690d Christos KK Loverdos
233 c8f8690d Christos KK Loverdos
234 c8f8690d Christos KK Loverdos
    }
235 53f8abed Christos Stathis
}