use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class FilteringHelper method extractValueFromFilter.
/**
* Extracts the actual value, from the set of filters, that matches the
* given combination.
*
* ie.: from a list of filters: "dimensionItemType:eq:INDICATOR",
* "name:ilike:john", extract the value from the combination NAME_ILIKE(
* "name:ilike:" ). This will return "john".
*
* @param filters
* @param filterCombination
* @return the value extracted from the respective filter combination
*/
public static String extractValueFromFilter(final Set<String> filters, final Filter.Combination filterCombination) {
final byte FILTER_VALUE = 2;
if (CollectionUtils.isNotEmpty(filters)) {
for (final String filter : filters) {
if (filterHasPrefix(filter, filterCombination.getCombination())) {
final String[] array = filter.split(":");
final boolean hasValue = array.length == 3;
if (hasValue) {
return array[FILTER_VALUE];
} else {
throw new IllegalQueryException(new ErrorMessage(E2014, filter));
}
}
}
}
return EMPTY;
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class OrderValidator method checkOrderParams.
/**
* Checks if the given set o filters are valid, and contains only filter
* names and operators supported.
*
* @param orderParams a set containing elements in the format
* "attributeName:asc"
* @throws IllegalQueryException if the set contains a non-supported name or
* operator, or contains invalid syntax.
*/
public static void checkOrderParams(final Set<String> orderParams) {
if (isNotEmpty(orderParams)) {
for (final String orderParam : orderParams) {
final String[] orderAttributeValuePair = orderParam.split(":");
final String orderAttributeName = trimToEmpty(orderAttributeValuePair[ORDERING_ATTRIBUTE_NAME]);
final String orderValue = trimToEmpty(orderAttributeValuePair[ORDERING_VALUE]);
final boolean filterHasCorrectForm = orderAttributeValuePair.length == 2;
if (filterHasCorrectForm) {
// attributes are allowed.
if (!getNames().contains(orderAttributeName)) {
throw new IllegalQueryException(new ErrorMessage(E2037, orderAttributeName));
}
// allowed.
if (!getValues().contains(orderValue)) {
throw new IllegalQueryException(new ErrorMessage(E2037, orderValue));
}
} else {
throw new IllegalQueryException(new ErrorMessage(E2015, orderParam));
}
}
}
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class OrderValidatorTest method testCheckOrderParamsWhenOrderingFormIsInvalid.
@Test
void testCheckOrderParamsWhenOrderingFormIsInvalid() {
// Given
final Set<String> orderings = new HashSet<>(singletonList("name:asc:invalid"));
// When throws
final IllegalQueryException thrown = assertThrows(IllegalQueryException.class, () -> checkOrderParams(orderings));
// Then
assertThat(thrown.getMessage(), containsString("Unable to parse order param: `name:asc:invalid`"));
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class OrderValidatorTest method testCheckOrderParamsWhenOrderAttributeIsNotSupported.
@Test
void testCheckOrderParamsWhenOrderAttributeIsNotSupported() {
// Given
final Set<String> orderings = new HashSet<>(singletonList("notSupportedAttribute:asc"));
// When throws
final IllegalQueryException thrown = assertThrows(IllegalQueryException.class, () -> checkOrderParams(orderings));
// Then
assertThat(thrown.getMessage(), containsString("Order not supported: `notSupportedAttribute`"));
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class FilterValidatorTest method testCheckNamesAndOperatorsWhenFilterHasInvalidFormat.
@Test
void testCheckNamesAndOperatorsWhenFilterHasInvalidFormat() {
// Given
final Set<String> filters = new HashSet<>(singletonList("name:ilike:someName:bla:bla"));
// When throws
final IllegalQueryException thrown = assertThrows(IllegalQueryException.class, () -> checkNamesAndOperators(filters));
// Then
assertThat(thrown.getMessage(), containsString("Unable to parse filter `name:ilike:someName:bla:bla`"));
}
Aggregations