From 517fa2111b16bc77a01360a066023b35f7023495 Mon Sep 17 00:00:00 2001 From: Sean Sundberg Date: Mon, 3 Jun 2024 00:08:49 -0500 Subject: [PATCH] Adds QueryClause builder Signed-off-by: Sean Sundberg --- .../openpages/support/models/BaseField.java | 5 +++ .../openpages/support/models/EnumValue.java | 5 +++ .../openpages/support/models/IObjectType.java | 5 +++ .../openpages/support/models/ObjectType.java | 17 +++++++ .../openpages/support/models/ResultValue.java | 1 + .../models/object_type/BusinessEntity.java | 3 ++ .../support/models/object_type/Metric.java | 3 ++ .../models/object_type/MetricValue.java | 3 ++ .../support/models/object_type/Model.java | 3 ++ .../object_type/ModelRiskScorecard.java | 3 ++ .../models/object_type/ModelUseCase.java | 3 ++ .../object_type/QuestionnaireAssessment.java | 3 ++ .../object_type/QuestionnaireTemplate.java | 3 ++ .../openpages/support/util/OpenPagesUtil.java | 5 +++ .../support/util/query/AndQueryClause.java | 37 +++++++++++++++ .../support/util/query/GroupQueryClause.java | 13 ++++++ .../support/util/query/Operation.java | 15 +++++++ .../support/util/query/OrQueryClause.java | 35 +++++++++++++++ .../support/util/query/QueryClause.java | 45 +++++++++++++++++++ .../support/util/query/SimpleQueryClause.java | 23 ++++++++++ 20 files changed, 230 insertions(+) create mode 100644 src/main/java/com/ibm/openpages/support/models/IObjectType.java create mode 100644 src/main/java/com/ibm/openpages/support/models/ObjectType.java create mode 100644 src/main/java/com/ibm/openpages/support/util/query/AndQueryClause.java create mode 100644 src/main/java/com/ibm/openpages/support/util/query/GroupQueryClause.java create mode 100644 src/main/java/com/ibm/openpages/support/util/query/Operation.java create mode 100644 src/main/java/com/ibm/openpages/support/util/query/OrQueryClause.java create mode 100644 src/main/java/com/ibm/openpages/support/util/query/QueryClause.java create mode 100644 src/main/java/com/ibm/openpages/support/util/query/SimpleQueryClause.java diff --git a/src/main/java/com/ibm/openpages/support/models/BaseField.java b/src/main/java/com/ibm/openpages/support/models/BaseField.java index 0357a43..8918cee 100644 --- a/src/main/java/com/ibm/openpages/support/models/BaseField.java +++ b/src/main/java/com/ibm/openpages/support/models/BaseField.java @@ -64,6 +64,11 @@ public FieldType fieldType() { return fieldType; } + @Override + public String baseValue() { + return originalValue; + } + @Override public String value() { return value; diff --git a/src/main/java/com/ibm/openpages/support/models/EnumValue.java b/src/main/java/com/ibm/openpages/support/models/EnumValue.java index 9f3f839..6546fa1 100644 --- a/src/main/java/com/ibm/openpages/support/models/EnumValue.java +++ b/src/main/java/com/ibm/openpages/support/models/EnumValue.java @@ -19,6 +19,11 @@ public String label() { return label; } + @Override + public String baseValue() { + return value; + } + @Override public String value() { return value; diff --git a/src/main/java/com/ibm/openpages/support/models/IObjectType.java b/src/main/java/com/ibm/openpages/support/models/IObjectType.java new file mode 100644 index 0000000..36c9f06 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/models/IObjectType.java @@ -0,0 +1,5 @@ +package com.ibm.openpages.support.models; + +public interface IObjectType { + String name(); +} diff --git a/src/main/java/com/ibm/openpages/support/models/ObjectType.java b/src/main/java/com/ibm/openpages/support/models/ObjectType.java new file mode 100644 index 0000000..485a915 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/models/ObjectType.java @@ -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; + } +} diff --git a/src/main/java/com/ibm/openpages/support/models/ResultValue.java b/src/main/java/com/ibm/openpages/support/models/ResultValue.java index db47bea..d43d834 100644 --- a/src/main/java/com/ibm/openpages/support/models/ResultValue.java +++ b/src/main/java/com/ibm/openpages/support/models/ResultValue.java @@ -1,5 +1,6 @@ package com.ibm.openpages.support.models; public interface ResultValue { + String baseValue(); String value(); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/BusinessEntity.java b/src/main/java/com/ibm/openpages/support/models/object_type/BusinessEntity.java index 4ef02da..a5ca832 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/BusinessEntity.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/BusinessEntity.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/Metric.java b/src/main/java/com/ibm/openpages/support/models/object_type/Metric.java index 3b87956..fb7a00c 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/Metric.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/Metric.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/MetricValue.java b/src/main/java/com/ibm/openpages/support/models/object_type/MetricValue.java index 4b5a1a7..2121078 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/MetricValue.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/MetricValue.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/Model.java b/src/main/java/com/ibm/openpages/support/models/object_type/Model.java index 38b3e69..7434b39 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/Model.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/Model.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/ModelRiskScorecard.java b/src/main/java/com/ibm/openpages/support/models/object_type/ModelRiskScorecard.java index 2bc75b0..7b29a38 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/ModelRiskScorecard.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/ModelRiskScorecard.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/ModelUseCase.java b/src/main/java/com/ibm/openpages/support/models/object_type/ModelUseCase.java index 68a7ca5..a265d2a 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/ModelUseCase.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/ModelUseCase.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireAssessment.java b/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireAssessment.java index 9127c1a..370e54f 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireAssessment.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireAssessment.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireTemplate.java b/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireTemplate.java index 5cd90b9..39c35b5 100644 --- a/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireTemplate.java +++ b/src/main/java/com/ibm/openpages/support/models/object_type/QuestionnaireTemplate.java @@ -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"); } diff --git a/src/main/java/com/ibm/openpages/support/util/OpenPagesUtil.java b/src/main/java/com/ibm/openpages/support/util/OpenPagesUtil.java index 5946fe6..f7049cf 100644 --- a/src/main/java/com/ibm/openpages/support/util/OpenPagesUtil.java +++ b/src/main/java/com/ibm/openpages/support/util/OpenPagesUtil.java @@ -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; @@ -600,4 +601,8 @@ public static List 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(); + } } diff --git a/src/main/java/com/ibm/openpages/support/util/query/AndQueryClause.java b/src/main/java/com/ibm/openpages/support/util/query/AndQueryClause.java new file mode 100644 index 0000000..582dd40 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/util/query/AndQueryClause.java @@ -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 clauses; + + protected AndQueryClause() { + this(new ArrayList<>()); + } + protected AndQueryClause(QueryClause... clauses) { + this(Arrays.asList(clauses)); + } + protected AndQueryClause(List 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 ")) + ")"; + } +} diff --git a/src/main/java/com/ibm/openpages/support/util/query/GroupQueryClause.java b/src/main/java/com/ibm/openpages/support/util/query/GroupQueryClause.java new file mode 100644 index 0000000..64c3e15 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/util/query/GroupQueryClause.java @@ -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); +} diff --git a/src/main/java/com/ibm/openpages/support/util/query/Operation.java b/src/main/java/com/ibm/openpages/support/util/query/Operation.java new file mode 100644 index 0000000..ca11873 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/util/query/Operation.java @@ -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; + } +} diff --git a/src/main/java/com/ibm/openpages/support/util/query/OrQueryClause.java b/src/main/java/com/ibm/openpages/support/util/query/OrQueryClause.java new file mode 100644 index 0000000..2a39b99 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/util/query/OrQueryClause.java @@ -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 clauses; + + protected OrQueryClause() { + this(new ArrayList<>()); + } + protected OrQueryClause(QueryClause... clauses) { + this(Arrays.asList(clauses)); + } + protected OrQueryClause(List 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 ")) + ")"; + } +} diff --git a/src/main/java/com/ibm/openpages/support/util/query/QueryClause.java b/src/main/java/com/ibm/openpages/support/util/query/QueryClause.java new file mode 100644 index 0000000..52d0527 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/util/query/QueryClause.java @@ -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(); +} diff --git a/src/main/java/com/ibm/openpages/support/util/query/SimpleQueryClause.java b/src/main/java/com/ibm/openpages/support/util/query/SimpleQueryClause.java new file mode 100644 index 0000000..fa41c74 --- /dev/null +++ b/src/main/java/com/ibm/openpages/support/util/query/SimpleQueryClause.java @@ -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; + } +}