FilterConverter implements Converter<SerializablePredicate<T>, com.ocs.dynamo.filter.Filter> { @Override public Filter convert(SerializablePredicate<T> filter) { if (filter == null) { return null; } Filter result = null; if (filter instanceof LikePredicate) { LikePredicate<T> p = (LikePredicate<T>) filter; result = new Like(p.getProperty(), (String) p.getValue(), p.isCaseSensitive()); } else if (filter instanceof IsNullPredicate) { IsNullPredicate<T> p = (IsNullPredicate<T>) filter; result = new IsNull(p.getProperty()); } else if (filter instanceof EqualsPredicate) { EqualsPredicate<T> p = (EqualsPredicate<T>) filter; result = new Compare.Equal(p.getProperty(), p.getValue()); } else if (filter instanceof GreaterThanPredicate) { GreaterThanPredicate<T> p = (GreaterThanPredicate<T>) filter; result = new Compare.Greater(p.getProperty(), p.getValue()); } else if (filter instanceof LessThanPredicate) { LessThanPredicate<T> p = (LessThanPredicate<T>) filter; result = new Compare.Less(p.getProperty(), p.getValue()); } else if (filter instanceof AndPredicate) { AndPredicate<T> p = (AndPredicate<T>) filter; And and = new And(); for (SerializablePredicate<T> operand : p.getOperands()) { Filter converted = convert(operand); if (converted != null) { and.getFilters().add(converted); } } result = and; } else if (filter instanceof OrPredicate) { OrPredicate<T> p = (OrPredicate<T>) filter; Or or = new Or(); for (SerializablePredicate<T> operand : p.getOperands()) { Filter converted = convert(operand); if (converted != null) { or.getFilters().add(converted); } } result = or; } else if (filter instanceof GreaterOrEqualPredicate) { GreaterOrEqualPredicate<T> p = (GreaterOrEqualPredicate<T>) filter; result = new Compare.GreaterOrEqual(p.getProperty(), p.getValue()); } else if (filter instanceof LessOrEqualPredicate) { LessOrEqualPredicate<T> p = (LessOrEqualPredicate<T>) filter; result = new Compare.LessOrEqual(p.getProperty(), p.getValue()); } else if (filter instanceof NotPredicate) { NotPredicate<T> p = (NotPredicate<T>) filter; result = new Not(convert(p.getOperand())); } else if (filter instanceof BetweenPredicate) { BetweenPredicate<T> p = (BetweenPredicate<T>) filter; result = new Between(((BetweenPredicate<T>) filter).getProperty(), p.getFromValue(), p.getToValue()); } else if (filter instanceof SimpleStringPredicate) { SimpleStringPredicate<T> p = (SimpleStringPredicate<T>) filter; result = new Like(p.getProperty(), (p.isOnlyMatchPrefix() ? "" : "%") + p.getValue() + "%", p.isCaseSensitive()); } else if (filter instanceof InPredicate) { InPredicate<T> p = (InPredicate<T>) filter; result = new In(p.getProperty(), p.getValue()); } else if (filter instanceof ContainsPredicate) { ContainsPredicate<T> p = (ContainsPredicate<T>) filter; result = new Contains(p.getProperty(), p.getValue()); } else if (filter instanceof ModuloPredicate) { ModuloPredicate<T> p = (ModuloPredicate<T>) filter; if (p.getModExpression() != null) { result = new Modulo(p.getProperty(), p.getModExpression(), p.getValue()); } else { result = new Modulo(p.getProperty(), p.getModValue(), p.getValue()); } } if (entityModel != null) { DynamoFilterUtil.replaceMasterAndDetailFilters(result, entityModel); } return result; } FilterConverter(EntityModel<T> entityModel); @Override Filter convert(SerializablePredicate<T> filter);  }
@Test public void testAnd() { Filter result = converter.convert(new AndPredicate<TestEntity>(f1, f2)); assertTrue(result instanceof And); }
@Test public void testBetween() { Filter result = converter.convert(new BetweenPredicate<TestEntity>("test", 1, 10)); assertTrue(result instanceof Between); }
@Test public void testCompareEqual() { Filter result = converter.convert(new EqualsPredicate<TestEntity>("test", "test")); assertTrue(result instanceof Compare.Equal); }
@Test public void testCompareGreater() { Filter result = converter.convert(new GreaterThanPredicate<TestEntity>("test", "test")); assertTrue(result instanceof Compare.Greater); }
@Test public void testCompareGreaterOrEqual() { Filter result = converter.convert(new GreaterOrEqualPredicate<TestEntity>("test", "test")); assertTrue(result instanceof Compare.GreaterOrEqual); }
@Test public void testCompareLess() { Filter result = converter.convert(new LessThanPredicate<TestEntity>("test", "test")); assertTrue(result instanceof Compare.Less); }
@Test public void testIn() { Filter result = converter.convert(new InPredicate<TestEntity>("test", Lists.newArrayList("v1", "v2"))); assertTrue(result instanceof In); }
@Test public void testContains() { Filter result = converter.convert(new ContainsPredicate<TestEntity>("test", "test")); assertTrue(result instanceof Contains); }
@Test public void testModulo1() { Filter result = converter.convert(new ModuloPredicate<TestEntity>("test", 4, 2)); assertTrue(result instanceof Modulo); Modulo mod = (Modulo) result; assertEquals(4, mod.getModValue()); assertEquals(2, mod.getResult()); }
@Test public void testModulo2() { Filter result = converter.convert(new ModuloPredicate<TestEntity>("test", "test2", 2)); assertTrue(result instanceof Modulo); Modulo mod = (Modulo) result; assertEquals("test2", mod.getModExpression()); assertEquals(2, mod.getResult()); }
@Test public void testCompareLessOrEqual() { Filter result = converter.convert(new LessOrEqualPredicate<TestEntity>("test", "test")); assertTrue(result instanceof Compare.LessOrEqual); }
@Test public void testIsNull() { Filter result = converter.convert(new IsNullPredicate<TestEntity>("test")); assertTrue(result instanceof IsNull); }
@Test public void testLike() { Filter result = converter.convert(new LikePredicate<TestEntity>("test", "%test%", false)); assertTrue(result instanceof Like); Like like = (Like) result; assertFalse(like.isCaseSensitive()); }
@Test public void testNot() { Filter result = converter.convert(new NotPredicate<TestEntity>(f1)); assertTrue(result instanceof Not); }
@Test public void testOr() { Filter result = converter.convert(new OrPredicate<TestEntity>(f1, f2)); assertTrue(result instanceof Or); }
@Test public void testSearchDetails() { Filter result = modelConverter.convert( new AndPredicate<TestEntity>(new EqualsPredicate<TestEntity>("testEntities", new TestEntity2()))); assertTrue(result instanceof And); Filter first = ((And) result).getFilters().iterator().next(); assertTrue(first instanceof Contains); }
@Test public void testSearchDetails2() { List<TestEntity2> entities = Lists.newArrayList(new TestEntity2(), new TestEntity2()); Filter result = modelConverter .convert(new AndPredicate<TestEntity>(new EqualsPredicate<TestEntity>("testEntities", entities))); assertTrue(result instanceof And); Filter first = ((And) result).getFilters().iterator().next(); assertTrue(first instanceof Or); for (Filter f : ((Or) first).getFilters()) { assertTrue(f instanceof Contains); } }
@Test public void testSimpleStringFilter1() { Filter result = converter.convert(new SimpleStringPredicate<TestEntity>("test", "test", false, true)); assertTrue(result instanceof Like); Like like = (Like) result; assertTrue(like.isCaseSensitive()); assertEquals("%test%", like.getValue()); }
@Test public void testSimpleStringFilter2() { Filter result = converter.convert(new SimpleStringPredicate<TestEntity>("test", "test", true, false)); assertTrue(result instanceof Like); Like like = (Like) result; assertFalse(like.isCaseSensitive()); assertEquals("test%", like.getValue()); }