Skip to content

Commit

Permalink
Merge pull request #2 from albertogoffi/master
Browse files Browse the repository at this point in the history
Add support for custom mapping of field names in JQB JSON
  • Loading branch information
BrandonKowalski authored Nov 25, 2018
2 parents 3034c79 + 6257b6c commit 3ebcada
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/jqb2jooq.iml
/target
.DS_Store
/.idea/
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-test-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
25 changes: 13 additions & 12 deletions src/main/java/io/kowalski/jqb2jooq/FilterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
class FilterParser {

protected static Filter parseJSON(final Class<? extends RuleTarget> targetClass, final Map<String, Object> jsonFilter) {
static Filter parseJSON(final RuleTargetBuilder ruleTargetBuilder, final Map<String, Object> 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<FilterPart> parseRaw(final Class<? extends RuleTarget> targetClass, List<Map<String, Object>> rules) {
private static Collection<FilterPart> parseRaw(final RuleTargetBuilder ruleTargetBuilder, List<Map<String, Object>> rules) {
List<FilterPart> 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<? extends RuleTarget> targetClass, final Map<String, Object> rawRuleSet) {
private static RuleSet parseRawRuleSet(final RuleTargetBuilder targetClass, final Map<String, Object> rawRuleSet) {
RuleSet ruleSet = new RuleSet();
ruleSet.setOperator(BooleanOperator.valueOf(rawRuleSet.get("condition").toString()));
ruleSet.setRules(parseRaw(targetClass, parseRulesList(rawRuleSet)));
Expand All @@ -39,9 +39,7 @@ private static RuleSet parseRawRuleSet(final Class<? extends RuleTarget> targetC
}

@SuppressWarnings("unchecked")
private static Rule parseRawRule(final Class<? extends RuleTarget> targetClass, final Map<String, Object> rawRule) {
String rawOperator = rawRule.get("operator").toString().toUpperCase();
String rawField = rawRule.get("field").toString().toUpperCase();
private static Rule parseRawRule(final RuleTargetBuilder ruleTargetBuilder, final Map<String, Object> rawRule) {
List<Object> rawValues;

if (rawRule.get("value") instanceof Collection) {
Expand All @@ -51,14 +49,17 @@ private static Rule parseRawRule(final Class<? extends RuleTarget> targetClass,
rawValues.add(rawRule.get("value"));
}

Class<? extends Enum> 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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/kowalski/jqb2jooq/JQB2JOOQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends RuleTarget> targetClass, final Map<String, Object> jsonFilter) {
Filter filter = FilterParser.parseJSON(targetClass, jsonFilter);
public static Condition parse(final RuleTargetBuilder targetBuilder, final Map<String, Object> jsonFilter) {
Filter filter = FilterParser.parseJSON(targetBuilder, jsonFilter);
return FilterTranslator.translate(filter);
}

Expand Down
8 changes: 0 additions & 8 deletions src/main/java/io/kowalski/jqb2jooq/RuleTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
5 changes: 5 additions & 0 deletions src/main/java/io/kowalski/jqb2jooq/RuleTargetBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.kowalski.jqb2jooq;

public interface RuleTargetBuilder {
RuleTarget build(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void sanityCheck() {
@Test
public void nameLike() {
Map<String, Object> 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> employees = dsl.select().from(EMPLOYEES)
Expand All @@ -70,7 +71,7 @@ public void nameLike() {
@Test
public void dobBetween() {
Map<String, Object> 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> employees = dsl.select().from(EMPLOYEES)
Expand All @@ -83,7 +84,7 @@ public void dobBetween() {
@Test
public void salaryLessThanOrEqual() {
Map<String, Object> 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> employees = dsl.select().from(EMPLOYEES)
Expand All @@ -97,7 +98,7 @@ public void salaryLessThanOrEqual() {
@Test
public void nestedDOBHourly() {
Map<String, Object> 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> employees = dsl.select().from(EMPLOYEES)
Expand All @@ -111,7 +112,7 @@ public void nestedDOBHourly() {
@Test
public void lazyCompleteTestCoverageGrabBag() {
Map<String, Object> 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> employees = dsl.select().from(EMPLOYEES)
Expand All @@ -126,4 +127,5 @@ private static Map<String, Object> jsonToMap(String json) {
return GSON.fromJson(json, MAP_TYPE);
}

private static RuleTargetBuilder ruleTargetBuilder = name -> TestFilterTargets.valueOf(name.toUpperCase());
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3ebcada

Please sign in to comment.