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()));
}
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);
}
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());
}
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());
}
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() + "'");
}
}
Aggregations