Author: tchemit Date: 2010-05-13 14:47:00 +0200 (Thu, 13 May 2010) New Revision: 1958 Url: http://nuiton.org/repositories/revision/topia/1958 Log: add javadoc on TopiaTransactionEvent use foreach in TopiaFireSupport Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionEvent.java trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionListener.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityState.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionEvent.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionEvent.java 2010-05-12 12:03:13 UTC (rev 1957) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionEvent.java 2010-05-13 12:47:00 UTC (rev 1958) @@ -25,20 +25,21 @@ package org.nuiton.topia.event; -import java.util.EventObject; -import java.util.Map; -import java.util.Set; - import org.apache.commons.collections.map.IdentityMap; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.framework.EntityState; import org.nuiton.topia.persistence.TopiaEntity; +import java.util.EventObject; +import java.util.Map; +import java.util.Set; + /** - * TODO-fdesbois-20100507 : Need javadoc. + * Event fires for {@link TopiaTransactionListener}. * * @author poussin <poussin@codelutin.com> * @version $Id$ + * @see TopiaTransactionListener */ public class TopiaTransactionEvent extends EventObject { @@ -51,7 +52,7 @@ } public TopiaTransactionEvent(TopiaContext source, - Map<TopiaEntity, EntityState> entities) { + Map<TopiaEntity, EntityState> entities) { this(source); this.entities.putAll(entities); } @@ -88,10 +89,29 @@ public boolean isModification(TopiaEntity entity) { EntityState state = entities.get(entity); return state != null - && (state.isCreate() || state.isUpdate() || state.isDelete()); + && (state.isCreate() || state.isUpdate() || state.isDelete()); } - public TopiaContext getTopiaContext() { + @Override + public TopiaContext getSource() { return (TopiaContext) super.getSource(); } + + /** + * @return the source context that fires the event + * @deprecated since 2.4, prefer the overriden {@link #getSource()}. + */ + @Deprecated + public TopiaContext getTopiaContext() { + return getSource(); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + if (entities != null) { + entities.clear(); + entities = null; + } + } } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionListener.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionListener.java 2010-05-12 12:03:13 UTC (rev 1957) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/event/TopiaTransactionListener.java 2010-05-13 12:47:00 UTC (rev 1958) @@ -25,19 +25,48 @@ package org.nuiton.topia.event; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaFiresSupport; + import java.util.EventListener; /** - * TODO-fdesbois-20100507 : Need javadoc. + * To listen transaction operations as commit and rollback. + * <p/> + * <b>Warning:</b> Must be attached to the current transaction. + * <p/> + * {@link TopiaContext} listens such listeners via {@code javaBeans} methods : + * <ul> + * <li>{@link TopiaContext#addTopiaTransactionListener(TopiaTransactionListener)}</li> + * <li>{@link TopiaContext#removeTopiaTransactionListener(TopiaTransactionListener)}</li> + * </ul> * * @author poussin <poussin@codelutin.com> - * @author tchemit <tchemit@codelutin.com> * @version $Id$ + * @see TopiaContext + * @see TopiaTransactionEvent + * @see TopiaFiresSupport#fireOnPostCommit(TopiaContextImplementor) + * @see TopiaFiresSupport#fireOnPostRollback(TopiaContextImplementor) */ public interface TopiaTransactionListener extends EventListener { + /** + * Fired by {@link TopiaFiresSupport#fireOnPostCommit(TopiaContextImplementor)}. + * <p/> + * Says after a commit was performed on listened transaction. + * + * @param event the transaction event + */ void commit(TopiaTransactionEvent event); + /** + * Fired by {@link TopiaFiresSupport#fireOnPostRollback(TopiaContextImplementor)}. + * <p/> + * Says after a rollback was performed on listened transaction. + * + * @param event the transaction event + */ void rollback(TopiaTransactionEvent event); } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityState.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityState.java 2010-05-12 12:03:13 UTC (rev 1957) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityState.java 2010-05-13 12:47:00 UTC (rev 1958) @@ -39,57 +39,118 @@ /** * Used to know the state of entity during transaction. - * + * * @author poussin <poussin@codelutin.com> */ -public class EntityState { +public class EntityState implements Comparable<EntityState>{ final static int NULL = 0; + final static int LOAD = 1; + final static int READ = 2; + final static int CREATE = 4; + final static int UPDATE = 8; + final static int DELETE = 16; + /** internal representation of all states of entity (use bitwise operation). */ protected int state = NULL; + /** + * Add load state. + * <p/> + * After the invocation, method {@link #isLoad()} will always return {@code true}. + */ public void addLoad() { state |= LOAD; } + /** + * Add read state + * <p/> + * After the invocation, method {@link #isRead()} will always return {@code true}. + */ public void addRead() { state |= READ; } + /** + * Add create state. + * <p/> + * After the invocation, method {@link #isCreate()} will always return {@code true}. + */ public void addCreate() { state |= CREATE; } + /** + * Add update state. + * <p/> + * After the invocation, method {@link #isUpdate()} will always return {@code true}. + */ public void addUpdate() { state |= UPDATE; } + /** + * Add delete state. + * <p/> + * After the invocation, method {@link #isDelete()} will always return {@code true}. + */ public void addDelete() { state |= DELETE; } + /** + * Tells if the {@link #LOAD} state is on. + * + * @return {@code true} if {@link #LOAD} state is on, {@code false} otherwise. + */ public boolean isLoad() { return (state & LOAD) == LOAD; } + /** + * Tells if the {@link #READ} state is on. + * + * @return {@code true} if {@link #READ} state is on, {@code false} otherwise. + */ public boolean isRead() { return (state & READ) == READ; } + /** + * Tells if the {@link #CREATE} state is on. + * + * @return {@code true} if {@link #CREATE} state is on, {@code false} otherwise. + */ public boolean isCreate() { return (state & CREATE) == CREATE; } + /** + * Tells if the {@link #UPDATE} state is on. + * + * @return {@code true} if {@link #UPDATE} state is on, {@code false} otherwise. + */ public boolean isUpdate() { return (state & UPDATE) == UPDATE; } + /** + * Tells if the {@link #DELETE} state is on. + * + * @return {@code true} if {@link #DELETE} state is on, {@code false} otherwise. + */ public boolean isDelete() { return (state & DELETE) == DELETE; } + + @Override + public int compareTo(EntityState o) { + return state - o.state; + } } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java 2010-05-12 12:03:13 UTC (rev 1957) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java 2010-05-13 12:47:00 UTC (rev 1958) @@ -215,10 +215,9 @@ log.debug("fireOnBeginTransaction"); } TopiaTransactionEvent e = new TopiaTransactionEvent(context); - for (Iterator<TopiaTransactionVetoable> l = transactionVetoables - .iterator(); l.hasNext();) { + for (TopiaTransactionVetoable listener : transactionVetoables) { try { - l.next().beginTransaction(e); + listener.beginTransaction(e); } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -231,10 +230,9 @@ } TopiaTransactionEvent e = new TopiaTransactionEvent(context, transactionEntities); - for (Iterator<TopiaTransactionListener> l = transactionListeners - .iterator(); l.hasNext();) { + for (TopiaTransactionListener listener : transactionListeners) { try { - l.next().commit(e); + listener.commit(e); } catch (Exception eee) { if (log.isErrorEnabled()) { log.error("Can't fireOnPostCommit", eee); @@ -250,10 +248,9 @@ } TopiaTransactionEvent e = new TopiaTransactionEvent(context, transactionEntities); - for (Iterator<TopiaTransactionListener> l = transactionListeners - .iterator(); l.hasNext();) { + for (TopiaTransactionListener listener : transactionListeners) { try { - l.next().rollback(e); + listener.rollback(e); } catch (Exception eee) { if (log.isErrorEnabled()) { log.error("Can't fireOnPostRollback", eee); @@ -414,7 +411,9 @@ /* Fires sur les propriétés */ public void fireOnPreRead(VetoableChangeSupport vetoables, - TopiaEntity entity, String propertyName, Object value) { + TopiaEntity entity, + String propertyName, + Object value) { if (log.isDebugEnabled()) { log.debug("fireOnPreRead"); @@ -445,8 +444,10 @@ } public void fireOnPostRead(PropertyChangeSupport listeners, - TopiaEntity entity, String propertyName, - int index, Object value) { + TopiaEntity entity, + String propertyName, + int index, + Object value) { if (log.isDebugEnabled()) { log.debug("fireOnPostRead"); @@ -464,8 +465,10 @@ } public void fireOnPreWrite(VetoableChangeSupport vetoables, - TopiaEntity entity, String propertyName, - Object oldValue, Object newValue) { + TopiaEntity entity, + String propertyName, + Object oldValue, + Object newValue) { if (log.isDebugEnabled()) { log.debug("fireOnPreWrite"); @@ -478,8 +481,10 @@ } public void fireOnPostWrite(PropertyChangeSupport listeners, - TopiaEntity entity, String propertyName, - Object oldValue, Object newValue) { + TopiaEntity entity, + String propertyName, + Object oldValue, + Object newValue) { if (log.isDebugEnabled()) { log.debug("fireOnPostWrite"); @@ -509,8 +514,11 @@ } public void fireOnPostWrite(PropertyChangeSupport listeners, - TopiaEntity entity, String propertyName, - int index, Object oldValue, Object newValue) { + TopiaEntity entity, + String propertyName, + int index, + Object oldValue, + Object newValue) { if (log.isDebugEnabled()) { log.debug("fireOnPostWrite"); @@ -725,10 +733,9 @@ log.debug("firePreCreateSchema"); } TopiaContextEvent event = new TopiaContextEvent(context); - for (Iterator<TopiaContextListener> l = topiaContextListeners - .iterator(); l.hasNext();) { + for (TopiaContextListener topiaContextListener : topiaContextListeners) { try { - l.next().preCreateSchema(event); + topiaContextListener.preCreateSchema(event); } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -745,10 +752,9 @@ log.debug("firePostCreateSchema"); } TopiaContextEvent event = new TopiaContextEvent(context); - for (Iterator<TopiaContextListener> l = topiaContextListeners - .iterator(); l.hasNext();) { + for (TopiaContextListener topiaContextListener : topiaContextListeners) { try { - l.next().postCreateSchema(event); + topiaContextListener.postCreateSchema(event); } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -765,10 +771,9 @@ log.debug("firePostCreateSchema"); } TopiaContextEvent event = new TopiaContextEvent(context); - for (Iterator<TopiaContextListener> l = topiaContextListeners - .iterator(); l.hasNext();) { + for (TopiaContextListener topiaContextListener : topiaContextListeners) { try { - l.next().preUpdateSchema(event); + topiaContextListener.preUpdateSchema(event); } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -785,10 +790,9 @@ log.debug("firePostCreateSchema"); } TopiaContextEvent event = new TopiaContextEvent(context); - for (Iterator<TopiaContextListener> l = topiaContextListeners - .iterator(); l.hasNext();) { + for (TopiaContextListener topiaContextListener : topiaContextListeners) { try { - l.next().postUpdateSchema(event); + topiaContextListener.postUpdateSchema(event); } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -805,10 +809,9 @@ log.debug("firePreRestoreSchema"); } TopiaContextEvent event = new TopiaContextEvent(context); - for (Iterator<TopiaContextListener> l = topiaContextListeners - .iterator(); l.hasNext();) { + for (TopiaContextListener topiaContextListener : topiaContextListeners) { try { - l.next().preRestoreSchema(event); + topiaContextListener.preRestoreSchema(event); } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -825,10 +828,9 @@ log.debug("firePostRestoreSchema"); } TopiaContextEvent event = new TopiaContextEvent(context); - for (Iterator<TopiaContextListener> l = topiaContextListeners - .iterator(); l.hasNext();) { + for (TopiaContextListener topiaContextListener : topiaContextListeners) { try { - l.next().postRestoreSchema(event); + topiaContextListener.postRestoreSchema(event); } catch (Exception eee) { throw new TopiaVetoException(eee); } @@ -850,12 +852,11 @@ } List<E> result = entities; - for (Iterator<TopiaEntitiesVetoable> it = entitiesVetoables.iterator(); - it.hasNext();) { + for (TopiaEntitiesVetoable entitiesVetoable : entitiesVetoables) { try { - TopiaEntitiesEvent event = new TopiaEntitiesEvent(context, - result); - result = it.next().load(event); + //FIXME tchemit 20100513 Why instanciate n events? if necessary MUST add a comment + TopiaEntitiesEvent<E> event = new TopiaEntitiesEvent<E>(context, result); + result = entitiesVetoable.load(event); } catch (Exception eee) { throw new TopiaVetoException(eee); }