KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 public class ReferencedConstraintsVerifier {
20
21     private _ReferencedConstraintCharacteristics referencedConstraintCharacteristics;
22     private _DataDictionary dataDictionary;
23     private QualifiedIdentifier tableName;
24     private boolean deferrable ;
25     private boolean checkedReferencedForUpdate,checkedReferencedForDelete;
26     private HashMap rcPoolUpdate;
27     private HashMap rcPoolDelete ;
28     _ReferentialConstraint[] constraints = null;
29      _ReferentialConstraint[] constraints_del = null;
30     /**
31     * Constructor: used to construct a new instance of refernced constraints verifier with the given arguments.
32     * @param refConstraintCharacteristics instance of _ReferencedConstraintCharacteristics used to get
33     * the refernced constraints.
34     * @param dd instance of _datadictionary
35     * @param tName name of the table.
36     * @param deff boolean to determine whether the constraints are deferred or not.
37     * @throws DException
38     */

39     public ReferencedConstraintsVerifier( _ReferencedConstraintCharacteristics refConstraintCharacteristics, _DataDictionary dd , QualifiedIdentifier tName , boolean deff ) throws DException {
40     referencedConstraintCharacteristics = refConstraintCharacteristics;
41     dataDictionary = dd;
42     deferrable = deff;
43     tableName = tName;
44     rcPoolDelete = new HashMap();
45     rcPoolUpdate = new HashMap();
46     }
47
48     /**
49     * makes a array list "rcPoolUpdate" for all the Referenced Constraints. and passes a call
50     * for updating foreign constraints.
51     * @param columns columns for which refernced constraints are verified.
52     * @param globalSession instance of _serversession.
53     * @param statementExecutionContext instance of statementexecutionContext.
54     * @param cnstraintTable
55     * @throws DException
56     */

57
58
59
60     public void verifyReferencedConstraintsForUpdate( int[] columns , _ServerSession globalSession , _StatementExecutionContext statementExecutionContext ) throws DException {
61       if (rcPoolUpdate.size() == 0 && checkedReferencedForUpdate) {
62         return;
63       }
64       else if(rcPoolUpdate.size() == 0 && !checkedReferencedForUpdate) {
65         synchronized (this) {
66           if(!checkedReferencedForUpdate){
67         constraints = referencedConstraintCharacteristics.getReferencedConstraintsForDelete();
68       }
69           if (constraints == null) {
70             return;
71           }
72
73         if(!checkedReferencedForUpdate){
74             for (int j = 0; j < constraints.length; j++) {
75               String JavaDoc name = constraints[j].getConstraintName();
76              ReferencedConstraints referencedConstraint = new
77             ReferencedConstraints(constraints[j], dataDictionary,
78                                         globalSession, false);
79               referencedConstraint.setBitSet(constraints[j].getReferencedBitSet());
80               rcPoolUpdate.put(name, referencedConstraint);
81               checkedReferencedForUpdate = true;
82             }
83           }
84         }
85       }
86
87     ConstraintInformation constraintInformation = getConstraintInformation(statementExecutionContext.getUserSession()) ;
88     if(!deferrable){
89       ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName );
90
91       if( statementExecutionContext.getOperationTypeSameOrOpposite() ){
92         if( immToDeff == null )
93           fireAllConstraints(statementExecutionContext,columns );
94         else {
95           ArrayList constraintsToFire = getRequiredConstraintsToFireForUpdate( immToDeff );
96           for( int i=0; i<constraintsToFire.size(); i++ )
97             fireThisConstraint( (ReferencedConstraints)constraintsToFire.get(i) , statementExecutionContext , columns );
98         }
99       }
100       else {
101         if( immToDeff != null ){
102           String JavaDoc Cname = null;
103           for( int j=0; j<immToDeff.size(); j++ ){
104             Cname = (String JavaDoc) immToDeff.get(j);
105             if( rcPoolUpdate.containsKey(Cname) )
106               fireThisConstraint( (ReferencedConstraints)rcPoolUpdate.get(Cname) , statementExecutionContext ,columns );
107           }
108         }
109       }
110     }
111     else { /// Deferred table constraints being fired.
112
ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName );
113
114       if( statementExecutionContext.getOperationTypeSameOrOpposite() ){
115         if( deffToImm == null )
116           fireAllConstraints(statementExecutionContext ,columns );
117         else {
118           ArrayList constraintsToFire = getRequiredConstraintsToFireForUpdate( deffToImm );
119           for( int i=0; i<constraintsToFire.size(); i++ )
120             fireThisConstraint((ReferencedConstraints)constraintsToFire.get(i),statementExecutionContext ,columns );
121         }
122       }
123       else//checking Deferred immediate constraints....
124
{
125         if( deffToImm != null ){
126           String JavaDoc Cname = null;
127           for( int j=0; j<deffToImm.size(); j++ ){
128             Cname = (String JavaDoc) deffToImm.get(j);
129             if( rcPoolUpdate.containsKey(Cname) )
130               fireThisConstraint( (ReferencedConstraints)rcPoolUpdate.get(Cname) , statementExecutionContext , columns );
131           }
132         }
133       }
134     }
135     }
136
137     /**
138     * makes a array list rcPoolDelete for all the Referenced Constraints. and passes a call for updating
139     * forigen constraints.
140     * @param globalSession instance of _serversession
141     * @param statementExecutionContext instance of statementExecutionContext
142     * @throws DException
143     */

