package org.drools.persistence.map;

import org.drools.persistence.TransactionManager;
import org.drools.persistence.TransactionSynchronization;
import org.drools.persistence.info.SessionInfo;
import org.drools.persistence.info.WorkItemInfo;

public class ManualTransactionManager
    implements
    TransactionManager {
    
    private NonTransactionalPersistentSession session;
    private KnowledgeSessionStorage storage;
    private TransactionSynchronization transactionSynchronization;
    
    public ManualTransactionManager(NonTransactionalPersistentSession session,
                                    KnowledgeSessionStorage storage) {
        this.session = session;
        this.storage = storage;
    }
    
    public int getStatus() {
        return 0;
    }

    public void begin() {
        //session.clear();
    }

    public void commit() {
        try{
            for(SessionInfo sessionInfo : session.getStoredKnowledgeSessions()){
                storage.saveOrUpdate(sessionInfo);
            }
            
            for(WorkItemInfo workItemInfo : session.getStoredWorkItems()){
                storage.saveOrUpdate( workItemInfo );
            }
            //session.clear();
            try{
                transactionSynchronization.afterCompletion(TransactionManager.STATUS_COMMITTED);
            } catch (RuntimeException re){
                //FIXME log error
            }
        } catch (RuntimeException re) {
            transactionSynchronization.afterCompletion(TransactionManager.STATUS_ROLLEDBACK);
        }
    }

    public void rollback() {
    }

    public void registerTransactionSynchronization(TransactionSynchronization ts) {
        this.transactionSynchronization = ts;
    }
}

@@ -800,41 +800,14 @@
 ) %7B%0A
-        //session.clear();%0A
     %7D%0A
+
 %0A   
@@ -920,24 +920,62 @@
 essions())%7B%0A
+                sessionInfo.update();%0A
             
@@ -1111,24 +1111,63 @@
 rkItems())%7B%0A
+                workItemInfo.update();%0A
             
@@ -1226,39 +1226,8 @@
   %7D%0A
-            //session.clear();%0A
     
@@ -1561,16 +1561,16 @@
 DBACK);%0A
-
         
@@ -1567,24 +1567,150 @@
 ;%0A        %7D%0A
+        //we shouldn't clear session here b/c doing so we lose the track of this objects on successive%0A        //interactions%0A
     %7D%0A%0A    p

clean up commit semantics