KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sessionsystem > Session


1 package com.daffodilwoods.daffodildb.server.sessionsystem;
2
3 import java.sql.*;
4 import java.sql.Date JavaDoc;
5 import java.util.*;
6
7 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
8 import com.daffodilwoods.daffodildb.server.datasystem.mergesystem.*;
9 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
10 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
11 import com.daffodilwoods.daffodildb.server.serversystem.*;
12 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.*;
13 import com.daffodilwoods.daffodildb.server.sessionsystem.sessioncondition.*;
14 import com.daffodilwoods.daffodildb.server.sessionsystem.sessionversioninfo.*;
15 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
16 import com.daffodilwoods.daffodildb.utils.*;
17 import com.daffodilwoods.database.general.*;
18 import com.daffodilwoods.database.resource.*;
19
20
21 /**
22  *
23  * <p>Title: Session</p>
24  * <p>Description: </p>
25  *
26  */

27 public class Session extends ConditionProvider implements _Session {
28
29    public Object JavaDoc sessionConstant;
30    public SessionDatabase sessionDatabase;
31    int isolationLevel = -1;
32    private TreeMap sessionTableList = new TreeMap(String.CASE_INSENSITIVE_ORDER);
33    private Lock lock;
34    boolean constraintChecking = true;
35    private SessionSQL sessionSQL;
36    TreeMap immediateModeConstraints = new TreeMap(String.CASE_INSENSITIVE_ORDER);
37    private ConstraintInformation constraintInformation;
38
39    private ArrayList arr;
40    private ForUpdateTableHandler forUpdateTableHandler;
41    public _DataRetriever dataRetriever;
42    boolean autoCommit = false, isTransactionStarted;
43
44    public Session(Object JavaDoc sessionConstant, Object JavaDoc sessionId, Object JavaDoc transactionId, SessionDatabase sessionDatabase) throws DException {
45       super(sessionId, transactionId);
46       this.sessionDatabase = sessionDatabase;
47       this.sessionConstant = sessionConstant;
48       lock = new Lock();
49       sessionSQL = new SessionSQL();
50       SessionTransactionMode transactionMode = new SessionTransactionMode();
51       transactionMode.setTransactionAccessMode("Read Write");
52       sessionSQL.setTransactionMode(transactionMode);
53       constraintInformation = new ConstraintInformation();
54       dataRetriever = new SessionSerializableDataRetriever(this);
55    }
56
57    /**
58     * Returns Table to user , and allows no other user to operate on it. User can
59     * perform insert, update and delete operation using this table.
60     * @param tableName
61     * @return
62     * @throws DException
63     */

64
65    public synchronized _SessionTable getSessionTable(QualifiedIdentifier tableName) throws DException {
66       _SessionTable sessionTable = (_SessionTable) sessionTableList.get(tableName.getIdentifier());
67       if (sessionTable != null)
68          return sessionTable;
69       _DataDictionary dataDictionary = sessionDatabase.getDataDictionary();
70       forUpdateTableHandler = sessionDatabase.getForUpdateTableHandler(tableName);
71       SessionVersionHandler sessionVersionHandler = ( (MergeDatabase) sessionDatabase.getMergeDatabase()).getSessionVersionHandler();
72       sessionTable = new SessionTable(tableName, this, sessionDatabase.getMergeDatabase().getTable(tableName), sessionDatabase.getTableRowIdGetter(tableName), forUpdateTableHandler, sessionVersionHandler);
73       ( (SessionTable) sessionTable).setRowLocker(sessionDatabase.getRowLocker(tableName));
74       sessionTableList.put(tableName.getIdentifier(), sessionTable);
75       return sessionTable;
76    }
77
78    public synchronized void commit(_StatementExecutionContext statementExecutionContext) throws DException {
79       throw new UnsupportedOperationException JavaDoc(" Method not should not be called ");
80    }
81
82    /**
83     * Rolls back all the changes performed in the
84     * session.
85     * <br><br>
86     * Get an iterator for session table List.
87     * Iterate through the list and passes the SEC to sessionTable to perform Rollback.
88     * @param statementExecutionContext
89     * @throws DException
90     */

91
92    public synchronized void rollback(_StatementExecutionContext statementExecutionContext) throws DException {
93       Iterator sessionTablesIterator = sessionTableList.values().iterator();
94       while (sessionTablesIterator.hasNext()) {
95          _SessionTable sessionTable = (_SessionTable) sessionTablesIterator.next();
96          ( (SessionTable) sessionTable).removeSession();
97          sessionTable.performRollBack(statementExecutionContext);
98
99       }
100    }
101
102    /**
103     *
104     * Returns a map of session tableList.
105     * @return Tree Map
106     * @throws DException
107     */

108
109    public TreeMap getSessionTableList() throws DException {
110       return sessionTableList;
111    }
112
113    public _ServerSession getGlobalSession() throws DException {
114       return sessionDatabase.getGlobalSession();
115    }
116
117    public Lock getLock() throws DException {
118       return lock;
119    }
120
121    /**
122     * returns Transaction ID made at the start of the session.
123     * @return Object
124     * @throws DException
125     */

126
127    public Object JavaDoc getTransactionIdAtStart() throws DException {
128       return transactionId;
129    }
130
131    /**
132     *
133     * Returns the integer representation of the isolation level.
134     * @return int
135     * @throws DException
136     */

137
138    /**
139     * Will return true if the role or user passed is the current one.
140     * @param authorizationIdentifier
141     * @return boolean
142     * @throws DException
143     */

144
145    public boolean isEnabledAuthorizationIdentifier(String JavaDoc authorizationIdentifier) throws DException {
146       return sessionSQL.isEnabledAuthorizationIdentifier(authorizationIdentifier, sessionDatabase.getGlobalSession());
147    }
148
149    public _SessionDatabase getSessionDatabase() throws DException {
150       return sessionDatabase;
151    }
152
153    /**
154     * returns current role of user from Session SQL.
155     * @return String
156     * @throws DException
157     */

158
159    public String JavaDoc getCurrentRole() throws DException {
160       return sessionSQL.getCurrentRole();
161    }
162
163    /**
164     * sets the value of Session Transaction mode to immiediate or deffered.
165     * @param sessionTransactionMode
166     * @throws DException
167     */

168
169    public void setTransactionMode(SessionTransactionMode sessionTransactionMode) throws DException {
170
171       if (sessionTransactionMode.getIsolationLevel() != null)
172          setIsolationLevel(sessionTransactionMode.getIsolationLevel().toString());
173       sessionSQL.setTransactionMode(sessionTransactionMode);
174    }
175
176    public Object JavaDoc getSessionId() throws DException {
177       return sessionId;
178    }
179
180    /**
181     * returns the Current Date.
182     * @return Date
183     * @throws DException
184     */

185
186    public Date JavaDoc getDate() throws DException {
187       return sessionSQL.getDate();
188    }
189
190    public void setIsolationLevel(int level) throws DException {
191      if (isDataModified())
192          throw new DException("DSE2052", null);
193
194      if (ReadUncommitted == level) {
195          sessionSQL.getTransactionMode().setTransactionAccessMode("Read Only");
196          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.READUNCOMMIT);
197          dataRetriever = new UncommittedDataRetriever(this);
198       } else if (ReadCommitted == level) {
199          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
200              READCOMMIT);
201          dataRetriever = new CommittedDataRetriever(this);
202       } else if (ReadRepeatable == level) {
203          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
204              REPEATABLEREAD);
205          dataRetriever = new RepeatableDataRetriever(this);
206       } else if (ReadTransactionSerializable == level) {
207          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
208              TRANSACTIONSERIALIZABLE);
209          dataRetriever = new TransactionSerializedDataRetriever(this);
210       } else if (ReadSessionSerializable == level) {
211          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
212              SESSIONSERIALIZABLE);
213          dataRetriever = new SessionSerializableDataRetriever(this);
214       } else if (ReadUnLokedCommitted == level) {
215          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.UNLOCKEDCOMMITTED);
216          dataRetriever = new UnLockedCommittedDataRetriever(this);
217       }
218       this.isolationLevel = level;
219    }
220
221    public void setIsolationLevel(String JavaDoc level) throws DException {
222      boolean isAnyTableDirty = false;
223      for (Iterator iter = sessionTableList.keySet().iterator(); iter.hasNext(); ) {
224        if(((_SessionTable)sessionTableList.get(iter.next())).isDataModified() ){
225          isAnyTableDirty = true;
226          break;
227       }
228     }
229     if(isAnyTableDirty)
230         throw new DException("DSE2052",null );
231      if (level.equalsIgnoreCase((String JavaDoc)SessionCharacteristics.READUNCOMMIT )) {
232          sessionSQL.getTransactionMode().setTransactionAccessMode("Read Only");
233          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.READUNCOMMIT);
234          dataRetriever = new UncommittedDataRetriever(this);
235          this.isolationLevel = ReadUncommitted;
236       } else if (level.equalsIgnoreCase((String JavaDoc)SessionCharacteristics.READCOMMIT )) {
237          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
238              READCOMMIT);
239          dataRetriever = new CommittedDataRetriever(this);
240          this.isolationLevel = ReadCommitted;
241       } else if (level.equalsIgnoreCase((String JavaDoc)SessionCharacteristics.REPEATABLEREAD )) {
242          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
243              REPEATABLEREAD);
244          dataRetriever = new RepeatableDataRetriever(this);
245          this.isolationLevel = ReadRepeatable;
246       } else if (level.equalsIgnoreCase((String JavaDoc)SessionCharacteristics.TRANSACTIONSERIALIZABLE )||
247               level.equalsIgnoreCase((String JavaDoc)SessionCharacteristics.SERIALIZABLE )) {
248          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
249              TRANSACTIONSERIALIZABLE);
250          dataRetriever = new TransactionSerializedDataRetriever(this);
251          this.isolationLevel = ReadTransactionSerializable;
252       } else if (level.equalsIgnoreCase((String JavaDoc)SessionCharacteristics.SESSIONSERIALIZABLE )) {
253          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.
254              SESSIONSERIALIZABLE);
255          dataRetriever = new SessionSerializableDataRetriever(this);
256          this.isolationLevel = ReadSessionSerializable;
257       } else if (level.equalsIgnoreCase((String JavaDoc)SessionCharacteristics.UNLOCKEDCOMMITTED )) {
258          sessionSQL.getTransactionMode().setIsolationLevel(SessionCharacteristics.UNLOCKEDCOMMITTED);
259          dataRetriever = new UnLockedCommittedDataRetriever(this);
260          this.isolationLevel = ReadUnLokedCommitted;
261       }
262    }
263
264    /**
265     * returns the current month .
266     * @return int
267     * @throws DException
268     */

