Skip to content

Commit

Permalink
fix sonar issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyen committed Nov 19, 2024
1 parent 9048c64 commit 2b42ed1
Showing 1 changed file with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Criterion> iterator = query.getCriterions().iterator();
Iterator<Criterion> 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();
}
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 2b42ed1

Please sign in to comment.