Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.6 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 com.google.gwt.core.client.GWT;
38
import com.google.gwt.core.client.Scheduler;
39
import com.google.gwt.dom.client.NativeEvent;
40
import com.google.gwt.event.dom.client.ClickEvent;
41
import com.google.gwt.event.dom.client.ClickHandler;
42
import com.google.gwt.event.dom.client.KeyCodes;
43
import com.google.gwt.event.dom.client.KeyDownEvent;
44
import com.google.gwt.http.client.Response;
45
import com.google.gwt.http.client.URL;
46
import com.google.gwt.user.client.Event.NativePreviewEvent;
47
import com.google.gwt.user.client.ui.*;
48
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
49
import gr.grnet.pithos.web.client.catalog.UserCatalogs;
50
import gr.grnet.pithos.web.client.grouptree.Group;
51
import gr.grnet.pithos.web.client.grouptree.User;
52
import gr.grnet.pithos.web.client.rest.PostRequest;
53
import gr.grnet.pithos.web.client.rest.RestException;
54

    
55
/**
56
 * The 'Folder properties' dialog box implementation.
57
 */
58
public class AddUserDialog extends DialogBox {
59

    
60
    protected Pithos app;
61

    
62
    Group group;
63
    /**
64
     * The widget that holds the folderName of the folder.
65
     */
66
    TextBox userDisplayNameTextBox = new TextBox();
67

    
68
    final VerticalPanel inner;
69

    
70
    /**
71
     * The widget's constructor.
72
     */
73
    public AddUserDialog(final Pithos app, Group _group) {
74
        this.app = app;
75
        this.group = _group;
76

    
77
        Anchor close = new Anchor("close");
78
        close.addStyleName("close");
79
        close.addClickHandler(new ClickHandler() {
80

    
81
            @Override
82
            public void onClick(ClickEvent event) {
83
                hide();
84
            }
85
        });
86

    
87
        setGlassEnabled(true);
88
        setStyleName("pithos-DialogBox");
89

    
90
        // Enable IE selection for the dialog (must disable it upon closing it)
91
        Pithos.enableIESelection();
92

    
93
        // Use this opportunity to set the dialog's caption.
94
        setText("Add user");
95

    
96
        // Outer contains inner and buttons
97
        VerticalPanel outer = new VerticalPanel();
98
        outer.add(close);
99
        // Inner contains generalPanel and permPanel
100
        inner = new VerticalPanel();
101
        inner.addStyleName("inner");
102

    
103
        VerticalPanel generalPanel = new VerticalPanel();
104
        FlexTable generalTable = new FlexTable();
105
        generalTable.setText(0, 0, "Username");
106

    
107
        generalTable.setWidget(0, 1, userDisplayNameTextBox);
108

    
109
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
110
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
111
        generalTable.setCellSpacing(4);
112
        generalPanel.add(generalTable);
113
        inner.add(generalPanel);
114

    
115
        outer.add(inner);
116

    
117
        // Create the 'Create/Update' button, along with a listener that hides the dialog
118
        // when the button is clicked and quits the application.
119
        String okLabel = "Create";
120
        final Button ok = new Button(okLabel, new ClickHandler() {
121
            @Override
122
            public void onClick(ClickEvent event) {
123
                addUser();
124
                closeDialog();
125
            }
126
        });
127
        ok.addStyleName("button");
128
        outer.add(ok);
129
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
130

    
131
        setWidget(outer);
132
    }
133

    
134
    @Override
135
    public void center() {
136
        super.center();
137
        userDisplayNameTextBox.setFocus(true);
138
    }
139

    
140
    @Override
141
    protected void onPreviewNativeEvent(NativePreviewEvent preview) {
142
        super.onPreviewNativeEvent(preview);
143

    
144
        NativeEvent evt = preview.getNativeEvent();
145
        if(evt.getType().equals(KeyDownEvent.getType().getName()))
146
        // Use the popup's key preview hooks to close the dialog when either
147
        // enter or escape is pressed.
148
        {
149
            switch(evt.getKeyCode()) {
150
                case KeyCodes.KEY_ENTER:
151
                    addUser();
152
                    closeDialog();
153
                    break;
154
                case KeyCodes.KEY_ESCAPE:
155
                    closeDialog();
156
                    break;
157
            }
158
        }
159
    }
160

    
161

    
162
    /**
163
     * Enables IE selection prevention and hides the dialog
164
     * (we disable the prevention on creation of the dialog)
165
     */
166
    public void closeDialog() {
167
        Pithos.preventIESelection();
168
        hide();
169
    }
170

    
171
    /**
172
     * Generate an RPC request to create a new folder.
173
     */
174
    void addUser() {
175
        final String userDisplayName = userDisplayNameTextBox.getText().trim();
176
        if(userDisplayName.length() == 0) {
177
            return;
178
        }
179
        if(!Const.EMAIL_REGEX.test(userDisplayName)) {
180
            app.displayWarning("Username must be a valid email address");
181
            return;
182
        }
183

    
184
        // Now get the userID
185
        final String userID = app.getUserIDForDisplayName(userDisplayName);
186
        if(userID != null) {
187
            doAddUser(userID, userDisplayName);
188
        }
189
        else {
190
            // Must call server to obtain userID
191
            new UpdateUserCatalogs(app, null, Helpers.toList(userDisplayName)) {
192
                @Override
193
                public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
194
                    final String userID = updatedUserCatalogs.getUserID(userDisplayName);
195
                    if(userID != null) {
196
                        doAddUser(userID, userDisplayName);
197
                    }
198
                    else {
199
                        app.displayError("Unknown user " + userDisplayName);
200
                    }
201
                }
202
            }.scheduleDeferred();
203
        }
204
    }
205

    
206
    private void doAddUser(String userID, String userDisplayName) {
207
        final User newUser = new User(userID, userDisplayName, group.getName());
208
        app.LOG("doAddUser() ", newUser);
209
        group.addUser(newUser);
210
        final String path = "?update=";
211
        PostRequest updateGroup = new PostRequest(app, app.getApiPath(), app.getUserID(), path) {
212
            @Override
213
            public void onSuccess(Resource result) {
214
                app.updateGroupNode(group);
215
            }
216

    
217
            @Override
218
            public void onError(Throwable t) {
219
                GWT.log("", t);
220
                app.setError(t);
221
                if(t instanceof RestException) {
222
                    app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
223
                }
224
                else {
225
                    app.displayError("System error updating group:" + t.getMessage());
226
                }
227
            }
228

    
229
            @Override
230
            protected void onUnauthorized(Response response) {
231
                app.sessionExpired();
232
            }
233
        };
234
        updateGroup.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
235
        final String groupMembers = group.encodeUserIDsForXAccountGroup();
236
        updateGroup.setHeader(Const.X_ACCOUNT_GROUP_ + URL.encodePathSegment(group.getName()), groupMembers);
237
        Scheduler.get().scheduleDeferred(updateGroup);
238
    }
239
}