269
270    public int getMonth() throws DException {
271       return sessionSQL.getMonth();
272    }
273
274    public int getIsolationLevel() throws DException {
275      SessionTransactionMode transactionMode = sessionSQL.getTransactionMode();
276       if (transactionMode == null)
277          return ReadSessionSerializable;
278       Object JavaDoc isolationLevel = transactionMode.getIsolationLevel();
279       if (isolationLevel == null)
280          return ReadSessionSerializable;
281       if (isolationLevel.equals( (String JavaDoc) SessionCharacteristics.READUNCOMMIT))
282          return ReadUncommitted;
283       else if (isolationLevel.equals( (String JavaDoc) SessionCharacteristics.READCOMMIT))
284          return ReadCommitted;
285       else if (isolationLevel.equals( (String JavaDoc) SessionCharacteristics.SERIALIZABLE) || isolationLevel.equals( (String JavaDoc) SessionCharacteristics.TRANSACTIONSERIALIZABLE))
286          return ReadTransactionSerializable;
287       else if (isolationLevel.equals( (String JavaDoc) SessionCharacteristics.REPEATABLEREAD))
288          return ReadRepeatable;
289       else if (isolationLevel.equals( (String JavaDoc) SessionCharacteristics.UNLOCKEDCOMMITTED))
290          return ReadUnLokedCommitted;
291       else
292          return ReadSessionSerializable;
293    }
294
295    /**
296     * returns the current hour.
297     * @return int
298     * @throws DException
299     */

