Author: chatellier Date: 2009-01-29 17:23:41 +0000 (Thu, 29 Jan 2009) New Revision: 1337 Added: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/DepthEntityVisitor.java Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/TopiaEntity.java topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/EntityVisitorExportXmlTest.java topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/ExportXMLVisitor.java Log: Ajout d'un implementation de parcourt par defaut. Added: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/DepthEntityVisitor.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/DepthEntityVisitor.java (rev 0) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/DepthEntityVisitor.java 2009-01-29 17:23:41 UTC (rev 1337) @@ -0,0 +1,112 @@ +/* *##% + * Copyright (C) 2009 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.topia.persistence; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.TopiaException; + +/** + * Parcourt du graphes d'entité en profondeur. + * + * @author chatellier + * @version $Revision: 1.0 $ + * + * Last update : $Date: 29 janv. 2009 $ + * By : $Author: chatellier $ + */ +public class DepthEntityVisitor implements EntityVisitor { + + private static Log log = LogFactory.getLog(DepthEntityVisitor.class); + + protected Collection<TopiaEntity> alreadyExplored; + + /** + * Le visiteur metier. + */ + protected EntityVisitor delegateVisitor; + + public DepthEntityVisitor(EntityVisitor delegateVisitor) { + + alreadyExplored = new ArrayList<TopiaEntity>(); + + this.delegateVisitor = delegateVisitor; + } + + /* + * @see org.codelutin.topia.persistence.EntityVisitor#start(org.codelutin.topia.persistence.TopiaEntity) + */ + @Override + public void start(TopiaEntity e) { + delegateVisitor.start(e); + alreadyExplored.add(e); + } + + /* + * @see org.codelutin.topia.persistence.EntityVisitor#visit(org.codelutin.topia.persistence.TopiaEntity, java.lang.Class, java.lang.Object) + */ + @Override + public void visit(TopiaEntity e, Class type, Object value) { + // si c'est une entité + if(value instanceof TopiaEntity) { + TopiaEntity entity = (TopiaEntity)value; + try { + if(!alreadyExplored.contains(entity)) { + entity.accept(this); + } + } catch (TopiaException e1) { + if(log.isErrorEnabled()) { + log.error("Error on depth exploration", e1); + } + } + } + else { + delegateVisitor.visit(e, type, value); + } + } + + /* + * @see org.codelutin.topia.persistence.EntityVisitor#visit(org.codelutin.topia.persistence.TopiaEntity, java.lang.Class, java.lang.Class, java.lang.Object) + */ + @Override + public void visit(TopiaEntity e, Class collectionType, Class type, + Object value) { + + Collection cValue = (Collection)value; + if(cValue != null && !cValue.isEmpty()) { + Iterator itOnValues = cValue.iterator(); + while(itOnValues.hasNext()) { + Object currentValue = itOnValues.next(); + visit(e, type, currentValue); + } + } + } + + /* + * @see org.codelutin.topia.persistence.EntityVisitor#end(org.codelutin.topia.persistence.TopiaEntity) + */ + @Override + public void end(TopiaEntity e) { + delegateVisitor.end(e); + } +} Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/TopiaEntity.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/TopiaEntity.java 2009-01-29 16:22:56 UTC (rev 1336) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/TopiaEntity.java 2009-01-29 17:23:41 UTC (rev 1337) @@ -130,4 +130,12 @@ public void removeVetoableListener(VetoableChangeListener vetoable); + /** + * Parcourt de l'entité via un visiteur. + * + * @param visitor visitor + * @throws TopiaException + */ + public void accept(EntityVisitor visitor) throws TopiaException; + } //TopiaEntity Modified: topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/EntityVisitorExportXmlTest.java =================================================================== --- topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/EntityVisitorExportXmlTest.java 2009-01-29 16:22:56 UTC (rev 1336) +++ topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/EntityVisitorExportXmlTest.java 2009-01-29 17:23:41 UTC (rev 1337) @@ -31,7 +31,6 @@ import org.codelutin.topiatest.Address; import org.codelutin.topiatest.AddressDAO; import org.codelutin.topiatest.Company; -import org.codelutin.topiatest.CompanyAbstract; import org.codelutin.topiatest.CompanyDAO; import org.codelutin.topiatest.Department; import org.codelutin.topiatest.DepartmentDAO; @@ -161,10 +160,13 @@ /** * Test l'export XML via un visiteur. + * + * Parcourt en profondeur. + * * @throws TopiaException */ @Test - public void testExportXML() throws TopiaException { + public void testExportXMLDepth() throws TopiaException { TopiaContext rootContext = TopiaContextFactory.getContext(config); TopiaContext context = rootContext.beginTransaction(); @@ -172,13 +174,13 @@ CompanyDAO companyDAO = TopiaTestDAOHelper.getCompanyDAO(context); Company clCompany = companyDAO.findByName("CodeLutin"); - ExportXMLVisitor visitor = new ExportXMLVisitor(); - // FIXME voir pour l'abstract - ((CompanyAbstract)clCompany).accept(visitor); + EntityVisitor delegateVisitor = new ExportXMLVisitor(); + EntityVisitor visitor = new DepthEntityVisitor(delegateVisitor); + clCompany.accept(visitor); context.closeContext(); if(log.isInfoEnabled()) { - log.info("Export XML = \n" + visitor.toString()); + log.info("Export XML = \n" + delegateVisitor.toString()); } } } Modified: topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/ExportXMLVisitor.java =================================================================== --- topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/ExportXMLVisitor.java 2009-01-29 16:22:56 UTC (rev 1336) +++ topia/trunk/topia-persistence/src/test/java/org/codelutin/topia/persistence/ExportXMLVisitor.java 2009-01-29 17:23:41 UTC (rev 1337) @@ -80,24 +80,7 @@ @Override public void visit(TopiaEntity e, Class collectionType, Class type, Object value) { - if(log.isDebugEnabled()) { - log.debug("visit collection : " + e); - } - - - Collection cValue = (Collection)value; - if(cValue != null && !cValue.isEmpty()) { - buffer.append("<" + collectionType.getName() + ">\n"); - Iterator itOnValues = cValue.iterator(); - while(itOnValues.hasNext()) { - TopiaEntityAbstract entity = (TopiaEntityAbstract)itOnValues.next(); - - buffer.append("<" + type.getName() + ">\n"); - buffer.append("TODO sublist visit\n"); - buffer.append("</" + type.getName() + ">\n"); - } - buffer.append("</" + collectionType.getName() + ">\n"); - } + } /*
participants (1)
-
chatellier@users.labs.libre-entreprise.org