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