Author: tchemit Date: 2010-08-14 15:53:11 +0200 (Sat, 14 Aug 2010) New Revision: 2097 Url: http://nuiton.org/repositories/revision/topia/2097 Log: introduce a TopiaEntityIdsMap + use it + reformat code Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityIdsMap.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 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-08-11 14:47:08 UTC (rev 2096) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2010-08-14 13:53:11 UTC (rev 2097) @@ -233,7 +233,7 @@ } log.info("create database from [" + databaseDump + "] at [" + - dbDirectory + "]"); + dbDirectory + "]"); TopiaContext ctxt = topiaContext.beginTransaction(); @@ -270,7 +270,7 @@ if (!gzipFile.getParentFile().exists()) { if (!gzipFile.getParentFile().mkdirs()) { throw new IOException("could not create directory " + - gzipFile.getParentFile()); + gzipFile.getParentFile()); } } log.info("store database to [" + gzipFile + "]"); @@ -577,19 +577,20 @@ * les entites dont le contract est dans l'ensemble voulu). * @throws TopiaException if a pb while visiting entities */ - public static Map<Class<? extends TopiaEntity>, List<String>> + public static TopiaEntityIdsMap detectEntityIds(final TopiaEntityEnum[] contracts, final Set<Class<? extends TopiaEntity>> types, TopiaEntity... entities) throws TopiaException { - Collector<Map<Class<? extends TopiaEntity>, List<String>>> collector; - collector = new Collector<Map<Class<? extends TopiaEntity>, List<String>>>(contracts) { + Collector<TopiaEntityIdsMap> collector; + collector = new Collector<TopiaEntityIdsMap>(contracts) { + /** * le dictionnaire des références détectées indexées par leur type */ - protected Map<Class<? extends TopiaEntity>, List<String>> detectedRefs = - new HashMap<Class<? extends TopiaEntity>, List<String>>(); + protected TopiaEntityIdsMap detectedRefs = + new TopiaEntityIdsMap(); @Override protected void onStarted(TopiaEntity e, boolean enter) { @@ -614,14 +615,13 @@ } @Override - protected Map<Class<? extends TopiaEntity>, List<String>> + protected TopiaEntityIdsMap afterAll(CollectorVisitor visitor, TopiaEntity... entities) { return detectedRefs; } }; - Map<Class<? extends TopiaEntity>, List<String>> result = - collector.detect(entities); + TopiaEntityIdsMap result = collector.detect(entities); return result; } @@ -641,7 +641,7 @@ String[] expressions, TopiaEntity entities) throws TopiaException { return detectReferences(contracts, expressions, - Collections.singleton(entities)); + Collections.singleton(entities)); } /** @@ -735,7 +735,7 @@ boolean contains = visitor.getStack().contains(e1); if (log.isDebugEnabled()) { log.debug("already enter " + e1.getTopiaId() + - ", can reenter ? " + !contains); + ", can reenter ? " + !contains); } if (!contains) { try { @@ -787,7 +787,7 @@ boolean contains = visitor.getStack().contains(e1); if (log.isDebugEnabled()) { log.debug("already enter " + e1.getTopiaId() + - ", can reenter ? " + !contains); + ", can reenter ? " + !contains); } if (!contains) { try { @@ -815,7 +815,7 @@ Class<?> type, int index, Object value, boolean enter) { super.onVisited(e, name, collectionType, type, index, value, - enter); + enter); if (enter) { removePath(); } @@ -860,8 +860,8 @@ // } if (log.isTraceEnabled()) { log.trace("add to stack : " + e.getTopiaId() + - ", new size : " + path.size() + ", path : " + - accessorExpression.toString()); + ", new size : " + path.size() + ", path : " + + accessorExpression.toString()); } } @@ -874,13 +874,13 @@ TopiaEntityRef.SEPARATOR); if (index > -1) { accessorExpression.delete(index, - accessorExpression.length()); + accessorExpression.length()); } } if (log.isTraceEnabled()) { log.trace("remove from stack : " + e.getTopiaId() + - ", new size : " + path.size() + ", path : " + - accessorExpression.toString()); + ", new size : " + path.size() + ", path : " + + accessorExpression.toString()); } } @@ -905,11 +905,11 @@ if (log.isDebugEnabled()) { log.debug(expression + " (" + e.getTopiaId() + ") - " + - list.size() + " , root:" + root.getTopiaId()); + list.size() + " , root:" + root.getTopiaId()); } if (log.isTraceEnabled()) { log.trace(e.getTopiaId() + " : new size " + list.size() + - ", path : " + expression); + ", path : " + expression); } } }; @@ -995,7 +995,7 @@ } } boolean wasModified = referentielEntity.getTopiaVersion() > - localeEntity.getTopiaVersion(); + localeEntity.getTopiaVersion(); if (wasModified) { result.get(DiffState.MODIFIED).add(refId); @@ -1047,7 +1047,7 @@ } if (klass.getSuperclass() != null && - TopiaEntity.class.isAssignableFrom(klass.getSuperclass())) { + TopiaEntity.class.isAssignableFrom(klass.getSuperclass())) { getInterfaces( (Class<? extends TopiaEntity>) klass.getSuperclass(), klassInterfaces Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityIdsMap.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityIdsMap.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityIdsMap.java 2010-08-14 13:53:11 UTC (rev 2097) @@ -0,0 +1,30 @@ +package org.nuiton.topia.persistence.util; + +import org.nuiton.topia.persistence.TopiaEntity; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * A map of ids of entities grouped by their type. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.4.3 + */ +public class TopiaEntityIdsMap extends HashMap<Class<? extends TopiaEntity>, List<String>> { + private static final long serialVersionUID = 1L; + + public void addIds(Class<? extends TopiaEntity> type, Iterable<String> ids) { + List<String> list = get(type); + if (list == null) { + list = new ArrayList<String>(); + put(type, list); + } + for (String id : ids) { + if (!list.contains(id)) { + list.add(id); + } + } + } +}