Search in sources :

Example 6 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultTrackedEntityInstanceService method getFromUrl.

@Override
public TrackedEntityInstanceQueryParams getFromUrl(String query, Set<String> attribute, Set<String> filter, Set<String> ou, OrganisationUnitSelectionMode ouMode, String program, ProgramStatus programStatus, Boolean followUp, Date lastUpdatedStartDate, Date lastUpdatedEndDate, Date programEnrollmentStartDate, Date programEnrollmentEndDate, Date programIncidentStartDate, Date programIncidentEndDate, String trackedEntity, EventStatus eventStatus, Date eventStartDate, Date eventEndDate, boolean skipMeta, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, boolean includeDeleted, List<String> orders) {
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    QueryFilter queryFilter = getQueryFilter(query);
    if (attribute != null) {
        for (String attr : attribute) {
            QueryItem it = getQueryItem(attr);
            params.getAttributes().add(it);
        }
    }
    if (filter != null) {
        for (String filt : filter) {
            QueryItem it = getQueryItem(filt);
            params.getFilters().add(it);
        }
    }
    if (ou != null) {
        for (String orgUnit : ou) {
            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnit);
            if (organisationUnit == null) {
                throw new IllegalQueryException("Organisation unit does not exist: " + orgUnit);
            }
            params.getOrganisationUnits().add(organisationUnit);
        }
    }
    Program pr = program != null ? programService.getProgram(program) : null;
    if (program != null && pr == null) {
        throw new IllegalQueryException("Program does not exist: " + program);
    }
    TrackedEntity te = trackedEntity != null ? trackedEntityService.getTrackedEntity(trackedEntity) : null;
    if (trackedEntity != null && te == null) {
        throw new IllegalQueryException("Tracked entity does not exist: " + program);
    }
    params.setQuery(queryFilter).setProgram(pr).setProgramStatus(programStatus).setFollowUp(followUp).setLastUpdatedStartDate(lastUpdatedStartDate).setLastUpdatedEndDate(lastUpdatedEndDate).setProgramEnrollmentStartDate(programEnrollmentStartDate).setProgramEnrollmentEndDate(programEnrollmentEndDate).setProgramIncidentStartDate(programIncidentStartDate).setProgramIncidentEndDate(programIncidentEndDate).setTrackedEntity(te).setOrganisationUnitMode(ouMode).setEventStatus(eventStatus).setEventStartDate(eventStartDate).setEventEndDate(eventEndDate).setSkipMeta(skipMeta).setPage(page).setPageSize(pageSize).setTotalPages(totalPages).setSkipPaging(skipPaging).setIncludeDeleted(includeDeleted).setOrders(orders);
    return params;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryFilter(org.hisp.dhis.common.QueryFilter) Program(org.hisp.dhis.program.Program) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)

Example 7 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultTrackedEntityInstanceService method getQueryFilter.

/**
     * Creates a QueryFilter from the given query string. Query is on format
     * {operator}:{filter-value}. Only the filter-value is mandatory. The EQ
     * QueryOperator is used as operator if not specified.
     */
private QueryFilter getQueryFilter(String query) {
    if (query == null || query.isEmpty()) {
        return null;
    }
    if (!query.contains(DimensionalObject.DIMENSION_NAME_SEP)) {
        return new QueryFilter(QueryOperator.EQ, query);
    } else {
        String[] split = query.split(DimensionalObject.DIMENSION_NAME_SEP);
        if (split == null || split.length != 2) {
            throw new IllegalQueryException("Query has invalid format: " + query);
        }
        QueryOperator op = QueryOperator.fromString(split[0]);
        return new QueryFilter(op, split[1]);
    }
}
Also used : QueryFilter(org.hisp.dhis.common.QueryFilter) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) QueryOperator(org.hisp.dhis.common.QueryOperator)

Example 8 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class ExecuteSqlViewQueryAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
    if (id == null || (id.intValue() == -1)) {
        message = i18n.getString("sql_view_instance_invalid");
        return ERROR;
    }
    SqlView sqlView = sqlViewService.getSqlView(id);
    String viewName = sqlView.getViewName();
    try {
        message = sqlViewService.createViewTable(sqlView);
    } catch (IllegalQueryException ex) {
        message = ex.getMessage();
        return ERROR;
    }
    if (message != null && !message.isEmpty()) {
        message = i18n.getString("failed_to_create_view_table_for") + ": " + sqlView.getName() + "<br/><br/>" + i18n.getString("error_at") + ": " + message;
        return ERROR;
    }
    message = i18n.getString("sql_view_table_name") + " [ " + viewName + " ] " + i18n.getString("is_created");
    return SUCCESS;
}
Also used : SqlView(org.hisp.dhis.sqlview.SqlView) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Example 9 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultQueryPlanner method groupByPartition.

