Author: tchemit Date: 2011-05-12 20:12:59 +0200 (Thu, 12 May 2011) New Revision: 2274 Url: http://nuiton.org/repositories/revision/topia/2274 Log: clean code + fix some javadoc tags Evolution #1523: Add a findAllContainsXXX and findContainsXXX method in generated dao for collection properties Anomalie #1522: generated findUsages in DAO are not correct Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DepthEntityVisitor.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/EntityVisitor.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.java 2011-05-06 09:26:31 UTC (rev 2273) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/DAOAbstractTransformer.java 2011-05-12 18:12:59 UTC (rev 2274) @@ -62,7 +62,6 @@ /*{generator option: parentheses = false}*/ - /*{generator option: writeString = +}*/ /** @@ -593,7 +592,8 @@ builder.addImport(result, usageType); String usageSimpleType = TopiaGeneratorUtil.getClassNameFromQualifiedName(usageType); - + String usageSimplePropertyMethod = "findAllBy" + usageSimpleType; + String usageCollectionPropertyMethod = "findAllContaining" + usageSimpleType; for (ObjectModelAttribute attr : usageClass.getAttributes()) { if (!attr.isNavigable()) { // skip this case @@ -616,13 +616,20 @@ continue; } ObjectModelClass targetEntity = allEntitiesByFQN.get(type); - //if (!type.equals(clazz.getQualifiedName())) { +// if (!type.equals(clazz.getQualifiedName())) { if (!targetEntity.equals(clazz)) { // not a good attribute reference continue; } // found something to seek + String methodNameSuffix = StringUtils.capitalize(attrName); + String methodName; + if (TopiaGeneratorUtil.isNMultiplicity(attr)) { + methodName = "findAllContains" + methodNameSuffix; + } else { + methodName = "findAllBy" + methodNameSuffix; + } String daoName = StringUtils.capitalize(usageSimpleType) + "DAO"; builder.addImport(result, usageClass.getPackageName() + '.' + daoName); @@ -632,7 +639,7 @@ if (type == <%=usageSimpleType%>.class) { <%=daoName%> dao = (<%=daoName%>) getContext().getDAO(<%=usageSimpleType%>.class); - tmp = dao.findAllByProperties(<%=usageSimpleType%>.<%=getConstantName(attrName)%>, entity); + tmp = dao.<%=methodName%>(entity); result.addAll(tmp); } }*/ @@ -861,8 +868,45 @@ protected void generateNMultiplicity(String clazzName, ObjectModelClass result, ObjectModelAttribute attr) { String attrName = attr.getName(); String attrType = attr.getType(); + if (attr.hasAssociationClass()) { + // do nothing for association class, too complex... + return; + } ObjectModelOperation op; // Since 2.4 do nothing, findContains and findAllContains are not generated anymore + op = addOperation(result, + "findContains" + StringUtils.capitalize(attrName), + "E", + ObjectModelModifier.PUBLIC); + addException(op, TopiaException.class); + addParameter(op, attrType, "v"); + setDocumentation(op, "Retourne le premier élément ayant comme valeur pour" + + " l'attribut " + + TopiaGeneratorUtil.toLowerCaseFirstLetter(attrName) + + " le paramètre."); + setOperationBody(op, "" +/*{ + E result = findContains(<%=clazzName + "." + getConstantName(attrName)%>, v); + return result; + }*/ + ); + + op = addOperation(result, + "findAllContains" + StringUtils.capitalize(attrName), + "List<E>", + ObjectModelModifier.PUBLIC); + addException(op, TopiaException.class); + addParameter(op, attrType, "v"); + setDocumentation(op, "Retourne les éléments ayant comme valeur pour" + + " l'attribut " + + TopiaGeneratorUtil.toLowerCaseFirstLetter(attrName) + + " le paramètre."); + setOperationBody(op, "" +/*{ + List<E> result = findAllContains(<%=clazzName + "." + getConstantName(attrName)%>, v); + return result; + }*/ + ); } private boolean isCollectionNeeded( Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DepthEntityVisitor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DepthEntityVisitor.java 2011-05-06 09:26:31 UTC (rev 2273) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DepthEntityVisitor.java 2011-05-12 18:12:59 UTC (rev 2274) @@ -25,17 +25,17 @@ package org.nuiton.topia.persistence; -import java.util.ArrayList; -import java.util.Collection; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.topia.TopiaException; +import java.util.ArrayList; +import java.util.Collection; + /** * Parcourt du graphe d'entité en profondeur. * - * @author chatellier <chatellier@codelutin.com> + * @author echatellier <chatellier@codelutin.com> * @author tchemit <tchemit@codelutin.com> * @version $Id$ */ Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/EntityVisitor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/EntityVisitor.java 2011-05-06 09:26:31 UTC (rev 2273) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/EntityVisitor.java 2011-05-12 18:12:59 UTC (rev 2274) @@ -27,10 +27,11 @@ /** * The contract of a visitor of any {@link TopiaEntity}. - * + * <p/> * Created: 28 janv. 2009 18:10:34 * - * @author poussin <poussin@codelutin.com> + * @author bpoussin <poussin@codelutin.com> + * @author tchemit <chemit@codelutin.com> * @version $Id$ */ public interface EntityVisitor { @@ -51,50 +52,53 @@ /** * Visit a none indexed property for the given entity. - * + * <p/> * The property visited is defined by the other parameters. * - * @param entity the visited entity + * @param entity the visited entity * @param propertyName the name of the visited property - * @param type the type of the visited property - * @param value the value of the visited property + * @param type the type of the visited property + * @param value the value of the visited property */ void visit(TopiaEntity entity, String propertyName, Class<?> type, Object value); /** * Visit a collection property for the given entity. - * + * <p/> * The property visited is defined by the other parameters. * - * @param entity the visited entity - * @param propertyName the name of the visited property - * @param collectionType the type of the visited collection - * @param type the type of the visited property - * @param value the value of the visited property + * @param entity the visited entity + * @param propertyName the name of the visited property + * @param collectionType the type of the visited collection + * @param type the type of the visited property + * @param value the value of the visited property */ void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, Object value); /** * Visit a indexed value from a collection property for the given entity. - * + * <p/> * The property visited is defined by the other parameters. * - * @param entity the visited entity - * @param propertyName the name of the visited property + * @param entity the visited entity + * @param propertyName the name of the visited property * @param collectionType the type of the container of the visited property - * @param type the type of the visited property - * @param index the index of the visited property in his container - * @param value the value of the visited property + * @param type the type of the visited property + * @param index the index of the visited property in his container + * @param value the value of the visited property */ void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, int index, Object value); /** * Reset all states of the visitor. - * - * TODO-chatellier-20091221 : need explanations : called by who ? what is it for ? + * <p/> + * If you use internal states inside the visitor, this method should clean + * all of them. + * <p/> + * This method should be invoked after usage of the visitor. */ void clear(); } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java 2011-05-06 09:26:31 UTC (rev 2273) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java 2011-05-12 18:12:59 UTC (rev 2274) @@ -25,18 +25,18 @@ package org.nuiton.topia.persistence; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaException; + import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.topia.TopiaException; - /** * Parcourt en largeur du modele et délegation à un autre visiteur. * - * @author chatellier <chatellier@codelutin.com> + * @author echatellier <chatellier@codelutin.com> * @author tchemit <tchemit@codelutin.com> * @version $Id$ */ Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2011-05-06 09:26:31 UTC (rev 2273) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2011-05-12 18:12:59 UTC (rev 2274) @@ -60,12 +60,12 @@ * DAOAbstractTransformer} for abstract implementation and {@link * DAOImplTransformer} for final implementation class. * <p/> - * TODO-fdesbois-20100508 : Need translation of javadoc. * - * @author poussin <poussin@codelutin.com> + * @param <E> the entity type linked with the dao + * @author bpoussin <poussin@codelutin.com> * @author fdesbois <fdesbois@codelutin.com> + * @author tchemit <chemit@codelutin.com> * @version $Id$ - * @param <E> the entity type linked with the dao */ public interface TopiaDAO<E extends TopiaEntity> { @@ -241,7 +241,7 @@ * Find an entity corresponding to the {@code id}. If the {@code id} is * null, nothing will be search. * - * @param id topiaId of the entity to found + * @param id topiaId of the entity to found * @return the entity found or null if not * @throws TopiaException for Topia errors on query */ @@ -349,6 +349,34 @@ throws TopiaException; /** + * Récupère toutes les entités (du type géré par ce dao) dont la + * collection nommée {@code propertyName} contient la {@code property} + * donnée. + * + * @param propertyName le nom de la propriété + * @param property la propriété recherchée + * @return toutes les entités recherchées + * @throws TopiaException pour tout erreur lors de la recherche + * @since 2.5.4 + */ + List<E> findAllContains(String propertyName, + Object property) throws TopiaException; + + /** + * Récupère la première entité (du type géré par ce dao) dont la + * collection nommée {@code propertyName} contient la {@code property} + * donnée. + * + * @param propertyName le nom de la propriété + * @param property la propriété recherchée + * @return la première entité recherchée + * @throws TopiaException pour tout erreur lors de la recherche + * @since 2.5.4 + */ + E findContains(String propertyName, + Object property) throws TopiaException; + + /** * Check the existence of an entity with technical {@code id}. * * @param id unique id of the entity to test existence. @@ -395,7 +423,7 @@ /** * Count the number of entities based on {@code query}. - * + * * @param query query * @return number of entities filtered by the query * @throws TopiaException if any pb while getting datas Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2011-05-06 09:26:31 UTC (rev 2273) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2011-05-12 18:12:59 UTC (rev 2274) @@ -57,8 +57,6 @@ import java.util.Map; /** - * TODO-fdesbois-20100508 : Need translation of javadoc. - * <p/> * Cette classe permet d'avoir un ensemble de méthode implantée de façon * standard et plus spécifiquement pour Hibernate. * <p/> @@ -67,9 +65,9 @@ * possible d'accèder au meta information hibernate sur les classes lorque l'on * en a besoin. * - * @author poussin <poussin@codelutin.com> + * @param <E> le type de l'entite + * @author bpoussin <poussin@codelutin.com> * @version $Id$ - * @param <E> le type de l'entite */ public class TopiaDAOImpl<E extends TopiaEntity> implements @@ -102,11 +100,11 @@ try { Serializable result; result = (Serializable) PropertyUtils.getSimpleProperty(e, - idPropName); + idPropName); return result; } catch (Exception eee) { throw new TopiaException("Impossible de récuperer l'identifiant " - + idPropName + " de l'entite: " + e); + + idPropName + " de l'entite: " + e); } } @@ -162,12 +160,12 @@ .newInstance(); if (log.isDebugEnabled()) { log.debug("Utilisation de la classe " + classname + "Impl" - + " pour " + classname); + + " pour " + classname); } } catch (ClassNotFoundException eee) { if (log.isWarnEnabled()) { log.warn("Impossible de trouver la classe " + classname - + "Impl"); + + "Impl"); } if (log.isDebugEnabled()) { log.debug("StackTrace", eee); @@ -187,7 +185,7 @@ result = entityClass.getConstructor().newInstance(); if (log.isDebugEnabled()) { log.debug("Utilisation de la classe " + classname - + " pour " + classname); + + " pour " + classname); } } catch (Exception eee) { if (log.isWarnEnabled()) { @@ -202,7 +200,7 @@ if (result == null) { throw new TopiaException( "Impossible de trouver ou d'instancier la classe " - + classname); + + classname); } return result; @@ -308,13 +306,13 @@ } } catch (ArrayIndexOutOfBoundsException eee) { throw new IllegalArgumentException("Wrong number of argument " - + properties.length - + ", you must have even number. Last property name read: " - + propertyName); + + properties.length + + ", you must have even number. Last property name read: " + + propertyName); } catch (ClassCastException eee) { throw new IllegalArgumentException( "Wrong argument type, wait property name as String and " + - "have " + propertyName.getClass().getName()); + "have " + propertyName.getClass().getName()); } E result = create(map); @@ -510,6 +508,24 @@ } @Override + public E findContains(String propertyName, + Object property) throws TopiaException { + TopiaQuery k = createQuery(). + addInElements(":K", propertyName). + addParam("K", property); + return findByQuery(k); + } + + @Override + public List<E> findAllContains(String propertyName, + Object property) throws TopiaException { + TopiaQuery k = createQuery(). + addInElements(":K", propertyName). + addParam("K", property); + return findAllByQuery(k); + } + + @Override public boolean existByTopiaId(String id) throws TopiaException { boolean result = existByProperties(TopiaEntity.TOPIA_ID, id); return result; @@ -571,13 +587,13 @@ } catch (ClassCastException eee) { throw new IllegalArgumentException( "Les noms des propriétés doivent être des chaines et " + - "non pas " + propertyName.getClass().getName(), + "non pas " + propertyName.getClass().getName(), eee); } catch (ArrayIndexOutOfBoundsException eee) { throw new IllegalArgumentException( "Le nombre d'argument n'est pas un nombre pair: " - + (others.length + 2) - + " La dernière propriété était: " + name, eee); + + (others.length + 2) + + " La dernière propriété était: " + name, eee); } } return properties; @@ -598,7 +614,7 @@ throw new TopiaException(eee); } throw new TopiaException("La classe " + entityClass.getName() - + " n'a pas de cle primaire naturelle"); + + " n'a pas de cle primaire naturelle"); } @@ -625,7 +641,7 @@ throw new TopiaException(eee); } throw new TopiaException("La classe " + entityClass.getName() - + " n'a pas de cle primaire naturelle"); + + " n'a pas de cle primaire naturelle"); } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java 2011-05-06 09:26:31 UTC (rev 2273) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOLegacy.java 2011-05-12 18:12:59 UTC (rev 2274) @@ -41,18 +41,6 @@ * <http://www.gnu.org/licenses/lgpl-3.0.html>. * ##%*/ -/* * - * TopiaDAOAbstract.java - * - * Created: 31 déc. 2005 13:10:34 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package org.nuiton.topia.persistence; import org.apache.commons.beanutils.PropertyUtils; @@ -86,19 +74,22 @@ * choix de se baser entièrement sur hibernate pour la persistence, et il * est ainsi possible d'accèder au meta information hibernate sur les classes * lorque l'on en a besoin. - * - * @param <E> le type de l'entite - * @author poussin * + * Created: 31 déc. 2005 13:10:34 + * + * @param <E> le type de l'entite + * @author bpoussin <poussin@codelutin.com> + * @author tchemit <chemit@codelutin.com> */ public class TopiaDAOLegacy<E extends TopiaEntity> extends TopiaDAOImpl<E> { // TopiaDAOImpl - /** to use log facility, just put in your code: log.info(\"...\"); */ + + /** Logger. */ private static Log log = LogFactory.getLog(TopiaDAOImpl.class); - protected Class<E> entityClass = null; + protected Class<E> entityClass; - protected TopiaContextImplementor context = null; + protected TopiaContextImplementor context; @Override public Class<E> getEntityClass() { @@ -120,15 +111,9 @@ try { result = (Serializable) PropertyUtils.getSimpleProperty(e, idPropName); - } catch (IllegalAccessException eee) { + } catch (Exception eee) { throw new TopiaException("Impossible de récuperer l'identifiant " + idPropName + " de l'entite: " + e); - } catch (InvocationTargetException eee) { - throw new TopiaException("Impossible de récuperer l'identifiant " - + idPropName + " de l'entite: " + e); - } catch (NoSuchMethodException eee) { - throw new TopiaException("Impossible de récuperer l'identifiant " - + idPropName + " de l'entite: " + e); } return result; }