diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/planner/DefaultQueryPlanner.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/planner/DefaultQueryPlanner.java index 083bbcdcfce4..e692133846c1 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/planner/DefaultQueryPlanner.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/planner/DefaultQueryPlanner.java @@ -181,33 +181,13 @@ public Path getQueryPath(Root root, Schema schema, String path) { */ private Query getQuery(Query query, boolean persistedOnly) { Query pQuery = Query.from(query.getSchema(), query.getRootJunctionType()); - Iterator iterator = query.getCriterions().iterator(); + Iterator criterions = query.getCriterions().iterator(); - while (iterator.hasNext()) { - Criterion criterion = iterator.next(); - - if (criterion instanceof Junction) { - Junction junction = handleJunction(pQuery, (Junction) criterion, persistedOnly); - - if (!junction.getCriterions().isEmpty()) { - pQuery.getAliases().addAll(junction.getAliases()); - pQuery.add(junction); - } - - if (((Junction) criterion).getCriterions().isEmpty()) { - iterator.remove(); - } - } else if (criterion instanceof Restriction) { - Restriction restriction = (Restriction) criterion; - restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath())); - - if (restriction.getQueryPath().isPersisted() && !isAttributeFilter(query, restriction)) { - pQuery.getCriterions().add(criterion); - iterator.remove(); - if (restriction.getQueryPath().haveAlias()) { - pQuery.getAliases().addAll(Arrays.asList(restriction.getQueryPath().getAlias())); - } - } + while (criterions.hasNext()) { + Criterion criterion = criterions.next(); + if (addJunction(criterion, pQuery, persistedOnly) + || addRestriction(query, criterion, pQuery)) { + criterions.remove(); } } @@ -279,4 +259,48 @@ private boolean isAttributeFilter(Query query, Restriction restriction) { return query.getSchema().getKlass().isAssignableFrom(Attribute.class) && Attribute.ObjectType.isValidType(restriction.getQueryPath().getPath()); } + + /** + * Add given criterion which is an instance of {@link Junction} to given {@link Query} pQuery. If + * successfully added, return TRUE indicating that the given {@link Criterion} criterion should be + * removed. + */ + private boolean addJunction(Criterion criterion, Query pQuery, boolean persistedOnly) { + if (!(criterion instanceof Junction)) { + return false; + } + boolean shouldRemove = false; + Junction junction = handleJunction(pQuery, (Junction) criterion, persistedOnly); + if (!junction.getCriterions().isEmpty()) { + pQuery.getAliases().addAll(junction.getAliases()); + pQuery.add(junction); + } + if (((Junction) criterion).getCriterions().isEmpty()) { + shouldRemove = true; + } + return shouldRemove; + } + + /** + * Add given criterion which is an instance of {@link Restriction} to given {@link Query} pQuery. + * If successfully added, return TRUE indicating that the given {@link Criterion} criterion should + * be removed. + */ + private boolean addRestriction(Query query, Criterion criterion, Query pQuery) { + if (!(criterion instanceof Restriction)) { + return false; + } + boolean shouldRemove = false; + Restriction restriction = (Restriction) criterion; + restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath())); + + if (restriction.getQueryPath().isPersisted() && !isAttributeFilter(query, restriction)) { + pQuery.getCriterions().add(criterion); + shouldRemove = true; + if (restriction.getQueryPath().haveAlias()) { + pQuery.getAliases().addAll(Arrays.asList(restriction.getQueryPath().getAlias())); + } + } + return shouldRemove; + } }