300
301    public int getHour() throws DException {
302       return sessionSQL.getHour();
303    }
304
305    /**
306     * returns the current seconds.
307     * @return int
308     * @throws DException
309     */

310
311    public int getSeconds() throws DException {
312       return sessionSQL.getSeconds();
313    }
314
315    public String JavaDoc getAuthorizationIdentifier() throws DException {
316       return sessionSQL.getAuthorizationIdentifier();
317    }
318
319    /**
320     * returns current User from Session SQL.
321     * @return String
322     * @throws DException
323     */

324
325    public String JavaDoc getCurrentUser() throws DException {
326       return sessionSQL.getCurrentUser();
327    }
328
329    /**
330     * returns the session Tranaction mode (immiedate or deffered)
331     * @return Session Ttransaction Mode
332     * @throws DException
333     */

334
335    public SessionTransactionMode getTransactionMode() throws DException {
336       return sessionSQL.getTransactionMode();
337    }
338
339    public Time getTime() throws DException {
340       return sessionSQL.getTime();
341    }
342
343    /**
344     * check the constrains with the one present in the table defenation.
345     * @return boolean
346     * @throws DException
347     */

348
349    public boolean checkConstraint() throws DException {
350       return constraintChecking;
351    }
352
353    public int getYear() throws DException {
354       return sessionSQL.getYear();
355    }
356
357    /**
358     * removes all the entries from the table list i.e contaning the names of all the table in use of same session.
359     * @throws DException
360     */

