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<>()));
}
Aggregations