Search in sources :

Example 1 with PathPredicate

use of org.openremote.model.query.filter.PathPredicate in project openremote by openremote.

the class AgentService method findAssets.

@Override
public List<Asset<?>> findAssets(String assetId, AssetQuery assetQuery) {
    if (TextUtil.isNullOrEmpty(assetId)) {
        return Collections.emptyList();
    }
    if (assetQuery == null) {
        assetQuery = new AssetQuery();
    }
    // Ensure agent ID is injected into each path predicate
    if (assetQuery.paths != null) {
        for (PathPredicate pathPredicate : assetQuery.paths) {
            int len = pathPredicate.path.length;
            pathPredicate.path = Arrays.copyOf(pathPredicate.path, len + 1);
            pathPredicate.path[len] = assetId;
        }
    } else {
        assetQuery.paths(new PathPredicate(assetId));
    }
    return assetStorageService.findAll(assetQuery);
}
Also used : PathPredicate(org.openremote.model.query.filter.PathPredicate) AssetQuery(org.openremote.model.query.AssetQuery)

Example 2 with PathPredicate

use of org.openremote.model.query.filter.PathPredicate in project openremote by openremote.

the class UsersFacade method getResults.

@Override
public Stream<String> getResults(UserQuery userQuery) {
    // No restriction for global rulesets
    if (TenantRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        // Restrict tenant
        userQuery.tenantPredicate = new TenantPredicate(rulesEngineId.getRealm().orElseThrow(() -> new IllegalArgumentException("Realm ID missing: " + rulesEngineId)));
    } else if (AssetRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        userQuery.tenantPredicate = null;
        String assetId = rulesEngineId.getAssetId().orElseThrow(() -> new IllegalArgumentException("Asset ID missing: " + rulesEngineId));
        // Asset<?> must be this engines asset or a child
        if (userQuery.assetPredicate != null) {
            if (assetId.equals(userQuery.assetPredicate.id)) {
                userQuery.pathPredicate = null;
            } else {
                userQuery.pathPredicate = new PathPredicate(rulesEngineId.getAssetId().orElseThrow(IllegalArgumentException::new));
            }
        } else if (userQuery.pathPredicate != null) {
            // Path must contain this engines asset ID
            List<String> path = new ArrayList<>(Arrays.asList(userQuery.pathPredicate.path));
            if (!path.contains(assetId)) {
                path.add(assetId);
                userQuery.pathPredicate.path = path.toArray(new String[userQuery.pathPredicate.path.length + 1]);
            }
        } else {
            // Force scope to this asset
            userQuery.assetPredicate = new UserAssetPredicate(assetId);
        }
    }
    // Prevent system users being retrieved
    userQuery.select(new UserQuery.Select().excludeSystemUsers(true));
    return Arrays.stream(identityService.getIdentityProvider().queryUsers(userQuery)).map(User::getId);
}
Also used : PathPredicate(org.openremote.model.query.filter.PathPredicate) User(org.openremote.model.security.User) UserAssetPredicate(org.openremote.model.query.filter.UserAssetPredicate) ArrayList(java.util.ArrayList) TenantPredicate(org.openremote.model.query.filter.TenantPredicate)

Example 3 with PathPredicate

use of org.openremote.model.query.filter.PathPredicate in project openremote by openremote.

the class AssetsFacade method getResults.

@Override
public Stream<Asset<?>> getResults(AssetQuery assetQuery) {
    if (TenantRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        // Realm is restricted to rules
        assetQuery.tenant = new TenantPredicate(rulesEngineId.getRealm().orElseThrow(() -> new IllegalArgumentException("Realm missing: " + rulesEngineId)));
    } else if (AssetRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        // Realm is restricted to assets'
        assetQuery.tenant = new TenantPredicate(rulesEngineId.getRealm().orElseThrow(() -> new IllegalArgumentException("Realm missing: " + rulesEngineId)));
        Asset<?> restrictedAsset = assetStorageService.find(rulesEngineId.getAssetId().orElseThrow(() -> new IllegalStateException("Asset ID missing: " + rulesEngineId)), true);
        if (restrictedAsset == null) {
            throw new IllegalStateException("Asset is no longer available: " + rulesEngineId);
        }
        assetQuery.paths(new PathPredicate(restrictedAsset.getPath()));
    }
    AssetQuery.Select oldValue = assetQuery.select;
    assetQuery.select = new AssetQuery.Select().excludeAttributes();
    try {
        return assetStorageService.findAll(assetQuery).stream();
    } finally {
        assetQuery.select = oldValue;
    }
}
Also used : AssetRuleset(org.openremote.model.rules.AssetRuleset) PathPredicate(org.openremote.model.query.filter.PathPredicate) AssetQuery(org.openremote.model.query.AssetQuery) Asset(org.openremote.model.asset.Asset) TenantPredicate(org.openremote.model.query.filter.TenantPredicate)

Aggregations

PathPredicate (org.openremote.model.query.filter.PathPredicate)3 AssetQuery (org.openremote.model.query.AssetQuery)2 TenantPredicate (org.openremote.model.query.filter.TenantPredicate)2 ArrayList (java.util.ArrayList)1 Asset (org.openremote.model.asset.Asset)1 UserAssetPredicate (org.openremote.model.query.filter.UserAssetPredicate)1 AssetRuleset (org.openremote.model.rules.AssetRuleset)1 User (org.openremote.model.security.User)1