r2872 - in trunk/topia-persistence/src: main/java/org/nuiton/topia/persistence test/java/org/nuiton/topia/persistence
Author: bleny Date: 2013-11-15 10:51:40 +0100 (Fri, 15 Nov 2013) New Revision: 2872 Url: http://nuiton.org/projects/topia/repository/revisions/2872 Log: fixes #2910 add addNotEquals addNotIn addNotContains in API and HQL builder Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java 2013-11-08 12:22:43 UTC (rev 2871) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HqlAndParametersBuilder.java 2013-11-15 09:51:40 UTC (rev 2872) @@ -107,16 +107,46 @@ } } + public void addNotEquals(String property, Object value) { + Preconditions.checkNotNull(property); + // TODO brendan 02/10/13 do not use HQL parameters of Object are primitive types + // TODO brendan 02/10/13 if value is intanceof TopiaEntity, we can check type + if (value == null) { + addNotNull(property); + } else { + String hqlParameterName = putHqlParameterWithAvailableName(property, value); + whereClauses.add(alias + "." + property + " != :" + hqlParameterName); + } + } + public void addIn(String property, Iterable<Object> values) { + addInOrNotIn(property, values, true); + } + + public void addNotIn(String property, Iterable<Object> values) { + addInOrNotIn(property, values, false); + } + + /** + * @param in true if property value must be in given collection, false if value + * must not be in given collection + */ + protected void addInOrNotIn(String property, Iterable<Object> values, boolean in) { Preconditions.checkNotNull(property); Preconditions.checkNotNull(values); // TODO brendan 02/10/13 if value is intanceof TopiaEntity, we can check type // TODO brendan 02/10/13 do not use HQL parameters of Object are primitive - Preconditions.checkArgument(!Iterables.isEmpty(values)); + String aliasedProperty = alias + "." + property; if (Iterables.size(values) == 1) { - addEquals(property, Iterables.getOnlyElement(values)); + // if there is only one possible value, replace "in" clause by a "=" clause + Object onlyElement = Iterables.getOnlyElement(values); + if (in) { + addEquals(property, onlyElement); + } else { + addNotEquals(property, onlyElement); + } } else { - boolean propertyMayBeNull = false; + boolean propertyMayBeNull = false; // true if values contains null Set<String> hqlParameterNames = Sets.newLinkedHashSet(); for (Object value : values) { if (value == null) { @@ -126,21 +156,33 @@ hqlParameterNames.add(hqlParameterName); } } - String inClause = alias + "." + property + " in (:" + StringUtils.join(hqlParameterNames, ", :")+ ")"; - String whereClause = inClause; - if (propertyMayBeNull) { - whereClause = alias + "." + property + " is null or " + inClause; + String whereClause; + if (in) { + String inClause = aliasedProperty + " in (:" + StringUtils.join(hqlParameterNames, ", :")+ ")"; + if (propertyMayBeNull) { + whereClause = aliasedProperty + " is null or " + inClause; + } else { + whereClause = inClause; + } + } else { + String inClause = aliasedProperty + " not in (:" + StringUtils.join(hqlParameterNames, ", :") + ")"; + if (propertyMayBeNull) { + whereClause = aliasedProperty + " is not null and " + inClause; + } else { + whereClause = inClause; + } } whereClauses.add(whereClause); } } public void addTopiaIdEquals(String property, String topiaId) { - addEquals(alias + "." + property + "." + TopiaEntity.PROPERTY_TOPIA_ID, topiaId); + Preconditions.checkNotNull(topiaId); + addEquals(property + "." + TopiaEntity.PROPERTY_TOPIA_ID, topiaId); } public void addTopiaIdIn(String property, Iterable<String> topiaIds) { - addIn(alias + "." + property + "." + TopiaEntity.PROPERTY_TOPIA_ID, (Iterable) topiaIds); + addIn(property + "." + TopiaEntity.PROPERTY_TOPIA_ID, (Iterable) topiaIds); } public void addContains(String property, Object value) { @@ -148,6 +190,11 @@ whereClauses.add(":" + hqlParameterName + " in elements(" + alias + "." + property + ")"); } + public void addNotContains(String property, Object value) { + String hqlParameterName = putHqlParameterWithAvailableName(property, value); + whereClauses.add(":" + hqlParameterName + " not in elements(" + alias + "." + property + ")"); + } + public void addWhereClause(String whereClause) { whereClauses.add(whereClause); } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java 2013-11-08 12:22:43 UTC (rev 2871) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java 2013-11-15 09:51:40 UTC (rev 2872) @@ -46,20 +46,41 @@ /** * @param property the name of a field of the queried entity, must be + * a one-to-one or a many-to-one property. + * @param value the value the field of the entity must not be equals to argument + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addNotEquals(String property, Object value); + + /** + * @param property the name of a field of the queried entity, must be * a one-to-one or a many-to-one property - * @param values the value the field of the entity must be equals to argument + * @param values the value the field of the entity must be equals to one of the given values */ TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addIn(String property, Iterable<Object> values); /** * @param property the name of a field of the queried entity, must be + * a one-to-one or a many-to-one property + * @param values the value the field of the entity must not be equals to any of the given values + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addNotIn(String property, Iterable<Object> values); + + /** + * @param property the name of a field of the queried entity, must be * a one-to-many or a many-to-many property - * @param value the value the field of the entity must be equals to argument + * @param value the property of the entity must be a collection that contains value */ TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addContains(String property, Object value); /** * @param property the name of a field of the queried entity, must be + * a one-to-many or a many-to-many property + * @param value the property of the entity must be a collection that doesn't contain value + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addNotContains(String property, Object value); + + /** + * @param property the name of a field of the queried entity, must be * a one-to-one or a many-to-one property */ TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addNull(String property); Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java 2013-11-08 12:22:43 UTC (rev 2871) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/HqlAndParametersBuilderTest.java 2013-11-15 09:51:40 UTC (rev 2872) @@ -24,24 +24,93 @@ * #L% */ +import com.google.common.collect.Lists; import org.junit.Assert; import org.junit.Test; +import java.util.Collection; + public class HqlAndParametersBuilderTest { + protected HqlAndParametersBuilder hqlAndParametersBuilder = + new HqlAndParametersBuilder(TopiaEntity.class); + + protected static final Collection<String> SOME_VALUES = + Lists.newArrayList("value1", "value2", "value3"); + + protected static final Collection<String> SOME_VALUES_WITH_NULL = + Lists.newArrayList("value1", "value2", null, "value3"); + @Test public void testFindAvailableHqlParameterName() { - HqlAndParametersBuilder hqlAndParametersBuilderTest = - new HqlAndParametersBuilder(TopiaEntity.class); + String availableHqlParameterName1 = hqlAndParametersBuilder.putHqlParameterWithAvailableName("survey.topiaId", "topiaId1"); + String availableHqlParameterName2 = hqlAndParametersBuilder.putHqlParameterWithAvailableName("survey.topiaId", "topiaId1"); - String availableHqlParameterName1 = hqlAndParametersBuilderTest.putHqlParameterWithAvailableName("survey.topiaId", "topiaId1"); - String availableHqlParameterName2 = hqlAndParametersBuilderTest.putHqlParameterWithAvailableName("survey.topiaId", "topiaId1"); - Assert.assertNotEquals(availableHqlParameterName1, availableHqlParameterName2); Assert.assertFalse(availableHqlParameterName1.contains(".")); Assert.assertFalse(availableHqlParameterName2.contains(".")); } + @Test + public void testEqualsNull() { + + hqlAndParametersBuilder.addEquals("myProp", null); + + String actualHql = hqlAndParametersBuilder.getHql(); + + Assert.assertFalse(actualHql.contains("myProp = null")); + Assert.assertTrue(actualHql.contains("myProp is null")); + + } + + @Test + public void testAddInWithNull() { + + hqlAndParametersBuilder.addIn("myProp", SOME_VALUES_WITH_NULL); + + String actualHql = hqlAndParametersBuilder.getHql(); + + Assert.assertTrue(actualHql.contains("myProp is null or topiaEntity_.myProp in (")); + Assert.assertEquals( + SOME_VALUES_WITH_NULL.size() - 1, // there should be as many argument as in collection minus 1 because null is removed + hqlAndParametersBuilder.getHqlParameters().size()); + + } + + @Test + public void testAddNotEqualsToValue() { + + hqlAndParametersBuilder.addNotEquals("myProp", "value"); + + String actualHql = hqlAndParametersBuilder.getHql(); + + Assert.assertTrue(actualHql.contains("myProp != ")); + Assert.assertFalse(actualHql.contains("myProp = ")); + + } + + @Test + public void testAddNotEqualsToNull() { + + hqlAndParametersBuilder.addNotEquals("myProp", null); + + String actualHql = hqlAndParametersBuilder.getHql(); + + Assert.assertTrue(actualHql.contains("myProp is not null")); + + } + + @Test + public void testAddNotInWithNull() { + + hqlAndParametersBuilder.addNotIn("myProp", SOME_VALUES_WITH_NULL); + + String actualHql = hqlAndParametersBuilder.getHql(); + + Assert.assertTrue(actualHql.contains("myProp is not null and ")); + + } + }
participants (1)
-
bleny@users.nuiton.org