use of org.eclipse.kapua.app.console.shared.model.GwtDeviceCreator in project kapua by eclipse.
the class GwtDeviceServiceImpl method createDevice.
public GwtDevice createDevice(GwtXSRFToken xsrfToken, GwtDeviceCreator gwtDeviceCreator) throws GwtKapuaException {
//
// Checking validity of the given XSRF Token
checkXSRFToken(xsrfToken);
KapuaLocator locator = KapuaLocator.getInstance();
DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
DeviceFactory deviceFactory = locator.getFactory(DeviceFactory.class);
GwtDevice gwtDevice = null;
try {
KapuaId scopeId = KapuaEid.parseShortId(gwtDeviceCreator.getScopeId());
DeviceCreator deviceCreator = deviceFactory.newCreator(scopeId, gwtDeviceCreator.getClientId());
deviceCreator.setDisplayName(gwtDeviceCreator.getDisplayName());
deviceCreator.setCredentialsMode(DeviceCredentialsMode.valueOf(gwtDeviceCreator.getGwtCredentialsTight().name()));
deviceCreator.setCustomAttribute1(gwtDeviceCreator.getCustomAttribute1());
deviceCreator.setCustomAttribute2(gwtDeviceCreator.getCustomAttribute2());
deviceCreator.setCustomAttribute3(gwtDeviceCreator.getCustomAttribute3());
deviceCreator.setCustomAttribute4(gwtDeviceCreator.getCustomAttribute4());
deviceCreator.setCustomAttribute5(gwtDeviceCreator.getCustomAttribute5());
Device device = deviceRegistryService.create(deviceCreator);
gwtDevice = KapuaGwtConverter.convert(device);
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtDevice;
}
use of org.eclipse.kapua.app.console.shared.model.GwtDeviceCreator in project kapua by eclipse.
the class DeviceForm method onRender.
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
FormData formData = new FormData("-20");
m_formPanel = new FormPanel();
m_formPanel.setFrame(false);
m_formPanel.setBodyBorder(false);
m_formPanel.setHeaderVisible(false);
m_formPanel.setWidth(310);
m_formPanel.setScrollMode(Scroll.AUTOY);
m_formPanel.setStyleAttribute("padding-bottom", "0px");
m_formPanel.setLayout(new FlowLayout());
// Device general info fieldset
FieldSet fieldSet = new FieldSet();
FormLayout layout = new FormLayout();
layout.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
fieldSet.setLayout(layout);
fieldSet.setHeading(MSGS.deviceFormFieldsetGeneralInfo());
// Device Client ID
clientIdLabel = new LabelField();
clientIdLabel.setFieldLabel(MSGS.deviceFormClientID());
clientIdLabel.setLabelSeparator(":");
clientIdLabel.setWidth(225);
fieldSet.add(clientIdLabel, formData);
clientIdField = new TextField<String>();
clientIdField.setAllowBlank(false);
clientIdField.setName("clientID");
clientIdField.setFieldLabel(MSGS.deviceFormClientID());
clientIdField.setValidator(new TextFieldValidator(clientIdField, FieldType.DEVICE_CLIENT_ID));
clientIdField.setWidth(225);
fieldSet.add(clientIdField, formData);
// Display name
displayNameField = new TextField<String>();
displayNameField.setAllowBlank(true);
displayNameField.setName("displayName");
displayNameField.setFieldLabel(MSGS.deviceFormDisplayName());
displayNameField.setWidth(225);
fieldSet.add(displayNameField, formData);
// Device Status
statusCombo = new SimpleComboBox<GwtDeviceQueryPredicates.GwtDeviceStatus>();
statusCombo.setName("status");
statusCombo.setFieldLabel(MSGS.deviceFormStatus());
statusCombo.setEditable(false);
statusCombo.setTriggerAction(TriggerAction.ALL);
statusCombo.setEmptyText(MSGS.deviceFilteringPanelStatusEmptyText());
statusCombo.add(GwtDeviceQueryPredicates.GwtDeviceStatus.ENABLED);
statusCombo.add(GwtDeviceQueryPredicates.GwtDeviceStatus.DISABLED);
fieldSet.add(statusCombo, formData);
// Tag fieldset
FieldSet fieldSetTags = new FieldSet();
FormLayout layoutTags = new FormLayout();
layoutTags.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
fieldSetTags.setLayout(layoutTags);
fieldSetTags.setHeading(MSGS.deviceFormFieldsetTags());
ContentPanel panel = new ContentPanel();
panel.setBorders(false);
panel.setBodyBorder(false);
panel.setHeaderVisible(false);
panel.setLayout(new RowLayout(Orientation.HORIZONTAL));
panel.setBodyStyle("background-color:transparent");
panel.setHeight(35);
// Device Custom attributes fieldset
FormLayout layoutSecurityOptions = new FormLayout();
layoutSecurityOptions.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
FieldSet fieldSetSecurityOptions = new FieldSet();
fieldSetSecurityOptions.setLayout(layoutSecurityOptions);
fieldSetSecurityOptions.setHeading(MSGS.deviceFormFieldsetSecurityOptions());
// Provisioned Credentials Tight
credentialsTightCombo = new SimpleComboBox<String>();
credentialsTightCombo.setName("provisionedCredentialsTight");
credentialsTightCombo.setEditable(false);
credentialsTightCombo.setTypeAhead(false);
credentialsTightCombo.setAllowBlank(false);
credentialsTightCombo.setFieldLabel(MSGS.deviceFormProvisionedCredentialsTight());
credentialsTightCombo.setToolTip(MSGS.deviceFormProvisionedCredentialsTightTooltip());
credentialsTightCombo.setTriggerAction(TriggerAction.ALL);
fieldSetSecurityOptions.add(credentialsTightCombo, formData);
credentialsTightCombo.add(GwtDeviceCredentialsTight.INHERITED.getLabel());
credentialsTightCombo.add(GwtDeviceCredentialsTight.LOOSE.getLabel());
credentialsTightCombo.add(GwtDeviceCredentialsTight.STRICT.getLabel());
credentialsTightCombo.setSimpleValue(GwtDeviceCredentialsTight.INHERITED.getLabel());
// Device User
RpcProxy<ListLoadResult<GwtUser>> deviceUserProxy = new RpcProxy<ListLoadResult<GwtUser>>() {
@Override
protected void load(Object loadConfig, AsyncCallback<ListLoadResult<GwtUser>> callback) {
gwtUserService.findAll(m_currentSession.getSelectedAccount().getId(), callback);
}
};
BaseListLoader<ListLoadResult<GwtUser>> deviceUserLoader = new BaseListLoader<ListLoadResult<GwtUser>>(deviceUserProxy);
ListStore<GwtUser> deviceUserStore = new ListStore<GwtUser>(deviceUserLoader);
deviceUserCombo = new ComboBox<GwtUser>();
deviceUserCombo.setName("deviceUserCombo");
deviceUserCombo.setEditable(false);
deviceUserCombo.setTypeAhead(false);
deviceUserCombo.setAllowBlank(false);
deviceUserCombo.setFieldLabel(MSGS.deviceFormDeviceUser());
deviceUserCombo.setTriggerAction(TriggerAction.ALL);
deviceUserCombo.setStore(deviceUserStore);
deviceUserCombo.setDisplayField("username");
deviceUserCombo.setValueField("id");
fieldSetSecurityOptions.add(deviceUserCombo, formData);
// Allow credential change
allowCredentialsChangeCheckbox = new CheckBox();
allowCredentialsChangeCheckbox.setName("allowNewUnprovisionedDevicesCheckbox");
allowCredentialsChangeCheckbox.setFieldLabel(MSGS.deviceFormAllowCredentialsChange());
allowCredentialsChangeCheckbox.setToolTip(MSGS.deviceFormAllowCredentialsChangeTooltip());
allowCredentialsChangeCheckbox.setBoxLabel("");
fieldSetSecurityOptions.add(allowCredentialsChangeCheckbox, formData);
// Device Custom attributes fieldset
FieldSet fieldSetCustomAttributes = new FieldSet();
FormLayout layoutCustomAttributes = new FormLayout();
layoutCustomAttributes.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
fieldSetCustomAttributes.setLayout(layoutCustomAttributes);
fieldSetCustomAttributes.setHeading(MSGS.deviceFormFieldsetCustomAttributes());
// Custom Attribute #1
customAttribute1Field = new TextField<String>();
customAttribute1Field.setName("customAttribute1");
customAttribute1Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute1());
customAttribute1Field.setWidth(225);
fieldSetCustomAttributes.add(customAttribute1Field, formData);
// Custom Attribute #2
customAttribute2Field = new TextField<String>();
customAttribute2Field.setName("customAttribute2");
customAttribute2Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute2());
customAttribute2Field.setWidth(225);
fieldSetCustomAttributes.add(customAttribute2Field, formData);
// Custom Attribute #3
customAttribute3Field = new TextField<String>();
customAttribute3Field.setName("customAttribute3");
customAttribute3Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute3());
customAttribute3Field.setWidth(225);
fieldSetCustomAttributes.add(customAttribute3Field, formData);
// Custom Attribute #4
customAttribute4Field = new TextField<String>();
customAttribute4Field.setName("customAttribute4");
customAttribute4Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute4());
customAttribute4Field.setWidth(225);
fieldSetCustomAttributes.add(customAttribute4Field, formData);
// Custom Attribute #5
customAttribute5Field = new TextField<String>();
customAttribute5Field.setName("customAttribute5");
customAttribute5Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute5());
customAttribute5Field.setWidth(225);
fieldSetCustomAttributes.add(customAttribute5Field, formData);
// Optlock
optlock = new NumberField();
optlock.setName("optlock");
optlock.setEditable(false);
optlock.setVisible(false);
fieldSet.add(optlock, formData);
m_formPanel.add(fieldSet);
m_formPanel.add(fieldSetTags);
m_formPanel.add(fieldSetSecurityOptions);
m_formPanel.add(fieldSetCustomAttributes);
m_formPanel.setButtonAlign(HorizontalAlignment.CENTER);
Button submitButton = new Button(MSGS.deviceFormSubmitButton());
submitButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
// make sure all visible fields are valid before performing the action
for (Field<?> field : m_formPanel.getFields()) {
if (field.isVisible() && !field.isValid()) {
MessageBox.alert(MSGS.error(), MSGS.formErrors(), null);
return;
}
}
if (m_selectedDevice == null) {
final GwtDeviceCreator gwtDeviceCreator = new GwtDeviceCreator();
gwtDeviceCreator.setScopeId(m_currentSession.getSelectedAccount().getId());
gwtDeviceCreator.setClientId(clientIdField.getValue());
gwtDeviceCreator.setDisplayName(displayNameField.getValue());
// Security Options
gwtDeviceCreator.setGwtCredentialsTight(credentialsTightCombo.getSimpleValue());
gwtDeviceCreator.setGwtPreferredUserId(deviceUserCombo.getValue().getId());
// Custom attributes
gwtDeviceCreator.setCustomAttribute1(unescapeValue(customAttribute1Field.getValue()));
gwtDeviceCreator.setCustomAttribute2(unescapeValue(customAttribute2Field.getValue()));
gwtDeviceCreator.setCustomAttribute3(unescapeValue(customAttribute3Field.getValue()));
gwtDeviceCreator.setCustomAttribute4(unescapeValue(customAttribute4Field.getValue()));
gwtDeviceCreator.setCustomAttribute5(unescapeValue(customAttribute5Field.getValue()));
//
// Getting XSRF token
gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {
@Override
public void onFailure(Throwable ex) {
FailureHandler.handle(ex);
}
@Override
public void onSuccess(GwtXSRFToken token) {
gwtDeviceService.createDevice(token, gwtDeviceCreator, new AsyncCallback<GwtDevice>() {
@Override
public void onFailure(Throwable caught) {
FailureHandler.handle(caught);
}
public void onSuccess(final GwtDevice gwtDevice) {
//
// Getting XSRF token
gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {
@Override
public void onFailure(Throwable ex) {
FailureHandler.handle(ex);
}
@Override
public void onSuccess(GwtXSRFToken token) {
hide();
ConsoleInfo.display(MSGS.info(), MSGS.deviceUpdateSuccess());
}
});
}
});
}
});
} else // Edit
{
// General info
m_selectedDevice.setDisplayName(unescapeValue(displayNameField.getValue()));
m_selectedDevice.setGwtDeviceStatus(statusCombo.getSimpleValue().name());
// Security Options
m_selectedDevice.setCredentialsTight(GwtDeviceCredentialsTight.getEnumFromLabel(credentialsTightCombo.getSimpleValue()).name());
m_selectedDevice.setCredentialsAllowChange(allowCredentialsChangeCheckbox.getValue());
m_selectedDevice.setDeviceUserId(deviceUserCombo.getValue().getId());
// Custom attributes
m_selectedDevice.setCustomAttribute1(unescapeValue(customAttribute1Field.getValue()));
m_selectedDevice.setCustomAttribute2(unescapeValue(customAttribute2Field.getValue()));
m_selectedDevice.setCustomAttribute3(unescapeValue(customAttribute3Field.getValue()));
m_selectedDevice.setCustomAttribute4(unescapeValue(customAttribute4Field.getValue()));
m_selectedDevice.setCustomAttribute5(unescapeValue(customAttribute5Field.getValue()));
// Optlock
m_selectedDevice.setOptlock(optlock.getValue().intValue());
//
// Getting XSRF token
gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {
@Override
public void onFailure(Throwable ex) {
FailureHandler.handle(ex);
}
@Override
public void onSuccess(GwtXSRFToken token) {
gwtDeviceService.updateAttributes(token, m_selectedDevice, new AsyncCallback<GwtDevice>() {
public void onFailure(Throwable caught) {
FailureHandler.handle(caught);
}
public void onSuccess(GwtDevice gwtDevice) {
//
// Getting XSRF token
gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {
@Override
public void onFailure(Throwable ex) {
FailureHandler.handle(ex);
}
@Override
public void onSuccess(GwtXSRFToken token) {
hide();
ConsoleInfo.display(MSGS.info(), m_selectedDevice == null ? MSGS.deviceCreationSuccess() : MSGS.deviceUpdateSuccess());
}
});
}
});
}
});
}
}
});
Button cancelButton = new Button(MSGS.deviceFormCancelButton());
cancelButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
hide();
}
});
m_formPanel.addButton(submitButton);
m_formPanel.addButton(cancelButton);
add(m_formPanel);
// Hide components according to NEW/EDIT mode
makeNewEditAppearance();
// Populate fields if we are in EDIT mode
if (m_selectedDevice != null) {
populateFields();
}
}
Aggregations