Search in sources :

Example 11 with IllegalQueryException

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

the class DefaultQueryPlanner method groupByStartEndDate.

@Override
public List<DataQueryParams> groupByStartEndDate(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (!params.getPeriods().isEmpty()) {
        for (DimensionalItemObject item : params.getPeriods()) {
            Period period = (Period) item;
            DataQueryParams query = DataQueryParams.newBuilder(params).withStartDate(period.getStartDate()).withEndDate(period.getEndDate()).build();
            BaseDimensionalObject staticPeriod = (BaseDimensionalObject) query.getDimension(PERIOD_DIM_ID);
            staticPeriod.setDimensionName(period.getIsoDate());
            staticPeriod.setFixed(true);
            queries.add(query);
        }
    } else if (!params.getFilterPeriods().isEmpty()) {
        Period period = (Period) params.getFilterPeriods().get(0);
        DataQueryParams query = DataQueryParams.newBuilder(params).withStartDate(period.getStartDate()).withEndDate(period.getEndDate()).removeFilter(PERIOD_DIM_ID).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 period: %d", queries.size()));
    }
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Example 12 with IllegalQueryException

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

the class DefaultEventQueryPlanner method validate.

// -------------------------------------------------------------------------
// EventQueryPlanner implementation
// -------------------------------------------------------------------------
@Override
public void validate(EventQueryParams params) throws IllegalQueryException, MaintenanceModeException {
    String violation = null;
    if (params == null) {
        throw new IllegalQueryException("Params cannot be null");
    }
    queryPlanner.validateMaintenanceMode();
    if (!params.hasOrganisationUnits()) {
        violation = "At least one organisation unit must be specified";
    }
    if (!params.getDuplicateDimensions().isEmpty()) {
        violation = "Dimensions cannot be specified more than once: " + params.getDuplicateDimensions();
    }
    if (!params.getDuplicateQueryItems().isEmpty()) {
        violation = "Query items cannot be specified more than once: " + params.getDuplicateQueryItems();
    }
    if (params.hasValueDimension() && params.getDimensionalObjectItems().contains(params.getValue())) {
        violation = "Value dimension cannot also be specified as an item or item filter";
    }
    if (params.hasAggregationType() && !(params.hasValueDimension() || params.isAggregateData())) {
        violation = "Value dimension or aggregate data must be specified when aggregation type is specified";
    }
    if (!params.hasPeriods() && (params.getStartDate() == null || params.getEndDate() == null)) {
        violation = "Start and end date or at least one period must be specified";
    }
    if (params.getStartDate() != null && params.getEndDate() != null && params.getStartDate().after(params.getEndDate())) {
        violation = "Start date is after end date: " + params.getStartDate() + " - " + params.getEndDate();
    }
    if (params.getPage() != null && params.getPage() <= 0) {
        violation = "Page number must be a positive number: " + params.getPage();
    }
    if (params.getPageSize() != null && params.getPageSize() < 0) {
        violation = "Page size must be zero or a positive number: " + params.getPageSize();
    }
    if (params.hasLimit() && getMaxLimit() > 0 && params.getLimit() > getMaxLimit()) {
        violation = "Limit of: " + params.getLimit() + " is larger than max limit: " + getMaxLimit();
    }
    if (params.hasClusterSize() && params.getClusterSize() <= 0) {
        violation = "Cluster size must be a positive number: " + params.getClusterSize();
    }
    if (params.hasBbox() && !ValidationUtils.bboxIsValid(params.getBbox())) {
        violation = "Bbox is invalid: " + params.getBbox() + ", must be on format: 'min-lng,min-lat,max-lng,max-lat'";
    }
    if ((params.hasBbox() || params.hasClusterSize()) && params.getCoordinateField() == null) {
        violation = "Cluster field must be specified when bbox or cluster size are specified";
    }
    for (QueryItem item : params.getItemsAndItemFilters()) {
        if (item.hasLegendSet() && item.hasOptionSet()) {
            violation = "Query item cannot specify both legend set and option set: " + item.getItemId();
        }
        if (params.isAggregateData() && !item.getAggregationType().isAggregateable()) {
            violation = "Query item must be aggregateable when used in aggregate query: " + item.getItemId();
        }
    }
    if (violation != null) {
        log.warn(String.format("Event analytics validation failed: %s", violation));
        throw new IllegalQueryException(violation);
    }
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Example 13 with IllegalQueryException

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

the class DefaultQueryValidator method validateForErrorMessage.

@Override
public ErrorMessage validateForErrorMessage(DataQueryParams params) {
    ErrorMessage error = null;
    if (params == null) {
        throw new IllegalQueryException(ErrorCode.E7100);
    }
    final List<DimensionalItemObject> dataElements = Lists.newArrayList(params.getDataElements());
    params.getProgramDataElements().forEach(pde -> dataElements.add(((ProgramDataElementDimensionItem) pde).getDataElement()));
    final List<DataElement> nonAggDataElements = FilterUtils.inverseFilter(asTypedList(dataElements), AggregatableDataElementFilter.INSTANCE);
    if (!params.isSkipDataDimensionValidation()) {
        if (params.getDimensions().isEmpty()) {
            error = new ErrorMessage(ErrorCode.E7101);
        } else if (!params.isSkipData() && params.getDataDimensionAndFilterOptions().isEmpty() && params.getAllDataElementGroups().isEmpty()) {
            error = new ErrorMessage(ErrorCode.E7102);
        } else if (!params.getDimensionsAsFilters().isEmpty()) {
            error = new ErrorMessage(ErrorCode.E7103, getDimensions(params.getDimensionsAsFilters()));
        }
    }
    if (!params.hasPeriods() && !params.isSkipPartitioning() && !params.hasStartEndDate()) {
        error = new ErrorMessage(ErrorCode.E7104);
    } else if (params.hasPeriods() && params.hasStartEndDate()) {
        error = new ErrorMessage(ErrorCode.E7105);
    } else if (params.hasStartEndDate() && params.startDateAfterEndDate()) {
        error = new ErrorMessage(ErrorCode.E7106);
    } else if (params.hasStartEndDate() && !params.getReportingRates().isEmpty()) {
        error = new ErrorMessage(ErrorCode.E7107);
    } else if ((!params.getFilterIndicators().isEmpty() || !params.getFilterProgramIndicators().isEmpty()) && params.getFilterOptions(DATA_X_DIM_ID).size() > 1) {
        error = new ErrorMessage(ErrorCode.E7108);
    } else if (!params.getFilterReportingRates().isEmpty() && params.getFilterOptions(DATA_X_DIM_ID).size() > 1) {
        error = new ErrorMessage(ErrorCode.E7109);
    } else if (params.getFilters().contains(new BaseDimensionalObject(CATEGORYOPTIONCOMBO_DIM_ID))) {
        error = new ErrorMessage(ErrorCode.E7110);
    } else if (!params.getDuplicateDimensions().isEmpty()) {
        error = new ErrorMessage(ErrorCode.E7111, getDimensions(params.getDuplicateDimensions()));
    } else if (!params.getAllReportingRates().isEmpty() && !params.containsOnlyDimensionsAndFilters(COMPLETENESS_DIMENSION_TYPES)) {
        error = new ErrorMessage(ErrorCode.E7112, COMPLETENESS_DIMENSION_TYPES);
    } else if (params.hasDimensionOrFilter(CATEGORYOPTIONCOMBO_DIM_ID) && params.getAllDataElements().isEmpty()) {
        error = new ErrorMessage(ErrorCode.E7113);
    } else if (params.hasDimensionOrFilter(CATEGORYOPTIONCOMBO_DIM_ID) && (params.getAllDataElements().size() != params.getAllDataDimensionItems().size())) {
        error = new ErrorMessage(ErrorCode.E7114);
    } else if (!nonAggDataElements.isEmpty()) {
        error = new ErrorMessage(ErrorCode.E7115, getUids(nonAggDataElements));
    } else if (!params.getSkipTotalDataElements().isEmpty()) {
        error = new ErrorMessage(ErrorCode.E7134);
    } else if (params.isOutputFormat(OutputFormat.DATA_VALUE_SET)) {
        if (!params.hasDimension(DATA_X_DIM_ID)) {
            error = new ErrorMessage(ErrorCode.E7117);
        } else if (!params.hasDimension(PERIOD_DIM_ID)) {
            error = new ErrorMessage(ErrorCode.E7118);
        } else if (!params.hasDimension(ORGUNIT_DIM_ID)) {
            error = new ErrorMessage(ErrorCode.E7119);
        }
    }
    return error;
}
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) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 14 with IllegalQueryException

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

the class DefaultDataQueryService method getDimension.

// TODO Optimize so that org unit levels + boundary are used in query
// instead of fetching all org units one by one.
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
    final boolean allItems = items.isEmpty();
    User user = currentUserService.getCurrentUser();
    if (DATA_X_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        for (String uid : items) {
            if (// DATA ELEMENT GROUP
            uid.startsWith(KEY_DE_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                    dimensionalKeywords.addKeyword(group);
                }
            } else if (// INDICATOR GROUP
            uid.startsWith(KEY_IN_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                    dimensionalKeywords.addKeyword(group);
                }
            } else {
                DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
                if (dimItemObject != null) {
                    dataDimensionItems.add(dimItemObject);
                }
            }
        }
        if (dataDimensionItems.isEmpty()) {
            throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.DATA_X_DIM_ID);
        }
        return new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems, dimensionalKeywords);
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
    } else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
    } else if (PERIOD_DIM_ID.equals(dimension)) {
        Calendar calendar = PeriodType.getCalendar();
        I18n i18n = i18nManager.getI18n();
        List<Period> periods = new ArrayList<>();
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        AnalyticsFinancialYearStartKey financialYearStart = systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_FINANCIAL_YEAR_START, AnalyticsFinancialYearStartKey.class);
        boolean containsRelativePeriods = false;
        for (String isoPeriod : items) {
            // Contains isoPeriod and timeField
            IsoPeriodHolder isoPeriodHolder = IsoPeriodHolder.of(isoPeriod);
            if (RelativePeriodEnum.contains(isoPeriodHolder.getIsoPeriod())) {
                containsRelativePeriods = true;
                RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriodHolder.getIsoPeriod());
                dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), i18n.getString(isoPeriodHolder.getIsoPeriod()));
                List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true, financialYearStart);
                // If custom time filter is specified, set it in periods
                if (isoPeriodHolder.hasDateField()) {
                    relativePeriods.forEach(period -> period.setDateField(isoPeriodHolder.getDateField()));
                }
                periods.addAll(relativePeriods);
            } else {
                Period period = PeriodType.getPeriodFromIsoString(isoPeriodHolder.getIsoPeriod());
                if (period != null) {
                    if (isoPeriodHolder.hasDateField()) {
                        period.setDescription(isoPeriodHolder.getIsoPeriod());
                        period.setDateField(isoPeriodHolder.getDateField());
                    }
                    dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), format != null ? i18n.getString(format.formatPeriod(period)) : isoPeriodHolder.getIsoPeriod());
                    periods.add(period);
                } else {
                    tryParseDateRange(isoPeriodHolder).ifPresent(periods::add);
                }
            }
        }
        // Remove duplicates
        periods = periods.stream().distinct().collect(Collectors.toList());
        if (containsRelativePeriods) {
            periods.sort(new AscendingPeriodComparator());
        }
        for (Period period : periods) {
            String name = format != null ? format.formatPeriod(period) : null;
            if (!period.getPeriodType().getName().contains(WeeklyPeriodType.NAME)) {
                period.setShortName(name);
            }
            period.setName(name);
            if (!calendar.isIso8601()) {
                period.setUid(getLocalPeriodIdentifier(period, calendar));
            }
        }
        return new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods), dimensionalKeywords);
    } else if (ORGUNIT_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> ous = new ArrayList<>();
        List<Integer> levels = new ArrayList<>();
        List<OrganisationUnitGroup> groups = new ArrayList<>();
        for (String ou : items) {
            if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(userOrgUnits);
            } else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
            } else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
            } else if (ou != null && ou.startsWith(KEY_LEVEL)) {
                String level = DimensionalObjectUtils.getValueFromKeywordParam(ou);
                Integer orgUnitLevel = organisationUnitService.getOrganisationUnitLevelByLevelOrUid(level);
                if (orgUnitLevel != null) {
                    levels.add(orgUnitLevel);
                }
            } else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
                String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
                OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
                if (group != null) {
                    groups.add(group);
                }
            } else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
                OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
                if (unit != null) {
                    ous.add(unit);
                }
            }
        }
        // Remove duplicates
        ous = ous.stream().distinct().collect(Collectors.toList());
        List<DimensionalItemObject> orgUnits = new ArrayList<>();
        List<OrganisationUnit> ousList = asTypedList(ous);
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        if (!levels.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
            dimensionalKeywords.addKeywords(levels.stream().map(level -> organisationUnitService.getOrganisationUnitLevelByLevel(level)).filter(Objects::nonNull).collect(Collectors.toList()));
        }
        if (!groups.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
            dimensionalKeywords.addKeywords(groups.stream().map(group -> new BaseNameableObject(group.getUid(), group.getCode(), group.getName())).collect(Collectors.toList()));
        }
        if (levels.isEmpty() && groups.isEmpty()) {
            orgUnits.addAll(ous);
        }
        if (!dimensionalKeywords.isEmpty()) {
            dimensionalKeywords.addKeywords(ousList);
        }
        if (orgUnits.isEmpty()) {
            throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.ORGUNIT_DIM_ID);
        }
        // Remove duplicates
        orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
        return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits, dimensionalKeywords);
    } else if (ORGUNIT_GROUP_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> ougs = new ArrayList<>();
        for (String uid : items) {
            OrganisationUnitGroup organisationUnitGroup = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
            if (organisationUnitGroup != null) {
                ougs.add(organisationUnitGroup);
            }
        }
        return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT_GROUP, null, DISPLAY_NAME_ORGUNIT_GROUP, ougs);
    } else if (LONGITUDE_DIM_ID.contains(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
    } else if (LATITUDE_DIM_ID.contains(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
    } else {
        DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
        if (dimObject != null && dimObject.isDataDimension()) {
            Class<?> dimClass = HibernateProxyUtils.getRealClass(dimObject);
            Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
            List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getOrdered(itemClass, inputIdScheme, items)) : getCanReadItems(user, dimObject);
            return new BaseDimensionalObject(dimObject.getDimension(), dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
        }
    }
    if (allowNull) {
        return null;
    }
    throw new IllegalQueryException(new ErrorMessage(ErrorCode.E7125, dimension));
}
Also used : KEY_USER_ORGUNIT_CHILDREN(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_CHILDREN) KEY_IN_GROUP(org.hisp.dhis.analytics.DataQueryParams.KEY_IN_GROUP) DISPLAY_NAME_LONGITUDE(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_LONGITUDE) DataQueryParams.getMeasureCriteriaFromParam(org.hisp.dhis.analytics.DataQueryParams.getMeasureCriteriaFromParam) Date(java.util.Date) DimensionService(org.hisp.dhis.common.DimensionService) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) AnalyticsSecurityManager(org.hisp.dhis.analytics.AnalyticsSecurityManager) Calendar(org.hisp.dhis.calendar.Calendar) I18nFormat(org.hisp.dhis.i18n.I18nFormat) IdentifiableProperty(org.hisp.dhis.common.IdentifiableProperty) ISO_FORMAT(org.hisp.dhis.period.DailyPeriodType.ISO_FORMAT) KEY_USER_ORGUNIT(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) AscendingPeriodComparator(org.hisp.dhis.period.comparator.AscendingPeriodComparator) Period(org.hisp.dhis.period.Period) KEY_USER_ORGUNIT_GRANDCHILDREN(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_GRANDCHILDREN) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Collection(java.util.Collection) LONGITUDE_DIM_ID(org.hisp.dhis.common.DimensionalObject.LONGITUDE_DIM_ID) FORMAT_DATE(org.hisp.dhis.i18n.I18nFormat.FORMAT_DATE) DISPLAY_NAME_PERIOD(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_PERIOD) Set(java.util.Set) AnalyticsUtils.throwIllegalQueryEx(org.hisp.dhis.analytics.util.AnalyticsUtils.throwIllegalQueryEx) DimensionType(org.hisp.dhis.common.DimensionType) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Objects(java.util.Objects) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) List(java.util.List) Stream(java.util.stream.Stream) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) LocalDate(java.time.LocalDate) AclService(org.hisp.dhis.security.acl.AclService) DISPLAY_NAME_LATITUDE(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_LATITUDE) ATTRIBUTEOPTIONCOMBO_DIM_ID(org.hisp.dhis.common.DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID) Optional(java.util.Optional) RelativePeriods(org.hisp.dhis.period.RelativePeriods) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) DISPLAY_NAME_DATA_X(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_DATA_X) ListUtils.sort(org.hisp.dhis.commons.collection.ListUtils.sort) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) ORGUNIT_GROUP_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_GROUP_DIM_ID) DataQueryService(org.hisp.dhis.analytics.DataQueryService) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalObjectUtils.asList(org.hisp.dhis.common.DimensionalObjectUtils.asList) DISPLAY_NAME_ORGUNIT_GROUP(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT_GROUP) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) I18n(org.hisp.dhis.i18n.I18n) ArrayList(java.util.ArrayList) LATITUDE_DIM_ID(org.hisp.dhis.common.DimensionalObject.LATITUDE_DIM_ID) BaseNameableObject(org.hisp.dhis.common.BaseNameableObject) KEY_LEVEL(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_LEVEL) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) DATA_X_DIM_ID(org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID) PERIOD_FREE_RANGE_SEPARATOR(org.hisp.dhis.common.DimensionalObject.PERIOD_FREE_RANGE_SEPARATOR) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) I18nManager(org.hisp.dhis.i18n.I18nManager) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) DISPLAY_NAME_CATEGORYOPTIONCOMBO(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_CATEGORYOPTIONCOMBO) IdentifiableObjectUtils.getLocalPeriodIdentifier(org.hisp.dhis.common.IdentifiableObjectUtils.getLocalPeriodIdentifier) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) DISPLAY_NAME_ORGUNIT(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) OutputFormat(org.hisp.dhis.analytics.OutputFormat) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DateTimeFormatter(java.time.format.DateTimeFormatter) PeriodType(org.hisp.dhis.period.PeriodType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) KEY_ORGUNIT_GROUP(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_ORGUNIT_GROUP) SettingKey(org.hisp.dhis.setting.SettingKey) DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdScheme(org.hisp.dhis.common.IdScheme) KEY_DE_GROUP(org.hisp.dhis.analytics.DataQueryParams.KEY_DE_GROUP) CATEGORYOPTIONCOMBO_DIM_ID(org.hisp.dhis.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID) Assert(org.springframework.util.Assert) User(org.hisp.dhis.user.User) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) ArrayList(java.util.ArrayList) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) DimensionalObjectUtils.asList(org.hisp.dhis.common.DimensionalObjectUtils.asList) ArrayList(java.util.ArrayList) I18n(org.hisp.dhis.i18n.I18n) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) AscendingPeriodComparator(org.hisp.dhis.period.comparator.AscendingPeriodComparator) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Objects(java.util.Objects) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) BaseNameableObject(org.hisp.dhis.common.BaseNameableObject)

