Skip to content

Commit

Permalink
Adds QueryClause builder (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Sundberg <[email protected]>
  • Loading branch information
seansund authored Jun 3, 2024
1 parent a469527 commit 152fc00
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/ibm/openpages/support/models/BaseField.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public FieldType<?> fieldType() {
return fieldType;
}

@Override
public String baseValue() {
return originalValue;
}

@Override
public String value() {
return value;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/ibm/openpages/support/models/EnumValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public String label() {
return label;
}

@Override
public String baseValue() {
return value;
}

@Override
public String value() {
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ibm.openpages.support.models;

public interface IObjectType {
String name();
}
17 changes: 17 additions & 0 deletions src/main/java/com/ibm/openpages/support/models/ObjectType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ibm.openpages.support.models;

public class ObjectType implements IObjectType {
private final String name;

public ObjectType(String name) {
this.name = name;
}

public static ObjectType from(String name) {
return new ObjectType(name);
}

public String name() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ibm.openpages.support.models;

public interface ResultValue {
String baseValue();
String value();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.MRG_BusEnt;
import com.ibm.openpages.support.models.field_group.OPSS_BE;
import com.ibm.openpages.support.models.field_group.OPSS_BusEnt;
import com.ibm.openpages.support.models.field_group.SystemFields;

public interface BusinessEntity extends SystemFields, MRG_BusEnt, OPSS_BE, OPSS_BusEnt {
IObjectType type = new ObjectType("BusinessEntity");
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.MRG_Metric;
import com.ibm.openpages.support.models.field_group.MRG_Metric_Shared;
import com.ibm.openpages.support.models.field_group.OPSS_Shared_Lib;
import com.ibm.openpages.support.models.field_group.SystemFields;

public interface Metric extends SystemFields, MRG_Metric, MRG_Metric_Shared, OPSS_Shared_Lib {
IObjectType type = new ObjectType("Metric");
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.MRG_MetricVal;
import com.ibm.openpages.support.models.field_group.MRG_Metric_Shared;
import com.ibm.openpages.support.models.field_group.SystemFields;

public interface MetricValue extends SystemFields, MRG_Metric_Shared, MRG_MetricVal {
IObjectType type = new ObjectType("MetricValue");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.*;

public interface Model extends SystemFields, Integration, IntegrationWKC, MRG_AIFacts_Model, MRG_Model, MRG_MTS_Shared, MRG_UserFact_Model {
IObjectType type = new ObjectType("Model");
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.MRG_MTS;
import com.ibm.openpages.support.models.field_group.MRG_MTS_Shared;
import com.ibm.openpages.support.models.field_group.SystemFields;

public interface ModelRiskScorecard extends SystemFields, MRG_MTS, MRG_MTS_Shared {
IObjectType type = new ObjectType("ModelRiskScorecard");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.*;

public interface ModelUseCase extends SystemFields, Integration, IntegrationWKC, MRG_AIFacts_ModelUseCase, MRG_ModelUseCase, MRG_UserFact_ModelUseCase, Watsonx_UseCaseChecklist {
IObjectType type = new ObjectType("ModelUseCase");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.*;

public interface QuestionnaireAssessment extends SystemFields, OPLC_QAssessment, OPLC_Std, OPSS_QAssessment, Watsonx_QAssessment {
IObjectType type = new ObjectType("QuestionnaireAssessment");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.ibm.openpages.support.models.object_type;

import com.ibm.openpages.support.models.IObjectType;
import com.ibm.openpages.support.models.ObjectType;
import com.ibm.openpages.support.models.field_group.*;

public interface QuestionnaireTemplate extends SystemFields, OPLC_Owners, OPSS_Qtemp, OPSS_Qtemp_Config, OPSS_Qtemp_Shared {
IObjectType type = new ObjectType("QuestionnaireTemplate");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.ibm.openpages.support.models.ResultValueAndLabel;
import com.ibm.openpages.support.models.object_type.Metric;
import com.ibm.openpages.support.models.object_type.MetricValue;
import com.ibm.openpages.support.util.query.QueryClause;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -600,4 +601,8 @@ public static List<IGRCObject> getAssessmentsFromModel(IResourceService service,
.map(node -> lookupResource(service, node, IncludeAssociations.CHILD))
.collect(Collectors.toList());
}

public static String buildQuery(String baseQuery, QueryClause clause) {
return baseQuery + " WHERE " + clause.toClause();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ibm.openpages.support.util.query;

import com.sun.org.apache.xpath.internal.operations.Or;

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;
}

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

@Override
public String toClause() {
return "(" + clauses.stream().map(QueryClause::toClause).collect(Collectors.joining(" AND ")) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ibm.openpages.support.util.query;

public abstract class GroupQueryClause extends QueryClause {

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

public abstract void add(QueryClause clause);
}
15 changes: 15 additions & 0 deletions src/main/java/com/ibm/openpages/support/util/query/Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ibm.openpages.support.util.query;

public enum Operation {
EQUAL("=");

private final String value;

Operation(String value) {
this.value = value;
}

public String value() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ibm.openpages.support.util.query;

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;
}

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

@Override
public String toClause() {
return "(" + clauses.stream().map(QueryClause::toClause).collect(Collectors.joining(" OR ")) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ibm.openpages.support.util.query;

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

public abstract class QueryClause {

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

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

public QueryClause and(QueryClause clause) {
if (this instanceof GroupQueryClause && ((GroupQueryClause)this).isAnd()) {
final GroupQueryClause groupClause = (GroupQueryClause)this;

groupClause.add(clause);

return this;
}

return new AndQueryClause(this, clause);
}

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

public QueryClause or(QueryClause clause) {
if (this instanceof GroupQueryClause && ((GroupQueryClause)this).isOr()) {
final GroupQueryClause groupClause = (GroupQueryClause)this;

groupClause.add(clause);

return this;
}

return new OrQueryClause(this, clause);
}

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

import com.ibm.openpages.support.models.IObjectType;
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;
this.field = field;
this.operation = operation;
this.value = value;
}

@Override
public String toClause() {
return "[" + type.name() + "].[" + field.baseValue() + "] " + operation.value() + " " + value;
}
}

0 comments on commit 152fc00

Please sign in to comment.