Source code: javatools/db/DbDynamicConstraint.java
1 /*
2 * DbDynamicConstraint.java
3 *
4 * Created on 19 febbraio 2002, 11.57
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.db;
26
27 import java.util.*;
28 import javatools.util.ArrayComparator;
29
30 /**
31 * It is a class representing a "dynamic" constraint, i.e. a constraint that
32 * changes references dynamically.
33 * @author Antonio Petrelli
34 * @version 0.2.0
35 */
36 public class DbDynamicConstraint extends javatools.db.DbConstraint {
37
38
39 /** Creates new DbDynamicConstraint
40 * @param tbl The table to be referenced.
41 */
42 public DbDynamicConstraint(DbAbstractTable tbl) {
43 super(tbl);
44 createReferences();
45 automaticBuild = true;
46 }
47
48 /** Performs checking in the table, after setting all dynamic and "static" data.
49 * @param operation The kind of operation (INSERT, UPDATE etc.) that should be done.
50 * @throws DbException If something goes wrong.
51 */
52 public void check(int operation) throws DbException {
53 if (automaticBuild)
54 build();
55 checkThis(operation);
56 }
57
58 /** Performs actual update in the table and, eventually, cascades. If automatic
59 * checking is set, it also checks before updating.
60 * @param operation The kind of operation (INSERT, UPDATE etc.) that should be done.
61 * @throws DbException If something goes wrong.
62 * @return The result from inserters, updaters or deleters.
63 */
64 public int update(int operation) throws DbException {
65 if (automaticBuild)
66 build();
67 return updateThis(operation);
68 }
69
70 /** Adds a new father table.
71 * @param fatherTable The father table to be referenced.
72 * @param fatherColumns The father columns to use as father keys.
73 * @param pRefColumns The columns to be used as son keys.
74 * @param updateOperation What kind of operation should I do in case of an update?
75 * @param deleteOperation What kind of operation should I do in case of a deletion?
76 */
77 public void addFatherTable(DbAbstractTable fatherTable, DbColumn[] fatherColumns, DbColumn[] pRefColumns,
78 int updateOperation, int deleteOperation) {
79 DbConstraint tempConstraint;
80 if (fatherTable != null && fatherColumns != null && pRefColumns != null) {
81 if (updateOperation < 0 || updateOperation > 3)
82 updateOperation = DO_NOTHING;
83 if (deleteOperation < 0 || deleteOperation > 3)
84 deleteOperation = DO_NOTHING;
85 fatherTables.add(fatherTable);
86 fatherTablesColumns.add(fatherColumns);
87 refColumns.add(pRefColumns);
88 updateOperations.add(new Integer(updateOperation));
89 deleteOperations.add(new Integer(deleteOperation));
90 tempConstraint = fatherTable.getConstraint();
91 if (tempConstraint != null && tempConstraint instanceof DbDynamicConstraint)
92 ((DbDynamicConstraint) tempConstraint).addSonTable(table, fatherTables.size() - 1);
93 }
94 }
95
96 /** Adds a son table.
97 * @param sonTable The table to be added as a son table.
98 * @param fatherIndex The index that the new son table uses to reference THIS table.
99 */
100 public void addSonTable(DbAbstractTable sonTable, int fatherIndex) {
101 if (sonTable != null) {
102 sonTables.add(sonTable);
103 sonIndexes.add(new Integer(fatherIndex));
104 }
105 }
106
107 /** Returns the number of referenced father tables.
108 * @return The father tables count.
109 */
110 public int getFatherTablesCount() {
111 return fatherTables.size();
112 }
113
114 /** Returns the number of son tables.
115 * @return The son tables count.
116 */
117 public int getSonTablesCount() {
118 return sonTables.size();
119 }
120
121 /** Returns a father table whose index is specified.
122 * @param index The index of the requested father table.
123 * @throws DbException If something goes wrong.
124 * @return The requested table.
125 */
126 public DbAbstractTable getFatherTable(int index) throws DbException {
127 if (index >= 0 && index < fatherTables.size()) {
128 return (DbAbstractTable) fatherTables.get(index);
129 }
130 else
131 throw new DbException("Index out of bounds in getting a father table");
132 }
133
134 /** Returns a son table whose index is specified.
135 * @param index The index of the requested son table.
136 * @throws DbException If something goes wrong.
137 * @return The requested son table.
138 */
139 public DbAbstractTable getSonTable(int index) throws DbException {
140 if (index >= 0 && index < sonTables.size()) {
141 return (DbAbstractTable) sonTables.get(index);
142 }
143 else
144 throw new DbException("Index out of bounds in getting a son table");
145 }
146
147 /** Returns the index that a son table uses to reference this table.
148 * @param index The index of the requested son table.
149 * @throws DbException If something goes wrong.
150 * @return The requested index.
151 */
152 public int getSonFatherIndex(int index) throws DbException {
153 if (index >= 0 && index < sonIndexes.size()) {
154 return ((Integer) sonIndexes.get(index)).intValue();
155 }
156 else
157 throw new DbException("Index out of bounds in getting a son table");
158 }
159
160 /** Returns the update operation that should be done for the father table whose
161 * index is specified.
162 * @param index The index of the requested father table.
163 * @throws DbException If something goes wrong.
164 * @return The requested operation.
165 */
166 public int getUpdateOperation(int index) throws DbException {
167 if (index >= 0 && index < updateOperations.size()) {
168 return ((Integer) updateOperations.get(index)).intValue();
169 }
170 else
171 throw new DbException("Index out of bounds in getting an update operation");
172 }
173
174 /** Returns the delete operation that should be done for the father table whose
175 * index is specified.
176 * @param index The index of the requested father table.
177 * @throws DbException If something goes wrong.
178 * @return The requested operation.
179 */
180 public int getDeleteOperation(int index) throws DbException {
181 if (index >= 0 && index < deleteOperations.size()) {
182 return ((Integer) deleteOperations.get(index)).intValue();
183 }
184 else
185 throw new DbException("Index out of bounds in getting a delete operation");
186 }
187
188 /** The referenced columns of father table whose index is specified.
189 * @param index The index of requested father table.
190 * @throws DbException If something goes wrong.
191 * @return An array containing all the referenced columns.
192 */
193 public DbColumn[] getFatherColumns(int index) throws DbException {
194 if (index >= 0 && index < fatherTablesColumns.size()) {
195 return (DbColumn[]) fatherTablesColumns.get(index);
196 }
197 else
198 throw new DbException("Index out of bounds in getting a father-columns-array");
199 }
200
201 /** Returns the referencing columns of THIS table to the father table whose index
202 * is specified.
203 * @param index The index of requested father table.
204 * @throws DbException If something goes wrong.
205 * @return An array containing the columns of THIS table referencing the requested father
206 * table.
207 */
208 public DbColumn[] getRefColumns(int index) throws DbException {
209 if (index >= 0 && index < refColumns.size()) {
210 return (DbColumn[]) refColumns.get(index);
211 }
212 else
213 throw new DbException("Index out of bounds in getting a referenced-columns-array");
214 }
215
216 /** Tells this object to build (or not) automatically all lists at all times.
217 * @param value <CODE>true</CODE>: automatic build will be done;
218 * <CODE>false</CODE>: it won't be done.
219 */
220 public void setAutomaticBuild(boolean value) {
221 automaticBuild = value;
222 }
223
224 /** Builds all static lists.
225 * @throws DbException If something goes wrong.
226 */
227 public void build() throws DbException {
228 buildArrays();
229 initStaticLists();
230 }
231
232 private LinkedList fatherTables, sonTables;
233 private LinkedList updateOperations, deleteOperations;
234 private LinkedList fatherTablesColumns, refColumns;
235 private LinkedList sonIndexes;
236 private boolean automaticBuild;
237
238 /** Performs base checking.
239 * @param operation The operation that should be done.
240 * @throws DbException If something goes wrong.
241 */
242 protected void checkThis(int operation) throws DbException {
243 if (automaticBuild)
244 build();
245 super.checkThis(operation);
246 }
247
248 /** Checks the fathers.
249 * @throws DbException If something goes wrong.
250 */
251 protected void checkFathers () throws DbException {
252 if (super.fatherTables == null)
253 buildArrays();
254 initStaticLists();
255 super.checkFathers();
256 }
257
258 private void createReferences() {
259 fatherTables = new LinkedList();
260 sonTables = new LinkedList();
261 updateOperations = new LinkedList();
262 deleteOperations = new LinkedList();
263 fatherTablesColumns = new LinkedList();
264 refColumns = new LinkedList();
265 sonIndexes = new LinkedList();
266 }
267
268 /** Returns the default values of referenced father table whose index is specified.
269 * @param index The index of requested father table.
270 * @throws DbException If something goes wrong.
271 * @return An array containing all default values for the requested father table.
272 */
273 public Object[] getDefaultValues(int index) throws DbException {
274 if (defaultValues == null)
275 build();
276 return super.getDefaultValues(index);
277 }
278
279 private void buildArrays() {
280
281 super.fatherTables = new DbAbstractTable[fatherTables.size()];
282 fatherTables.toArray(super.fatherTables);
283 super.sonTables = new DbAbstractTable[sonTables.size()];
284 sonTables.toArray(super.sonTables);
285 super.updateOperations = new Integer[updateOperations.size()];
286 updateOperations.toArray(super.updateOperations);
287 super.deleteOperations = new Integer[deleteOperations.size()];
288 deleteOperations.toArray(super.deleteOperations);
289 super.fatherTablesColumns = new DbColumn[fatherTablesColumns.size()][];
290 fatherTablesColumns.toArray(super.fatherTablesColumns);
291 super.refColumns = new DbColumn[refColumns.size()][];
292 refColumns.toArray(super.refColumns);
293 super.sonIndexes = new Integer[sonIndexes.size()];
294 sonIndexes.toArray(super.sonIndexes);
295 }
296 }