Java Code Examples for cz.jirutka.rsql.parser.ast.ComparisonNode#getArguments()

The following examples show how to use cz.jirutka.rsql.parser.ast.ComparisonNode#getArguments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityRefTypeFilterVisitor.java    From gemini with Apache License 2.0 6 votes vote down vote up
private QueryWithParams handleMultipleLogicalKeyEntities(EntityField field, ComparisonNode node, String sqlOperator, FilterVisitorContext filterVisitorContext) {
    List<String> arguments = node.getArguments();
    if (arguments.size() == 1) {
        Entity entityRef = field.getEntityRef();
        String entityName = wrapDoubleQuotes(entityRef.getName().toLowerCase());
        String idName = wrapDoubleQuotes(entityRef.getIdEntityField().getName().toLowerCase());
        String argument = arguments.get(0);

        // we need to parse again the argument if we dont' have a UUID.. since it contains other conditions on keys
        Node rootNode = new RSQLParser().parse(argument);
        QueryWithParams innerQuery = rootNode.accept(this.parentFilterVisitor, FilterVisitorContext.of(entityRef, filterVisitorContext.counterByParameter));
        return new QueryWithParams(String.format("SELECT %1$s.%2$s" +
                "  FROM %1$s WHERE ", entityName, idName)
                + innerQuery.getSql(), innerQuery.getParams());
    }
    throw new GeminiRuntimeException(String.format("EntityRefTypeFilterVisitor unsupported operator %s withRecord for that one argument", node.getOperator().getSymbol()));
}
 
Example 2
Source File: EntityRefTypeFilterVisitor.java    From gemini with Apache License 2.0 6 votes vote down vote up
private QueryWithParams handleSingleLogicalKeyEntity(EntityField field, ComparisonNode node, String sqlOperator, FilterVisitorContext filterVisitorContext) {
    Entity entityRef = field.getEntityRef();
    Entity.LogicalKey logicalKey = entityRef.getLogicalKey();
    EntityField lkField = logicalKey.getLogicalKeyList().get(0);
    List<String> arguments = node.getArguments();
    String parameterName = filterVisitorContext.parameterFor(fieldName(lkField, false));
    String innerFilter = ":" + parameterName;

    List<Object> parameters = arguments.stream().map(a -> resolveArgumentValue(lkField, a)).collect(Collectors.toList());

    String sqlFilter = String.format("" +
                    "SELECT %1$s.%2$s" +
                    "  FROM %1$s " +
                    " WHERE %1$s.%3$s ",
            wrapDoubleQuotes(entityRef.getName().toLowerCase()),
            wrapDoubleQuotes(entityRef.getIdEntityField().getName().toLowerCase()),
            fieldName(lkField, true));
    sqlFilter += String.format(sqlOperator, innerFilter);
    return new QueryWithParams(sqlFilter, Map.of(parameterName, parameters));
}
 
Example 3
Source File: SortRSQLNodeTraveller.java    From pnc with Apache License 2.0 6 votes vote down vote up
@Override
public SortInfo visit(ComparisonNode node) {
    SortingDirection sortingDirection;
    List<String> sortingFields = new ArrayList<>();

    if (node.getOperator().equals(ASC)) {
        sortingDirection = SortInfo.SortingDirection.ASC;
    } else if (node.getOperator().equals(DESC)) {
        sortingDirection = SortInfo.SortingDirection.DESC;
    } else {
        throw new UnsupportedOperationException("Unsupported sorting: " + node.getOperator());
    }

    logger.trace("Sorting direction - {}, arguments {}", sortingDirection, node.getArguments());
    for (String argument : node.getArguments()) {
        if ("id".equals(argument)) { // Disable sorting by id
            throw new RSQLException("Sorting by id is not supported.");
        }
        sortingFields.add(toPath.apply(RSQLSelectorPath.get(argument)));
    }
    return new DefaultSortInfo(sortingDirection, sortingFields);
}
 
