Search in sources :

Example 1 with AssetQuery

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);
    }
}
Also used : Tenant(org.openremote.model.security.Tenant) WebApplicationException(javax.ws.rs.WebApplicationException) AssetQuery(org.openremote.model.asset.AssetQuery) Select(org.openremote.model.asset.BaseAssetQuery.Select)

Example 2 with AssetQuery

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<>()));
}
Also used : org.openremote.app.client.assets.attributes(org.openremote.app.client.assets.attributes) java.util(java.util) Environment(org.openremote.app.client.Environment) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration) ATTRIBUTE_NAME_VALIDATOR(org.openremote.model.attribute.Attribute.ATTRIBUTE_NAME_VALIDATOR) ObjectValueMapper(org.openremote.app.client.interop.value.ObjectValueMapper) FormButton(org.openremote.app.client.widget.FormButton) ShowSuccessEvent(org.openremote.app.client.event.ShowSuccessEvent) AgentLink(org.openremote.model.asset.agent.AgentLink) Inject(javax.inject.Inject) AttributeRefEditor(org.openremote.app.client.widget.AttributeRefEditor) IsWidget(com.google.gwt.user.client.ui.IsWidget) AssetTreeNode(org.openremote.app.client.assets.browser.AssetTreeNode) org.openremote.model.asset(org.openremote.model.asset) FileInfoMapper(org.openremote.app.client.interop.jackson.FileInfoMapper) BrowserTreeNode(org.openremote.app.client.assets.browser.BrowserTreeNode) Consumer(org.openremote.model.interop.Consumer) AgentResource(org.openremote.model.asset.agent.AgentResource) MapResource(org.openremote.model.map.MapResource) ValueType(org.openremote.model.value.ValueType) AttributeLinkEditor(org.openremote.app.client.widget.AttributeLinkEditor) Predicate(java.util.function.Predicate) Pair(org.openremote.model.util.Pair) ShowFailureEvent(org.openremote.app.client.event.ShowFailureEvent) ValueEditors(org.openremote.app.client.widget.ValueEditors) EnumUtil(org.openremote.model.util.EnumUtil) AssetAttribute.attributesFromJson(org.openremote.model.asset.AssetAttribute.attributesFromJson) JsonEditor(org.openremote.app.client.app.dialog.JsonEditor) Collectors(java.util.stream.Collectors) org.openremote.app.client.assets(org.openremote.app.client.assets) Value(org.openremote.model.value.Value) Provider(com.google.inject.Provider) AssetBrowser(org.openremote.app.client.assets.browser.AssetBrowser) AssetQuery(org.openremote.model.asset.AssetQuery) org.openremote.model.attribute(org.openremote.model.attribute) ConstraintViolation(org.openremote.model.http.ConstraintViolation) ProtocolDescriptor(org.openremote.model.asset.agent.ProtocolDescriptor) TenantTreeNode(org.openremote.app.client.assets.browser.TenantTreeNode) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) ValueHolder(org.openremote.model.ValueHolder) Attribute.isAttributeNameEqualTo(org.openremote.model.attribute.Attribute.isAttributeNameEqualTo) MetaItem.isMetaNameEqualTo(org.openremote.model.attribute.MetaItem.isMetaNameEqualTo) AssetQuery(org.openremote.model.asset.AssetQuery) AttributeRefEditor(org.openremote.app.client.widget.AttributeRefEditor) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration)

Example 3 with AssetQuery

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);
}
Also used : AssetQuery(org.openremote.model.asset.AssetQuery) Select(org.openremote.model.asset.AssetQuery.Select) AssetAttribute(org.openremote.model.asset.AssetAttribute) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with AssetQuery

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;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AssetQuery(org.openremote.model.asset.AssetQuery) IOException(java.io.IOException)

Example 5 with AssetQuery

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);
    });
}
Also used : AssetQuery(org.openremote.model.asset.AssetQuery) ArrayList(java.util.ArrayList) Asset(org.openremote.model.asset.Asset)

Aggregations

AssetQuery (org.openremote.model.asset.AssetQuery)5 WebApplicationException (javax.ws.rs.WebApplicationException)2 IsWidget (com.google.gwt.user.client.ui.IsWidget)1 Provider (com.google.inject.Provider)1 IOException (java.io.IOException)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Environment (org.openremote.app.client.Environment)1 JsonEditor (org.openremote.app.client.app.dialog.JsonEditor)1 org.openremote.app.client.assets (org.openremote.app.client.assets)1 org.openremote.app.client.assets.attributes (org.openremote.app.client.assets.attributes)1 AssetBrowser (org.openremote.app.client.assets.browser.AssetBrowser)1 AssetTreeNode (org.openremote.app.client.assets.browser.AssetTreeNode)1 BrowserTreeNode (org.openremote.app.client.assets.browser.BrowserTreeNode)1 TenantTreeNode (org.openremote.app.client.assets.browser.TenantTreeNode)1 ShowFailureEvent (org.openremote.app.client.event.ShowFailureEvent)1