diff --git a/.gitignore b/.gitignore index f1822f8..1e2c7c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /jqb2jooq.iml /target .DS_Store +/.idea/ diff --git a/pom.xml b/pom.xml index 28f3a72..79e8acf 100644 --- a/pom.xml +++ b/pom.xml @@ -153,6 +153,24 @@ + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + + generate-test-sources + + add-test-source + + + + ${project.build.directory}/generated-test-sources + + + + + diff --git a/src/main/java/io/kowalski/jqb2jooq/FilterParser.java b/src/main/java/io/kowalski/jqb2jooq/FilterParser.java index 3aa723a..db56e34 100644 --- a/src/main/java/io/kowalski/jqb2jooq/FilterParser.java +++ b/src/main/java/io/kowalski/jqb2jooq/FilterParser.java @@ -13,24 +13,24 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) class FilterParser { - protected static Filter parseJSON(final Class targetClass, final Map jsonFilter) { + static Filter parseJSON(final RuleTargetBuilder ruleTargetBuilder, final Map jsonFilter) { RuleSet outerRuleSet = new RuleSet(); outerRuleSet.setOperator(BooleanOperator.valueOf(jsonFilter.get("condition").toString())); - outerRuleSet.setRules(parseRaw(targetClass, parseRulesList(jsonFilter))); + outerRuleSet.setRules(parseRaw(ruleTargetBuilder, parseRulesList(jsonFilter))); return new Filter(outerRuleSet); } - private static Collection parseRaw(final Class targetClass, List> rules) { + private static Collection parseRaw(final RuleTargetBuilder ruleTargetBuilder, List> rules) { List filterParts = new ArrayList<>(); rules.forEach(rule -> filterParts.add(rule.containsKey("condition") - ? parseRawRuleSet(targetClass, rule) : parseRawRule(targetClass, rule))); + ? parseRawRuleSet(ruleTargetBuilder, rule) : parseRawRule(ruleTargetBuilder, rule))); return filterParts; } - private static RuleSet parseRawRuleSet(final Class targetClass, final Map rawRuleSet) { + private static RuleSet parseRawRuleSet(final RuleTargetBuilder targetClass, final Map rawRuleSet) { RuleSet ruleSet = new RuleSet(); ruleSet.setOperator(BooleanOperator.valueOf(rawRuleSet.get("condition").toString())); ruleSet.setRules(parseRaw(targetClass, parseRulesList(rawRuleSet))); @@ -39,9 +39,7 @@ private static RuleSet parseRawRuleSet(final Class targetC } @SuppressWarnings("unchecked") - private static Rule parseRawRule(final Class targetClass, final Map rawRule) { - String rawOperator = rawRule.get("operator").toString().toUpperCase(); - String rawField = rawRule.get("field").toString().toUpperCase(); + private static Rule parseRawRule(final RuleTargetBuilder ruleTargetBuilder, final Map rawRule) { List rawValues; if (rawRule.get("value") instanceof Collection) { @@ -51,14 +49,17 @@ private static Rule parseRawRule(final Class targetClass, rawValues.add(rawRule.get("value")); } - Class enumClass = on(targetClass).get(); - Enum enumTarget = Enum.valueOf(enumClass, rawField); - RuleTarget target = on(enumTarget).get(); - Rule rule = new Rule(); + + String rawOperator = rawRule.get("operator").toString().toUpperCase(); rule.setOperator(RuleOperator.valueOf(rawOperator)); + + String rawField = rawRule.get("field").toString(); + RuleTarget target = ruleTargetBuilder.build(rawField); rule.setTarget(target); + rule.setParameters(rawValues); + return rule; } diff --git a/src/main/java/io/kowalski/jqb2jooq/JQB2JOOQ.java b/src/main/java/io/kowalski/jqb2jooq/JQB2JOOQ.java index 1513799..74ed8e2 100644 --- a/src/main/java/io/kowalski/jqb2jooq/JQB2JOOQ.java +++ b/src/main/java/io/kowalski/jqb2jooq/JQB2JOOQ.java @@ -15,12 +15,12 @@ public class JQB2JOOQ { /** * parse performs the transformation of filter to condition. - * @param targetClass must be an Enum that implements {@link RuleTarget} + * @param targetBuilder produces an Enum that implements {@link RuleTarget} * @param jsonFilter is the raw Json String representation of the jQuery QueryBuilder filter deserialized into a Map * @return the JOOQ Condition equivalent of the provided Json filter */ - public static Condition parse(final Class targetClass, final Map jsonFilter) { - Filter filter = FilterParser.parseJSON(targetClass, jsonFilter); + public static Condition parse(final RuleTargetBuilder targetBuilder, final Map jsonFilter) { + Filter filter = FilterParser.parseJSON(targetBuilder, jsonFilter); return FilterTranslator.translate(filter); } diff --git a/src/main/java/io/kowalski/jqb2jooq/RuleTarget.java b/src/main/java/io/kowalski/jqb2jooq/RuleTarget.java index c38aaf5..2331767 100644 --- a/src/main/java/io/kowalski/jqb2jooq/RuleTarget.java +++ b/src/main/java/io/kowalski/jqb2jooq/RuleTarget.java @@ -19,12 +19,4 @@ public interface RuleTarget { // Getter Contract Field getField(); Condition[] getImplicitConditions(); - - /** - * parse is accessed via reflection and will call your Enum's valueOf method - * @param value the rule id as it appears in jQuery QueryBuilder - * @return the parsed RuleTarget as defined by your Enumeration definition - */ - RuleTarget parse(String value); - } diff --git a/src/main/java/io/kowalski/jqb2jooq/RuleTargetBuilder.java b/src/main/java/io/kowalski/jqb2jooq/RuleTargetBuilder.java new file mode 100644 index 0000000..fcfa1f3 --- /dev/null +++ b/src/main/java/io/kowalski/jqb2jooq/RuleTargetBuilder.java @@ -0,0 +1,5 @@ +package io.kowalski.jqb2jooq; + +public interface RuleTargetBuilder { + RuleTarget build(String name); +} diff --git a/src/test/java/io/kowalski/jqb2jooq/test/FilterConversionTest.java b/src/test/java/io/kowalski/jqb2jooq/test/FilterConversionTest.java index 03daf2f..968fd66 100644 --- a/src/test/java/io/kowalski/jqb2jooq/test/FilterConversionTest.java +++ b/src/test/java/io/kowalski/jqb2jooq/test/FilterConversionTest.java @@ -5,6 +5,7 @@ import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import io.kowalski.jqb2jooq.JQB2JOOQ; +import io.kowalski.jqb2jooq.RuleTargetBuilder; import io.kowalski.jqb2jooq.test.jooq.tables.pojos.Employees; import org.jooq.Condition; import org.jooq.DSLContext; @@ -56,7 +57,7 @@ public void sanityCheck() { @Test public void nameLike() { Map filter = jsonToMap(FULLNAME_EQUALS_FILTER); - Condition condition = JQB2JOOQ.parse(TestFilterTargets.class, filter); + Condition condition = JQB2JOOQ.parse(ruleTargetBuilder, filter); try (DSLContext dsl = DSL.using(dataSource, SQLDialect.H2)) { List employees = dsl.select().from(EMPLOYEES) @@ -70,7 +71,7 @@ public void nameLike() { @Test public void dobBetween() { Map filter = jsonToMap(DOB_BETWEEN_FILTER); - Condition condition = JQB2JOOQ.parse(TestFilterTargets.class, filter); + Condition condition = JQB2JOOQ.parse(ruleTargetBuilder, filter); try (DSLContext dsl = DSL.using(dataSource, SQLDialect.H2)) { List employees = dsl.select().from(EMPLOYEES) @@ -83,7 +84,7 @@ public void dobBetween() { @Test public void salaryLessThanOrEqual() { Map filter = jsonToMap(SALARY_LESS_OR_EQUAL_FILTER); - Condition condition = JQB2JOOQ.parse(TestFilterTargets.class, filter); + Condition condition = JQB2JOOQ.parse(ruleTargetBuilder, filter); try (DSLContext dsl = DSL.using(dataSource, SQLDialect.H2)) { List employees = dsl.select().from(EMPLOYEES) @@ -97,7 +98,7 @@ public void salaryLessThanOrEqual() { @Test public void nestedDOBHourly() { Map filter = jsonToMap(NESTED_DOB_HOURLY_FILTER); - Condition condition = JQB2JOOQ.parse(TestFilterTargets.class, filter); + Condition condition = JQB2JOOQ.parse(ruleTargetBuilder, filter); try (DSLContext dsl = DSL.using(dataSource, SQLDialect.H2)) { List employees = dsl.select().from(EMPLOYEES) @@ -111,7 +112,7 @@ public void nestedDOBHourly() { @Test public void lazyCompleteTestCoverageGrabBag() { Map filter = jsonToMap(GRAB_BAG_FILTER); - Condition condition = JQB2JOOQ.parse(TestFilterTargets.class, filter); + Condition condition = JQB2JOOQ.parse(ruleTargetBuilder, filter); try (DSLContext dsl = DSL.using(dataSource, SQLDialect.H2)) { List employees = dsl.select().from(EMPLOYEES) @@ -126,4 +127,5 @@ private static Map jsonToMap(String json) { return GSON.fromJson(json, MAP_TYPE); } + private static RuleTargetBuilder ruleTargetBuilder = name -> TestFilterTargets.valueOf(name.toUpperCase()); } diff --git a/src/test/java/io/kowalski/jqb2jooq/test/TestFilterTargets.java b/src/test/java/io/kowalski/jqb2jooq/test/TestFilterTargets.java index b6b1e33..c37ffd4 100644 --- a/src/test/java/io/kowalski/jqb2jooq/test/TestFilterTargets.java +++ b/src/test/java/io/kowalski/jqb2jooq/test/TestFilterTargets.java @@ -22,11 +22,6 @@ public enum TestFilterTargets implements RuleTarget { this.implicitConditions = implicitConditions; } - @Override - public TestFilterTargets parse(String value) { - return TestFilterTargets.valueOf(value); - } - @Override public Field getField() { return field;