Example 4
Source File: ComparatorRSQLNodeTraveller.java    From pnc with Apache License 2.0 6 votes vote down vote up
@Override
public Comparator<DTO> visit(ComparisonNode node) {
    logger.trace("Sorting direction - {}, arguments {}", node.getOperator(), node.getArguments());
    Comparator<DTO> comparator = null;
    for (String argument : node.getArguments()) {
        Comparator<DTO> comp = Comparator.comparing(dto -> getProperty(dto, argument));
        if (comparator == null) {
            comparator = comp;
        } else {
            comparator = comparator.thenComparing(comp);
        }
    }
    if (comparator == null) {
        throw new RSQLException("No argument for RSQL comparsion found.");
    }
    if (node.getOperator().equals(DESC)) {
        comparator = comparator.reversed();
    }

    return comparator;
}
 
Example 5
Source File: AggregateQueryRsqlVisitor.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Attribute getAttribute(ComparisonNode node) {
  List<String> args = node.getArguments();
  if (args.size() != 1) {
    throw new MolgenisQueryException(
        String.format(
            "RSQL query value must have exactly one value instead of [%s]",
            StringUtils.join(args, ',')));
  }
  String attrName = args.iterator().next();

  String[] attrTokens = attrName.split("\\.");
  Attribute attr = entityType.getAttribute(attrTokens[0]);
  if (attr == null) {
    throw new UnknownAttributeException(entityType, attrName);
  }
  EntityType entityTypeAtDepth;
  for (int i = 1; i < attrTokens.length; ++i) {
    entityTypeAtDepth = attr.getRefEntity();
    attr = entityTypeAtDepth.getAttribute(attrTokens[i]);
    if (attr == null) {
      throw new UnknownAttributeException(entityTypeAtDepth, attrName);
    }
  }

  return attr;
}
 
Example 6
Source File: BasicTypeFilterVisitor.java    From gemini with Apache License 2.0 5 votes vote down vote up
public QueryWithParams visit(EntityField field, ComparisonNode node, FilterVisitorContext filterVisitorContext) {
    ComparisonOperator operator = node.getOperator();
    List<String> arguments = node.getArguments();
    QueryWithParams singleArg = handleSingleArgumentOperator(field, operator, arguments, filterVisitorContext);
    if (singleArg != null)
        return singleArg;
    QueryWithParams multipleArg = handleMultipleArgumentOperator(field, operator, arguments, filterVisitorContext);
    if (multipleArg == null) {
        throw new GeminiRuntimeException(String.format("Filter not impemented for type %s", field.getType()));
    }
    return multipleArg;
}
 
Example 7
Source File: RSQLUtility.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
// Exception squid:S2095 - see
// https://jira.sonarsource.com/browse/SONARJAVA-1478
@SuppressWarnings({ "squid:S2095" })
public List<Predicate> visit(final ComparisonNode node, final String param) {
    A fieldName = null;
    try {
        fieldName = getFieldEnumByName(node);
    } catch (final IllegalArgumentException e) {
        throw new RSQLParameterUnsupportedFieldException("The given search parameter field {"
                + node.getSelector() + "} does not exist, must be one of the following fields {"
                + Arrays.stream(enumType.getEnumConstants()).map(v -> v.name().toLowerCase())
                        .collect(Collectors.toList())
                + "}", e);

    }
    final String finalProperty = getAndValidatePropertyFieldName(fieldName, node);

    final List<String> values = node.getArguments();
    final List<Object> transformedValues = new ArrayList<>();
    final Path<Object> fieldPath = getFieldPath(fieldName, finalProperty);

    for (final String value : values) {
        transformedValues.add(convertValueIfNecessary(node, fieldName, value, fieldPath));
    }

    this.joinsNeeded = this.joinsNeeded || areJoinsNeeded(node);

    return mapToPredicate(node, fieldPath, node.getArguments(), transformedValues, fieldName, finalProperty);
}
 
