KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sessionsystem > sessioncondition > ConditionProvider


1 package com.daffodilwoods.daffodildb.server.sessionsystem.sessioncondition;
2
3 import com.daffodilwoods.database.resource.DException;
4 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
5 import com.daffodilwoods.daffodildb.server.sessionsystem.Session;
6 import java.util.ArrayList JavaDoc;
7 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
8
9 /**
10  *
11  * <p>Title: Condition Provider </p>
12  * <p>Description: A base class for session , it developes and provides all the session conditions
13  * for all the isolation level </p>
14  * <p>Copyright: Copyright (c) 2002</p>
15  * <p>Company: Daffodil Software Ltd. </p>
16  * @version 2.0
17  */

18 public class ConditionProvider {
19
20
21
22
23    public static final int ImmidiateConstraintChecking = 0;
24    public static final int deferredConstraintChecking = 1;
25    public static final int IgnoreConstraintChecking = 2;
26
27    protected _SessionCondition recordValidityCondition , transactionSessionCondition, readCommitted, readRepeatable,
28     transactionSerializableCondition , unlockedCommitted,
29     criteriaForRollBackNCommit, InsertDeletedCondition, insertedCondition,
30     invalidCommittedCondition, invalidTransactionCondition, invalidRecordCondition, multipleTransactionCondition,
31     persistentCondition, transactionCondition, readCommittedForLock, readCommittedInternal,
32     unlockSessionTableMemoryCondition, invalidTransactionConditionWithChild, readCommittedForLockModified, makePersistentForInserted;
33
34     protected Object JavaDoc sessionId;
35     protected Object JavaDoc transactionId;
36     protected ArrayList JavaDoc sessionIdList = new ArrayList JavaDoc();
37     ArrayList JavaDoc hiddenSessionidList = new ArrayList JavaDoc();
38     ArrayList JavaDoc parallelSessionIdList = new ArrayList JavaDoc();
39     protected ArrayList JavaDoc constraintCheckingStatusList;
40     ArrayList JavaDoc sessionsWithModifiedData = new ArrayList JavaDoc();
41
42     public ConditionProvider(Object JavaDoc sessionId0, Object JavaDoc transactionId0) throws DException {
43         this.sessionId = sessionId0;
44         this.transactionId = transactionId0;
45         sessionIdList.add(sessionId);
46     }
47
48     /**
49      * returns supermost sessionId
50      * @return
51      */

52     public Object JavaDoc getSuperMostSessionId() {
53         return sessionIdList.get(0);
54     }
55
56
57     public _SessionCondition getRecordValidityCondition () throws DException{ // Read Uncommitted
58
return ( recordValidityCondition == null ) ? recordValidityCondition = new ReadUncommittedCondition()
59                 : recordValidityCondition;
60     }
61
62     public _SessionCondition getTransactionSessionCondition() throws DException{
63         return ( transactionSessionCondition == null ) ? transactionSessionCondition = new TransactionSessionCondition (new Object JavaDoc[]{transactionId, sessionIdList.toArray(new FieldBase[0]) })
64                 : transactionSessionCondition ;
65     }
66
67
68     public _SessionCondition getReadCommitted()throws DException{
69         return ( readCommitted == null ) ? readCommitted = new ReadCommitedCondition(new Object JavaDoc[]{ sessionIdList.toArray(new FieldBase[0])})
70                                 : readCommitted ;
71     }
72
73
74     public _SessionCondition getReadRepeatable( ) throws DException{
75         return ( readRepeatable == null ) ? readRepeatable = new ReadRepeatableCondition(transactionId)
76                                  : readRepeatable;
77     }
78
79     public _SessionCondition getTransactionSerializableCondition ( ) throws DException{
80         return (transactionSerializableCondition == null ) ? transactionSerializableCondition =
81                  new TransactionSerializableCondition(sessionId)
82                 : transactionSerializableCondition ;
83     }
84
85
86     public _SessionCondition getUnlockedCommitted ( ) throws DException{
87         return (unlockedCommitted == null ) ? unlockedCommitted =
88              new UnlockCommittedCondition(sessionIdList.toArray(new FieldBase[0]))
89                                    : unlockedCommitted ;
90     }
91
92
93     public _SessionCondition getCriteriaForRollBackNCommit( ) throws DException{
94         return (criteriaForRollBackNCommit == null ) ? criteriaForRollBackNCommit =
95                  new CriteriaForRollBackNCommit(sessionId) : criteriaForRollBackNCommit;
96     }
97
98
99     public _SessionCondition getInsertDeletedCondition()throws DException{
100             return (InsertDeletedCondition == null ) ? InsertDeletedCondition =
101                  new InsertDeletedCondition(sessionId ) : InsertDeletedCondition;
102     }
103
104     public _SessionCondition getInsertedCondition( )throws DException{
105             return (insertedCondition == null ) ? insertedCondition =
106                      new InsertedCondition(sessionId ) : insertedCondition;
107     }
108
109     public _SessionCondition getInvalidCommittedCondition( )throws DException{
110         return (invalidCommittedCondition == null ) ? invalidCommittedCondition =
111                  new InvalidCommittedCondition(sessionId) : invalidCommittedCondition;
112     }
113
114     public _SessionCondition getInvalidTransactionCondition( ) throws DException{
115         return (invalidTransactionCondition == null ) ? invalidTransactionCondition =
116                  new InvalidTransactionCondition( transactionId ) : invalidTransactionCondition;
117     }
118
119     public _SessionCondition getInvalidTransactionConditionWithChild( ) throws DException{
120         return (invalidTransactionConditionWithChild == null ) ? invalidTransactionConditionWithChild =
121                  new InvalidTransactionConditionWithChild() : invalidTransactionConditionWithChild;
122     }
123
124     public _SessionCondition getInvlidRecordCondition( )throws DException{
125         return (invalidRecordCondition == null ) ? invalidRecordCondition =
126                  new InvalidRecordCondition( sessionId ) : invalidRecordCondition;
127     }
128
129     public _SessionCondition getPersistentCondition( )throws DException{
130         return (persistentCondition == null ) ? persistentCondition =
131                  new PersistentCondition( transactionId ) : persistentCondition;
132     }
133
134     public _SessionCondition getTransactionCondition()throws DException{
135         return (transactionCondition == null ) ? transactionCondition =
136                  new TransactionCondition( transactionId ) : transactionCondition;
137     }
138
139     public _SessionCondition getReadCommittedForLock( )throws DException{
140         return (readCommittedForLock == null ) ? readCommittedForLock =
141                  new ReadCommittedForLock( sessionIdList.toArray(new FieldBase[0])) : readCommittedForLock;
142     }
143
144     public _SessionCondition getReadCommittedForLockModified( )throws DException{
145         return (readCommittedForLockModified == null ) ? readCommittedForLockModified =
146                  new ReadCommittedForLockModified( sessionIdList.toArray(new FieldBase[0])) : readCommittedForLockModified;
147     }
148
149
150     public _SessionCondition getReadCommittedInternal()throws DException{
151         return (readCommittedInternal == null ) ? readCommittedInternal =
152                  new ReadCommittedInternal( new Object JavaDoc[]{ sessionIdList.toArray(new FieldBase[0]), sessionId}) : readCommittedInternal;
153     }
154
155     /**
156      * Refreshes the session condition(Isolation level) with the newly set sessionId/parent sessionid
157      * so that all the records corresponding to the new sessionid/parent sessionId can be retrived.
158      * @param integer isolationLevel tells the type of isolation level set.
159      * @throws DException
160      */

161     private void refresh(int isolationLevel) throws DException{
162         switch (isolationLevel) {
163             case Session.ReadCommitted :
164                 if (readCommitted != null )
165                     readCommitted.setParameterValues(new Object JavaDoc[]{ sessionIdList.toArray(new FieldBase[0])});
166                 break;
167             case Session.ReadRepeatable:
168                 if (readRepeatable != null )
169                     readRepeatable.setParameterValues(new Object JavaDoc[]{transactionId});
170                 if (transactionSessionCondition != null )
171                     transactionSessionCondition.setParameterValues(new Object JavaDoc[]{transactionId , sessionIdList.toArray(new FieldBase[0]) });
172                 break;
173             case Session.ReadSessionSerializable:
174                 if (transactionSessionCondition != null )
175                     transactionSessionCondition.setParameterValues(new Object JavaDoc[]{transactionId , sessionIdList.toArray(new FieldBase[0]) });
176                 break;
177             case Session.ReadTransactionSerializable:
178               if (transactionSessionCondition != null )
179                   transactionSessionCondition.setParameterValues(new Object JavaDoc[]{transactionId , sessionIdList.toArray(new FieldBase[0]) });
180                 break;
181             case Session.ReadUnLokedCommitted:
182                 if (unlockedCommitted != null )
183                     unlockedCommitted.setParameterValues(new Object JavaDoc[]{sessionIdList.toArray(new FieldBase[0]) });
184                 break;
185         }
186
187         if ( criteriaForRollBackNCommit != null )
188             criteriaForRollBackNCommit.setParameterValues(new Object JavaDoc[]{ sessionId } );
189
190         if ( InsertDeletedCondition != null )
191             InsertDeletedCondition.setParameterValues(new Object JavaDoc[]{sessionId });
192
193         if ( insertedCondition != null )
194             insertedCondition.setParameterValues(new Object JavaDoc[]{sessionId });
195
196         if ( invalidCommittedCondition != null )
197             invalidCommittedCondition.setParameterValues(new Object JavaDoc[]{sessionId});
198
199         if ( invalidRecordCondition != null )
200             invalidRecordCondition.setParameterValues(new Object JavaDoc[]{sessionId});
201
202         if ( persistentCondition != null )
203             persistentCondition.setParameterValues(new Object JavaDoc[]{transactionId});
204
205         if ( readCommittedForLock != null )
206             readCommittedForLock.setParameterValues(new Object JavaDoc[]{sessionIdList.toArray(new FieldBase[0])});
207
208         if ( readCommittedForLockModified != null )
209             readCommittedForLockModified.setParameterValues(new Object JavaDoc[]{sessionIdList.toArray(new FieldBase[0])});
210
211         if ( readCommittedInternal != null )
212             readCommittedInternal.setParameterValues(new Object JavaDoc[]{ sessionIdList.toArray(new FieldBase[0]),sessionId});
213         if(makePersistentForInserted != null)
214            makePersistentForInserted.setParameterValues(new Object JavaDoc[]{sessionId });
215
216
217     }
218
219
220
221     /**
222      * Set new sessionId and TransactionId for the SessionCondition
223      * @param sessionId0
224      * @param transactionId0
225      * @param isolationLevel
226      * @throws DException
227      */
// Start Save Point.
228
public void setSessionIdTransactionId( Object JavaDoc sessionId0, Object JavaDoc transactionId0 , int isolationLevel) throws DException{
229         sessionId = sessionId0;
230         transactionId = transactionId0;
231         sessionIdList.add(sessionId0);
232         refresh(isolationLevel);
233     }
234
235     /**
236      * @return all the Id's of sessions started
237      * @throws DException
238      */

239     public Object JavaDoc[] getRecursiveSessionIds() throws DException{
240         return sessionIdList.toArray(new FieldBase[0]);
241     }
242
243     /**
244      * Set new TransactionId for the SessionCondition
245      * @param transactionId0
246      * @param isolationLevel
247      * @throws DException
248      */

249     public void setTransactionId(Object JavaDoc transactionId0, int isolationLevel) throws DException{
250         transactionId = transactionId0;
251         refresh(isolationLevel);
252     }
253
254
255     /**
256      * @return parent session id
257      * @throws DException
258      */

259     public Object JavaDoc getParentSessionId () throws DException{
260         if( sessionIdList.size() < 2 )
261             throw new DException("DSE5529", null);
262         return sessionIdList.get(sessionIdList.size() - 2);
263     }
264
265     /**
266      * release last saved session and refreshes the session condition according new sessionId
267      * @param isolationLevel
268      * @throws DException
269      */

270     public void releaseSession(int isolationLevel) throws DException{
271        removeConstraintStatus();
272        setDataModifiedStatusFalse(); // setting the status to false for parent session
273
int len = sessionIdList.size();
274        try{
275          sessionId = sessionIdList.get(len-2);
276          sessionIdList.remove(len-1);
277        }catch(RuntimeException JavaDoc rte){
278           throw rte;
279        }
280        refresh(isolationLevel);
281     }
282
283     /**
284      * @return Number of save points started
285      * @throws DException
286      */

287     public int getNumberOfSavePiontsStarted() throws DException{
288         return sessionIdList.size() -1;
289     }
290
291     /**
292      * hides last session and refreshes the sessionCondition(unlockedCommitted only)
293      * for this session Id.
294      * @throws DException
295      */

296     public void hideSavePoint() throws DException{
297         if(sessionIdList.size() <= 1)
298             throw new DException("DSE5530",null);
299         int length = sessionIdList.size();
300         hiddenSessionidList.add(sessionIdList.remove(length - 1));
301         sessionId = sessionIdList.get(length - 2);
302         if(unlockedCommitted != null)
303             unlockedCommitted.setParameterValues(new Object JavaDoc[]{sessionIdList.toArray(new FieldBase[0])});
304     }
305
306     /**
307      * unhides last hidden session nad refreshes the SessionCondition(unlockedCommitted only)
308      * @throws DException
309      */

310     public void unhideSavePoint() throws DException{
311       if( hiddenSessionidList.isEmpty() )
312         throw new DException("DSE5531",null);
313       sessionIdList.add(sessionId = hiddenSessionidList.remove(hiddenSessionidList.size()-1));
314       if(unlockedCommitted != null)
315         unlockedCommitted.setParameterValues(new Object JavaDoc[]{sessionIdList.toArray(new FieldBase[0])});
316     }
317
318     /**
319      * will remove and maintain the last sessionId to parallel session id list.
320      * @throws DException
321      */

322     public void allowParallelSavePoint() throws DException {
323         if ( sessionIdList.size() <= 1 )
324             throw new DException("DSE5532",null);
325         int index = sessionIdList.size()-1;
326         parallelSessionIdList.add(sessionIdList.get(index));
327         sessionIdList.remove(index);
328     }
329
330     /**
331      * will restore the removed last sessionId from parallelSessionId list to current sessionID list.
332      * @param isolationlevel
333      * @throws DException
334      */

335     public void ignoreParallelSavePoint(int isolationlevel) throws DException {
336         if ( sessionIdList.size() <= 1 || parallelSessionIdList.size() <= 0)
337             throw new DException("DSE5533",null);
338         int index = sessionIdList.size()-1;
339         int pindex = parallelSessionIdList.size()-1;
340         Object JavaDoc lastSessionId = sessionIdList.get(index);
341         Object JavaDoc lastParallelSessionId = parallelSessionIdList.get(pindex);
342         Long JavaDoc lastSessionIdLong = (Long JavaDoc)((FieldBase)lastSessionId).getObject();
343         Long JavaDoc lastParallelSessionIdLong = (Long JavaDoc)((FieldBase)lastParallelSessionId).getObject();
344         if ( ((Long JavaDoc)lastSessionIdLong).compareTo((Long JavaDoc)lastParallelSessionIdLong) >= 0 )
345             throw new DException("DSE5534",null);
346         sessionIdList.add(parallelSessionIdList.get(pindex));
347         parallelSessionIdList.remove(pindex);
348         refresh(isolationlevel);
349     }
350
351     /**
352      * throws exception if last parallel sessionId is less than last current session.
353      * @throws DException
354      */

355     public void checkForException() throws DException{
356         if (parallelSessionIdList.size() >= 1){
357             int index = sessionIdList.size()-1;
358             int pindex = parallelSessionIdList.size()-1;
359             Object JavaDoc lastSessionId = sessionIdList.get(index);
360             Object JavaDoc lastParallelSessionId = parallelSessionIdList.get(pindex);
361             Long JavaDoc lastSessionIdLong = (Long JavaDoc)((FieldBase)lastSessionId).getObject();
362             Long JavaDoc lastParallelSessionIdLong = (Long JavaDoc)((FieldBase)lastParallelSessionId).getObject();
363
364             if ( ((Long JavaDoc)lastSessionIdLong).compareTo((Long JavaDoc)lastParallelSessionIdLong) < 0 )
365                 throw new DException("DSE5535",null);
366         }
367     }
368
369     /**
370      * maintains a constraint check list.
371      * @throws DException
372      */

373     public void checkImmediateConstraintsOnCommit() throws DException{
374        if(constraintCheckingStatusList == null){
375           constraintCheckingStatusList = new ArrayList JavaDoc();
376           ConstraintStatus cs = new ConstraintStatus( sessionId, deferredConstraintChecking );
377           constraintCheckingStatusList.add(cs);
378           return;
379        }
380        maintainConstraintStatus();
381     }
382
383     /**
384      * maintains a constraint check list for parent session weather its Immidiate , deferred or Ignore.
385      * @throws DException
386      */

387     protected void maintainConstraintStatus() throws DException{
388        int parentSessionStatus = getConstraintStatusOfParentSession();
389        ConstraintStatus cs = null;
390        switch(parentSessionStatus){
391           case ImmidiateConstraintChecking :
392                cs = new ConstraintStatus( sessionId, deferredConstraintChecking );
393                break;
394           case deferredConstraintChecking :
395           case IgnoreConstraintChecking :
396                cs = new ConstraintStatus( sessionId, IgnoreConstraintChecking );
397                break;
398        }
399        constraintCheckingStatusList.add(cs);
400     }
401
402     /**
403      * returns constraint status for parent session weather its Immidiate , deferred or Ignore.
404      * @throws DException
405      */

406     private int getConstraintStatusOfParentSession() throws DException{
407        int no_of_sessions = sessionIdList.size();
408        Object JavaDoc parentSessionId = sessionIdList.get( no_of_sessions - 2 );
409        for (int i = 0; i < constraintCheckingStatusList.size(); i++) {
410           ConstraintStatus cs = ( ConstraintStatus )constraintCheckingStatusList.get(i) ;
411           if(cs.sessionId.equals(parentSessionId))
412               return cs.status;
413        }
414        return ImmidiateConstraintChecking;
415     }
416
417     /**
418      * returns constraint status for current session weather its Immidiate , deferred or Ignore.
419      * @throws DException
420      */

421     protected int getConstraintStatusOfCurrentSession() {
422        if(constraintCheckingStatusList != null)
423         for (int i = 0; i < constraintCheckingStatusList.size(); i++) {
424           ConstraintStatus cs = ( ConstraintStatus )constraintCheckingStatusList.get(i) ;
425           if(cs.sessionId.equals(sessionId))
426               return cs.status;
427        }
428        return ImmidiateConstraintChecking;
429     }
430
431
432     /**
433      * will empty the list containing the constraint check status.
434      * @throws DException
435      */

436     private void removeConstraintStatus() throws DException{
437        if(constraintCheckingStatusList == null)
438            return;
439        for (int i = 0; i < constraintCheckingStatusList.size(); i++) {
440           ConstraintStatus cs = ( ConstraintStatus )constraintCheckingStatusList.get(i) ;
441           if(cs.sessionId.equals(sessionId)){
442               constraintCheckingStatusList.remove(cs);
443               if(constraintCheckingStatusList.size() == 0)
444                 constraintCheckingStatusList = null;
445               return;
446           }
447        }
448     }
449
450     /**
451      * updates the status to true if a record is updated , interted or deleted
452      * @throws DException
453      */

454     public void updateDataModifiedStatus() throws DException{
455        if(!sessionsWithModifiedData.contains(sessionId))
456           sessionsWithModifiedData.add(sessionId);
457     }
458
459     /**
460      * updates the status to false after commit is called
461      * @throws DException
462      */

463     protected void setDataModifiedStatusFalse() throws DException{
464           sessionsWithModifiedData.remove(sessionId);
465     }
466
467     /**
468      * checks for if a record is inserted , deleted or updated
469      * @return boolean value true / false
470      * @throws DException
471      */

472     public boolean isDataModified() throws DException{
473        return sessionsWithModifiedData.contains(sessionId);
474     }
475
476     /**
477      * maintain the constraint status along with the corresponding session id.
478      */

479     private class ConstraintStatus{
480        Object JavaDoc sessionId ;
481        int status;
482        public ConstraintStatus( Object JavaDoc sessionId0, int status0 ) throws DException {
483           sessionId = sessionId0;
484           status = status0;
485        }
486     }
487
488     public boolean hasAnyChild() throws DException{
489         return sessionIdList.size() > 1 ;
490     }
491
492     public _SessionCondition getMakePersistentForInsertedCondition()throws DException{
493             return (makePersistentForInserted == null ) ? makePersistentForInserted =
494                  new MakePersistentForInsertedCondition(sessionId ) : makePersistentForInserted;
495     }
496
497 }
498
Popular Tags