-
Notifications
You must be signed in to change notification settings - Fork 160
/
RoleAddDialog.java
138 lines (116 loc) · 5.61 KB
/
RoleAddDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
* Copyright (c) 2011, 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.authorization.client.role.dialog;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaErrorCode;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaException;
import org.eclipse.kapua.app.console.module.api.client.messages.ConsoleMessages;
import org.eclipse.kapua.app.console.module.api.client.ui.dialog.entity.EntityAddEditDialog;
import org.eclipse.kapua.app.console.module.api.client.ui.panel.FormPanel;
import org.eclipse.kapua.app.console.module.api.client.ui.widget.KapuaTextField;
import org.eclipse.kapua.app.console.module.api.client.util.ConsoleInfo;
import org.eclipse.kapua.app.console.module.api.client.util.DialogUtils;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.client.util.validator.TextFieldValidator;
import org.eclipse.kapua.app.console.module.api.client.util.validator.TextFieldValidator.FieldType;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.authorization.client.messages.ConsoleRoleMessages;
import org.eclipse.kapua.app.console.module.authorization.shared.model.GwtRole;
import org.eclipse.kapua.app.console.module.authorization.shared.model.GwtRoleCreator;
import org.eclipse.kapua.app.console.module.authorization.shared.service.GwtRoleService;
import org.eclipse.kapua.app.console.module.authorization.shared.service.GwtRoleServiceAsync;
public class RoleAddDialog extends EntityAddEditDialog {
private static final ConsoleRoleMessages MSGS = GWT.create(ConsoleRoleMessages.class);
private static final ConsoleMessages CONSOLE_MSGS = GWT.create(ConsoleMessages.class);
private static final GwtRoleServiceAsync GWT_ROLE_SERVICE = GWT.create(GwtRoleService.class);
protected KapuaTextField<String> roleNameField;
protected KapuaTextField<String> roleDescriptionField;
public RoleAddDialog(GwtSession currentSession) {
super(currentSession);
DialogUtils.resizeDialog(this, 400, 200);
}
public void validateRoles() {
if (roleNameField.getValue() == null) {
ConsoleInfo.display("Error", CONSOLE_MSGS.allFieldsRequired());
}
}
@Override
protected void preSubmit() {
validateRoles();
super.preSubmit();
}
@Override
public void submit() {
GwtRoleCreator gwtRoleCreator = new GwtRoleCreator();
gwtRoleCreator.setScopeId(currentSession.getSelectedAccountId());
gwtRoleCreator.setName(roleNameField.getValue());
gwtRoleCreator.setDescription(roleDescriptionField.getValue());
GWT_ROLE_SERVICE.create(xsrfToken, gwtRoleCreator, new AsyncCallback<GwtRole>() {
@Override
public void onSuccess(GwtRole gwtRole) {
exitStatus = true;
exitMessage = MSGS.dialogAddConfirmation();
hide();
}
@Override
public void onFailure(Throwable cause) {
exitStatus = false;
status.hide();
formPanel.getButtonBar().enable();
unmask();
submitButton.enable();
cancelButton.enable();
if (!isPermissionErrorMessage(cause)) {
if (cause instanceof GwtKapuaException) {
GwtKapuaException gwtCause = (GwtKapuaException) cause;
if (gwtCause.getCode().equals(GwtKapuaErrorCode.DUPLICATE_NAME)) {
roleNameField.markInvalid(gwtCause.getMessage());
}
}
FailureHandler.handleFormException(formPanel, cause);
}
}
});
}
@Override
public String getHeaderMessage() {
return MSGS.dialogAddHeader();
}
@Override
public String getInfoMessage() {
return MSGS.dialogAddInfo();
}
@Override
public void createBody() {
submitButton.disable();
FormPanel roleFormPanel = new FormPanel(FORM_LABEL_WIDTH);
//
// Name
roleNameField = new KapuaTextField<String>();
roleNameField.setAllowBlank(false);
roleNameField.setMinLength(3);
roleNameField.setMaxLength(255);
roleNameField.setFieldLabel("* " + MSGS.dialogAddFieldName());
roleNameField.setValidator(new TextFieldValidator(roleNameField, FieldType.NAME_SPACE));
roleNameField.setToolTip(MSGS.dialogAddFieldNameTooltip());
roleFormPanel.add(roleNameField);
roleDescriptionField = new KapuaTextField<String>();
roleDescriptionField.setAllowBlank(true);
roleDescriptionField.setMaxLength(255);
roleDescriptionField.setName("description");
roleDescriptionField.setFieldLabel(MSGS.dialogAddFieldDescription());
roleNameField.setToolTip(MSGS.dialogAddFieldDescriptionTooltip());
roleFormPanel.add(roleDescriptionField);
bodyPanel.add(roleFormPanel);
}
}