361
362    public void removeAllTables() throws DException {
363       sessionTableList.clear();
364    }
365
366    public int getMinutes() throws DException {
367       return sessionSQL.getMinutes();
368    }
369
370    /**
371     * returns the current time zone.
372     * @return TimeZone
373     * @throws DException
374     */

375
376    public TimeZone getTimeZone() throws DException {
377       return sessionSQL.getTimeZone();
378    }
379
380    /**
381     * Adds/Removes the constraint from the immiediate mode list according to the mode (deferred/immediate)
382     * <br><br>
383     * add the constraint Name to the immediateModeConstraints Tree Map if its mode is
384     * Immediate otherwise otherwise it removes the Constraint Name from the immediateModeConstraints Tree Map
385     * @param constraintName
386     * @param mode
387     * @throws DException
388     */

389
390    public void setConstraintStatus(Object JavaDoc constraintName, String JavaDoc mode) throws DException {
391       constraintInformation.setConstraints(constraintName, mode);
392    }
393
394    /**
395     * returns a list of tables in immediate mode.
396     * @param tableName
397     * @return ArrayList
398     * @throws DException
399     */

400
401    public ArrayList getImmediateConstriants(QualifiedIdentifier tableName) throws DException {
402       return (ArrayList) immediateModeConstraints.get(tableName);
403    }
404
405    /**
406     * returns map of immeidate constraints.
407     * @return Tree Map
408     * @throws DException
409     */

410
411    public TreeMap getImmediateConstriants() throws DException {
412       return immediateModeConstraints;
413    }
414
415    /**
416     * removes the table name from session table list.
417     * @param tableName
418     * @throws DException
419     */

420
421    public void removeTable(QualifiedIdentifier tableName) throws DException {
422       Object JavaDoc obj = sessionTableList.remove(tableName.getIdentifier());
423       constraintInformation.removeTable(tableName);
424    }
425
426    /**
427     * fetches the time stamp from session SQL.
428     * @return TimeStamp
429     * @throws DException
430     */

431
432    public java.sql.Timestamp JavaDoc getTimeStamp() throws DException {
433       return sessionSQL.getTimeStamp();
434    }
435
436    /**
437     * will add the user or role to a Stack.
438     * @param athrznIdntfr
439     * @throws DException
440     */

441
442    public void pushAuthorizationIdentifier(AuthorizationIdentifier athrznIdntfr) throws DException {
443       sessionSQL.pushAuthorizationIdentifier(athrznIdntfr);
444    }
445
446    /**
447     * returns the session constant
448     * @return Object
449     * @throws DException
450     */

