r2489 - trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence
Author: tchemit Date: 2012-05-17 00:50:12 +0200 (Thu, 17 May 2012) New Revision: 2489 Url: http://nuiton.org/repositories/revision/topia/2489 Log: refs #2078: Remove the TopiaContext from TopiaEntityAbstract (deprecate getComposite and getAggregate from TopiaEntity and rewritte them in DAO) Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaPersistenceUtil.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 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 2012-05-16 22:44:49 UTC (rev 2488) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2012-05-16 22:50:12 UTC (rev 2489) @@ -37,6 +37,8 @@ package org.nuiton.topia.persistence; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -52,6 +54,7 @@ import javax.persistence.metamodel.Metamodel; import java.lang.reflect.InvocationTargetException; import java.security.Permission; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -173,21 +176,22 @@ @Override public E create(E entity) throws TopiaException { - // TODO EC-20100322 this code may be merged with other create() methods - try { - // first set topiaId - String topiaId = TopiaId.create(entityClass); - TopiaEntityAbstract entityAbstract = (TopiaEntityAbstract) entity; - entityAbstract.setTopiaId(topiaId); - entityAbstract.setTopiaContext(getContext()); - - // save entity - getSession().persist(entity); - getContext().getFiresSupport().warnOnUpdateEntity(entity); - return entity; - } catch (PersistenceException eee) { - throw new TopiaException(eee); - } + return create0(entity); +// // TODO EC-20100322 this code may be merged with other create() methods +// try { +// // first set topiaId +// String topiaId = TopiaId.create(entityClass); +// TopiaEntityAbstract entityAbstract = (TopiaEntityAbstract) entity; +// entityAbstract.setTopiaId(topiaId); +// entityAbstract.setTopiaContext(getContext()); +// +// // save entity +// getSession().persist(entity); +// getContext().getFiresSupport().warnOnUpdateEntity(entity); +// return entity; +// } catch (PersistenceException eee) { +// throw new TopiaException(eee); +// } } @Override @@ -227,13 +231,13 @@ // TODO reflechir s'il ne faudrait pas creer l'id avant l'event precedent // reflechir toujours dans un context on les E pourrait ne pas - // etre des TopiaEntity - if (result instanceof TopiaEntity) { - String topiaId = TopiaId.create(entityClass); - TopiaEntityAbstract entity = (TopiaEntityAbstract) result; - entity.setTopiaId(topiaId); - entity.setTopiaContext(getContext()); - } + // etre des TopiaEntity (ce qui n'arrivera jamais vu la signature (TC)...) +// if (result instanceof TopiaEntity) { +// String topiaId = TopiaId.create(entityClass); + TopiaEntityAbstract entity = (TopiaEntityAbstract) result; +// entity.setTopiaId(topiaId); + entity.setTopiaContext(getContext()); +// } try { for (Map.Entry<String, Object> e : properties.entrySet()) { String propertyName = e.getKey(); @@ -250,7 +254,16 @@ throw new IllegalArgumentException( "Can't put properties on new Object", eee); } + return create0(result); +// // on fait un save maintenant, car puisqu'on a creer l'entity au +// // travers du DAO, on s'attend a l'avoir a disposition tout de +// // suite pour les requetes sans avoir a faire un update dessus +// getSession().persist(result); +// getContext().getFiresSupport().warnOnCreateEntity(result); +// return result; + } + protected E create0(E result) throws TopiaException { // on fait un save maintenant, car puisqu'on a creer l'entity au // travers du DAO, on s'attend a l'avoir a disposition tout de // suite pour les requetes sans avoir a faire un update dessus @@ -281,13 +294,11 @@ } @Override - @Deprecated public TopiaQuery createQuery() { return new TopiaQuery(getEntityClass()); } @Override - @Deprecated public TopiaQuery createQuery(String entityAlias) { return new TopiaQuery(getEntityClass(), entityAlias); } @@ -552,10 +563,22 @@ } + @Override + public Multimap<Class<? extends TopiaEntity>, TopiaEntity> getComposite(E entity) throws TopiaException { + throw new UnsupportedOperationException( + "This method must be overided in generated DAO"); + } + + @Override + public Multimap<Class<? extends TopiaEntity>, TopiaEntity> getAggregate(E entity) throws TopiaException { + throw new UnsupportedOperationException( + "This method must be overided in generated DAO"); + } + /** * Renvoie la Session contenue dans le contexte * - * @return hibernate session + * @return jpa session * @throws TopiaException if any pb */ EntityManager getSession() throws TopiaException { @@ -580,5 +603,34 @@ // } return meta; } - + + protected Multimap<Class<? extends TopiaEntity>, TopiaEntity> fillComposite(Multimap<Class<? extends TopiaEntity>, TopiaEntity> tmp2) throws TopiaException { + + Multimap<Class<? extends TopiaEntity>, TopiaEntity> result = + ArrayListMultimap.create(); + for (Class<? extends TopiaEntity> aClass : tmp2.keySet()) { + Collection<TopiaEntity> eee = tmp2.get(aClass); + result.putAll(aClass, eee); + TopiaDAO dao = getContext().getDAO(aClass); + for (TopiaEntity entity : eee) { + tmp2.putAll(dao.getComposite(entity)); + } + } + return result; + } + + protected Multimap<Class<? extends TopiaEntity>, TopiaEntity> fillAggregate(Multimap<Class<? extends TopiaEntity>, TopiaEntity> tmp2) throws TopiaException { + + Multimap<Class<? extends TopiaEntity>, TopiaEntity> result = + ArrayListMultimap.create(); + for (Class<? extends TopiaEntity> aClass : tmp2.keySet()) { + Collection<TopiaEntity> eee = tmp2.get(aClass); + result.putAll(aClass, eee); + TopiaDAO dao = getContext().getDAO(aClass); + for (TopiaEntity entity : eee) { + tmp2.putAll(dao.getAggregate(entity)); + } + } + return result; + } } //TopiaDAOImpl Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaPersistenceUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaPersistenceUtil.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaPersistenceUtil.java 2012-05-16 22:50:12 UTC (rev 2489) @@ -0,0 +1,44 @@ +package org.nuiton.topia.persistence; + +/** + * Util class for persistence parts of ToPIA. + * + * @author tchemit <chemit@codelutin.com> + * @since 3.0 + */ +public class TopiaPersistenceUtil { + + protected TopiaPersistenceUtil() { + // no constructor for a util class + } + + + /** + * Obtain the *entity* contract from a given class. + * <p/> + * This is usefull when you obtain an implementation of an entity and + * you want his contract. + * + * @param entityType type to scan + * @return the *entity* contract found or {@code null} if not found. + * @since 3.0 + */ + public static Class<?> getEntityContract(Class<?> entityType) { + Class<?> entityInterface = null; + Class<?>[] interfaces = entityType.getInterfaces(); + for (Class<?> anInterface : interfaces) { + if (TopiaEntity.class.isAssignableFrom(anInterface)) { + entityInterface = anInterface; + break; + } + } + if (entityInterface == null) { + // try on super class + Class<?> superclass = entityType.getSuperclass(); + if (superclass != null && !Object.class.equals(superclass)) { + entityInterface = getEntityContract(superclass); + } + } + return entityInterface; + } +}
participants (1)
-
tchemit@users.nuiton.org