RestrictionFactory { public static Criterion forFilterCriterion(FilterCriterion criterion) { if (criterion.getValue() instanceof Date) { return forDateCriterion(criterion); } final CriterionFieldEnum field = criterion.getField(); String propertyName = field.getPropertyName(); Criterion restriction; final Object value = criterion.getValue(); switch (criterion.getOperator()) { case EQ: restriction = Restrictions.eq(propertyName, value); break; case NE: restriction = Restrictions.neq(propertyName, value); break; case GT: restriction = Restrictions.gt(propertyName, value); break; case GTE: restriction = Restrictions.gte(propertyName, value); break; case LT: restriction = Restrictions.lt(propertyName, value); break; case LTE: restriction = Restrictions.lte(propertyName, value); break; case ANY_OF: restriction = Restrictions.in(propertyName, (Object[]) value); break; case CONTAINS: restriction = Restrictions.like(propertyName, '%' + (String) value + '%'); break; case ENDS_WITH: restriction = Restrictions.like(propertyName, '%' + (String) value); break; case STARTS_WITH: restriction = Restrictions.like(propertyName, (String) value + '%'); break; case NOT_CONTAINS: restriction = Restrictions.notLike(propertyName, '%' + (String) value + '%'); break; case NOT_ENDS_WITH: restriction = Restrictions.notLike(propertyName, '%' + (String) value); break; case NOT_STARTS_WITH: restriction = Restrictions.notLike(propertyName, (String) value + '%'); break; case NONE_OF: restriction = Restrictions.notIn(propertyName, (Object[]) value); break; default: throw new IllegalArgumentException( String.format("Invalid operator [%s]", criterion.getOperator())); } return restriction; } private  RestrictionFactory(); static Criterion forFilterCriterion(FilterCriterion criterion);  }
@Test public void testEqualDate() { when(filterCriterion.getOperator()).thenReturn(CriterionOperator.EQ); queryBuilder.add(RestrictionFactory.forFilterCriterion(filterCriterion)); assertEquals("(foo.metaData.lastModifiedDate >= ? " + "AND foo.metaData.lastModifiedDate < ?)", queryBuilder.toEjbQl()); assertArrayEquals(new Date[] {from, to}, queryBuilder.getValues()); }
@Test public void testNotEqualDate() { when(filterCriterion.getOperator()).thenReturn(CriterionOperator.NE); queryBuilder.add(RestrictionFactory.forFilterCriterion(filterCriterion)); assertEquals("(foo.metaData.lastModifiedDate < ? " + "OR foo.metaData.lastModifiedDate >= ?)", queryBuilder.toEjbQl()); assertArrayEquals(new Date[] {from, to}, queryBuilder.getValues()); }
@Test public void testLessThanDate() { when(filterCriterion.getOperator()).thenReturn(CriterionOperator.LT); queryBuilder.add(RestrictionFactory.forFilterCriterion(filterCriterion)); assertEquals("foo.metaData.lastModifiedDate < ?", queryBuilder.toEjbQl()); assertArrayEquals(new Date[] {from}, queryBuilder.getValues()); }
@Test public void testLessThanEqualDate() { when(filterCriterion.getOperator()).thenReturn(CriterionOperator.LTE); queryBuilder.add(RestrictionFactory.forFilterCriterion(filterCriterion)); assertEquals("foo.metaData.lastModifiedDate < ?", queryBuilder.toEjbQl()); assertArrayEquals(new Date[] {to}, queryBuilder.getValues()); }
@Test public void testGreaterThanDate() { when(filterCriterion.getOperator()).thenReturn(CriterionOperator.GT); queryBuilder.add(RestrictionFactory.forFilterCriterion(filterCriterion)); assertEquals("foo.metaData.lastModifiedDate >= ?", queryBuilder.toEjbQl()); assertArrayEquals(new Date[] {to}, queryBuilder.getValues()); }
@Test public void testGreaterThanEqualDate() { when(filterCriterion.getOperator()).thenReturn(CriterionOperator.GTE); queryBuilder.add(RestrictionFactory.forFilterCriterion(filterCriterion)); assertEquals("foo.metaData.lastModifiedDate >= ?", queryBuilder.toEjbQl()); assertArrayEquals(new Date[] {from}, queryBuilder.getValues()); }