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;
}
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;
}
Aggregations