r1737 - trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence
Author: echatellier Date: 2009-12-21 12:39:53 +0100 (Mon, 21 Dec 2009) New Revision: 1737 Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DepthEntityVisitor.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/EntityVisitor.java Log: Add horizontal entity visitor. 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 2009-12-20 19:08:53 UTC (rev 1736) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/DepthEntityVisitor.java 2009-12-21 11:39:53 UTC (rev 1737) @@ -36,10 +36,14 @@ */ public class DepthEntityVisitor implements EntityVisitor { + /** Class logger. */ private static Log log = LogFactory.getLog(DepthEntityVisitor.class); + + /** Cache of already explored entities. */ protected Collection<TopiaEntity> alreadyExplored; + /** - * Le visiteur metier (optionel) + * Le visiteur metier (optionel). */ protected EntityVisitor delegateVisitor; @@ -65,7 +69,7 @@ } @Override - public void visit(TopiaEntity e, String name, Class<?> type, Object value) { + public void visit(TopiaEntity e, String propertyName, Class<?> type, Object value) { // si c'est une entité if (value instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) value; @@ -80,26 +84,26 @@ } } else { if (delegateVisitor != null) { - delegateVisitor.visit(e, name, type, value); + delegateVisitor.visit(e, propertyName, type, value); } } } @Override - public void visit(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, + public void visit(TopiaEntity e, String propertyName, Class<?> collectionType, Class<?> type, Object value) { Collection<?> cValue = (Collection<?>) value; if (cValue != null && !cValue.isEmpty()) { int i = 0; for (Object currentValue : cValue) { - visit(e, name, type, collectionType, i++, currentValue); + visit(e, propertyName, type, collectionType, i++, currentValue); } } } @Override - public void visit(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, int index, Object value) { + public void visit(TopiaEntity e, String propertyName, Class<?> collectionType, Class<?> type, int index, Object value) { // si c'est une entité if (value instanceof TopiaEntity) { TopiaEntity entity = (TopiaEntity) value; @@ -114,7 +118,7 @@ } } else { if (delegateVisitor != null) { - delegateVisitor.visit(e, name, collectionType, type, index, value); + delegateVisitor.visit(e, propertyName, collectionType, type, index, value); } } } 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 2009-12-20 19:08:53 UTC (rev 1736) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/EntityVisitor.java 2009-12-21 11:39:53 UTC (rev 1737) @@ -20,7 +20,6 @@ package org.nuiton.topia.persistence; /** - * * The contract of a visitor of any {@link TopiaEntity}. * * Created: 28 janv. 2009 18:10:34 @@ -36,58 +35,60 @@ /** * Start the visit of the given entity. * - * @param e the visited entity + * @param entity the visited entity */ - void start(TopiaEntity e); + void start(TopiaEntity entity); /** * Ends the visit of the given entity. * - * @param e the visited entity + * @param entity the visited entity */ - void end(TopiaEntity e); + void end(TopiaEntity entity); /** * Visit a none indexed property for the given entity. * * The property visited is defined by the other parameters. * - * @param e the visited entity - * @param name the name of the visited property + * @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 visitied property + * @param value the value of the visited property */ - void visit(TopiaEntity e, String name, Class<?> type, Object value); + void visit(TopiaEntity entity, String propertyName, Class<?> type, Object value); /** * Visit a collection property for the given entity. * * The property visited is defined by the other parameters. * - * @param e the visited entity - * @param name 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 visited collection * @param type the type of the visited property - * @param value the value of the visitied property + * @param value the value of the visited property */ - void visit(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, Object value); + void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, Object value); /** * Visit a indexed value from a collection property for the given entity. * * The property visited is defined by the other parameters. * - * @param e the visited entity - * @param name 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 visitied property + * @param value the value of the visited property */ - void visit(TopiaEntity e, String name, Class<?> collectionType, Class<?> type, int index, Object value); + void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, int index, Object value); /** - * reset all states of the visitor. + * Reset all states of the visitor. + * + * TODO called by who ? what is it for ? */ void clear(); } Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java 2009-12-21 11:39:53 UTC (rev 1737) @@ -0,0 +1,154 @@ +/* *##% + * Copyright (C) 2009 Code Lutin, Chatellier Eric + * + * 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; + +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; +import org.nuiton.topia.persistence.EntityVisitor; +import org.nuiton.topia.persistence.TopiaEntity; + +/** + * Parcourt en largeur du modele et délegation à un autre visiteur. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class HorizontalEntityVisitor implements EntityVisitor { + + /** Class logger. */ + private static Log log = LogFactory.getLog(HorizontalEntityVisitor.class); + + /** Delegate visitor. */ + protected EntityVisitor delegateVisitor; + + /** Cache used to remember entity during exploration. */ + protected List<TopiaEntity> alreadyExplored; + + /** Entity to be visited later. */ + protected List<TopiaEntity> toVisitEntities; + + /** + * Constructor. + * + * @param delegateVisitor delegate visitor + */ + public HorizontalEntityVisitor(EntityVisitor delegateVisitor) { + this.delegateVisitor = delegateVisitor; + this.alreadyExplored = new ArrayList<TopiaEntity>(); + this.toVisitEntities = new ArrayList<TopiaEntity>(); + } + + /* + * @see org.nuiton.topia.persistence.EntityVisitor#start(org.nuiton.topia.persistence.TopiaEntity) + */ + @Override + public void start(TopiaEntity entity) { + delegateVisitor.start(entity); + if (!alreadyExplored.contains(entity)) { + alreadyExplored.add(entity); + } + } + + /* + * @see org.nuiton.topia.persistence.EntityVisitor#visit(org.nuiton.topia.persistence.TopiaEntity, java.lang.String, java.lang.Class, java.lang.Object) + */ + @Override + public void visit(TopiaEntity entity, String propertyName, Class<?> type, Object value) { + // si c'est une entité + if (value instanceof TopiaEntity) { + TopiaEntity entityValue = (TopiaEntity) value; + toVisitEntities.add(entityValue); + } else { + delegateVisitor.visit(entity, propertyName, type, value); + } + } + + /* + * @see org.nuiton.topia.persistence.EntityVisitor#visit(org.nuiton.topia.persistence.TopiaEntity, java.lang.String, java.lang.Class, java.lang.Class, java.lang.Object) + */ + @Override + public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, + Class<?> type, Object value) { + Collection<?> values = (Collection<?>) value; + if (values != null && !values.isEmpty()) { + int i = 0; + for (Object currentValue : values) { + visit(entity, propertyName, type, collectionType, i++, currentValue); + } + } + } + + /* + * @see org.nuiton.topia.persistence.EntityVisitor#visit(org.nuiton.topia.persistence.TopiaEntity, java.lang.String, java.lang.Class, java.lang.Class, int, java.lang.Object) + */ + @Override + public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, + Class<?> type, int index, Object value) { + // si c'est une entité + if (value instanceof TopiaEntity) { + TopiaEntity entityValue = (TopiaEntity) value; + toVisitEntities.add(entityValue); + } else { + delegateVisitor.visit(entity, propertyName, collectionType, type, index, value); + } + } + + /* + * @see org.nuiton.topia.persistence.EntityVisitor#end(org.nuiton.topia.persistence.TopiaEntity) + */ + @Override + public void end(TopiaEntity entity) { + delegateVisitor.end(entity); + + // here, must revisit all remembered entities + List<TopiaEntity> currentEntities = new ArrayList<TopiaEntity>(toVisitEntities); + // TODO verify if clearing here is enough + toVisitEntities.clear(); + for (TopiaEntity currentEntity : currentEntities) { + try { + if (!alreadyExplored.contains(currentEntity)) { + currentEntity.accept(this); + log.debug("Post process " + currentEntity); + } + } catch (TopiaException ex) { + if (log.isErrorEnabled()) { + log.error("Error on horizontal exploration", ex); + } + } + } + } + + /* + * @see org.nuiton.topia.persistence.EntityVisitor#clear() + */ + @Override + public void clear() { + alreadyExplored.clear(); + toVisitEntities.clear(); + delegateVisitor.clear(); + } +} // HorizontallyEntityVisitor Property changes on: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/HorizontalEntityVisitor.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"
participants (1)
-
echatellier@users.nuiton.org