Author: tchemit Date: 2010-05-24 14:32:28 +0200 (Mon, 24 May 2010) New Revision: 1970 Url: http://nuiton.org/repositories/revision/topia/1970 Log: - improve ListUpdator api - use last version of nuiton-utils (binder api) Modified: trunk/pom.xml trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityListUpdator.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/ListUpdator.java Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2010-05-20 16:54:46 UTC (rev 1969) +++ trunk/pom.xml 2010-05-24 12:32:28 UTC (rev 1970) @@ -233,7 +233,7 @@ <!-- libs version --> <eugene.version>2.0.2-SNAPSHOT</eugene.version> - <lutinutil.version>1.3</lutinutil.version> + <lutinutil.version>1.3.1-SNAPSHOT</lutinutil.version> <processor.version>1.0.3</processor.version> <i18n.version>1.2.2</i18n.version> <xmlrpc.version>3.1.2</xmlrpc.version> Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityListUpdator.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityListUpdator.java 2010-05-20 16:54:46 UTC (rev 1969) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityListUpdator.java 2010-05-24 12:32:28 UTC (rev 1970) @@ -41,9 +41,9 @@ * Some factory methods are defined to simplify the generic cast, prefer used them * instead of the (protected) constructor. * + * @author tchemit <chemit@codelutin.com> * @param <P> type of parent of childs * @param <E> type of child - * @author tchemit <chemit@codelutin.com> */ public class EntityListUpdator<P extends TopiaEntity, E extends TopiaEntity> implements ListUpdator<P, E> { @@ -51,13 +51,24 @@ return new EntityListUpdator<P, E>(parentClass, childClass, propertyName); } + /** name of the field containing the childs */ protected String propertyName; + /** descriptor of the filed containing the childs */ protected PropertyDescriptor descriptor; + protected Method getMethod; + protected Method addMethod; + protected Method removeMethod; + protected Method removeAllMethod; + + protected Method sizeMethod; + + protected Method emptyMethod; + protected EntityListUpdator(Class<P> parentClass, Class<E> childClass, String propertyName) { this.propertyName = propertyName; @@ -71,22 +82,40 @@ getMethod = parentClass.getMethod("get" + cap + "ByTopiaId", String.class); addMethod = parentClass.getMethod("add" + cap, childClass); removeMethod = parentClass.getMethod("remove" + cap, childClass); + removeAllMethod = parentClass.getMethod("clear" + cap); + sizeMethod = parentClass.getMethod("size" + cap); + emptyMethod = parentClass.getMethod("is" + cap + "Empty"); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } @Override + public String getPropertyName() { + return propertyName; + } + + @Override public E getChild(P parent, String topiaId) { return EntityListUpdator.<E>invokeWithResult(getMethod, parent, topiaId); } @Override public Collection<E> getChilds(P parent) { - return EntityListUpdator.invokeWithResult(descriptor.getReadMethod(), parent); + return EntityListUpdator.<Collection<E>>invokeWithResult(descriptor.getReadMethod(), parent); } @Override + public int size(P parent) { + return EntityListUpdator.<Integer>invokeWithResult(sizeMethod, parent); + } + + @Override + public boolean isEmpty(P parent) { + return EntityListUpdator.<Boolean>invokeWithResult(emptyMethod, parent); + } + + @Override public void setChilds(P parent, Collection<E> childs) { invoke(descriptor.getWriteMethod(), parent, childs); } @@ -101,6 +130,11 @@ invoke(removeMethod, parent, bean); } + @Override + public void removeAll(P parent) { + invoke(removeAllMethod, parent); + } + protected static void invoke(Method m, Object bean, Object... args) { try { m.invoke(bean, args); Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/ListUpdator.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/ListUpdator.java 2010-05-20 16:54:46 UTC (rev 1969) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/ListUpdator.java 2010-05-24 12:32:28 UTC (rev 1970) @@ -42,6 +42,13 @@ public interface ListUpdator<P, E> { /** + * Obtain the name of the property containing childs. + * + * @return the name of the property containing childs. + */ + String getPropertyName(); + + /** * Obtain a child from the entity given his id. * * @param parent the entity to query @@ -60,6 +67,22 @@ Collection<E> getChilds(P parent); /** + * Obtain the number of childs for the given parent. + * + * @param parent the entity to query + * @return the number of childs for the given entity + */ + int size(P parent); + + /** + * Tests if the given entity has some childs. + * + * @param parent the entity to query + * @return {@code true} is the given parent has no child. + */ + boolean isEmpty(P parent); + + /** * Set the childs of an entity * * @param parent the entity to be setted @@ -70,7 +93,7 @@ /** * Add a erntity to his parent * - * @param parent the parent entity + * @param parent the entity to modifiy * @param e the entity to add in parent. * @throws TopiaException if any db problem while operation */ @@ -79,9 +102,16 @@ /** * Remove from a prent entity a given child. * - * @param parent the parent entity + * @param parent the entity to modifiy * @param e the child to remove. * @throws TopiaException if any pb while operation. */ void removeFromList(P parent, E e) throws TopiaException; + + /** + * Remove all childs of the given parent. + * + * @param parent the entity to modify + */ + void removeAll(P parent); }