144
145     public void verifyReferencedConstraintsForDelete( _ServerSession globalSession , _StatementExecutionContext statementExecutionContext ) throws DException {
146       if (rcPoolDelete.size() == 0 && checkedReferencedForDelete) {
147         return;
148       }
149       else if(rcPoolDelete.size() == 0 && !checkedReferencedForDelete) {
150
151         synchronized (this) {
152           if(!checkedReferencedForDelete){
153         constraints = referencedConstraintCharacteristics.getReferencedConstraintsForDelete();
154       }
155           if (constraints == null) {
156             return;
157           }
158           if(!checkedReferencedForDelete){
159             for (int i = 0; i < constraints.length; i++) {
160               String JavaDoc name = constraints[i].getConstraintName();
161               ReferencedConstraints referencedConstraint = new
162                   ReferencedConstraints(constraints[i], dataDictionary,
163                                         globalSession, true);
164
165               rcPoolDelete.put(name, referencedConstraint);
166               checkedReferencedForDelete = true;
167             }
168           }
169         }
170       }
171
172     ConstraintInformation constraintInformation = getConstraintInformation(statementExecutionContext.getUserSession()) ;
173     if(!deferrable){
174       ArrayList immToDeff = constraintInformation.getImmediateToDefferedConstraints( tableName );
175       if( statementExecutionContext.getOperationTypeSameOrOpposite() ){
176         if( immToDeff == null )
177           fireAllConstraints(statementExecutionContext);
178         else {
179           ArrayList constraintsToFire = getRequiredConstraintsToFireForDelete( immToDeff );
180           for( int i=0; i<constraintsToFire.size(); i++ )
181             fireThisConstraint( (ReferencedConstraints)constraintsToFire.get(i) , statementExecutionContext );
182         }
183       }
184       else {
185         if( immToDeff != null ){
186           String JavaDoc Cname = null;
187           for( int j=0; j<immToDeff.size(); j++ ){
188             Cname = (String JavaDoc) immToDeff.get(j);
189             if( rcPoolDelete.containsKey(Cname) )
190               fireThisConstraint( (ReferencedConstraints)rcPoolDelete.get(Cname) , statementExecutionContext );
191           }
192         }
193       }
194       return;
195     }
196     else {
197       ArrayList deffToImm = constraintInformation.getDefferedToImmediateConstraints( tableName );
198       if( statementExecutionContext.getOperationTypeSameOrOpposite() ){
199         if( deffToImm == null )
200           fireAllConstraints(statementExecutionContext);
201         else {
202           ArrayList constraintsToFire = getRequiredConstraintsToFireForDelete( deffToImm );
203           for( int i=0; i<constraintsToFire.size(); i++ )
204             fireThisConstraint((ReferencedConstraints)constraintsToFire.get(i),statementExecutionContext );
205         }
206       }
207       else//checking Deferred immediate constraints....
208
{
209         if( deffToImm != null ){
210           String JavaDoc Cname = null;
211           for( int j=0; j<deffToImm.size(); j++ ){
212             Cname = (String JavaDoc) deffToImm.get(j);
213             if( rcPoolDelete.containsKey(Cname) )
214               fireThisConstraint( (ReferencedConstraints)rcPoolDelete.get(Cname) , statementExecutionContext );
215           }
216         }
217       }
218     }
219     }
220
221
222     private ArrayList getDefferedDefferedConstraints( ArrayList sessionImmediateConstraints ) {
223       ArrayList toRet = new ArrayList();
224       Iterator iterator = rcPoolUpdate.keySet().iterator() ;
225       String JavaDoc Cname = null ;
226       do{
227         Cname = (String JavaDoc)iterator.next();
228         if( !sessionImmediateConstraints.contains(Cname) )
229           toRet.add(rcPoolUpdate.get(Cname));
230       }while( iterator.hasNext() );
231       return toRet;
232     }
233
234     /**
235      * This method fires all the constraints from the HashMap rcPoolUpdate.
236      * This method calls the private method to fire each constraint from the Map.
237      * @param sec instance of _StatementExecutionContext
238      * @param columns column indexexs of the table on which constraint is fired
239      * @throws DException
240      */

