Skip to content

Commit

Permalink
Update QueryClause logic to inherit type if not provided (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Sundberg <[email protected]>
  • Loading branch information
seansund authored Jun 4, 2024
1 parent 2289af2 commit ead773d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
package com.ibm.openpages.support.util.query;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.ibm.openpages.support.models.IObjectType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AndQueryClause extends GroupQueryClause {
private final List<QueryClause> clauses;

protected AndQueryClause() {
this(new ArrayList<>());
}
protected AndQueryClause(QueryClause... clauses) {
this(Arrays.asList(clauses));
}
protected AndQueryClause(List<QueryClause> clauses) {
this.clauses = clauses;
}

@Override
public boolean isAnd() {
return true;
protected AndQueryClause(IObjectType type) {
this(type, new ArrayList<>());
}

@Override
public void add(QueryClause clause) {
clauses.add(clause);
protected AndQueryClause(IObjectType type, QueryClause... clauses) {
this(type, Arrays.asList(clauses));
}

@Override
public String toClause() {
return "(" + clauses.stream().map(QueryClause::toClause).collect(Collectors.joining(" AND ")) + ")";
protected AndQueryClause(IObjectType type, List<QueryClause> clauses) {
super(true, type, clauses);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
package com.ibm.openpages.support.util.query;

import com.ibm.openpages.support.models.IObjectType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public abstract class GroupQueryClause extends QueryClause {
private final boolean and;
private final List<QueryClause> clauses;

protected GroupQueryClause(boolean and, IObjectType type) {
this(and, type, new ArrayList<>());
}
protected GroupQueryClause(boolean and, IObjectType type, QueryClause... clauses) {
this(and, type, Arrays.asList(clauses));
}
protected GroupQueryClause(boolean and, IObjectType type, List<QueryClause> clauses) {
super(type);

this.and = and;
this.clauses = clauses;
}

public boolean isAnd() {
return false;
return and;
}
public boolean isOr() {
return false;
return !and;
}

public void add(QueryClause clause) {
clauses.add(clause);
}

public abstract void add(QueryClause clause);
@Override
public String toClause() {
final String delimiter = and ? " AND " : " OR ";

return "(" + clauses.stream().map(QueryClause::toClause).collect(Collectors.joining(delimiter)) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
package com.ibm.openpages.support.util.query;

import com.ibm.openpages.support.models.IObjectType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class OrQueryClause extends GroupQueryClause {
private final List<QueryClause> clauses;

protected OrQueryClause() {
this(new ArrayList<>());
}
protected OrQueryClause(QueryClause... clauses) {
this(Arrays.asList(clauses));
}
protected OrQueryClause(List<QueryClause> clauses) {
this.clauses = clauses;
}

@Override
public boolean isOr() {
return true;
protected OrQueryClause(IObjectType type) {
this(type, new ArrayList<>());
}

@Override
public void add(QueryClause clause) {
clauses.add(clause);
protected OrQueryClause(IObjectType type, QueryClause... clauses) {
this(type, Arrays.asList(clauses));
}

@Override
public String toClause() {
return "(" + clauses.stream().map(QueryClause::toClause).collect(Collectors.joining(" OR ")) + ")";
protected OrQueryClause(IObjectType type, List<QueryClause> clauses) {
super(false, type, clauses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@

public abstract class QueryClause {

protected final IObjectType type;

protected QueryClause(IObjectType type) {
this.type = type;
}

public QueryClauseType forType(IObjectType type) {
return new QueryClauseType(type);
}

public static QueryClause where(IObjectType type, ResultValue field, Operation operation, ResultValue value) {
return where(type, field, operation, value.value());
}
public static QueryClause where(IObjectType type, ResultValue field, Operation operation, Object value) {
return new SimpleQueryClause(type, field, operation, value);
}

public QueryClause and(ResultValue field, Operation operation, ResultValue value) {
return and(type, field, operation, value);
}
public QueryClause and(ResultValue field, Operation operation, Object value) {
return and(type, field, operation, value);
}
public QueryClause and(IObjectType type, ResultValue field, Operation operation, ResultValue value) {
return and(type, field, operation, value.value());
}
Expand All @@ -28,9 +44,15 @@ public QueryClause and(QueryClause clause) {
return this;
}

return new AndQueryClause(this, clause);
return new AndQueryClause(this.type, this, clause);
}

public QueryClause or(ResultValue field, Operation operation, ResultValue value) {
return or(type, field, operation, value);
}
public QueryClause or(ResultValue field, Operation operation, Object value) {
return or(type, field, operation, value);
}
public QueryClause or(IObjectType type, ResultValue field, Operation operation, ResultValue value) {
return or(type, field, operation, value.value());
}
Expand All @@ -47,7 +69,7 @@ public QueryClause or(QueryClause clause) {
return this;
}

return new OrQueryClause(this, clause);
return new OrQueryClause(this.type, this, clause);
}

public abstract String toClause();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ibm.openpages.support.util.query;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ResultValue;

public class QueryClauseType {

private final IObjectType type;

protected QueryClauseType(IObjectType type) {
this.type = type;
}

public QueryClauseType forType(IObjectType type) {
return new QueryClauseType(type);
}

public QueryClause where(ResultValue field, Operation operation, ResultValue value) {
return where(field, operation, value.value());
}
public QueryClause where(ResultValue field, Operation operation, Object value) {
return new SimpleQueryClause(type, field, operation, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import com.ibm.openpages.support.models.ResultValue;

public class SimpleQueryClause extends QueryClause {
private final IObjectType type;
private final ResultValue field;
private final Operation operation;
private final Object value;

protected SimpleQueryClause(IObjectType type, ResultValue field, Operation operation, Object value) {
this.type = type;
super(type);
this.field = field;
this.operation = operation;
this.value = value;
Expand Down

0 comments on commit ead773d

Please sign in to comment.