use of org.jboss.hal.ballroom.form.FormItem in project console by hal.
the class MailSessionPresenter method launchAddServer.
void launchAddServer() {
SortedSet<String> availableServers = new TreeSet<>(asList(SMTP.toUpperCase(), IMAP.toUpperCase(), POP3.toUpperCase()));
ResourceAddress selectedSessionAddress = SELECTED_MAIL_SESSION_TEMPLATE.resolve(statementContext);
Operation serverNamesOp = new Operation.Builder(selectedSessionAddress, READ_CHILDREN_NAMES_OPERATION).param(CHILD_TYPE, SERVER).build();
dispatcher.execute(serverNamesOp, serversResult -> {
Set<String> existingServers = serversResult.asList().stream().map(node -> node.asString().toUpperCase()).collect(toSet());
availableServers.removeAll(existingServers);
if (availableServers.isEmpty()) {
MessageEvent.fire(getEventBus(), Message.error(resources.messages().allMailServersExist()));
} else {
FormItem<String> serverTypeItem;
if (availableServers.size() == 1) {
serverTypeItem = new TextBoxItem(ModelDescriptionConstants.SERVER_TYPE, resources.constants().type());
serverTypeItem.setValue(availableServers.first());
serverTypeItem.setEnabled(false);
} else {
serverTypeItem = new SingleSelectBoxItem(ModelDescriptionConstants.SERVER_TYPE, resources.constants().type(), new ArrayList<>(availableServers), false);
serverTypeItem.setRequired(true);
}
Metadata metadata = metadataRegistry.lookup(AddressTemplates.SERVER_TEMPLATE);
Form<ModelNode> form = new ModelNodeForm.Builder<>(Ids.MAIL_SERVER_DIALOG, metadata).unboundFormItem(serverTypeItem, 0).include(OUTBOUND_SOCKET_BINDING_REF, USERNAME, PASSWORD, "ssl", "tls").fromRequestProperties().requiredOnly().unsorted().build();
form.getFormItem(OUTBOUND_SOCKET_BINDING_REF).registerSuggestHandler(new ReadChildrenAutoComplete(dispatcher, statementContext, AddressTemplates.SOCKET_BINDING_TEMPLATE));
AddResourceDialog dialog = new AddResourceDialog(resources.messages().addResourceTitle(Names.SERVER), form, (name, modelNode) -> {
String serverType = serverTypeItem.getValue().toLowerCase();
ResourceAddress address = SELECTED_MAIL_SESSION_TEMPLATE.append(SERVER + EQUALS + serverType).resolve(statementContext);
Operation operation = new Operation.Builder(address, ModelDescriptionConstants.ADD).payload(modelNode).build();
dispatcher.execute(operation, result -> {
MessageEvent.fire(getEventBus(), Message.success(resources.messages().addResourceSuccess(Names.SERVER, serverType)));
reload();
});
});
dialog.show();
}
});
}
use of org.jboss.hal.ballroom.form.FormItem in project console by hal.
the class ModelNodeFormTest method noRuntime.
@Test
public void noRuntime() throws Exception {
ModelNodeForm<ModelNode> form = builder("noRuntime", new ResourceDescriptionBuilder().storage(ImmutableMap.of("foo", CONFIGURATION, "bar", RUNTIME))).build();
Iterable<FormItem> formItems = form.getFormItems();
Iterator<FormItem> iterator = formItems.iterator();
assertEquals(1, Iterables.size(formItems));
assertEquals("foo", iterator.next().getName());
}
use of org.jboss.hal.ballroom.form.FormItem in project console by hal.
the class ConfigurationStep method onNext.
@Override
protected boolean onNext(final EnableSSLContext context) {
boolean valid = form.save();
if (valid) {
// set default values, as metadata comes from an artifical resource description (ssl-mgmt-wizard.dmr)
description.getAttributes(ATTRIBUTES).forEach(p -> {
FormItem formItem = getFormItem(p.getName());
if (p.getValue().hasDefined(DEFAULT) && formItem != null && formItem.isEmpty()) {
model.get(p.getName()).set(p.getValue().get(DEFAULT));
}
});
context.model = model;
}
return valid;
}
use of org.jboss.hal.ballroom.form.FormItem in project console by hal.
the class ModelNodeMapping method persistModel.
@Override
@SuppressWarnings("unchecked")
public void persistModel(String id, T model, Iterable<FormItem> formItems) {
for (FormItem formItem : formItems) {
String name = formItem.getName();
if (formItem.isModified()) {
ModelNode attributeDescription = findAttribute(name);
if (attributeDescription == null) {
logger.error("{}: Unable to persist attribute '{}': No attribute description found in\n{}", id, name, attributeDescriptions);
continue;
}
if (formItem instanceof ModelNodeItem) {
if (formItem.getValue() == null) {
failSafeRemove(model, name);
} else {
model.get(name).set(((ModelNodeItem) formItem).getValue());
}
} else {
if (formItem.isExpressionValue()) {
model.get(name).setExpression(formItem.getExpressionValue());
} else {
ModelType type = attributeDescription.get(TYPE).asType();
Object value = formItem.getValue();
switch(type) {
case BOOLEAN:
Boolean booleanValue = (Boolean) value;
if (booleanValue == null) {
failSafeRemove(model, name);
} else {
model.get(name).set(booleanValue);
}
break;
case BIG_INTEGER:
case INT:
case LONG:
Long longValue = (Long) value;
if (longValue == null) {
failSafeRemove(model, name);
} else {
if (type == BIG_INTEGER) {
model.get(name).set(BigInteger.valueOf(longValue));
} else if (type == INT) {
model.get(name).set(longValue.intValue());
} else {
model.get(name).set(longValue);
}
}
break;
case DOUBLE:
Double doubleValue = (Double) value;
if (doubleValue == null) {
failSafeRemove(model, name);
} else {
model.get(name).set(doubleValue);
}
break;
case LIST:
List<String> list = (List<String>) value;
if (list == null || list.isEmpty()) {
failSafeRemove(model, name);
} else {
ModelNode listNode = new ModelNode();
for (String s : list) {
listNode.add(s);
}
model.get(name).set(listNode);
}
break;
case OBJECT:
boolean stringValueType = attributeDescription.get(VALUE_TYPE).getType().equals(ModelType.TYPE) && attributeDescription.get(VALUE_TYPE).asType().equals(ModelType.STRING);
if (stringValueType) {
Map<String, String> map = (Map<String, String>) value;
if (map == null || map.isEmpty()) {
failSafeRemove(model, name);
} else {
ModelNode mapNode = new ModelNode();
for (Map.Entry<String, String> entry : map.entrySet()) {
mapNode.get(entry.getKey()).set(entry.getValue());
}
model.get(name).set(mapNode);
}
}
break;
case STRING:
String stringValue = value == null ? null : String.valueOf(value);
if (Strings.isNullOrEmpty(stringValue)) {
failSafeRemove(model, name);
} else {
model.get(name).set(stringValue);
}
break;
// unsupported types
case BIG_DECIMAL:
case BYTES:
case EXPRESSION:
case PROPERTY:
case TYPE:
case UNDEFINED:
logger.warn("{}: persisting form field '{}' to type '{}' not implemented", id, name, type);
break;
default:
break;
}
}
}
}
}
}
use of org.jboss.hal.ballroom.form.FormItem in project console by hal.
the class FilterView method updateHostRefs.
@SuppressWarnings("ConstantConditions")
private void updateHostRefs(final Table<NamedNode> table, final Form<NamedNode> form) {
FormItem<String> formItem = form.getFormItem(HOSTS);
if (formItem != null) {
if (table.hasSelection()) {
ResourceAddress filterRefAddress = HOST_TEMPLATE.append(FILTER_REF + "=" + table.selectedRow().getName()).resolve(mbuiContext.statementContext());
Operation filterRefOp = new Operation.Builder(filterRefAddress, READ_RESOURCE_OPERATION).build();
ResourceAddress locationFilterRefAddress = HOST_TEMPLATE.append(LOCATION + "=*").append(FILTER_REF + "=" + table.selectedRow().getName()).resolve(mbuiContext.statementContext());
Operation locationFilterRefOp = new Operation.Builder(locationFilterRefAddress, READ_RESOURCE_OPERATION).build();
mbuiContext.dispatcher().execute(new Composite(filterRefOp, locationFilterRefOp), (CompositeResult result) -> {
SortedSet<String> hosts = new TreeSet<>();
result.step(0).get(RESULT).asList().stream().map(node -> {
ResourceAddress adr = new ResourceAddress(node.get(ADDRESS));
ResourceAddress host = adr.getParent();
ResourceAddress server = host.getParent();
return server.lastValue() + "/" + host.lastValue();
}).forEach(hosts::add);
result.step(1).get(RESULT).asList().stream().map(node -> {
ResourceAddress adr = new ResourceAddress(node.get(ADDRESS));
ResourceAddress host = adr.getParent().getParent();
ResourceAddress server = host.getParent();
return server.lastValue() + "/" + host.lastValue();
}).forEach(hosts::add);
formItem.setValue(String.join(", ", hosts));
}, (op1, failure) -> formItem.clearValue());
} else {
formItem.clearValue();
}
}
}
Aggregations