Search in sources :

Example 11 with QueryItem

use of org.hisp.dhis.common.QueryItem 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 12 with QueryItem

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

the class DefaultEventQueryPlanner method groupByQueryItems.

/**
     * Group by items if query items are to be collapsed in order to aggregate
     * each item individually.
     * 
     * @param params the event query parameters.
     * @return a list of {@link EventQueryParams}.
     */
private List<EventQueryParams> groupByQueryItems(EventQueryParams params) {
    List<EventQueryParams> queries = new ArrayList<>();
    if (params.isAggregateData()) {
        for (QueryItem item : params.getItemsAndItemFilters()) {
            EventQueryParams.Builder query = new EventQueryParams.Builder(params).removeItems().removeItemProgramIndicators().withValue(item.getItem());
            if (item.hasProgram()) {
                query.withProgram(item.getProgram());
            }
            queries.add(query.build());
        }
        for (ProgramIndicator programIndicator : params.getItemProgramIndicators()) {
            EventQueryParams query = new EventQueryParams.Builder(params).removeItems().removeItemProgramIndicators().withProgramIndicator(programIndicator).withProgram(programIndicator.getProgram()).build();
            queries.add(query);
        }
    } else if (params.isCollapseDataDimensions() && !params.getItems().isEmpty()) {
        for (QueryItem item : params.getItems()) {
            EventQueryParams.Builder query = new EventQueryParams.Builder(params).removeItems().addItem(item);
            if (item.hasProgram()) {
                query.withProgram(item.getProgram());
            }
            queries.add(query.build());
        }
    } else {
        queries.add(new EventQueryParams.Builder(params).build());
    }
    return queries;
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) QueryItem(org.hisp.dhis.common.QueryItem) ArrayList(java.util.ArrayList) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator)

Example 13 with QueryItem

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

the class EventQueryParams method fromDataQueryParams.

public static EventQueryParams fromDataQueryParams(DataQueryParams dataQueryParams) {
    EventQueryParams params = new EventQueryParams();
    dataQueryParams.copyTo(params);
    EventQueryParams.Builder builder = new EventQueryParams.Builder(params);
    for (DimensionalItemObject object : dataQueryParams.getProgramDataElements()) {
        ProgramDataElementDimensionItem element = (ProgramDataElementDimensionItem) object;
        DataElement dataElement = element.getDataElement();
        QueryItem item = new QueryItem(dataElement, (dataElement.getLegendSets().isEmpty() ? null : dataElement.getLegendSets().get(0)), dataElement.getValueType(), dataElement.getAggregationType(), dataElement.getOptionSet());
        item.setProgram(element.getProgram());
        builder.addItem(item);
    }
    for (DimensionalItemObject object : dataQueryParams.getProgramAttributes()) {
        ProgramTrackedEntityAttributeDimensionItem element = (ProgramTrackedEntityAttributeDimensionItem) object;
        TrackedEntityAttribute attribute = element.getAttribute();
        QueryItem item = new QueryItem(attribute, (attribute.getLegendSets().isEmpty() ? null : attribute.getLegendSets().get(0)), attribute.getValueType(), attribute.getAggregationType(), attribute.getOptionSet());
        item.setProgram(element.getProgram());
        builder.addItem(item);
    }
    for (DimensionalItemObject object : dataQueryParams.getFilterProgramDataElements()) {
        ProgramDataElementDimensionItem element = (ProgramDataElementDimensionItem) object;
        DataElement dataElement = element.getDataElement();
        QueryItem item = new QueryItem(dataElement, (dataElement.getLegendSets().isEmpty() ? null : dataElement.getLegendSets().get(0)), dataElement.getValueType(), dataElement.getAggregationType(), dataElement.getOptionSet());
        item.setProgram(element.getProgram());
        builder.addItemFilter(item);
    }
    for (DimensionalItemObject object : dataQueryParams.getFilterProgramAttributes()) {
        ProgramTrackedEntityAttributeDimensionItem element = (ProgramTrackedEntityAttributeDimensionItem) object;
        TrackedEntityAttribute attribute = element.getAttribute();
        QueryItem item = new QueryItem(attribute, (attribute.getLegendSets().isEmpty() ? null : attribute.getLegendSets().get(0)), attribute.getValueType(), attribute.getAggregationType(), attribute.getOptionSet());
        builder.addItemFilter(item);
    }
    for (DimensionalItemObject object : dataQueryParams.getProgramIndicators()) {
        ProgramIndicator programIndicator = (ProgramIndicator) object;
        builder.addItemProgramIndicator(programIndicator);
    }
    return builder.withAggregateData(true).removeDimension(DATA_X_DIM_ID).build();
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) QueryItem(org.hisp.dhis.common.QueryItem) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttributeDimensionItem(org.hisp.dhis.program.ProgramTrackedEntityAttributeDimensionItem) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator)

Example 14 with QueryItem

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

the class EventQueryParams method getDuplicateQueryItems.

/**
     * Returns a list of query items which occur more than once, not including
     * the first duplicate.
     */
public List<QueryItem> getDuplicateQueryItems() {
    Set<QueryItem> dims = new HashSet<>();
    List<QueryItem> duplicates = new ArrayList<>();
    for (QueryItem dim : items) {
        if (!dims.add(dim)) {
            duplicates.add(dim);
        }
    }
    return duplicates;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 15 with QueryItem

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

the class TrackedEntityInstanceQueryParams method getDuplicateFilters.

/**
     * Returns a list of attributes which appear more than once.
     */
public List<QueryItem> getDuplicateFilters() {
    Set<QueryItem> items = new HashSet<>();
    List<QueryItem> duplicates = new ArrayList<>();
    for (QueryItem item : getFilters()) {
        if (!items.add(item)) {
            duplicates.add(item);
        }
    }
    return duplicates;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

QueryItem (org.hisp.dhis.common.QueryItem)28 ArrayList (java.util.ArrayList)9 QueryFilter (org.hisp.dhis.common.QueryFilter)8 Grid (org.hisp.dhis.common.Grid)7 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Map (java.util.Map)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)4 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)4 SqlHelper (org.hisp.dhis.commons.util.SqlHelper)4 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)4 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 PatientList (org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList)3 DimensionalObject (org.hisp.dhis.common.DimensionalObject)3