KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > serversystem > dmlvalidation > constraintsystem > ReferencingConstraintsVerifier


1 package com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.
2     constraintsystem;
3
4 import java.util.*;
5
6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
8 import com.daffodilwoods.daffodildb.server.serversystem.*;
9 import com.daffodilwoods.daffodildb.server.sessionsystem.*;
10 import com.daffodilwoods.database.general.*;
11 import com.daffodilwoods.database.resource.*;
12
13 /**
14  *
15  * <p>Title: Referencing Constraint Verifier</p>
16  * <p>Description: </p>
17  * Objective of the Referencing constraints verifier is to verify all the referenced constraints
18  * that are applied on the table and performs the task accordingly.
19  */

20 public class ReferencingConstraintsVerifier {
21
22   _ReferencingConstraintCharacteristics referencingConstraintCharacteristics;
23   QualifiedIdentifier tableName;
24   boolean deferrable;
25   HashMap constraintPool;
26   boolean checkedReferencing = false;
27
28   /**
29    * constructs an instance of referncing constraints verifier with the given arguments.
30    * @param referencingCharacteristics instance of _referencingconstraintscharacteristics used to
31    * get the referencing constraints.
32    * @param tName name of the table.
33    * @param deff boolean to determine whether the constraints are deferred or not.
34    * @throws DException
35    */

36   public ReferencingConstraintsVerifier(_ReferencingConstraintCharacteristics
37                                         referencingCharacteristics,
38                                         QualifiedIdentifier tName, boolean deff) throws
39       DException {
40     referencingConstraintCharacteristics = referencingCharacteristics;
41     tableName = tName;
42     deferrable = deff;
43     constraintPool = new HashMap();
44   }
45
46   /**
47    * makes a pool "constraintPool" of descriptors from referencing constraint characteristics and the constraints which are changed
48    * (e.g from deffered to immidiate or vica-versa)
49    * @param columns array of columns for which constraints are to be verified.
50    * @param globalSession instance of _serversession used to get the immediate constraints.
51    * @param statementExecutionContext instance of _statement execution context to get the usersession.
52    * @throws DException
53    */

54
55   public void verifyReferencingConstraintsForUpdate(int[] columns,
56       _ServerSession globalSession,
57       _StatementExecutionContext statementExecutionContext) throws DException {
58     _ReferentialConstraint[] referencingDescriptor = null;
59
60     Iterator iterator = constraintPool.keySet().iterator();
61     if (constraintPool.size() == 0 && checkedReferencing) {
62       return;
63     }
64     else if(constraintPool.size() == 0 && !checkedReferencing){
65       synchronized (this) {
66         if (!checkedReferencing) {
67           referencingDescriptor = referencingConstraintCharacteristics.
68               getReferencingConstraints();
69         }
70         if (referencingDescriptor == null) {
71           return;
72         }
73          if (!checkedReferencing){
74            for (int i = 0; i < referencingDescriptor.length; i++) {
75              String JavaDoc name = referencingDescriptor[i].getQualifiedIdentifier().
76                  getIdentifier();
77              ReferencingConstraints referencingConstraints = new
78                  ReferencingConstraints(referencingDescriptor[i], globalSession);
79              referencingConstraints.setBitSet(referencingDescriptor[i].
80                                               getReferencingBitSet());
81              constraintPool.put(name, referencingConstraints);
82              checkedReferencing = true;
83            }
84          }
85       }
86     }
87
88
89     ConstraintInformation constraintInformation = getConstraintInformation(
90         statementExecutionContext.getUserSession());
91     if (!deferrable) {
92       ArrayList immToDeff = constraintInformation.
93           getImmediateToDefferedConstraints(tableName);
94       if (statementExecutionContext.getOperationTypeSameOrOpposite()) {
95         if (immToDeff == null) {
96           fireAllConstraints(statementExecutionContext.getRecordVersion(),
97                              columns, globalSession);
98         }
99         else {
100           ArrayList constraintsToFire = getRequiredConstraintsToFire(immToDeff);
101           for (int i = 0; i < constraintsToFire.size(); i++) {
102             fireThisConstraint( (ReferencingConstraints) constraintsToFire.get(
103                 i), statementExecutionContext.getRecordVersion(), columns,
104                                globalSession);
105           }
106         }
107       }
108       else {
109         if (immToDeff != null) {
110           String JavaDoc Cname = null;
111           for (int j = 0; j < immToDeff.size(); j++) {
112             Cname = (String JavaDoc) immToDeff.get(j);
113             if (constraintPool.containsKey(Cname)) {
114               fireThisConstraint( (ReferencingConstraints) constraintPool.get(
115                   Cname), statementExecutionContext.getRecordVersion(), columns,
116                                  globalSession);
117             }
118           }
119         }
120       }
121     }
122     else { /// Deferred table constraints being fired.
123
ArrayList deffToImm = constraintInformation.
124           getDefferedToImmediateConstraints(tableName);
125
126       if (statementExecutionContext.getOperationTypeSameOrOpposite()) {
127         if (deffToImm == null) {
128           fireAllConstraints(statementExecutionContext.getRecordVersion(),
129                              columns, globalSession);
130         }
131         else {
132           ArrayList constraintsToFire = getRequiredConstraintsToFire(deffToImm);
133           for (int i = 0; i < constraintsToFire.size(); i++) {
134             fireThisConstraint( (ReferencingConstraints) constraintsToFire.get(
135                 i), statementExecutionContext.getRecordVersion(), columns,
136                                globalSession);
137           }
138         }
139       }
140       else { //checking Deferred immediate constraints....
141
if (deffToImm != null) {
142           String JavaDoc Cname = null;
143           for (int j = 0; j < deffToImm.size(); j++) {
144             Cname = (String JavaDoc) deffToImm.get(j);
145             if (constraintPool.containsKey(Cname)) {
146               fireThisConstraint( (ReferencingConstraints) constraintPool.get(
147                   Cname), statementExecutionContext.getRecordVersion(), columns,
148                                  globalSession);
149             }
150           }
151         }
152       }
153     }
154   }
155
156   /**
157    * returns a array of constraints which are changed from one one type to another say from immidiate to deffered or vice-versa.
158    * @param referentialConstraints
159    * @param userSession
160    * @return _ReferentialConstraint[]
161    * @throws DException
162    */

163
164   public void verifyReferencingConstraints(int[] columns,
165                                            _ServerSession globalSession,
166                                            _StatementExecutionContext
167                                            statementExecutionContext) throws
168       DException {
169
170     if (constraintPool.size() == 0 && checkedReferencing) {
171       return;
172     }
173     else if(constraintPool.size() == 0 && !checkedReferencing) {
174       _ReferentialConstraint[] referencingDescriptor = null;
175       synchronized (this) {
176         if (!checkedReferencing) {
177           referencingDescriptor = referencingConstraintCharacteristics.
178               getReferencingConstraints();
179
180         }
181         if (referencingDescriptor == null) {
182           return;
183         }
184        if (!checkedReferencing){
185          for (int i = 0; i < referencingDescriptor.length; i++) {
186            String JavaDoc name = referencingDescriptor[i].getQualifiedIdentifier().
187                getIdentifier();
188            ReferencingConstraints referencingConstraints = new
189                ReferencingConstraints(referencingDescriptor[i], globalSession);
190            referencingConstraints.setBitSet(referencingDescriptor[i].
191                                             getReferencingBitSet());
192            constraintPool.put(name, referencingConstraints);
193            checkedReferencing = true;
194          }
195        }
196       }
197     }
198
199
200     ConstraintInformation constraintInformation = getConstraintInformation(
201         statementExecutionContext.getUserSession());
202     if (!deferrable) { /// Immediate Constraints being fired
203
ArrayList immToDeff = constraintInformation.
204           getImmediateToDefferedConstraints(tableName);
205       if (statementExecutionContext.getOperationTypeSameOrOpposite()) {
206         if (immToDeff == null) {
207           fireAllConstraints(statementExecutionContext.getRecordVersion(),
208                              globalSession);
209         }
210         else {
211           ArrayList constraintsToFire = getRequiredConstraintsToFire(immToDeff);
212           for (int i = 0; i < constraintsToFire.size(); i++) {
213             fireThisConstraint( (ReferencingConstraints) constraintsToFire.get(
214                 i), statementExecutionContext.getRecordVersion(), globalSession);
215           }
216         }
217       }
218       else {
219         if (immToDeff != null) {
220           String JavaDoc Cname = null;
221           for (int j = 0; j < immToDeff.size(); j++) {
222             Cname = (String JavaDoc) immToDeff.get(j);
223             if (constraintPool.containsKey(Cname)) {
224               fireThisConstraint( (ReferencingConstraints) constraintPool.get(
225                   Cname), statementExecutionContext.getRecordVersion(),
226                                  globalSession);
227             }
228           }
229         }
230       }
231       return;
232     }
233     else { /// Deferred table constraints being fired.
234
ArrayList deffToImm = constraintInformation.
235           getDefferedToImmediateConstraints(tableName);
236
237       if (statementExecutionContext.getOperationTypeSameOrOpposite()) {
238         if (deffToImm == null) {
239           fireAllConstraints(statementExecutionContext.getRecordVersion(),
240                              globalSession);
241         }
242         else {
243           ArrayList constraintsToFire = getRequiredConstraintsToFire(deffToImm);
244           for (int i = 0; i < constraintsToFire.size(); i++) {
245             fireThisConstraint( (ReferencingConstraints) constraintsToFire.get(
246                 i), statementExecutionContext.getRecordVersion(), globalSession);
247           }
248         }
249       }
250       else { //checking Deferred immediate constraints....
251
if (deffToImm != null) {
252           String JavaDoc Cname = null;
253           for (int j = 0; j < deffToImm.size(); j++) {
254             Cname = (String JavaDoc) deffToImm.get(j);
255             if (constraintPool.containsKey(Cname)) {
256               fireThisConstraint( (ReferencingConstraints) constraintPool.get(
257                   Cname), statementExecutionContext.getRecordVersion(),
258                                  globalSession);
259             }
260           }
261         }
262       }
263     }
264   }
265
266   /**
267    * This method fires appropriate constraints.
268    * @param recordVersion
269    * @param globalSession
270    * @throws DException
271    */

272   private void fireAllConstraints(RecordVersion recordVersion,
273                                   _ServerSession globalSession) throws
274       DException {
275     Iterator iterator = constraintPool.keySet().iterator();
276     ReferencingConstraints tempCStore = null;
277     do {
278       tempCStore = (ReferencingConstraints) constraintPool.get(iterator.next());
279       fireThisConstraint(tempCStore, recordVersion, globalSession);
280     }
281     while (iterator.hasNext());
282
283   }
284
285   /**
286    * This method fires appropriate constraints.
287    * @param referencingConstraints
288    * @param recordVersion
289    * @param globalSession
290    * @throws DException
291    */

292   private void fireThisConstraint(ReferencingConstraints referencingConstraints,
293                                   RecordVersion recordVersion,
294                                   _ServerSession globalSession) throws
295       DException {
296     referencingConstraints.checkReferencingConstraints(recordVersion,
297         globalSession);
298   }
299
300   /**
301    * This method fires appropriate constraints.
302    * @param recordVersion
303    * @param columns
304    * @param globalSession
305    * @throws DException
306    */

307   private void fireAllConstraints(RecordVersion recordVersion, int[] columns,
308                                   _ServerSession globalSession) throws
309       DException {
310     Iterator iterator = constraintPool.keySet().iterator();
311     ReferencingConstraints tempCStore = null;
312     do {
313       tempCStore = (ReferencingConstraints) constraintPool.get(iterator.next());
314       fireThisConstraint(tempCStore, recordVersion, columns, globalSession);
315     }
316     while (iterator.hasNext());
317   }
318
319   /**
320    * This method fires appropriate constraint.
321    * @param referencingConstraints
322    * @param recordVersion
323    * @param columns
324    * @param globalSession
325    * @throws DException
326    */

327   public void fireThisConstraint(ReferencingConstraints referencingConstraints,
328                                  RecordVersion recordVersion, int[] columns,
329                                  _ServerSession globalSession) throws
330       DException {
331     if (referencingConstraints.fireDescriptor(columns)) {
332       referencingConstraints.checkReferencingConstraints(recordVersion,
333           globalSession);
334     }
335   }
336
337   public String JavaDoc toString() {
338     String JavaDoc tab = null;
339     try {
340       tab = tableName.getName();
341     }
342     catch (Exception JavaDoc e) {
343       e.printStackTrace();
344     }
345     return " -------------------- Referencing Constraint Verifeir :::::::: " +
346         tab;
347   }
348
349   /**
350    * This method returns a instance of ConstraintInformation for the passed
351    * user session.
352    * @param userSession
353    * @return
354    * @throws DException
355    */

356   private ConstraintInformation getConstraintInformation(_UserSession
357       userSession) throws DException {
358     return ( (Session) userSession.getSession()).getConstraintInformation();
359   }
360
361   /**
362    * This method retruns list of deferred constraints.
363    * @param sessionImmediateConstraints
364    * @return
365    */

366   private ArrayList getRequiredConstraintsToFire(ArrayList
367                                                  sessionImmediateConstraints) {
368     ArrayList toRet = new ArrayList();
369     Iterator iterator = constraintPool.keySet().iterator();
370     String JavaDoc Cname = null;
371     do {
372       Cname = (String JavaDoc) iterator.next();
373       if (!sessionImmediateConstraints.contains(Cname)) {
374         toRet.add(constraintPool.get(Cname));
375       }
376     }
377     while (iterator.hasNext());
378     return toRet;
379   }
380
381 }
382
Popular Tags