451
452    public Object JavaDoc getSessionConstant() throws DException {
453       return sessionConstant;
454    }
455
456    /**
457     * Return a condition corresponding to the Isolation Level for the current user session.
458     * and return Parameterised Condition by setting runtime variables too.
459     * @return ParameterisedCondition
460     */

461
462    public SessionConditionInfo getSessionCondition() {
463       /* try{
464            if( getIsolationLevel() == Session.ReadUncommitted ){
465            booleanvalueexpression condition = SystemFieldsCharacteristics.getRecordsValidityCondition();
466            SessionConditionInfo p = new SessionConditionInfo(condition,null);
467            return p;
468            }
469            if( getIsolationLevel() == Session.ReadCommitted ){
470              booleanvalueexpression condition = SystemFieldsCharacteristics.getReadCommitted();
471              SessionConditionInfo p = new SessionConditionInfo(condition,new Object[]{ SystemFields.maxIntegerValue, getRecursiveSessionIds() });
472              return p;
473            }
474            if( getIsolationLevel() == Session.ReadUnLokedCommitted ){
475              booleanvalueexpression condition = SystemFieldsCharacteristics.getUnlockedCommittedCondition();
476              Object[] param = getRecursiveSessionIds();
477              SessionConditionInfo p = new SessionConditionInfo(condition,new Object[]{ param, param,param });
478              return p;
479            }
480            if( getIsolationLevel() == Session.ReadSessionSerializable ){
481               throw new UnsupportedOperationException("Not Supported ");
482            }
483            if( getIsolationLevel() == Session.ReadTransactionSerializable ){
484               throw new UnsupportedOperationException("Not Supported ");
485            }
486            if( getIsolationLevel() == Session.ReadRepeatable ){
487               throw new UnsupportedOperationException("Not Supported ");
488         }
489         }catch(Exception e){e.printStackTrace();}
490         return null;
491         } finally{ D.pop("com.daffodilwoods.daffodildb.server.sessionsystem.Session.getSessionCondition"); }
492        */

493       throw new UnsupportedOperationException JavaDoc(" Method not supported ");
494    }
495
496    /**
497     * Will return a array of Objects containing the values for transaction session parameters (like SessionId).
498     * @return Object[]
499     * @throws DException
500     */

501
502
503    /**
504     * Sets the value to false so that no constraint checking can take place.
505     * @throws DException
506     */

507
508    public void stopConstraintChecking() throws DException {
509       constraintChecking = false;
510    }
511
512    /**
513     * return transaction access mode from session SQL
514     * @return Object
515     * @throws DException
516     */

517
518    public Object JavaDoc getTransactionAccessMode() throws DException {
519       return sessionSQL.getTransactionMode().getTransactionAccessMode();
520    }
521
522    /**
523     * will set the User name and place the use name to Stack through pushAuthorizationIdentifier.
524     * @param userName
525     * @throws DException
526     */