241
242     private void fireAllConstraints(_StatementExecutionContext sec,int[] columns) throws DException {
243         Iterator iterator = rcPoolUpdate.keySet().iterator();
244         ReferencedConstraints tempCStore = null;
245         do {
246           tempCStore = (ReferencedConstraints) rcPoolUpdate.get(iterator.next());
247           fireThisConstraint(tempCStore, sec, columns);
248         }
249         while (iterator.hasNext());
250     }
251
252     /**
253      * This method checks the column indexes and fires the referenced constraint which
254      * calls the checkReferencedConstraints method of the appropriate executer depending
255      * on the match option and delete/update rule action specified.
256      * @param referencedConstraints
257      * @param sec
258      * @param columns
259      * @throws DException
260      */

261
262     private void fireThisConstraint(ReferencedConstraints referencedConstraints , _StatementExecutionContext sec,int[] columns) throws DException {
263       if( referencedConstraints.fireDescriptor( columns ) )
264         referencedConstraints.checkReferencedConstraints(sec);
265     }
266
267
268     /**
269      * This method fires all the constraints from the HashMap rcPoolDelete.
270      * This method calls the private method to fire each constraint from the Map.
271      * @param sec instance of _StatementExecutionContext
272      * @param columns column indexexs of the table on which constraint is fired
273      * @throws DException
274      */

275
276     private void fireAllConstraints(_StatementExecutionContext sec) throws DException {
277         Iterator iterator = rcPoolDelete.keySet().iterator() ;
278         ReferencedConstraints tempCStore = null;
279         do{
280           String JavaDoc nn = (String JavaDoc) iterator.next();
281           tempCStore = (ReferencedConstraints)rcPoolDelete.get(nn);
282           fireThisConstraint(tempCStore,sec);
283         }while( iterator.hasNext() );
284     }
285
286     /**
287      * This method checks the column indexes and fires the referenced constraint which
288      * calls the checkReferencedConstraints method of the appropriate executer depending
289      * on the match option and delete/update rule action specified.
290      * @param referencedConstraints instance of referencedConstraints
291      * @param sec instance of _StatementExecutionContext
292      * @throws DException
293      */

294
295     private void fireThisConstraint(ReferencedConstraints referencedConstraints , _StatementExecutionContext sec ) throws DException {
296       referencedConstraints.checkReferencedConstraints(sec);
297     }
298
299     /**
300      * This method retruns a list of the all the immediate constraints that are
301      * present in passed user session.
302      * @param userSession instance of _UserSession
303      * @return A list of immediate constraints in passed user session
304      * @throws DException
305      */

306
307
308
309     private ConstraintInformation getConstraintInformation( _UserSession userSession ) throws DException {
310       return ((Session)userSession.getSession()).getConstraintInformation();
311     }
312
313     /**
314      * This method returns all the constraints from the hash map rcPoolDelete
315      * which are not present in passed list of constraints.
316      * @param sessionImmediateConstraints list of constraint names.
317      * @return list of required constraints to be fired
318      */

319     private ArrayList getRequiredConstraintsToFireForDelete( ArrayList sessionImmediateConstraints ) {
320       ArrayList toRet = new ArrayList();
321       Iterator iterator = rcPoolDelete.keySet().iterator() ;
322       String JavaDoc Cname = null ;
323       do{
324         Cname = (String JavaDoc)iterator.next();
325         if( !sessionImmediateConstraints.contains(Cname) )
326           toRet.add(rcPoolDelete.get(Cname));
327       }while( iterator.hasNext() );
328       return toRet;
329     }
330
331     /**
332      * This method returns all the constraints from the hash map rcPoolUpdate
333      * which are not present in passed list of constraints.
334      * @param sessionImmediateConstraints list of constraint names.
335      * @return list of required constraints to be fired
336      */

337     private ArrayList getRequiredConstraintsToFireForUpdate( ArrayList sessionImmediateConstraints ) {
338       ArrayList toRet = new ArrayList();
339       Iterator iterator = rcPoolUpdate.keySet().iterator() ;
340       String JavaDoc Cname = null ;
341       do{
342         Cname = (String JavaDoc)iterator.next();
343         if( !sessionImmediateConstraints.contains(Cname) )
344           toRet.add(rcPoolUpdate.get(Cname));
345       }while( iterator.hasNext() );
346       return toRet;
347     }
348 }
349
Popular Tags