Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.2 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.rest.PostRequest;
52
import gr.grnet.pithos.web.client.rest.RestException;
53

    
54
import java.util.Arrays;
55

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

    
61
    protected Pithos app;
62

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

    
69
    final VerticalPanel inner;
70

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

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

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

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

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

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

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

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

    
108
        generalTable.setWidget(0, 1, userNameInput);
109

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

    
116
        outer.add(inner);
117

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

    
132
        setWidget(outer);
133
    }
134

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

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

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

    
162

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

    
172
    private void doAddUser(final String userID) {
173
        group.addMember(userID);
174
        String path = "?update=";
175
        PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUserID(), path) {
176

    
177
            @Override
178
            public void onSuccess(Resource result) {
179
                app.updateGroupNode(group);
180
            }
181

    
182
            @Override
183
            public void onError(Throwable t) {
184
                GWT.log("", t);
185
                app.setError(t);
186
                if(t instanceof RestException) {
187
                    app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
188
                }
189
                else {
190
                    app.displayError("System error updating group:" + t.getMessage());
191
                }
192
            }
193

    
194
            @Override
195
            protected void onUnauthorized(Response response) {
196
                app.sessionExpired();
197
            }
198
        };
199
        updateGroup.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
200
        String groupMembers = "";
201
        for(String u : group.getMembers()) {
202
            groupMembers += (URL.encodePathSegment(u) + ",");
203
        }
204
        updateGroup.setHeader(Const.X_ACCOUNT_GROUP_ + URL.encodePathSegment(group.getName()), groupMembers);
205
        Scheduler.get().scheduleDeferred(updateGroup);
206
    }
207
    /**
208
     * Generate an RPC request to create a new folder.
209
     */
210
    void addUser() {
211
        final String userDisplayName = userNameInput.getText().trim();
212
        if(userDisplayName.length() == 0) {
213
            return;
214
        }
215
        if(!Const.EMAIL_REGEX.test(userDisplayName)) {
216
            app.displayWarning("Username must be a valid email address");
217
            return;
218
        }
219

    
220
        if(app.hasIDForUserDisplayName(userDisplayName)) {
221
            final String userID = app.getIDForUserDisplayName(userDisplayName);
222
            doAddUser(userID);
223
        }
224
        else {
225
            new UpdateUserCatalogs(app, null, Arrays.asList(userDisplayName)) {
226
                @Override
227
                public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
228
                    final String userID = app.getIDForUserDisplayName(userDisplayName);
229
                    doAddUser(userID);
230
                }
231
            }.scheduleDeferred();
232
        }
233

    
234

    
235
    }
236
}