KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > TableModificationEvent


1 /**
2  * com.mckoi.database.TableModificationEvent 07 Mar 2003
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 /**
28  * The event information of when a table is modified inside a transaction.
29  *
30  * @author Tobias Downer
31  */

32
33 public class TableModificationEvent {
34
35   // ----- Statics -----
36

37   /**
38    * Event that occurs before the action
39    */

40   public static final int BEFORE = 0x010;
41
42   /**
43    * Event that occurs after the action
44    */

45   public static final int AFTER = 0x020;
46
47   // ---
48

49   /**
50    * Event type for insert action.
51    */

52   public static final int INSERT = 0x001;
53
54   /**
55    * Event type for update action.
56    */

57   public static final int UPDATE = 0x002;
58
59   /**
60    * Event type for delete action.
61    */

62   public static final int DELETE = 0x004;
63
64   // ---
65

66   /**
67    * Event for before an insert.
68    */

69   public static final int BEFORE_INSERT = BEFORE | INSERT;
70
71   /**
72    * Event for after an insert.
73    */

74   public static final int AFTER_INSERT = AFTER | INSERT;
75
76   /**
77    * Event for before an update.
78    */

79   public static final int BEFORE_UPDATE = BEFORE | UPDATE;
80
81   /**
82    * Event for after an update.
83    */

84   public static final int AFTER_UPDATE = AFTER | UPDATE;
85
86   /**
87    * Event for before a delete.
88    */

89   public static final int BEFORE_DELETE = BEFORE | DELETE;
90
91   /**
92    * Event for after a delete.
93    */

94   public static final int AFTER_DELETE = AFTER | DELETE;
95
96
97   // ----- Members -----
98

99   /**
100    * The DatabaseConnection of the table that the modification occurred in.
101    */

102   private DatabaseConnection connection;
103
104   /**
105    * The name of the table that was modified.
106    */

107   private TableName table_name;
108
109   /**
110    * The type of event that occurred.
111    */

112   private int event_type;
113
114   /**
115    * A RowData object representing the row that is being inserted by this
116    * modification. This is set for INSERT and UPDATE events. If the event
117    * type is BEFORE then this data represents the new data in the table and
118    * can be modified. This represents the NEW information.
119    */

120   private RowData row_data;
121
122   /**
123    * The row index of the table that is before removed by this modification.
124    * This is set for UPDATE and DELETE events. This represents the OLD
125    * information.
126    */

127   private int row_index = -1;
128
129   /**
130    * General Constructor.
131    */

132   private TableModificationEvent(DatabaseConnection connection,
133          TableName table_name, int row_index, RowData row_data,
134          int type, boolean before) {
135     this.connection = connection;
136     this.table_name = table_name;
137     this.row_index = row_index;
138     this.row_data = row_data;
139     this.event_type = type | (before ? BEFORE : AFTER);
140   }
141
142   /**
143    * Constructs an insert event.
144    */

145   TableModificationEvent(DatabaseConnection connection, TableName table_name,
146                          RowData row_data, boolean before) {
147     this(connection, table_name, -1, row_data, INSERT, before);
148   }
149
150   /**
151    * Constructs an update event.
152    */

153   TableModificationEvent(DatabaseConnection connection, TableName table_name,
154                          int row_index, RowData row_data, boolean before) {
155     this(connection, table_name, row_index, row_data, UPDATE, before);
156   }
157
158   /**
159    * Constructs a delete event.
160    */

161   TableModificationEvent(DatabaseConnection connection, TableName table_name,
162                          int row_index, boolean before) {
163     this(connection, table_name, row_index, null, DELETE, before);
164   }
165
166   /**
167    * Returns the DatabaseConnection that this event fired in.
168    */

169   public DatabaseConnection getDatabaseConnection() {
170     return connection;
171   }
172
173   /**
174    * Returns the event type.
175    */

176   public int getType() {
177     return event_type;
178   }
179
180   /**
181    * Returns true if this is a BEFORE event.
182    */

183   public boolean isBefore() {
184     return (event_type & BEFORE) != 0;
185   }
186   
187   /**
188    * Returns true if this is a AFTER event.
189    */

190   public boolean isAfter() {
191     return (event_type & AFTER) != 0;
192   }
193   
194   /**
195    * Returns true if this is an INSERT event.
196    */

197   public boolean isInsert() {
198     return (event_type & INSERT) != 0;
199   }
200
201   /**
202    * Returns true if this is an UPDATE event.
203    */

204   public boolean isUpdate() {
205     return (event_type & UPDATE) != 0;
206   }
207
208   /**
209    * Returns true if this is an DELETE event.
210    */

211   public boolean isDelete() {
212     return (event_type & DELETE) != 0;
213   }
214
215   /**
216    * Returns the name of the table of this modification.
217    */

218   public TableName getTableName() {
219     return table_name;
220   }
221
222   /**
223    * Returns the index of the row in the table that was affected by this
224    * event or -1 if event type is INSERT.
225    */

226   public int getRowIndex() {
227     return row_index;
228   }
229
230   /**
231    * Returns the RowData object that represents the change that is being
232    * made to the table either by an INSERT or UPDATE. For a DELETE event this
233    * return null.
234    */

235   public RowData getRowData() {
236     return row_data;
237   }
238
239   /**
240    * Returns true if the given listener type should be notified of this type
241    * of table modification event. For example, if this is a BEFORE event then
242    * the BEFORE bit on the given type must be set and if this is an INSERT event
243    * then the INSERT bit on the given type must be set.
244    */

245   public boolean listenedBy(int listen_t) {
246     // If this is a BEFORE trigger, then we must be listening for BEFORE events,
247
// etc.
248
boolean ba_match =
249        ( (event_type & BEFORE) != 0 && (listen_t & BEFORE) != 0 ) ||
250        ( (event_type & AFTER) != 0 && (listen_t & AFTER) != 0 );
251     // If this is an INSERT trigger, then we must be listening for INSERT
252
// events, etc.
253
boolean trig_match =
254        ( (event_type & INSERT) != 0 && (listen_t & INSERT) != 0 ) ||
255        ( (event_type & DELETE) != 0 && (listen_t & DELETE) != 0 ) ||
256        ( (event_type & UPDATE) != 0 && (listen_t & UPDATE) != 0 );
257     // If both of the above are true
258
return (ba_match && trig_match);
259   }
260   
261 }
262
263
Popular Tags