/*
   Copyright 2013 Nationale-Nederlanden

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package nl.nn.adapterframework.core;

import nl.nn.adapterframework.util.LogUtil;
import nl.nn.adapterframework.util.Misc;

import org.apache.log4j.Logger;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * Class which generates extra logging when starting and committing transactions.
 *
 * @author  Peter Leeuwenburgh
 */

public class IbisTransaction {
	protected Logger log = LogUtil.getLogger(this);

	private PlatformTransactionManager txManager;
	private TransactionStatus txStatus;
	private TransactionDefinition txDef;
	private String object;

	private boolean txClientIsActive;
	private String txClientName;

	private boolean txIsActive;
	private String txName;
	private boolean txIsNew;

	public IbisTransaction(PlatformTransactionManager txManager, TransactionDefinition txDef, String object) {
		this.txManager = txManager;
		this.txDef = txDef;
		this.object = object;

		txClientIsActive = TransactionSynchronizationManager.isActualTransactionActive();
		txClientName = TransactionSynchronizationManager.getCurrentTransactionName();

		txStatus = txManager.getTransaction(txDef);

		txIsActive = TransactionSynchronizationManager.isActualTransactionActive();
		txIsNew = txStatus.isNewTransaction();

		if (txIsNew) {
			txName = Misc.createSimpleUUID();
			TransactionSynchronizationManager.setCurrentTransactionName(txName);
			int txTimeout = txDef.getTimeout();
			log.debug("Created a new transaction ["+txName+"] for " + object + " with timeout [" + (txTimeout<0?"system default(=120s)":""+txTimeout) + "]");
		} else {
			txName = TransactionSynchronizationManager.getCurrentTransactionName();
			if (txClientIsActive && !txIsActive) {
				log.debug("Suspended the transaction [" + txClientName + "] for " + object);
			}
		}
	}

	public TransactionStatus getStatus() {
		return txStatus;
	}

	public void commit() {
		if (txIsNew) {
			if (txStatus.isRollbackOnly()) {
				log.debug("Transaction ["+txName+"] marked for rollback, so rolling back the transaction for " + object);
			} else {
				log.debug("Transaction ["+txName+"] is not marked for rollback, so committing the transaction for " + object);
			}
		}
		txManager.commit(txStatus);
		if (!txIsNew && txClientIsActive && !txIsActive) {
			log.debug("Resumed the transaction [" + txClientName + "] for " + object);
		}
	}
}

@@ -629,16 +629,62 @@
 .core;%0A%0A
+import javax.transaction.TransactionManager;%0A%0A
 import n
@@ -759,16 +759,73 @@
 il.Misc;
+%0Aimport nl.nn.adapterframework.util.SpringTxManagerProxy;
 %0A%0Aimport
@@ -1037,16 +1037,82 @@
 Status;%0A
+import org.springframework.transaction.jta.JtaTransactionManager;%0A
 import o
@@ -2381,17 +2381,71 @@
 .debug(%22
-C
+Transaction manager %5B%22+getRealTransactionManager()+%22%5D c
 reated a
@@ -2713,17 +2713,71 @@
 .debug(%22
-S
+Transaction manager %5B%22+getRealTransactionManager()+%22%5D s
 uspended
@@ -2905,24 +2905,633 @@
 Status;%0A%09%7D%0A%0A
+%09private String getRealTransactionManager() %7B%0A%09%09if (txManager instanceof SpringTxManagerProxy) %7B%0A%09%09%09SpringTxManagerProxy springTxMgr = (SpringTxManagerProxy) txManager;%0A%09%09%09PlatformTransactionManager platformTxMgr = springTxMgr.getRealTxManager();%0A%09%09%09if (platformTxMgr instanceof JtaTransactionManager) %7B%0A%09%09%09%09JtaTransactionManager jtaTxMgr = (JtaTransactionManager) platformTxMgr;%0A%09%09%09%09TransactionManager txMgr = jtaTxMgr.getTransactionManager();%0A%09%09%09%09return txMgr.getClass().getName();%0A%09%09%09%7D else %7B%0A%09%09%09%09return platformTxMgr.getClass().getName();%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09return txManager.getClass().getName();%0A%09%09%7D%0A%09%7D%0A%0A
 %09public void
@@ -3659,16 +3659,73 @@
 ack, so 
+transaction manager %5B%22+getRealTransactionManager()+%22%5D is 
 rolling 
@@ -3845,16 +3845,73 @@
 ack, so 
+transaction manager %5B%22+getRealTransactionManager()+%22%5D is 
 committi
@@ -4052,17 +4052,71 @@
 .debug(%22
-R
+Transaction manager %5B%22+getRealTransactionManager()+%22%5D r
 esumed t

add classname of real transaction manager in log records