Search in sources :

Example 16 with Asset

use of org.openremote.model.asset.Asset in project openremote by openremote.

the class AssetBrowserPresenter method loadAssets.

protected void loadAssets(BrowserTreeNode parent, HasData<BrowserTreeNode> display) {
    // TODO Pagination?
    // final Range range = display.getVisibleRange();
    environment.getApp().getRequests().sendWithAndReturn(assetArrayMapper, assetQueryMapper, requestParams -> {
        if (parent instanceof TenantTreeNode) {
            assetResource.queryAssets(requestParams, new AssetQuery().tenant(new AssetQuery.TenantPredicate(parent.getId())).parent(new AssetQuery.ParentPredicate(true)));
        } else if (parent instanceof RootTreeNode) {
            assetResource.getCurrentUserAssets(requestParams);
        } else {
            assetResource.queryAssets(requestParams, new AssetQuery().parent(parent.getId()));
        }
    }, 200, assets -> {
        List<BrowserTreeNode> treeNodes = new ArrayList<>();
        for (Asset asset : assets) {
            treeNodes.add(new AssetTreeNode(asset));
        }
        display.setRowData(0, treeNodes);
        display.setRowCount(assets.length, true);
        afterNodeLoadChildren(treeNodes);
    });
}
Also used : AssetQuery(org.openremote.model.asset.AssetQuery) ArrayList(java.util.ArrayList) Asset(org.openremote.model.asset.Asset)

Example 17 with Asset

use of org.openremote.model.asset.Asset in project openremote by openremote.

the class RulesetResourceImpl method createAssetRuleset.

/* ################################################################################################# */
@Override
public void createAssetRuleset(@BeanParam RequestParams requestParams, AssetRuleset ruleset) {
    String assetId = ruleset.getAssetId();
    if (assetId == null || assetId.length() == 0) {
        throw new WebApplicationException("Missing asset identifier value", BAD_REQUEST);
    }
    Asset asset = assetStorageService.find(assetId, false);
    if (asset == null) {
        throw new WebApplicationException(NOT_FOUND);
    }
    if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), asset.getId())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    rulesetStorageService.merge(ruleset);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Asset(org.openremote.model.asset.Asset)

Example 18 with Asset

use of org.openremote.model.asset.Asset in project openremote by openremote.

the class RulesetResourceImpl method updateAssetRuleset.

@Override
public void updateAssetRuleset(@BeanParam RequestParams requestParams, Long id, AssetRuleset ruleset) {
    AssetRuleset existingRuleset = rulesetStorageService.findById(AssetRuleset.class, id);
    if (existingRuleset == null) {
        throw new WebApplicationException(NOT_FOUND);
    }
    Asset asset = assetStorageService.find(existingRuleset.getAssetId(), false);
    if (asset == null) {
        throw new WebApplicationException(NOT_FOUND);
    }
    if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), asset.getId())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    if (!id.equals(ruleset.getId())) {
        throw new WebApplicationException("Requested ID and ruleset ID don't match", BAD_REQUEST);
    }
    if (!existingRuleset.getAssetId().equals(ruleset.getAssetId())) {
        throw new WebApplicationException("Can't update asset ID, delete and create the ruleset to reassign", BAD_REQUEST);
    }
    rulesetStorageService.merge(ruleset);
}
Also used : AssetRuleset(org.openremote.model.rules.AssetRuleset) WebApplicationException(javax.ws.rs.WebApplicationException) Asset(org.openremote.model.asset.Asset)

Example 19 with Asset

use of org.openremote.model.asset.Asset 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

Asset (org.openremote.model.asset.Asset)19 WebApplicationException (javax.ws.rs.WebApplicationException)5 ServerAsset (org.openremote.manager.asset.ServerAsset)5 AssetAttribute (org.openremote.model.asset.AssetAttribute)5 PersistenceService (org.openremote.container.persistence.PersistenceService)3 TimerService (org.openremote.container.timer.TimerService)3 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)3 AssetRuleset (org.openremote.model.rules.AssetRuleset)3 ArrayList (java.util.ArrayList)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 EntityManager (javax.persistence.EntityManager)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 Container (org.openremote.container.Container)2 ContainerService (org.openremote.container.ContainerService)2 GlobalLock.withLock (org.openremote.container.concurrent.GlobalLock.withLock)2 MessageBrokerService (org.openremote.container.message.MessageBrokerService)2 MessageBrokerSetupService (org.openremote.container.message.MessageBrokerSetupService)2 AgentService (org.openremote.manager.agent.AgentService)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1