Search in sources :

Example 1 with PathPredicate

use of org.openremote.model.asset.AssetQuery.PathPredicate in project openremote by openremote.

the class AssetsFacade method query.

@Override
public Assets.RestrictedQuery query() {
    Assets.RestrictedQuery query = new Assets.RestrictedQuery() {

        @Override
        public Assets.RestrictedQuery select(Select select) {
            throw new IllegalArgumentException("Overriding query projection is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery id(String id) {
            throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery tenant(TenantPredicate tenantPredicate) {
            if (GlobalRuleset.class.isAssignableFrom(rulesEngineId.getScope()))
                return super.tenant(tenantPredicate);
            throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery userId(String userId) {
            throw new IllegalArgumentException("Overriding query restriction is not allowed in this rules scope");
        }

        @Override
        public Assets.RestrictedQuery orderBy(OrderBy orderBy) {
            throw new IllegalArgumentException("Overriding query result order is not allowed in this rules scope");
        }

        @Override
        public String getResult() {
            ServerAsset asset = assetStorageService.find(this);
            return asset != null ? asset.getId() : null;
        }

        @Override
        public Stream<String> stream() {
            if (this.select == null)
                this.select = new Select();
            Include oldValue = this.select.include;
            this.select.include = Include.ONLY_ID_AND_NAME;
            try {
                return assetStorageService.findAll(this).stream().map(Asset::getId);
            } finally {
                this.select.include = oldValue;
            }
        }
    };
    if (TenantRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        query.tenant = new TenantPredicate(rulesEngineId.getRealmId().orElseThrow(() -> new IllegalArgumentException("Realm ID missing: " + rulesEngineId)));
    }
    if (AssetRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {
        ServerAsset 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);
        }
        query.path = new PathPredicate(restrictedAsset.getPath());
    }
    return query;
}
Also used : PathPredicate(org.openremote.model.asset.AssetQuery.PathPredicate) ServerAsset(org.openremote.manager.asset.ServerAsset) TenantPredicate(org.openremote.model.asset.AssetQuery.TenantPredicate) Asset(org.openremote.model.asset.Asset) ServerAsset(org.openremote.manager.asset.ServerAsset)

Aggregations

ServerAsset (org.openremote.manager.asset.ServerAsset)1 Asset (org.openremote.model.asset.Asset)1 PathPredicate (org.openremote.model.asset.AssetQuery.PathPredicate)1 TenantPredicate (org.openremote.model.asset.AssetQuery.TenantPredicate)1