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);
}
}
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;
}
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();
}
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;
}
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;
}
Aggregations