// -------------------------------------------------------------------------
// Supportive - group by methods
// -------------------------------------------------------------------------
@Override
public List<DataQueryParams> groupByPartition(DataQueryParams params, QueryPlannerParams plannerParams) {
    Set<String> validPartitions = partitionManager.getDataValueAnalyticsPartitions();
    String tableName = plannerParams.getTableName();
    String tableSuffix = plannerParams.getTableSuffix();
    List<DataQueryParams> queries = new ArrayList<>();
    if (params.isSkipPartitioning()) {
        DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(new Partitions().add(tableName)).build();
        queries.add(query);
    } else if (!params.getPeriods().isEmpty()) {
        ListMap<Partitions, DimensionalItemObject> partitionPeriodMap = PartitionUtils.getPartitionPeriodMap(params.getPeriods(), tableName, tableSuffix, validPartitions);
        for (Partitions partitions : partitionPeriodMap.keySet()) {
            if (partitions.hasAny()) {
                DataQueryParams query = DataQueryParams.newBuilder(params).withPeriods(partitionPeriodMap.get(partitions)).withPartitions(partitions).build();
                queries.add(query);
            }
        }
    } else if (!params.getFilterPeriods().isEmpty()) {
        Partitions partitions = PartitionUtils.getPartitions(params.getFilterPeriods(), tableName, tableSuffix, validPartitions);
        if (partitions.hasAny()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(partitions).build();
            queries.add(query);
        }
    } else if (params.hasStartEndDate()) {
        Partitions partitions = PartitionUtils.getPartitions(params.getStartDate(), params.getEndDate(), tableName, tableSuffix, validPartitions);
        if (partitions.hasAny()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(partitions).build();
            queries.add(query);
        }
    } else {
        throw new IllegalQueryException("Query does not contain any period dimension items");
    }
    if (queries.size() > 1) {
        log.debug(String.format("Split on partition: %d", queries.size()));
    }
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Partitions(org.hisp.dhis.analytics.Partitions) ArrayList(java.util.ArrayList) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ListMap(org.hisp.dhis.common.ListMap)

Example 10 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultQueryPlanner method validate.

// -------------------------------------------------------------------------
// DefaultQueryPlanner implementation
// -------------------------------------------------------------------------
@Override
public void validate(DataQueryParams params) throws IllegalQueryException {
    String violation = null;
    if (params == null) {
        throw new IllegalQueryException("Params cannot be null");
    }
    final List<DimensionalItemObject> dataElements = Lists.newArrayList(params.getDataElements());
    params.getProgramDataElements().stream().forEach(pde -> dataElements.add(((ProgramDataElementDimensionItem) pde).getDataElement()));
    final List<DataElement> nonAggDataElements = FilterUtils.inverseFilter(asTypedList(dataElements), AggregatableDataElementFilter.INSTANCE);
    if (params.getDimensions().isEmpty()) {
        violation = "At least one dimension must be specified";
    }
    if (!params.getDimensionsAsFilters().isEmpty()) {
        violation = "Dimensions cannot be specified as dimension and filter simultaneously: " + params.getDimensionsAsFilters();
    }
    if (!params.hasPeriods() && !params.isSkipPartitioning() && !params.hasStartEndDate()) {
        violation = "At least one period must be specified as dimension or filter";
    }
    if (params.hasPeriods() && params.hasStartEndDate()) {
        violation = "Periods and start and end dates cannot be specified simultaneously";
    }
    if (!params.getFilterIndicators().isEmpty() && params.getFilterOptions(DATA_X_DIM_ID).size() > 1) {
        violation = "Only a single indicator can be specified as filter";
    }
    if (!params.getFilterReportingRates().isEmpty() && params.getFilterOptions(DATA_X_DIM_ID).size() > 1) {
        violation = "Only a single reporting rate can be specified as filter";
    }
    if (params.getFilters().contains(new BaseDimensionalObject(CATEGORYOPTIONCOMBO_DIM_ID))) {
        violation = "Category option combos cannot be specified as filter";
    }
    if (!params.getDuplicateDimensions().isEmpty()) {
        violation = "Dimensions cannot be specified more than once: " + params.getDuplicateDimensions();
    }
    if (!params.getAllReportingRates().isEmpty() && !params.containsOnlyDimensionsAndFilters(COMPLETENESS_DIMENSION_TYPES)) {
        violation = "Reporting rates can only be specified together with dimensions of type: " + COMPLETENESS_DIMENSION_TYPES;
    }
    if (params.hasDimensionOrFilter(CATEGORYOPTIONCOMBO_DIM_ID) && params.getAllDataElements().isEmpty()) {
        violation = "Assigned categories cannot be specified when data elements are not specified";
    }
    if (params.hasDimensionOrFilter(CATEGORYOPTIONCOMBO_DIM_ID) && (params.getAllDataElements().size() != params.getAllDataDimensionItems().size())) {
        violation = "Assigned categories can only be specified together with data elements, not indicators or reporting rates";
    }
    if (!nonAggDataElements.isEmpty()) {
        violation = "Data elements must be of a value and aggregation type that allow aggregation: " + getUids(nonAggDataElements);
    }
    if (params.isOutputFormat(OutputFormat.DATA_VALUE_SET)) {
        if (!params.hasDimension(DATA_X_DIM_ID)) {
            violation = "A data dimension 'dx' must be specified when output format is DATA_VALUE_SET";
        }
        if (!params.hasDimension(PERIOD_DIM_ID)) {
            violation = "A period dimension 'pe' must be specified when output format is DATA_VALUE_SET";
        }
        if (!params.hasDimension(ORGUNIT_DIM_ID)) {
            violation = "An organisation unit dimension 'ou' must be specified when output format is DATA_VALUE_SET";
        }
    }
    if (violation != null) {
        log.warn(String.format("Analytics validation failed: %s", violation));
        throw new IllegalQueryException(violation);
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

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