Search in sources :

Example 6 with QueryExpr

use of com.enonic.xp.query.expr.QueryExpr in project xp by enonic.

the class FindNodesByQueryCommandTest method query_instant_different_field_name_case.

@Test
public void query_instant_different_field_name_case() throws Exception {
    final Instant BASE_INSTANT = Instant.parse("2000-01-01T00:00:00.00Z");
    final PropertyTree data13 = new PropertyTree();
    data13.addInstant("myProperty", BASE_INSTANT.plus(10, ChronoUnit.DAYS));
    final PropertyTree data2 = new PropertyTree();
    data2.addInstant("myProperty", BASE_INSTANT.plus(20, ChronoUnit.DAYS));
    final Node node1 = createNode(CreateNodeParams.create().name("my-node-1").parent(NodePath.ROOT).data(data13).build());
    final Node node2 = createNode(CreateNodeParams.create().name("my-node-2").parent(NodePath.ROOT).data(data2).build());
    final Node node3 = createNode(CreateNodeParams.create().name("child-node").parent(node1.path()).data(data13).build());
    final Value from = ValueFactory.newDateTime(BASE_INSTANT);
    final Value to = ValueFactory.newDateTime(BASE_INSTANT.plus(15, ChronoUnit.DAYS));
    final QueryExpr queryExpr = QueryExpr.from(CompareExpr.eq(FieldExpr.from("MYProperty"), ValueExpr.instant(BASE_INSTANT.plus(10, ChronoUnit.DAYS).toString())));
    final NodeQuery query = NodeQuery.create().query(queryExpr).addPostFilter(RangeFilter.create().fieldName("MYProperty").from(from).to(to).build()).build();
    final FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(2, result.getNodeIds().getSize());
    assertTrue(result.getNodeIds().contains(node1.id()));
    assertTrue(result.getNodeIds().contains(node3.id()));
    assertFalse(result.getNodeIds().contains(node2.id()));
}
Also used : QueryExpr(com.enonic.xp.query.expr.QueryExpr) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Instant(java.time.Instant) PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) NodeQuery(com.enonic.xp.node.NodeQuery) Value(com.enonic.xp.data.Value) Test(org.junit.jupiter.api.Test)

Example 7 with QueryExpr

use of com.enonic.xp.query.expr.QueryExpr in project xp by enonic.

the class FindNodesByQueryCommandTest_order method sort.

private FindNodesByQueryResult sort(String[] orders) {
    StringBuilder order = new StringBuilder();
    for (String curOrder : orders) {
        order.append(curOrder).append(",");
    }
    order.deleteCharAt(order.length() - 1);
    final List<OrderExpr> orderExpressions = QueryParser.parseOrderExpressions(order.toString());
    final ConstraintExpr constraintExpr = QueryParser.parseCostraintExpression(PARENT_QUERY);
    final QueryExpr queryExpr = QueryExpr.from(constraintExpr, orderExpressions);
    final NodeQuery query = NodeQuery.create().query(queryExpr).build();
    return doFindByQuery(query);
}
Also used : ConstraintExpr(com.enonic.xp.query.expr.ConstraintExpr) QueryExpr(com.enonic.xp.query.expr.QueryExpr) OrderExpr(com.enonic.xp.query.expr.OrderExpr) NodeQuery(com.enonic.xp.node.NodeQuery)

Example 8 with QueryExpr

use of com.enonic.xp.query.expr.QueryExpr in project xp by enonic.

the class PrincipalQueryNodeQueryTranslator method getQueryExpression.

private static QueryExpr getQueryExpression(final String query) {
    final String fields = String.join(",", DISPLAY_NAME_KEY, ALL_TEXT_FIELD_NAME);
    final FunctionExpr fullText = FunctionExpr.from("fulltext", ValueExpr.string(fields), ValueExpr.string(query), ValueExpr.string("AND"));
    final FunctionExpr nGram = FunctionExpr.from("ngram", ValueExpr.string(fields), ValueExpr.string(query), ValueExpr.string("AND"));
    final LogicalExpr fullTextOrNgram = LogicalExpr.or(new DynamicConstraintExpr(fullText), new DynamicConstraintExpr(nGram));
    return new QueryExpr(fullTextOrNgram, Collections.emptySet());
}
Also used : FunctionExpr(com.enonic.xp.query.expr.FunctionExpr) DynamicConstraintExpr(com.enonic.xp.query.expr.DynamicConstraintExpr) QueryExpr(com.enonic.xp.query.expr.QueryExpr) LogicalExpr(com.enonic.xp.query.expr.LogicalExpr)

Example 9 with QueryExpr

use of com.enonic.xp.query.expr.QueryExpr in project xp by enonic.

the class SecurityServiceImpl method findByEmail.

private User findByEmail(final IdProviderKey idProvider, final String email) {
    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.EMAIL_KEY), CompareExpr.Operator.EQ, ValueExpr.string(email));
    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 email " + email + " 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 10 with QueryExpr

use of com.enonic.xp.query.expr.QueryExpr in project xp by enonic.

the class SecurityServiceImpl method duplicateEmailValidation.

private void duplicateEmailValidation(final PrincipalKey key, final String email) {
    if (email == null) {
        return;
    }
    final CompareExpr idProviderExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.ID_PROVIDER_KEY), CompareExpr.Operator.EQ, ValueExpr.string(key.getIdProviderKey().toString()));
    final CompareExpr emailExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.EMAIL_KEY), CompareExpr.Operator.EQ, ValueExpr.string(email));
    final QueryExpr query = QueryExpr.from(LogicalExpr.and(idProviderExpr, emailExpr));
    final Nodes nodes = callWithContext(() -> {
        final FindNodesByQueryResult result = nodeService.findByQuery(NodeQuery.create().query(query).build());
        return this.nodeService.getByIds(result.getNodeIds());
    });
    final User user = nodes.isEmpty() ? null : PrincipalNodeTranslator.userFromNode(nodes.first());
    if (nodes.getSize() > 1 || (user != null && !user.getKey().equals(key))) {
        throw new IllegalArgumentException("A user with email '" + email + "' already exists in id provider '" + key.getIdProviderKey() + "'");
    }
}
Also used : User(com.enonic.xp.security.User) 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)

Aggregations

QueryExpr (com.enonic.xp.query.expr.QueryExpr)13 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)4 CompareExpr (com.enonic.xp.query.expr.CompareExpr)4 Nodes (com.enonic.xp.node.Nodes)3 ConstraintExpr (com.enonic.xp.query.expr.ConstraintExpr)3 Node (com.enonic.xp.node.Node)2 NodeQuery (com.enonic.xp.node.NodeQuery)2 OrderExpr (com.enonic.xp.query.expr.OrderExpr)2 Filters (com.enonic.xp.query.filter.Filters)2 HighlightQuery (com.enonic.xp.query.highlight.HighlightQuery)2 Test (org.junit.jupiter.api.Test)2 ContentQuery (com.enonic.xp.content.ContentQuery)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 Value (com.enonic.xp.data.Value)1 NodeIds (com.enonic.xp.node.NodeIds)1 AggregationQueries (com.enonic.xp.query.aggregation.AggregationQueries)1 AggregationQuery (com.enonic.xp.query.aggregation.AggregationQuery)1 DynamicConstraintExpr (com.enonic.xp.query.expr.DynamicConstraintExpr)1 FunctionExpr (com.enonic.xp.query.expr.FunctionExpr)1