use of org.openremote.model.interop.Consumer in project openremote by openremote.
the class AssetEditActivity method getLinkableAssetsAndAttributes.
@Override
public void getLinkableAssetsAndAttributes(ValueHolder valueHolder, Consumer<Map<AttributeRefEditor.AssetInfo, List<AttributeRefEditor.AttributeInfo>>> assetAttributeConsumer) {
AssetQuery query;
Predicate<AssetAttribute> attributeFilter = null;
// Is it agent or attribute link?
if ((valueHolder instanceof MetaItem) && AgentLink.isAgentLink((MetaItem) valueHolder)) {
query = new AssetQuery().select(new AssetQuery.Select(AssetQuery.Include.ONLY_ID_AND_NAME_AND_ATTRIBUTES)).type(AssetType.AGENT);
// the query will be automatically restricted to the logged in users realm)
if (!isNullOrEmpty(asset.getRealmId())) {
query.tenant(new TenantPredicate(asset.getRealmId()));
}
// Agents must have protocol configurations
query.attributeMeta(new AttributeMetaPredicate(AssetMeta.PROTOCOL_CONFIGURATION, new BooleanPredicate(true)));
// Only show protocol configurations
attributeFilter = ProtocolConfiguration::isProtocolConfiguration;
} else {
query = new AssetQuery().select(new AssetQuery.Select(AssetQuery.Include.ONLY_ID_AND_NAME_AND_ATTRIBUTE_NAMES));
// otherwise the query will be automatically restricted to the logged in users realm)
if (!isNullOrEmpty(asset.getRealmId())) {
query.tenant(new AssetQuery.TenantPredicate(asset.getRealmId()));
}
}
// Do request
final Predicate<AssetAttribute> finalAttributeFilter = attributeFilter;
environment.getApp().getRequests().sendWithAndReturn(assetArrayMapper, assetQueryMapper, requestParams -> assetResource.queryAssets(requestParams, query), 200, assets -> {
Map<AttributeRefEditor.AssetInfo, List<AttributeRefEditor.AttributeInfo>> assetAttributeMap = Arrays.stream(assets).filter(asset -> !asset.getAttributesList().isEmpty()).collect(Collectors.toMap(asset -> new AttributeRefEditor.AssetInfo(asset.getName(), asset.getId()), asset -> asset.getAttributesStream().filter(attribute -> finalAttributeFilter == null || finalAttributeFilter.test(attribute)).map(attribute -> new AttributeRefEditor.AttributeInfo(attribute.getName().orElse(null), attribute.getLabelOrName().orElse(null))).collect(Collectors.toList())));
assetAttributeConsumer.accept(assetAttributeMap);
}, exception -> assetAttributeConsumer.accept(new HashMap<>()));
}
use of org.openremote.model.interop.Consumer in project openremote by openremote.
the class Simulator method writeView.
protected void writeView() {
clear();
addLabel(environment.getMessages().simulator());
formGroups.clear();
List<SimulatorElement> sortedElements = Arrays.asList(simulatorState.getElements());
sortedElements.sort(Comparator.comparing(o -> simulatorState.getElementName(o)));
for (SimulatorElement element : sortedElements) {
FormGroup formGroup = new FormGroup();
String elementName = simulatorState.getElementName(element);
FormLabel formLabel = new FormLabel(elementName);
formLabel.addStyleName("largest");
formGroup.setFormLabel(formLabel);
FormField formField = new FormField();
formGroup.setFormField(formField);
// Don't push simulator value validation up to the presenter as it is a special case that should
// just be evaluated in-situ and shouldn't invalidate the parent attribute
Consumer<Value> onModified = value -> {
element.setValue(value);
List<ValidationFailure> failures = element.getValidationFailures();
formGroup.setError(failures != null && !failures.isEmpty());
};
ValueType valueType = element.getExpectedType().getValueType();
IsWidget editor = valueEditorSupplier.createValueEditor(element, valueType, style, parentView, onModified);
formField.add(editor);
formGroups.put(element.getAttributeRef(), formGroup);
add(formGroup);
}
if (sortedElements.size() > 0) {
FormGroup submitGroup = new FormGroup();
submitGroup.getElement().getStyle().setWidth(80, com.google.gwt.dom.client.Style.Unit.PCT);
FormField submitField = new FormField();
submitGroup.setFormField(submitField);
FormButton writeButton = new FormButton(environment.getMessages().writeSimulatorState());
writeButton.setPrimary(true);
writeButton.addClickHandler(event -> {
if (isValid()) {
environment.getEventService().dispatch(simulatorState);
environment.getEventBus().dispatch(new ShowSuccessEvent(environment.getMessages().simulatorStateSubmitted()));
}
});
submitField.add(writeButton);
add(submitGroup);
} else {
add(new FormInlineLabel(environment.getMessages().noAttributesLinkedToSimulator()));
}
// "Blink" the editor so users know there might be a new value
for (FormGroup formGroup : formGroups.values()) {
formGroup.addStyleName(environment.getWidgetStyle().HighlightBackground());
}
Browser.getWindow().setTimeout(() -> {
for (FormGroup formGroup : formGroups.values()) formGroup.removeStyleName(environment.getWidgetStyle().HighlightBackground());
}, 250);
}
Aggregations