// Path: connector/genericconnector-api/src/main/java/ch/maxant/generic_jca_adapter/BasicTransactionAssistanceFactory.java
// public interface BasicTransactionAssistanceFactory {
// 
//     /**
//      * Get transaction assistant from factory so that a callback can be 
//      * bound into the transaction as well as recovery which is controlled
//      * by the app server's transaction manager.
//      * @exception ResourceException Thrown if an assistant can't be obtained
//      */
//     TransactionAssistant getTransactionAssistant() throws ResourceException;   
//     
// }
// 
// Path: connector/genericconnector-api/src/main/java/ch/maxant/generic_jca_adapter/TransactionAssistant.java
// public interface TransactionAssistant extends AutoCloseable {
// 
//     /** Submit some work (a function) to be bound into the
//      * currently active transaction. */
//     <O> O executeInActiveTransaction(ExecuteCallback<O> tc) throws Exception;
// 
//     /** Call before completing the transaction in order 
//      * to free up resources used by the app server. */
//     @Override
//     void close();
// }


import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import ch.maxant.generic_jca_adapter.BasicTransactionAssistanceFactory;
import ch.maxant.generic_jca_adapter.TransactionAssistant;
import ch.maxant.jca_demo.bookingsystem.BookingSystem;
import ch.maxant.jca_demo.bookingsystem.BookingSystemWebServiceService;
import ch.maxant.jca_demo.letterwriter.LetterWebServiceService;
import ch.maxant.jca_demo.letterwriter.LetterWriter;
			Account acct = new Account();
			acct.setName(username);
			acct.setId(5000);
//TODO not working in Atomikos with Mysql: 
//			this.appRepository.save(acct);
		}

		String msResponse = null;

		//call microservice #1
        try(TransactionAssistant transactionAssistant = bookingServiceFactory.getTransactionAssistant()){
        	msResponse = transactionAssistant.executeInActiveTransaction(txid->{
        		final BookingSystem service = new BookingSystemWebServiceService().getBookingSystemPort();
        		return service.reserveTickets(txid, username);
        	});
        }

        //call microservice #2
        try(TransactionAssistant transactionAssistant = letterServiceFactory.getTransactionAssistant()){
        	msResponse = transactionAssistant.executeInActiveTransaction(txid->{
        		final LetterWriter service = new LetterWebServiceService().getLetterWriterPort();
        		return service.writeLetter(txid, username);
        	});
        }
        
        //simulate exception to cause rollback
		if ("john".equals(username)) {
			throw new RuntimeException("Simulated error");
		}

// Path: connector/genericconnector-api/src/main/java/ch/maxant/generic_jca_adapter/BasicTransactionAssistanceFactory.java
// public interface BasicTransactionAssistanceFactory {
// 
//     /**
//      * Get transaction assistant from factory so that a callback can be 
//      * bound into the transaction as well as recovery which is controlled
//      * by the app server's transaction manager.
//      * @exception ResourceException Thrown if an assistant can't be obtained
//      */
//     TransactionAssistant getTransactionAssistant() throws ResourceException;   
//     
// }
// 
// Path: connector/genericconnector-api/src/main/java/ch/maxant/generic_jca_adapter/TransactionAssistant.java
// public interface TransactionAssistant extends AutoCloseable {
// 
//     /** Submit some work (a function) to be bound into the
//      * currently active transaction. */
//     <O> O executeInActiveTransaction(ExecuteCallback<O> tc) throws Exception;
// 
//     /** Call before completing the transaction in order 
//      * to free up resources used by the app server. */
//     @Override
//     void close();
// }


// Path: demo/genericconnector-demo-springboot-common/src/main/java/ch/maxant/generic_jca_adapter/demo/AppService.java
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import ch.maxant.generic_jca_adapter.BasicTransactionAssistanceFactory;
import ch.maxant.generic_jca_adapter.TransactionAssistant;
import ch.maxant.jca_demo.bookingsystem.BookingSystem;
import ch.maxant.jca_demo.bookingsystem.BookingSystemWebServiceService;
import ch.maxant.jca_demo.letterwriter.LetterWebServiceService;
import ch.maxant.jca_demo.letterwriter.LetterWriter;

			Account acct = new Account();
			acct.setName(username);
			acct.setId(5000);
//TODO not working in Atomikos with Mysql: 
//			this.appRepository.save(acct);
		}

		String msResponse = null;

		//call microservice #1
        try(TransactionAssistant transactionAssistant = bookingServiceFactory.getTransactionAssistant()){
        	msResponse = transactionAssistant.executeInActiveTransaction(txid->{
        		final BookingSystem service = new BookingSystemWebServiceService().getBookingSystemPort();
        		return service.reserveTickets(txid, username);
        	});
        }

        //call microservice #2
        try(TransactionAssistant transactionAssistant = letterServiceFactory.getTransactionAssistant()){
        	msResponse = transactionAssistant.executeInActiveTransaction(txid->{
        		final LetterWriter service = new LetterWebServiceService().getLetterWriterPort();
        		return service.writeLetter(txid, username);
        	});
        }
        
        //simulate exception to cause rollback
		if ("john".equals(username)) {
			throw new RuntimeException("Simulated error");
		}

        return msResponse;