001package gu.sql2java; 002 003import gu.sql2java.exception.RuntimeDaoException; 004 005/** 006 * Listener that is notified of table changes. 007 * @param <B> java bean type 008 * @author guyadong 009 */ 010public interface TableListener<B>{ 011 /** 012 * This adapter class provides default implementations for the 013 * methods declared by the {@link TableListener} interface.<br> 014 * 015 * @author guyadong 016 */ 017 public static class Adapter<B> implements TableListener<B>{ 018 019 @Override 020 public void beforeInsert(B bean)throws RuntimeDaoException {} 021 022 @Override 023 public void afterInsert(B bean)throws RuntimeDaoException {} 024 025 @Override 026 public void beforeUpdate(B bean)throws RuntimeDaoException {} 027 028 @Override 029 public void afterUpdate(B bean)throws RuntimeDaoException {} 030 031 @Override 032 public void beforeDelete(B bean)throws RuntimeDaoException {} 033 034 @Override 035 public void afterDelete(B bean)throws RuntimeDaoException {} 036 037 @Override 038 public void done()throws RuntimeDaoException {} 039 } 040 /** 041 * Invoked just before inserting a B record into the database. 042 * 043 * @param bean the B that is about to be inserted 044 * @throws RuntimeDaoException 045 */ 046 public void beforeInsert(B bean)throws RuntimeDaoException; 047 048 049 /** 050 * Invoked just after a B record is inserted in the database. 051 * 052 * @param bean the B that was just inserted 053 * @throws RuntimeDaoException 054 */ 055 public void afterInsert(B bean)throws RuntimeDaoException; 056 057 058 /** 059 * Invoked just before updating a B record in the database. 060 * 061 * @param bean the B that is about to be updated 062 * @throws RuntimeDaoException 063 */ 064 public void beforeUpdate(B bean)throws RuntimeDaoException; 065 066 067 /** 068 * Invoked just after updating a B record in the database. 069 * 070 * @param bean the B that was just updated 071 * @throws RuntimeDaoException 072 */ 073 public void afterUpdate(B bean)throws RuntimeDaoException; 074 075 076 /** 077 * Invoked just before deleting a B record in the database. 078 * 079 * @param bean the B that is about to be deleted 080 * @throws RuntimeDaoException 081 */ 082 public void beforeDelete(B bean)throws RuntimeDaoException; 083 084 085 /** 086 * Invoked just after deleting a B record in the database. 087 * 088 * @param bean the B that was just deleted 089 * @throws RuntimeDaoException 090 */ 091 public void afterDelete(B bean)throws RuntimeDaoException; 092 093 /** 094 * Invoked in finally block, just after insert,update,delete. 095 * 096 * @throws RuntimeDaoException 097 */ 098 public void done()throws RuntimeDaoException; 099 100 /** 101 * listener event:<br> 102 * {@code INSERT} insert a bean<br> 103 * {@code UPDATE} update a bean<br> 104 * {@code DELETE} delete a bean<br> 105 * {@code UPDATE_BEFORE} before updating a bean<br> 106 * @author guyadong 107 * 108 */ 109 public static enum Event{ 110 /** insert a bean */INSERT, 111 /** update a bean */UPDATE, 112 /** delete a bean */DELETE, 113 /** before updating a bean */UPDATE_BEFORE,; 114 /** 115 * fire current event by {@link ListenerContainer} 116 * @param container 117 * @param bean 118 * @throws RuntimeDaoException 119 */ 120 public <B> void fire(ListenerContainer<B> container,B bean)throws RuntimeDaoException { 121 if(null == container || null == bean){ 122 return; 123 } 124 switch(this){ 125 case INSERT: 126 container.afterInsert(bean); 127 break; 128 case UPDATE: 129 container.afterUpdate(bean); 130 break; 131 case DELETE: 132 container.afterDelete(bean); 133 break; 134 case UPDATE_BEFORE: 135 container.beforeUpdate(bean); 136 break; 137 default: 138 break; 139 } 140 } 141 public <B extends BaseBean> void fire(TableManager<B > manager,B bean)throws RuntimeDaoException { 142 if(null == manager || null == bean){ 143 return; 144 } 145 manager.fire(this, bean); 146 } 147 } 148} 149