use of org.openremote.model.asset.AssetQuery in project openremote by openremote.
the class AssetResourceImpl method getCurrentUserAssets.
@Override
public Asset[] getCurrentUserAssets(RequestParams requestParams) {
try {
if (isSuperUser()) {
return new Asset[0];
}
if (!isRestrictedUser()) {
List<ServerAsset> result = assetStorageService.findAll(new AssetQuery().parent(new ParentPredicate(true)).tenant(new TenantPredicate().realm(getAuthenticatedRealm())));
return result.toArray(new Asset[result.size()]);
}
List<ServerAsset> assets = assetStorageService.findAll(new AssetQuery().select(new Select(Include.ALL_EXCEPT_PATH_AND_ATTRIBUTES, Access.RESTRICTED_READ)).userId(getUserId()));
// Filter assets that might have been moved into a different realm and can no longer be accessed by user
// TODO: Should we forbid moving assets between realms?
Tenant authenticatedTenant = getAuthenticatedTenant();
Iterator<ServerAsset> it = assets.iterator();
while (it.hasNext()) {
ServerAsset asset = it.next();
if (!asset.getRealmId().equals(authenticatedTenant.getId())) {
LOG.warning("User '" + getUsername() + "' linked to asset in other realm, skipping: " + asset);
it.remove();
}
}
// Compress response (the request attribute enables the interceptor)
request.setAttribute(HttpHeaders.CONTENT_ENCODING, "gzip");
return assets.toArray(new ServerAsset[assets.size()]);
} catch (IllegalStateException ex) {
throw new WebApplicationException(ex, BAD_REQUEST);
}
}
use of org.openremote.model.asset.AssetQuery 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<>()));
}
use of org.openremote.model.asset.AssetQuery in project openremote by openremote.
the class AssetAttributeLinkingService method getCurrentValue.
protected static Value getCurrentValue(EntityManager em, AssetStorageService assetStorageService, AttributeRef attributeRef) throws NoSuchElementException {
ServerAsset asset = assetStorageService.find(em, new AssetQuery().id(attributeRef.getEntityId()).select(new Select(Include.ALL, false, attributeRef.getAttributeName())));
Optional<AssetAttribute> attribute;
if (asset == null || !(attribute = asset.getAttribute(attributeRef.getAttributeName())).isPresent()) {
throw new NoSuchElementException("Attribute or asset could not be found: " + attributeRef);
}
return attribute.get().getValue().orElse(null);
}
use of org.openremote.model.asset.AssetQuery in project openremote by openremote.
the class AssetResourceImpl method getPublicAssets.
@Override
public Asset[] getPublicAssets(RequestParams requestParams, String q) {
AssetQuery assetQuery;
try {
assetQuery = JSON.readValue(q, AssetQuery.class);
} catch (IOException ex) {
throw new WebApplicationException("Error parsing query parameter 'q' as JSON object", BAD_REQUEST);
}
Asset[] result = queryPublicAssets(requestParams, assetQuery);
// Compress response (the request attribute enables the interceptor)
request.setAttribute(HttpHeaders.CONTENT_ENCODING, "gzip");
return result;
}
use of org.openremote.model.asset.AssetQuery 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);
});
}
Aggregations