527
528    public void setAuthorizationIdentifier(String JavaDoc userName) throws DException {
529       AuthorizationIdentifier authorizationIdentifier = new AuthorizationIdentifier();
530       authorizationIdentifier.setUser(userName);
531       sessionSQL.pushAuthorizationIdentifier(authorizationIdentifier);
532    }
533
534
535    public boolean getImmediateConstriantsForChecking() {
536       int status = getConstraintStatusOfCurrentSession();
537       return status == ImmidiateConstraintChecking;
538    }
539
540    public boolean getDeferredConstriantsForChecking() {
541       int status = getConstraintStatusOfCurrentSession();
542       return status == deferredConstraintChecking;
543    }
544
545    public void startSavePoint() throws DException {
546       Object JavaDoc childSessionId = sessionDatabase.getSystemFieldsValue().getNextSessionId();
547       setSessionIdTransactionId(childSessionId, transactionId, getIsolationLevel());
548       if (constraintCheckingStatusList != null)
549          maintainConstraintStatus();
550
551    }
552
553    public void commitSavePoint(_StatementExecutionContext sec) throws DException {
554       checkForException();
555       if (getDeferredConstriantsForChecking())
556          checkConstraintsChild(sec);
557       Iterator sessionTablesIterator = getSessionTableList().values().iterator();
558       sessionTablesIterator = getSessionTableList().values().iterator();
559       while (sessionTablesIterator.hasNext()) {
560          _SessionTable sessionTable = (_SessionTable) sessionTablesIterator.next();
561          sessionTable.performCommitInParent(getParentSessionId(), sec);
562       }
563       boolean childHasModifiedTheData = super.isDataModified(); // note
564
releaseSession(getIsolationLevel());
565       if (childHasModifiedTheData)
566          updateDataModifiedStatus();
567    }
568
569    public void rollbackSavePoint(_StatementExecutionContext sec) throws DException {
570       checkForException();
571       Iterator sessionTablesIterator = getSessionTableList().values().iterator();
572
573       sessionTablesIterator = getSessionTableList().values().iterator();
574       while (sessionTablesIterator.hasNext()) {
575          _SessionTable sessionTable = (_SessionTable) sessionTablesIterator.next();
576          sessionTable.performRollBackWithChildSession(sec);
577       }
578       releaseSession(getIsolationLevel());
579    }
580
581    public void releaseSavePoint(_StatementExecutionContext sec) throws DException {
582       commitSavePoint(sec);
583    }
584
585    public void checkConstraintsChild(_StatementExecutionContext statementExecutionContext) throws DException {
586       Iterator sessionTablesIterator = getSessionTableList().values().iterator();
587       ArrayList tableList = new ArrayList();
588       while (sessionTablesIterator.hasNext()) {
589          _SessionTable sessionTable = (_SessionTable) sessionTablesIterator.next();
590          tableList.add(sessionTable);
591       }
592       arr = new ArrayList();
593       for (int i = 0; i < tableList.size(); i++) {
594          _SessionTable sessionTable = (_SessionTable) tableList.get(i);
595          QualifiedIdentifier tableName = sessionTable.getTableName();
596
597          _ConstraintTable constTable = ( (SessionDatabase) getSessionDatabase()).getConstraintTable(tableName);
598          if (sessionTable.isDataModified())
599             fireSetConstraints( (SessionTable) sessionTable, constTable, statementExecutionContext);
600
601       }
602       lookForArrayListTables(statementExecutionContext);
603    }
604
605    private void lookForArrayListTables(_StatementExecutionContext sec) throws DException {
606       int sizeToLook = arr.size();
607       while (arr.size() > 0) {
608          for (int i = 0; i < arr.size(); i++) {
609             try {
610                SessionTable sessTable = (SessionTable) arr.get(i);
611                _ConstraintTable cTable = ( (SessionDatabase) getSessionDatabase()).getConstraintTable(sessTable.getTableName());
612                fireSetConstraints(sessTable, cTable, sec);
613                arr.remove(i);
614                i--;
615             } catch (DException e) {
616                e.printStackTrace();
617                continue;
618             }
619          }
620          if (arr.size() >= sizeToLook)
621             throw new DException("DSE5536", (Object JavaDoc[])null);
622          sizeToLook = arr.size();
623       }
624    }
625
626    public void fireSetConstraints(SessionTable sessTable, _ConstraintTable constraintTable, _StatementExecutionContext statementExecutionContext) throws DException {
627       _Iterator iterator = sessTable.getUncommitedRecordsIterator();
628       boolean isFirst = iterator.first();
629       if (isFirst) {
630          _Record oldRecord = iterator.getRecord();
631          Object JavaDoc oldRecordRowId = oldRecord.getObject(SystemFields.rowId);
632          boolean isNext = iterator.next();
633          if (!isNext) {
634             iterator.previous();
635             insertOrDelete(oldRecord, statementExecutionContext, constraintTable);
636             return;
637          }
638
639          while (isNext) {
640             _Record newRecord = iterator.getRecord();
641             int[] columnIndexes = null;
642             if (oldRecordRowId.equals(newRecord.getObject(SystemFields.rowId))) {
643                if (SystemFields.maxIntegerValue.equals(newRecord.getObject(SystemFields.invalidSessionId))) {
644                   if (SystemFields.maxIntegerValue.equals(oldRecord.getObject(SystemFields.transactionId))
645                       && oldRecord.getObject(SystemFields.invalidSessionId).equals(getSessionId())
646                       && oldRecord.getObject(SystemFields.sessionId).equals(getSessionId())
647                       && newRecord.getObject(SystemFields.sessionId).equals(getSessionId())) {
648                      columnIndexes = SystemFieldsCharacteristics.getUserColumnIndexes(newRecord);
649                      RecordVersion recordVersion = new RecordVersion(newRecord);
650                      statementExecutionContext.setRecordVersion(recordVersion);
651                      constraintTable.checkInsertConstraints(getGlobalSession(), statementExecutionContext, columnIndexes);
652                   } else {
653                      RecordVersion recordVersion = new RecordVersion(oldRecord);
654                      columnIndexes = getDifference(newRecord, oldRecord);
655                      Object JavaDoc[] obj = getValuesOfRecordAtIndexes(columnIndexes, newRecord);
656                      if (obj != null)
657                         recordVersion.update(columnIndexes, obj);
658                      statementExecutionContext.setRecordVersion(recordVersion);
659                      constraintTable.checkUpdateConstraints(getGlobalSession(), statementExecutionContext, columnIndexes);
660                   }
661                } else {
662                   if (SystemFields.maxIntegerValue.equals(newRecord.getObject(SystemFields.transactionId))
663                       && newRecord.getObject(SystemFields.invalidSessionId).equals(getSessionId())
664                       && newRecord.getObject(SystemFields.sessionId).equals(getSessionId())
665                       && oldRecord.getObject(SystemFields.sessionId).equals(getSessionId())) {
666                      columnIndexes = SystemFieldsCharacteristics.getUserColumnIndexes(newRecord);
667                      RecordVersion recordVersion = new RecordVersion(oldRecord);
668                      statementExecutionContext.setRecordVersion(recordVersion);
669                      constraintTable.checkInsertConstraints(getGlobalSession(), statementExecutionContext, columnIndexes);
670                   } else {
671                      RecordVersion recordVersion = new RecordVersion(newRecord);
672                      columnIndexes = getDifference(oldRecord, newRecord);
673                      Object JavaDoc[] obj = getValuesOfRecordAtIndexes(columnIndexes, oldRecord);
674                      if (obj != null)
675                         recordVersion.update(columnIndexes, obj);
676                      statementExecutionContext.setRecordVersion(recordVersion);
677                      constraintTable.checkUpdateConstraints(getGlobalSession(), statementExecutionContext, columnIndexes);
678                   }
679                }
680
681                if (iterator.next()) {
682                   oldRecord = iterator.getRecord();
683                   oldRecordRowId = oldRecord.getObject(SystemFields.rowId);
684                   isNext = iterator.next();
685                   if (!isNext) {
686                      iterator.previous();
687                      insertOrDelete(oldRecord, statementExecutionContext, constraintTable);
688                   }
689                } else
690                   isNext = false;
691
692             } else {
693                insertOrDelete(oldRecord, statementExecutionContext, constraintTable);
694                oldRecord = newRecord;
695                oldRecordRowId = oldRecord.getObject(SystemFields.rowId);
696                isNext = iterator.next();
697                if (!isNext) {
698                   iterator.previous();
699                   insertOrDelete(oldRecord, statementExecutionContext, constraintTable);
700                }
701             }
702          }
703       }
704    }
705
706    private int[] getDifference(_Record currentRecord, _Record oldRecord) throws DException {
707       ArrayList indexes = new ArrayList(1);
708       for (int i = SystemFieldsCharacteristics.getSystemColumnCount(); i < currentRecord.getColumnCount(); i++) {
709          Object JavaDoc currentValue = currentRecord.getObject(i);
710          Object JavaDoc oldValue = oldRecord.getObject(i);
711          if (currentValue == null)
712             if (oldValue == null)
713                continue;
714             else
715                indexes.add(new Integer JavaDoc(i));
716          else
717          if (!currentValue.equals(oldValue)) {
718             indexes.add(new Integer JavaDoc(i));
719          }
720       }
721       int[] uncommonColumns = new int[indexes.size()];
722       for (int i = 0; i < indexes.size(); i++)
723          uncommonColumns[i] = ( (Integer JavaDoc) indexes.get(i)).intValue();
724       return uncommonColumns;
725
726    }
727
728    private void insertOrDelete(_Record rec, _StatementExecutionContext sec, _ConstraintTable constraintTable) throws DException {
729       RecordVersion recordVersion = new RecordVersion(rec);
730       sec.setRecordVersion(recordVersion);
731       if (SystemFields.maxIntegerValue.equals(rec.getObject(SystemFields.invalidSessionId))) {
732          int[] columnIndexes = SystemFieldsCharacteristics.getUserColumnIndexes(rec);
733          constraintTable.checkInsertConstraints(getGlobalSession(), sec, columnIndexes);
734       } else
735          constraintTable.checkDeleteConstraints(getGlobalSession(), sec);
736    }
737
738    private Object JavaDoc[] getValuesOfRecordAtIndexes(int[] indexes, _Record rec) throws DException {
739       if (indexes != null) {
740          ArrayList arr = new ArrayList();
741          for (int i = 0; i < indexes.length; i++)
742             arr.add(rec.getObject(indexes[i]));
743          return arr.toArray();
744       }
745       return null;
746    }
747
748    public void ignoreParallelSavePoint() throws DException {
749       super.ignoreParallelSavePoint(getIsolationLevel());
750    }
751
752    public void allowParallelSavePoint() throws DException {
753       super.allowParallelSavePoint();
754    }
755
756    public ConstraintInformation getConstraintInformation() throws DException {
757       return constraintInformation;
758    }
759
760    public void setRole(String JavaDoc roleName) throws DException {
761       sessionSQL.setRole(roleName);
762    }
763
764
765    public _DataRetriever getDataRetriever() {
766       return dataRetriever;
767    }
768
769    public boolean prepare(_StatementExecutionContext statementExecutionContext) throws DException {
770       throw new UnsupportedOperationException JavaDoc(" method prepare not supported");
771    }
772
773    public boolean makePersistent(_StatementExecutionContext statementExecutionContext) throws DException {
774       throw new UnsupportedOperationException JavaDoc(" method makePersistent not supported");
775    }
776
777    public void resetTransactionDate() {
778       sessionSQL.resetTransactionDate();
779    }
780
781    public ArrayList getSessionIdList() throws DException {
782       return sessionIdList;
783    }
784
785    public void resetTime() throws DException {
786       sessionSQL.resetTransactionDate();
787    }
788
789    public void setAutoCommit(boolean autoCommit0) {
790       autoCommit = autoCommit0;
791    }
792
793    public boolean getAutoCommit() {
794       return autoCommit;
795    }
796
797    public void addTableInChangedTableList(QualifiedIdentifier tableName) {
798       throw new UnsupportedOperationException JavaDoc(" method addTableInChangedTableList not supported");
799    }
800
801    public void startTransaction() throws DException {
802       if ((getIsolationLevel() == ReadTransactionSerializable || getIsolationLevel() == ReadRepeatable) && !isTransactionStarted) {
803          isTransactionStarted = true;
804          SystemFieldsValue systemFieldsValue = sessionDatabase.getSystemFieldsValue();
805          setTransactionId(systemFieldsValue.getLastTransactionId(), ReadTransactionSerializable);
806          TransactionIsolationLevelHandler tih = sessionDatabase.getTransactionIsolationLevelHandler();
807          tih.transactionStarted(sessionId, isolationLevel);
808       }
809
810    }
811
812    /**
813     * It is implemented by kuldeep while resolving bug no. 12497
814     * In it while a user changes session authorisation than
815     * now we check in setSessionUseridentifierstatement class
816     * than is data modified by user if it is than we don't change it.
817     *
818     * It checks all session tables geted by user for checking
819     * that is data has been modified by user of any table.
820     * If data of any table has been changed by user than return true else false.
821     * @throws DException
822     * @return boolean
823     */

824    public boolean isDataModified() throws DException{
825      for (Iterator iter = sessionTableList.keySet().iterator(); iter.hasNext(); ) {
826        if ( ( (_SessionTable) sessionTableList.get(iter.next())).isDataModified()) {
827          return true;
828        }
829      }
830      return false;
831
832    }
833 }
834
Popular Tags