Search in sources :

Example 1 with ValueHolder

use of org.openremote.model.ValueHolder 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<>()));
}
Also used : org.openremote.app.client.assets.attributes(org.openremote.app.client.assets.attributes) java.util(java.util) Environment(org.openremote.app.client.Environment) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration) ATTRIBUTE_NAME_VALIDATOR(org.openremote.model.attribute.Attribute.ATTRIBUTE_NAME_VALIDATOR) ObjectValueMapper(org.openremote.app.client.interop.value.ObjectValueMapper) FormButton(org.openremote.app.client.widget.FormButton) ShowSuccessEvent(org.openremote.app.client.event.ShowSuccessEvent) AgentLink(org.openremote.model.asset.agent.AgentLink) Inject(javax.inject.Inject) AttributeRefEditor(org.openremote.app.client.widget.AttributeRefEditor) IsWidget(com.google.gwt.user.client.ui.IsWidget) AssetTreeNode(org.openremote.app.client.assets.browser.AssetTreeNode) org.openremote.model.asset(org.openremote.model.asset) FileInfoMapper(org.openremote.app.client.interop.jackson.FileInfoMapper) BrowserTreeNode(org.openremote.app.client.assets.browser.BrowserTreeNode) Consumer(org.openremote.model.interop.Consumer) AgentResource(org.openremote.model.asset.agent.AgentResource) MapResource(org.openremote.model.map.MapResource) ValueType(org.openremote.model.value.ValueType) AttributeLinkEditor(org.openremote.app.client.widget.AttributeLinkEditor) Predicate(java.util.function.Predicate) Pair(org.openremote.model.util.Pair) ShowFailureEvent(org.openremote.app.client.event.ShowFailureEvent) ValueEditors(org.openremote.app.client.widget.ValueEditors) EnumUtil(org.openremote.model.util.EnumUtil) AssetAttribute.attributesFromJson(org.openremote.model.asset.AssetAttribute.attributesFromJson) JsonEditor(org.openremote.app.client.app.dialog.JsonEditor) Collectors(java.util.stream.Collectors) org.openremote.app.client.assets(org.openremote.app.client.assets) Value(org.openremote.model.value.Value) Provider(com.google.inject.Provider) AssetBrowser(org.openremote.app.client.assets.browser.AssetBrowser) AssetQuery(org.openremote.model.asset.AssetQuery) org.openremote.model.attribute(org.openremote.model.attribute) ConstraintViolation(org.openremote.model.http.ConstraintViolation) ProtocolDescriptor(org.openremote.model.asset.agent.ProtocolDescriptor) TenantTreeNode(org.openremote.app.client.assets.browser.TenantTreeNode) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) ValueHolder(org.openremote.model.ValueHolder) Attribute.isAttributeNameEqualTo(org.openremote.model.attribute.Attribute.isAttributeNameEqualTo) MetaItem.isMetaNameEqualTo(org.openremote.model.attribute.MetaItem.isMetaNameEqualTo) AssetQuery(org.openremote.model.asset.AssetQuery) AttributeRefEditor(org.openremote.app.client.widget.AttributeRefEditor) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration)

Aggregations

IsWidget (com.google.gwt.user.client.ui.IsWidget)1 Provider (com.google.inject.Provider)1 java.util (java.util)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Environment (org.openremote.app.client.Environment)1 JsonEditor (org.openremote.app.client.app.dialog.JsonEditor)1 org.openremote.app.client.assets (org.openremote.app.client.assets)1 org.openremote.app.client.assets.attributes (org.openremote.app.client.assets.attributes)1 AssetBrowser (org.openremote.app.client.assets.browser.AssetBrowser)1 AssetTreeNode (org.openremote.app.client.assets.browser.AssetTreeNode)1 BrowserTreeNode (org.openremote.app.client.assets.browser.BrowserTreeNode)1 TenantTreeNode (org.openremote.app.client.assets.browser.TenantTreeNode)1 ShowFailureEvent (org.openremote.app.client.event.ShowFailureEvent)1 ShowSuccessEvent (org.openremote.app.client.event.ShowSuccessEvent)1 FileInfoMapper (org.openremote.app.client.interop.jackson.FileInfoMapper)1 ObjectValueMapper (org.openremote.app.client.interop.value.ObjectValueMapper)1 AttributeLinkEditor (org.openremote.app.client.widget.AttributeLinkEditor)1 AttributeRefEditor (org.openremote.app.client.widget.AttributeRefEditor)1