Author: tchemit Date: 2012-08-18 12:02:32 +0200 (Sat, 18 Aug 2012) New Revision: 2630 Url: http://nuiton.org/repositories/revision/topia/2630 Log: refs #2266: Add some api about import / export in csv format (improve export api) Added: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/ExportEntityVisitor.java Removed: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/AbstractExportEntityVisitor.java Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java Deleted: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/AbstractExportEntityVisitor.java =================================================================== --- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/AbstractExportEntityVisitor.java 2012-08-18 09:57:33 UTC (rev 2629) +++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/AbstractExportEntityVisitor.java 2012-08-18 10:02:32 UTC (rev 2630) @@ -1,181 +0,0 @@ -package org.nuiton.topia.persistence.csv.out; -/* - * #%L - * ToPIA :: Persistence - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2012 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>. - * #L% - */ - -import com.google.common.base.Preconditions; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.TopiaRuntimeException; -import org.nuiton.topia.persistence.EntityVisitor; -import org.nuiton.topia.persistence.TopiaEntity; -import org.nuiton.topia.persistence.TopiaEntityEnum; -import org.nuiton.util.TimeLog; - -import java.util.Collection; -import java.util.Map; - -/** - * entity visitor to export data to csv files. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.3 - */ -public abstract class AbstractExportEntityVisitor<T extends TopiaEntityEnum> implements EntityVisitor { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(AbstractExportEntityVisitor.class); - - public static final TimeLog TIME_LOG = - new TimeLog(AbstractExportEntityVisitor.class); - - - protected abstract boolean isNoChildVisit(String propertyName, TopiaEntity entity); - - - /** Export for simple entity. */ - protected final Map<Class<?>, TopiaCsvExports.EntityExportContext> entityExporters; - - public AbstractExportEntityVisitor(Map<Class<?>, TopiaCsvExports.EntityExportContext> entityExporters) { - this.entityExporters = entityExporters; - } - - public void export(TopiaEntity entity) { - Preconditions.checkNotNull(entity); - long s1 = TimeLog.getTime(); - try { - entity.accept(this); - } catch (TopiaException e) { - throw new TopiaRuntimeException( - "Could not export entity " + entity.getTopiaId(), e); - } finally { - TIME_LOG.log(s1, "export::" + entity.getTopiaId()); - } - } - - @Override - public void start(TopiaEntity entity) { - String topiaId = entity.getTopiaId(); - try { - if (log.isDebugEnabled()) { - log.debug("Starts export of entity " + topiaId); - } - TopiaCsvExports.EntityExportContext entityExporter = - entityExporters.get(entity.getClass()); - entityExporter.write(entity); - } catch (Exception e) { - throw new TopiaRuntimeException( - "Could not export entity " + entity, e); - } finally { - if (log.isDebugEnabled()) { - log.debug("Ends export of entity " + topiaId); - } - } - } - - @Override - public void end(TopiaEntity entity) { - try { - if (log.isDebugEnabled()) { - log.debug("Starts export of association of entity " + - entity.getTopiaId()); - } - TopiaCsvExports.EntityExportContext entityExporter = - entityExporters.get(entity.getClass()); - entityExporter.writeAssociations(entity); - } catch (Exception e) { - throw new TopiaRuntimeException( - "Could not export associations of entity " + entity, e); - } finally { - if (log.isDebugEnabled()) { - log.debug("Ends export of association of entity " + - entity.getTopiaId()); - } - } - } - - @Override - public void visit(TopiaEntity entity, String propertyName, - Class<?> type, Object value) { - } - - @Override - public void visit(TopiaEntity entity, - String propertyName, - Class<?> collectionType, - Class<?> type, - Object value) { - - if (TopiaEntity.class.isAssignableFrom(type) && - entityExporters.containsKey(type)) { - Collection<?> cValue = (Collection<?>) value; - - if (CollectionUtils.isNotEmpty(cValue)) { - - if (isNoChildVisit(propertyName, entity) && - Entity2.class.isAssignableFrom(type)) { - - // special case, when visiting a entity with no child - // visisting at this time... - for (Object currentValue : cValue) { - ((Entity2) currentValue).accept2(this); - } - } else { - for (Object currentValue : cValue) { - try { - ((TopiaEntity) currentValue).accept(this); - } catch (TopiaException e) { - if (log.isErrorEnabled()) { - log.error("Can not visit entity " + value, e); - } - } - } - } - } - } - } - - @Override - public void visit(TopiaEntity entity, - String propertyName, - Class<?> collectionType, Class<?> type, - int index, - Object value) { - - // nothing to do - } - - @Override - public void clear() { - - // use at the end of visit (or later) - - for (TopiaCsvExports.EntityExportContext exportContext : entityExporters.values()) { - IOUtils.closeQuietly(exportContext); - } - } -} Copied: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/ExportEntityVisitor.java (from rev 2627, branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/AbstractExportEntityVisitor.java) =================================================================== --- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/ExportEntityVisitor.java (rev 0) +++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/ExportEntityVisitor.java 2012-08-18 10:02:32 UTC (rev 2630) @@ -0,0 +1,200 @@ +package org.nuiton.topia.persistence.csv.out; +/* + * #%L + * ToPIA :: Persistence + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 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>. + * #L% + */ + +import com.google.common.base.Preconditions; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaRuntimeException; +import org.nuiton.topia.persistence.EntityVisitor; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.topia.persistence.TopiaEntityEnum; +import org.nuiton.topia.persistence.metadata.TopiaEntityEnumProvider; +import org.nuiton.util.TimeLog; + +import java.io.Closeable; +import java.io.IOException; +import java.util.Collection; +import java.util.Map; + +/** + * Entity visitor to export data to csv files. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class ExportEntityVisitor<T extends TopiaEntityEnum> implements EntityVisitor, Closeable { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ExportEntityVisitor.class); + + public static final TimeLog TIME_LOG = + new TimeLog(ExportEntityVisitor.class); + + /** Export for simple entity. */ + protected final Map<T, TopiaCsvExports.EntityExportContext<T>> entityExporters; + + protected final TopiaEntityEnumProvider<T> entityEnumProvider; + + public ExportEntityVisitor(TopiaEntityEnumProvider<T> entityEnumProvider, Map<T, TopiaCsvExports.EntityExportContext<T>> entityExporters) { + this.entityEnumProvider = entityEnumProvider; + this.entityExporters = entityExporters; + } + + public void export(TopiaEntity entity) { + Preconditions.checkNotNull(entity); + long s1 = TimeLog.getTime(); + try { + entity.accept(this); + } catch (TopiaException e) { + throw new TopiaRuntimeException( + "Could not export entity " + entity.getTopiaId(), e); + } finally { + TIME_LOG.log(s1, "export::" + entity.getTopiaId()); + } + } + + @Override + public void start(TopiaEntity entity) { + String topiaId = entity.getTopiaId(); + try { + if (log.isDebugEnabled()) { + log.debug("Starts export of entity " + topiaId); + } + TopiaCsvExports.EntityExportContext entityExporter = + getEntityContext(entity.getClass()); + Preconditions.checkNotNull(entityExporter); + entityExporter.write(entity); + } catch (Exception e) { + throw new TopiaRuntimeException( + "Could not export entity " + entity, e); + } finally { + if (log.isDebugEnabled()) { + log.debug("Ends export of entity " + topiaId); + } + } + } + + @Override + public void end(TopiaEntity entity) { + try { + if (log.isDebugEnabled()) { + log.debug("Starts export of association of entity " + + entity.getTopiaId()); + } + TopiaCsvExports.EntityExportContext entityExporter = + getEntityContext(entity.getClass()); + Preconditions.checkNotNull(entityExporter); + entityExporter.writeAssociations(entity); + } catch (Exception e) { + throw new TopiaRuntimeException( + "Could not export associations of entity " + entity, e); + } finally { + if (log.isDebugEnabled()) { + log.debug("Ends export of association of entity " + + entity.getTopiaId()); + } + } + } + + + @Override + public void visit(TopiaEntity entity, String propertyName, + Class<?> type, Object value) { + } + + @Override + public void visit(TopiaEntity entity, + String propertyName, + Class<?> collectionType, + Class<?> type, + Object value) { + + if (TopiaEntity.class.isAssignableFrom(type) && + getEntityContext(type) != null) { + Collection<?> cValue = (Collection<?>) value; + + if (CollectionUtils.isNotEmpty(cValue)) { + + if (isNoChildVisit(propertyName, entity) && + Entity2.class.isAssignableFrom(type)) { + + // special case, when visiting a entity with no child + // visisting at this time... + for (Object currentValue : cValue) { + ((Entity2) currentValue).accept2(this); + } + } else { + for (Object currentValue : cValue) { + try { + ((TopiaEntity) currentValue).accept(this); + } catch (TopiaException e) { + if (log.isErrorEnabled()) { + log.error("Can not visit entity " + value, e); + } + } + } + } + } + } + } + + @Override + public void visit(TopiaEntity entity, + String propertyName, + Class<?> collectionType, Class<?> type, + int index, + Object value) { + + // nothing to do + } + + @Override + public void close() throws IOException { + + // use at the end of visit (or later) + + for (TopiaCsvExports.EntityExportContext<T> exportContext : entityExporters.values()) { + exportContext.close(); + } + } + + @Override + public void clear() { + // prefer use the close api + } + + protected TopiaCsvExports.EntityExportContext getEntityContext(Class<?> entityType) { + T entityEnum = entityEnumProvider.getEntityEnum(entityType); + return entityEnum == null ? null : entityExporters.get(entityEnum); + } + + protected boolean isNoChildVisit(String propertyName, TopiaEntity entity) { + + // by default accept all associations + return false; + } +} Property changes on: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/ExportEntityVisitor.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java =================================================================== --- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java 2012-08-18 09:57:33 UTC (rev 2629) +++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/out/TopiaCsvExports.java 2012-08-18 10:02:32 UTC (rev 2630) @@ -150,24 +150,21 @@ return Export.newExport(model, datas); } - public static <T extends TopiaEntityEnum> Map<Class<?>, EntityExportContext> createReplicateEntityVisitorContexts(ExportModelFactory<T> modelFactory, - MetaFilenameAware<T>[] entityMetas, - Multimap<T, MetaFilenameAware<T>> associations, - File container) { + public static <T extends TopiaEntityEnum> Map<T, EntityExportContext<T>> createReplicateEntityVisitorContexts(ExportModelFactory<T> modelFactory, + MetaFilenameAware<T>[] entityMetas, + Multimap<T, MetaFilenameAware<T>> associations, + File container) { Preconditions.checkNotNull(modelFactory); Preconditions.checkNotNull(entityMetas); Preconditions.checkNotNull(associations); Preconditions.checkNotNull(container); - Map<Class<?>, EntityExportContext> contexts = Maps.newHashMap(); + Map<T, EntityExportContext<T>> contexts = Maps.newHashMap(); for (MetaFilenameAware<T> entityMeta : entityMetas) { TableMeta<T> meta = (TableMeta<T>) entityMeta; - T source = meta.getSource(); - Collection<MetaFilenameAware<T>> metaFilenameAwares = associations.get(source); - ExportModel<TopiaEntity> model = modelFactory.buildForExport(meta); EntityExportContext<T> exportContext = EntityExportContext.newExportContext( @@ -175,13 +172,14 @@ meta, container); - // save both for contract and implementation with same context - contexts.put(source.getContract(), exportContext); - contexts.put(source.getImplementation(), exportContext); + T source = meta.getSource(); - for (MetaFilenameAware<T> metaFilenameAware : metaFilenameAwares) { - AssociationMeta<T> associationMeta = (AssociationMeta<T>) metaFilenameAware; + contexts.put(source, exportContext); + for (MetaFilenameAware<T> metaFilenameAware : associations.get(source)) { + AssociationMeta<T> associationMeta = + (AssociationMeta<T>) metaFilenameAware; + ExportModel<TopiaEntity> associationModel = modelFactory.buildForExport(associationMeta); exportContext.addAssociationExportContext(associationMeta, @@ -224,8 +222,7 @@ public static <T extends TopiaEntityEnum> EntityExportContext<T> newExportContext( ExportModel<TopiaEntity> model, TableMeta<T> meta, - File container - ) { + File container) { return new EntityExportContext<T>(model, meta, container); }