Example 8
Source File: PermissionRsqlVisitor.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private PermissionsQuery setQuery(
    ComparisonNode comparisonNode, PermissionsQuery permissionsQuery) {
  String key = comparisonNode.getSelector();
  List<String> arguments = comparisonNode.getArguments();
  if (key.equalsIgnoreCase("user")) {
    permissionsQuery.setUsers(arguments);
  } else if (key.equalsIgnoreCase("role")) {
    permissionsQuery.setRoles(arguments);
  } else {
    throw new UnknownPermissionQueryParamException(key);
  }
  return permissionsQuery;
}
 
Example 9
Source File: MolgenisRSQLVisitor.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Query<Entity> visit(ComparisonNode node) {
  initQuery();
  String attrName = node.getSelector();
  String symbol = node.getOperator().getSymbol();
  List<String> values = node.getArguments();
  switch (symbol) {
    case "=notlike=":
      String notLikeValue = values.get(0);
      q.not().like(attrName, notLikeValue);
      break;
    case "=q=":
      String searchValue = values.get(0);
      if (attrName.equals("*")) {
        q.search(searchValue);
      } else {
        q.search(attrName, searchValue);
      }
      break;
    case "=sq=":
      String query = values.get(0);
      if (attrName.equals("*")) {
        q.searchQuery(query);
      } else {
        q.searchQuery(attrName, query);
      }
      break;
    case "==":
      Object eqValue = rsqlValueParser.parse(values.get(0), getAttribute(node));
      q.eq(attrName, eqValue);
      break;
    case "=in=":
      Attribute inAttr = getAttribute(node);
      q.in(
          attrName,
          values.stream().map(value -> rsqlValueParser.parse(value, inAttr)).collect(toList()));
      break;
    case "=lt=":
    case "<":
      Attribute ltAttr = getAttribute(node);
      validateNumericOrDate(ltAttr);
      Object ltValue = rsqlValueParser.parse(values.get(0), ltAttr);
      q.lt(attrName, ltValue);
      break;
    case "=le=":
    case "<=":
      Attribute leAttr = getAttribute(node);
      validateNumericOrDate(leAttr);
      Object leValue = rsqlValueParser.parse(values.get(0), leAttr);
      q.le(attrName, leValue);
      break;
    case "=gt=":
    case ">":
      Attribute gtAttr = getAttribute(node);
      validateNumericOrDate(gtAttr);
      Object gtValue = rsqlValueParser.parse(values.get(0), gtAttr);
      q.gt(attrName, gtValue);
      break;
    case "=ge=":
    case ">=":
      Attribute geAttr = getAttribute(node);
      validateNumericOrDate(geAttr);
      Object geValue = rsqlValueParser.parse(values.get(0), geAttr);
      q.ge(attrName, geValue);
      break;
    case "=rng=":
      Attribute rngAttr = getAttribute(node);
      validateNumericOrDate(rngAttr);
      Object fromValue =
          values.get(0) != null ? rsqlValueParser.parse(values.get(0), rngAttr) : null;
      Object toValue =
          values.get(1) != null ? rsqlValueParser.parse(values.get(1), rngAttr) : null;
      q.rng(attrName, fromValue, toValue);
      break;
    case "=like=":
      String likeValue = values.get(0);
      q.like(attrName, likeValue);
      break;
    case "!=":
      Object notEqValue = rsqlValueParser.parse(values.get(0), getAttribute(node));
      q.not().eq(attrName, notEqValue);
      break;
    case "=should=":
      throw new MolgenisQueryException("Unsupported RSQL query operator [" + symbol + "]");
    case "=dismax=":
      throw new MolgenisQueryException("Unsupported RSQL query operator [" + symbol + "]");
    case "=fuzzy=":
      throw new MolgenisQueryException("Unsupported RSQL query operator [" + symbol + "]");
    default:
      throw new MolgenisQueryException("Unknown RSQL query operator [" + symbol + "]");
  }
  return q;
}