Search in sources :

Example 16 with FindNodesByQueryResult

use of com.enonic.xp.node.FindNodesByQueryResult in project xp by enonic.

the class SecurityServiceImpl method query.

@Override
public PrincipalQueryResult query(final PrincipalQuery query) {
    try {
        final NodeQuery nodeQueryBuilder = PrincipalQueryNodeQueryTranslator.translate(query);
        final FindNodesByQueryResult result = callWithContext(() -> this.nodeService.findByQuery(nodeQueryBuilder));
        final Nodes nodes = callWithContext(() -> this.nodeService.getByIds(result.getNodeIds()));
        final Principals principals = PrincipalNodeTranslator.fromNodes(nodes);
        return PrincipalQueryResult.create().addPrincipals(principals).totalSize(Ints.checkedCast(result.getTotalHits())).build();
    } catch (NodeNotFoundException e) {
        return PrincipalQueryResult.create().build();
    }
}
Also used : NodeNotFoundException(com.enonic.xp.node.NodeNotFoundException) Principals(com.enonic.xp.security.Principals) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) NodeQuery(com.enonic.xp.node.NodeQuery) Nodes(com.enonic.xp.node.Nodes)

Example 17 with FindNodesByQueryResult

use of com.enonic.xp.node.FindNodesByQueryResult in project xp by enonic.

the class SecurityServiceImpl method query.

@Override
public UserQueryResult query(final UserQuery query) {
    try {
        final NodeQuery nodeQueryBuilder = UserQueryNodeQueryTranslator.translate(query);
        final FindNodesByQueryResult result = callWithContext(() -> this.nodeService.findByQuery(nodeQueryBuilder));
        final Nodes nodes = callWithContext(() -> this.nodeService.getByIds(result.getNodeIds()));
        final Principals principals = PrincipalNodeTranslator.fromNodes(nodes);
        return UserQueryResult.create().addUsers(principals).totalSize(Ints.checkedCast(result.getTotalHits())).build();
    } catch (NodeNotFoundException e) {
        return UserQueryResult.create().build();
    }
}
Also used : NodeNotFoundException(com.enonic.xp.node.NodeNotFoundException) Principals(com.enonic.xp.security.Principals) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) NodeQuery(com.enonic.xp.node.NodeQuery) Nodes(com.enonic.xp.node.Nodes)

Example 18 with FindNodesByQueryResult

use of com.enonic.xp.node.FindNodesByQueryResult in project xp by enonic.

the class SecurityServiceImpl method findByUsername.

private User findByUsername(final IdProviderKey idProvider, final String username) {
    final CompareExpr idProviderExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.ID_PROVIDER_KEY), CompareExpr.Operator.EQ, ValueExpr.string(idProvider.toString()));
    final CompareExpr userNameExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.LOGIN_KEY), CompareExpr.Operator.EQ, ValueExpr.string(username));
    final QueryExpr query = QueryExpr.from(LogicalExpr.and(idProviderExpr, userNameExpr));
    final Nodes nodes = callWithContext(() -> {
        final FindNodesByQueryResult result = nodeService.findByQuery(NodeQuery.create().query(query).build());
        return this.nodeService.getByIds(result.getNodeIds());
    });
    if (nodes.getSize() > 1) {
        throw new IllegalArgumentException("Expected at most 1 user with username " + username + " in id provider " + idProvider);
    }
    return nodes.isEmpty() ? null : PrincipalNodeTranslator.userFromNode(nodes.first());
}
Also used : CompareExpr(com.enonic.xp.query.expr.CompareExpr) QueryExpr(com.enonic.xp.query.expr.QueryExpr) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Nodes(com.enonic.xp.node.Nodes)

Example 19 with FindNodesByQueryResult

use of com.enonic.xp.node.FindNodesByQueryResult in project xp by enonic.

the class AbstractNodeTest method queryAndAssert.

protected void queryAndAssert(final String queryString, final int expected) {
    final FindNodesByQueryResult result = doQuery(queryString);
    assertEquals(expected, result.getNodeIds().getSize());
}
Also used : FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult)

Example 20 with FindNodesByQueryResult

use of com.enonic.xp.node.FindNodesByQueryResult in project xp by enonic.

the class ValueCountAggregationTest method testCountAggregation.

@Test
public void testCountAggregation() {
    createNode("c1", "n1", NodePath.ROOT);
    createNode("c2", "n2", NodePath.ROOT);
    createNode("c3", "n3", NodePath.ROOT);
    createNode(null, "n4", NodePath.ROOT);
    NodeQuery query = NodeQuery.create().addAggregationQuery(ValueCountAggregationQuery.create("testCountAgg").fieldName("category").build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    Aggregation agg = result.getAggregations().get("testCountAgg");
    assertTrue(agg instanceof SingleValueMetricAggregation);
    SingleValueMetricAggregation categoryAgg = (SingleValueMetricAggregation) agg;
    assertEquals(3, categoryAgg.getValue());
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) SingleValueMetricAggregation(com.enonic.xp.aggregation.SingleValueMetricAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) SingleValueMetricAggregation(com.enonic.xp.aggregation.SingleValueMetricAggregation) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) NodeQuery(com.enonic.xp.node.NodeQuery) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)84 NodeQuery (com.enonic.xp.node.NodeQuery)61 Test (org.junit.jupiter.api.Test)55 Node (com.enonic.xp.node.Node)32 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)19 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)19 Bucket (com.enonic.xp.aggregation.Bucket)14 PropertyTree (com.enonic.xp.data.PropertyTree)13 Nodes (com.enonic.xp.node.Nodes)9 DynamicConstraintExpr (com.enonic.xp.query.expr.DynamicConstraintExpr)9 Aggregation (com.enonic.xp.aggregation.Aggregation)8 Buckets (com.enonic.xp.aggregation.Buckets)6 QueryExpr (com.enonic.xp.query.expr.QueryExpr)4 DateRangeBucket (com.enonic.xp.aggregation.DateRangeBucket)3 NodeHit (com.enonic.xp.node.NodeHit)3 CompareExpr (com.enonic.xp.query.expr.CompareExpr)3 NumericRangeBucket (com.enonic.xp.aggregation.NumericRangeBucket)2 SingleValueMetricAggregation (com.enonic.xp.aggregation.SingleValueMetricAggregation)2 ContentId (com.enonic.xp.content.ContentId)2 Context (com.enonic.xp.context.Context)2