Search in sources :

Example 1 with QueryFormatException

use of com.github.tfaga.lynx.exceptions.QueryFormatException in project lynx by TFaga.

the class QueryStringBuilder method buildOrder.

private QueryOrder buildOrder(String key, String value) {
    log.finest("Building order string: " + value);
    if (value == null || value.isEmpty())
        return null;
    QueryOrder o = new QueryOrder();
    String[] pair = value.split("(\\s|\\+)");
    if (pair[0].isEmpty()) {
        String msg = "Value for '" + key + "' is malformed: '" + value + "'";
        log.finest(msg);
        throw new QueryFormatException(msg, key, QueryFormatError.MALFORMED);
    }
    o.setField(pair[0]);
    if (pair.length > 1) {
        try {
            o.setOrder(OrderDirection.valueOf(pair[1].toUpperCase()));
        } catch (IllegalArgumentException e) {
            String msg = "Constant in '" + key + "' does not exist: '" + value + "'";
            log.finest(msg);
            throw new QueryFormatException(msg, key, QueryFormatError.NO_SUCH_CONSTANT);
        }
    } else {
        o.setOrder(OrderDirection.ASC);
    }
    return o;
}
Also used : QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) QueryFormatException(com.github.tfaga.lynx.exceptions.QueryFormatException)

Example 2 with QueryFormatException

use of com.github.tfaga.lynx.exceptions.QueryFormatException in project lynx by TFaga.

the class QueryStringBuilder method buildFilter.

private List<QueryFilter> buildFilter(String key, String value) {
    log.finest("Building filter string: " + value);
    List<QueryFilter> filterList = new ArrayList<>();
    if (value == null || value.isEmpty())
        return filterList;
    Pattern filterPattern = Pattern.compile("[:]+(?=([^']*'[^']*')*[^']*$)");
    List<String[]> filters = Arrays.stream(value.split("[(\\s|+)]+(?=([^']*'[^']*')*[^']*$)")).map(filterPattern::split).collect(Collectors.toList());
    filters.stream().filter(f -> f.length == 2).forEach(f -> {
        QueryFilter qf = new QueryFilter();
        qf.setField(f[0]);
        qf.setOperation(parseFilterOperation(key, f[1].toUpperCase()));
        if (qf.getOperation() == FilterOperation.ISNULL || qf.getOperation() == FilterOperation.ISNOTNULL) {
            filterList.add(qf);
        }
    });
    Pattern valueListPattern = Pattern.compile("^\\[(.*)]$");
    Pattern valuesPattern = Pattern.compile("[,]+(?=([^']*'[^']*')*[^']*$)");
    Pattern valueDateTimePattern = Pattern.compile("^dt'(.*)'$");
    Pattern valuePattern = Pattern.compile("(^')|('$)");
    filters.stream().filter(f -> f.length == 3).forEach(f -> {
        QueryFilter qf = new QueryFilter();
        qf.setField(f[0]);
        qf.setOperation(parseFilterOperation(key, f[1].toUpperCase()));
        Matcher matcher;
        if ((matcher = valueListPattern.matcher(f[2])).find() && (qf.getOperation() == FilterOperation.IN || qf.getOperation() == FilterOperation.NIN || qf.getOperation() == FilterOperation.NINIC || qf.getOperation() == FilterOperation.INIC)) {
            String values = matcher.group(1);
            Arrays.stream(valuesPattern.split(values)).filter(e -> !e.isEmpty()).distinct().map(e -> valuePattern.matcher(e).replaceAll("")).forEach(e -> qf.getValues().add(e));
        } else if ((matcher = valueDateTimePattern.matcher(f[2])).find()) {
            Date d = parseDate(matcher.group(1));
            if (d == null) {
                String msg = "Value for '" + key + "' is malformed: '" + value + "'";
                log.finest(msg);
                throw new QueryFormatException(msg, key, QueryFormatError.MALFORMED);
            }
            qf.setDateValue(d);
        } else {
            qf.setValue(valuePattern.matcher(f[2]).replaceAll(""));
        }
        filterList.add(qf);
    });
    return filterList;
}
Also used : java.util(java.util) URLDecoder(java.net.URLDecoder) OrderDirection(com.github.tfaga.lynx.enums.OrderDirection) QueryFormatException(com.github.tfaga.lynx.exceptions.QueryFormatException) ZonedDateTime(java.time.ZonedDateTime) QueryFormatError(com.github.tfaga.lynx.enums.QueryFormatError) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) URLEncoder(java.net.URLEncoder) DateTimeParseException(java.time.format.DateTimeParseException) QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) Matcher(java.util.regex.Matcher) FilterOperation(com.github.tfaga.lynx.enums.FilterOperation) URI(java.net.URI) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) Pattern(java.util.regex.Pattern) QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) Matcher(java.util.regex.Matcher) QueryFormatException(com.github.tfaga.lynx.exceptions.QueryFormatException)

Aggregations

QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)2 QueryFormatException (com.github.tfaga.lynx.exceptions.QueryFormatException)2 QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)1 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)1 FilterOperation (com.github.tfaga.lynx.enums.FilterOperation)1 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)1 QueryFormatError (com.github.tfaga.lynx.enums.QueryFormatError)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URLDecoder (java.net.URLDecoder)1 URLEncoder (java.net.URLEncoder)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 java.util (java.util)1 Logger (java.util.logging.Logger)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1