Search in sources :

Example 31 with IllegalQueryException

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;
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ValueType.fromString(org.hisp.dhis.common.ValueType.fromString) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 32 with IllegalQueryException

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));
            }
        }
    }
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 33 with IllegalQueryException

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`"));
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 34 with IllegalQueryException

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`"));
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 35 with IllegalQueryException

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`"));
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HashSet(java.util.HashSet) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Test(org.junit.jupiter.api.Test)

Aggregations

IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)98 Test (org.junit.jupiter.api.Test)26 ErrorMessage (org.hisp.dhis.feedback.ErrorMessage)22 HashSet (java.util.HashSet)17 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)17 User (org.hisp.dhis.user.User)14 QueryItem (org.hisp.dhis.common.QueryItem)13 ArrayList (java.util.ArrayList)12 Date (java.util.Date)11 Program (org.hisp.dhis.program.Program)11 QueryFilter (org.hisp.dhis.common.QueryFilter)10 TrackedEntityInstanceCriteria (org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria)10 Transactional (org.springframework.transaction.annotation.Transactional)10 QueryOperator (org.hisp.dhis.common.QueryOperator)9 List (java.util.List)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 DataElement (org.hisp.dhis.dataelement.DataElement)8 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)7 Map (java.util.Map)6 Set (java.util.Set)6