Author: tchemit Date: 2010-02-21 21:39:35 +0100 (Sun, 21 Feb 2010) New Revision: 1810 Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityBinder.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/BeanPropertyLoador.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Collector.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityLoador.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperator.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperatorStore.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Loador.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java Log: introduce TopiaEntityBinder, deprecate Loador api + reformat code Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/BeanPropertyLoador.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/BeanPropertyLoador.java 2010-02-21 19:38:11 UTC (rev 1809) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/BeanPropertyLoador.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -1,7 +1,7 @@ -/** +/* * *##% * ToPIA :: Persistence - * Copyright (C) 2004 - 2009 CodeLutin + * Copyright (C) 2004 - 2010 CodeLutin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -38,8 +38,10 @@ * instead of the (protected) constructor. * * @param <E> type of bean to bind - * @author chemit + * @author tchemit < chemit@codelutin.com > + * @deprecated since 2.3.0, prefer use the {@link TopiaEntityBinder} */ +@Deprecated public class BeanPropertyLoador<E> implements Loador<E> { public static <E> BeanPropertyLoador<E> newLoador(Class<E> klass, String... properties) { Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Collector.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Collector.java 2010-02-21 19:38:11 UTC (rev 1809) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Collector.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -29,14 +29,13 @@ import org.nuiton.topia.persistence.DepthEntityVisitor; import org.nuiton.topia.persistence.TopiaEntityEnum; import org.nuiton.topia.persistence.TopiaEntity; -import org.nuiton.topia.persistence.util.Collector.CollectorVisitor; /** - * Un objet qui permet de parcourir des entites (via un {@link CollectorVisitor}) - * et de collecter des donnees pendant le parcours. + * Un objet qui permet de parcourir des entites (via un + * {@link CollectorVisitor})et de collecter des donnees pendant le parcours. * * La classe a un type qui est le type de retour de la methode - * {@link #detect(TopiaEntity[])}. + * {@link #detect(TopiaEntity...)}. * * On peut donc très facilement en faire un collecteur de donnees. * @@ -46,7 +45,7 @@ * * @since 2.2.0 */ -public abstract class Collector<R extends Object> { +public abstract class Collector<R> { private static final Log log = LogFactory.getLog(Collector.class); /** @@ -79,25 +78,36 @@ protected void onStarted(TopiaEntity e, boolean enter) { } - protected boolean onVisiting(TopiaEntity e, String name, Class<?> type, Object value) { + protected boolean onVisiting(TopiaEntity e, String name, Class<?> type, + Object value) { return true; } - protected void onVisited(TopiaEntity e, String name, Class<?> type, Object value, boolean enter) { + protected void onVisited(TopiaEntity e, String name, Class<?> type, + Object value, boolean enter) { } - protected boolean onVisiting(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, Object value) { + protected boolean onVisiting(TopiaEntity e, String name, + Class<?> collectionType, + Class<?> type, Object value) { return true; } - protected void onVisited(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, Object value, boolean enter) { + protected void onVisited(TopiaEntity e, String name, + Class<?> collectionType, + Class<?> type, Object value, boolean enter) { } - protected boolean onVisiting(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, int index, Object value) { + protected boolean onVisiting(TopiaEntity e, String name, + Class<?> collectionType, + Class<?> type, int index, Object value) { return true; } - protected void onVisited(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, int index, Object value, boolean enter) { + protected void onVisited(TopiaEntity e, String name, + Class<?> collectionType, + Class<?> type, + int index, Object value, boolean enter) { } protected boolean onEnding(TopiaEntity e) { @@ -107,7 +117,8 @@ protected void onEnded(TopiaEntity e, boolean enter) { } - protected void beforeAll(CollectorVisitor visitor, TopiaEntity... entities) { + protected void beforeAll(CollectorVisitor visitor, + TopiaEntity... entities) { } protected void before(CollectorVisitor visitor, TopiaEntity entity) { @@ -116,9 +127,11 @@ protected void after(CollectorVisitor visitor, TopiaEntity entity) { } - protected abstract R afterAll(CollectorVisitor visitor, TopiaEntity... entities); + protected abstract R afterAll(CollectorVisitor visitor, + TopiaEntity... entities); - public synchronized R detect(TopiaEntity... entities) throws TopiaException { + public synchronized R detect(TopiaEntity... entities) + throws TopiaException { try { beforeAll(visitor, entities); @@ -180,7 +193,8 @@ } @Override - public void visit(TopiaEntity e, String name, Class<?> type, Object value) { + public void visit(TopiaEntity e, String name, Class<?> type, + Object value) { boolean canContinue = collector.onVisiting(e, name, type, value); if (canContinue) { super.visit(e, name, type, value); @@ -189,21 +203,28 @@ } @Override - public void visit(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, Object value) { - boolean canContinue = collector.onVisiting(e, name, collectionType, type, value); + public void visit(TopiaEntity e, String name, Class<?> collectionType, + Class<?> type, Object value) { + boolean canContinue = + collector.onVisiting(e, name, collectionType, type, value); if (canContinue) { super.visit(e, name, collectionType, type, value); } - collector.onVisited(e, name, collectionType, type, value, canContinue); + collector.onVisited(e, name, collectionType, type, value, + canContinue); } @Override - public void visit(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, int index, Object value) { - boolean canContinue = collector.onVisiting(e, name, collectionType, type, index, value); + public void visit(TopiaEntity e, String name, Class<?> collectionType, + Class<?> type, int index, Object value) { + boolean canContinue = + collector.onVisiting(e, name, collectionType, type, index, + value); if (canContinue) { super.visit(e, name, collectionType, type, index, value); } - collector.onVisited(e, name, collectionType, type, index, value, canContinue); + collector.onVisited(e, name, collectionType, type, index, value, + canContinue); } @Override @@ -235,7 +256,8 @@ if (contracts.length == 0) { return null; } - Class<? extends TopiaEntity> contractClass = TopiaEntityHelper.getContractClass(contracts, e.getClass()); + Class<? extends TopiaEntity> contractClass = + TopiaEntityHelper.getContractClass(contracts, e.getClass()); if (contractClass != null) { return contractClass; } @@ -243,6 +265,7 @@ } protected TopiaEntity getTopiaValue(Object value) { - return (TopiaEntity) (value != null && value instanceof TopiaEntity ? value : null); + return (TopiaEntity) (value != null && + value instanceof TopiaEntity ? value : null); } } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityLoador.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityLoador.java 2010-02-21 19:38:11 UTC (rev 1809) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityLoador.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -1,7 +1,7 @@ -/** +/* * *##% * ToPIA :: Persistence - * Copyright (C) 2004 - 2009 CodeLutin + * Copyright (C) 2004 - 2010 CodeLutin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -29,18 +29,22 @@ * instead of the (protected) constructor). * * @param <E> type of entity - * @author chemit + * @author tchemit < chemit@codelutin.com > + * @deprecated since 2.3.0, prefer use the {@link TopiaEntityBinder} */ +@Deprecated public class EntityLoador<E extends TopiaEntity> extends BeanPropertyLoador<E> { private static final long serialVersionUID = 1L; protected E empty; - public static <E extends TopiaEntity> EntityLoador<E> newEntityLoador(Class<E> klass, E empty, String... properties) { + public static <E extends TopiaEntity> EntityLoador<E> newEntityLoador( + Class<E> klass, E empty, String... properties) { return new EntityLoador<E>(klass, empty, properties); } - public static <E extends TopiaEntity> EntityLoador<E> newEntityLoador(Class<E> klass, String... properties) { + public static <E extends TopiaEntity> EntityLoador<E> newEntityLoador( + Class<E> klass, String... properties) { return newEntityLoador(klass, null, properties); } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperator.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperator.java 2010-02-21 19:38:11 UTC (rev 1809) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperator.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -1,7 +1,7 @@ -/** +/* * *##% * ToPIA :: Persistence - * Copyright (C) 2004 - 2009 CodeLutin + * Copyright (C) 2004 - 2010 CodeLutin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -59,9 +59,9 @@ * D'autres methodes permettent d'effectuer des operations en lot (sur plusieurs * proprietes en meme temps) sur les proprietes : * - * {@link #copyProperties(TopiaEntity, TopiaEntity, boolean, String[])} - * {@link #obtainProperties(TopiaEntity, String[])} - * {@link #clearProperties(TopiaEntity, String[])} + * {@link #copyProperties(TopiaEntity, TopiaEntity, boolean, String...)} + * {@link #obtainProperties(TopiaEntity, String...)} + * {@link #clearProperties(TopiaEntity, String...)} * * Note : cet objet ne permet pas d'operation vers les bases. * @@ -88,7 +88,8 @@ protected EntityOperator(TopiaEntityEnum contract) { if (contract == null) { - throw new NullPointerException("contract parameter can not be null!"); + throw new NullPointerException("contract parameter can not be " + + "null!"); } this.contract = contract; init(); @@ -266,7 +267,8 @@ * @param tech un drapeau pour recopier aussi les infos techniques * @param properties les proprietes a recopier */ - public void copyProperties(B from, B dst, boolean tech, String... properties) { + public void copyProperties(B from, B dst, boolean tech, + String... properties) { // if (from == null) { // // from = newInstance(); @@ -378,7 +380,8 @@ try { return super.clone(); } catch (CloneNotSupportedException e) { - throw new RuntimeException("could not clone " + this + " for reason : " + e.getMessage(), e); + throw new RuntimeException("could not clone " + this + + " for reason : " + e.getMessage(), e); } } @@ -400,7 +403,7 @@ @Override public String toString() { - return super.toString() + "<" + getClazz() + ">"; + return super.toString() + '<' + getClazz() + '>'; } protected Collection<String> getProperties(String[] properties) { @@ -455,7 +458,10 @@ log.debug("===== start for " + getClazz()); } - init(getClazz(), explored, properties, associationProperties, getters, setters, childGetters, childAdders, childAddersAll, childRemovers, childSize, childClearers, childIsEmpty); + init(getClazz(), explored, properties, associationProperties, + getters, setters, childGetters, childAdders, + childAddersAll, childRemovers, childSize, + childClearers, childIsEmpty); if (!properties.isEmpty()) { int size = properties.size(); @@ -478,7 +484,9 @@ associationProperties = Collections.unmodifiableList(associationProperties); } if (log.isDebugEnabled()) { - log.debug("===== end for " + getClazz() + " (" + properties.size() + " properties, " + associationProperties.size() + " associations)"); + log.debug("===== end for " + getClazz() + " (" + + properties.size() + " properties, " + + associationProperties.size() + " associations)"); } } catch (IntrospectionException e) { throw new RuntimeException(e); @@ -496,9 +504,11 @@ } } - private Method getMethod(String name, int nbParams, Method[] methodDescriptors) { + private Method getMethod(String name, int nbParams, + Method[] methodDescriptors) { for (Method m : methodDescriptors) { - if (name.equals(m.getName()) && m.getParameterTypes().length == nbParams) { + if (name.equals(m.getName()) && + m.getParameterTypes().length == nbParams) { return m; } } @@ -508,7 +518,8 @@ private int checkPropertyIndex(String name) { int index = properties.indexOf(name); if (index == -1) { - throw new IllegalArgumentException("property " + name + " is unknown for " + this); + throw new IllegalArgumentException("property " + name + + " is unknown for " + this); } return index; } @@ -516,7 +527,8 @@ private int checkAssociationPropertyIndex(String name) { int index = associationProperties.indexOf(name); if (index == -1) { - throw new IllegalArgumentException("association property " + name + " is unknown for " + this); + throw new IllegalArgumentException("association property " + + name + " is unknown for " + this); } return index; } @@ -535,7 +547,8 @@ } @SuppressWarnings({"unchecked"}) - protected static <V> V invokeWithResult(Method m, Object bean, Object... args) { + protected static <V> V invokeWithResult(Method m, Object bean, + Object... args) { try { if (log.isTraceEnabled()) { log.trace(bean.getClass() + "#" + m.getName()); @@ -566,7 +579,8 @@ throw new NullPointerException("entityClass can not be null!"); } if (!entityClass.isInterface()) { - throw new IllegalArgumentException("entityClass parameter " + entityClass + " is not interface!"); + throw new IllegalArgumentException("entityClass parameter " + + entityClass + " is not interface!"); } if (explored.contains(entityClass)) { return; @@ -585,7 +599,8 @@ for (PropertyDescriptor propertydescriptor : propertyDescriptors) { String propertyName = propertydescriptor.getName(); - if (propertydescriptor.getReadMethod() != null && propertydescriptor.getWriteMethod() != null) { + if (propertydescriptor.getReadMethod() != null && + propertydescriptor.getWriteMethod() != null) { // on a detecte une propriete (lecture/ecriture) if (log.isDebugEnabled()) { log.debug("detected property : " + propertyName); @@ -634,7 +649,9 @@ Class<?>[] interfaces = entityClass.getInterfaces(); if (interfaces.length > 0) { for (Class<?> c : interfaces) { - if (TopiaEntity.class == c || !(TopiaEntity.class.isAssignableFrom(c)) || explored.contains(c)) { + if (TopiaEntity.class == c || + !(TopiaEntity.class.isAssignableFrom(c)) || + explored.contains(c)) { // on ne traite pas continue; } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperatorStore.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperatorStore.java 2010-02-21 19:38:11 UTC (rev 1809) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/EntityOperatorStore.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -50,9 +50,11 @@ * @see EntityOperator */ @SuppressWarnings("unchecked") - public static <E extends TopiaEntity> EntityOperator<E> getOperator(TopiaEntityEnum contract) { + public static <E extends TopiaEntity> EntityOperator<E> getOperator( + TopiaEntityEnum contract) { - EntityOperator<E> operator = (EntityOperator<E>) shared.store.get(contract); + EntityOperator<E> operator = + (EntityOperator<E>) shared.store.get(contract); if (operator == null) { synchronized (shared.store) { operator = new EntityOperator<E>(contract); Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Loador.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Loador.java 2010-02-21 19:38:11 UTC (rev 1809) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/Loador.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -27,7 +27,10 @@ * * @param <E> type of bean to load * @author chemit + * @deprecated since 2.3.0, prefer use the {@link org.nuiton.util.beans.Binder} + * api (and {@link TopiaEntityBinder} extension. */ +@Deprecated public interface Loador<E> extends java.io.Serializable { /** Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityBinder.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityBinder.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityBinder.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -0,0 +1,55 @@ +/* + * *##% + * ToPIA :: Persistence + * Copyright (C) 2004 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package org.nuiton.topia.persistence.util; + +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.util.beans.Binder; + +/** + * A {@link Binder} dedicated to {@link TopiaEntity} dealing with technical + * values. + * <p/> + * There is an internal sate {@link #empty} to deal with default values of + * properties of an entity. + * + * @author tchemit < chemit@codelutin.com > + * @param <E> the type of entity + * @since 2.3.0 + */ +public class TopiaEntityBinder<E extends TopiaEntity> extends Binder<E, E> { + private static final long serialVersionUID = 1L; + + protected E empty; + + public void setEmpty(E empty) { + this.empty = empty; + } + + public void copy(E source, E target, boolean tech) { + if (source == null) { + source = empty; + } + if (tech) { + TopiaEntityHelper.bindTechnical(source, target); + } + copy(source, target); + } +} \ No newline at end of file Property changes on: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityBinder.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2010-02-21 19:38:11 UTC (rev 1809) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2010-02-21 20:39:35 UTC (rev 1810) @@ -68,6 +68,14 @@ protected static final Double ZEROD = 0.; protected static final Byte ZEROB = 0; + /** + * + * @param type the type to test + * @return null object (default value for primitive types) + * @deprecated since 2.3.0, prefer use the + * {@link org.nuiton.util.ObjectUtil#getNullValue(Class)} method + */ + @Deprecated protected static Object getNullValue(Class<?> type) { if (type.isPrimitive()) { type = MethodUtils.getPrimitiveWrapper(type); @@ -93,6 +101,14 @@ return null; } + /** + * @param value the value to test + * @return {@code true} if value is null or default value for primitive + * types) + * @deprecated since 2.3.0, prefer use the + * {@link org.nuiton.util.ObjectUtil#isNullValue(Object)} method + */ + @Deprecated public static boolean isNullValue(Object value) { if (value == null) { return true;