Example 15 with IllegalQueryException

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

the class DefaultSqlViewService method validateSqlView.

@Override
public void validateSqlView(SqlView sqlView, Map<String, String> criteria, Map<String, String> variables) throws IllegalQueryException {
    ErrorMessage error = null;
    if (sqlView == null || sqlView.getSqlQuery() == null) {
        throw new IllegalQueryException(ErrorCode.E4300);
    }
    final Set<String> sqlVars = SqlViewUtils.getVariables(sqlView.getSqlQuery());
    final String sql = sqlView.getSqlQuery().replaceAll("\\r|\\n", " ").toLowerCase();
    final boolean ignoreSqlViewTableProtection = config.isDisabled(ConfigurationKey.SYSTEM_SQL_VIEW_TABLE_PROTECTION);
    final Set<String> allowedVariables = variables == null ? STANDARD_VARIABLES : Sets.union(variables.keySet(), STANDARD_VARIABLES);
    if (!SELECT_PATTERN.matcher(sql).matches()) {
        error = new ErrorMessage(ErrorCode.E4301);
    }
    if (sql.contains(";") && !sql.trim().endsWith(";")) {
        error = new ErrorMessage(ErrorCode.E4302);
    }
    if (variables != null && variables.containsKey(null)) {
        error = new ErrorMessage(ErrorCode.E4303);
    }
    if (variables != null && variables.containsValue(null)) {
        error = new ErrorMessage(ErrorCode.E4304);
    }
    if (variables != null && !getInvalidQueryParams(variables.keySet()).isEmpty()) {
        error = new ErrorMessage(ErrorCode.E4305, getInvalidQueryParams(variables.keySet()));
    }
    if (variables != null && !getInvalidQueryValues(variables.values()).isEmpty()) {
        error = new ErrorMessage(ErrorCode.E4306, getInvalidQueryValues(variables.values()));
    }
    if (sqlView.isQuery() && !sqlVars.isEmpty() && (!allowedVariables.containsAll(sqlVars))) {
        error = new ErrorMessage(ErrorCode.E4307, sqlVars);
    }
    if (sqlView.isQuery() && !sqlVars.isEmpty() && !getInvalidQueryParams(sqlVars).isEmpty()) {
        error = new ErrorMessage(ErrorCode.E4313, getInvalidQueryParams(sqlVars));
    }
    if (criteria != null && !getInvalidQueryParams(criteria.keySet()).isEmpty()) {
        error = new ErrorMessage(ErrorCode.E4308, getInvalidQueryParams(criteria.keySet()));
    }
    if (criteria != null && !getInvalidQueryValues(criteria.values()).isEmpty()) {
        error = new ErrorMessage(ErrorCode.E4309, getInvalidQueryValues(criteria.values()));
    }
    if (!ignoreSqlViewTableProtection && sql.matches(SqlView.getProtectedTablesRegex())) {
        error = new ErrorMessage(ErrorCode.E4310);
    }
    if (sql.matches(SqlView.getIllegalKeywordsRegex())) {
        error = new ErrorMessage(ErrorCode.E4311);
    }
    if (error != null) {
        log.warn(String.format("Validation failed for SQL view '%s' with code: '%s' and message: '%s'", sqlView.getUid(), error.getErrorCode(), error.getMessage()));
        throw new IllegalQueryException(error);
    }
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

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