package client;


import inventory.InventoryService;

import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import payment.Payment;
import payment.PaymentService;

import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.remoting.spring.httpinvoker.TransactionalHttpInvokerRequestExecutor;

public class HttpInvokerClient {
static String cardno = "card10";
	

	
	public static void main(String[] args) throws Exception {
		System.setProperty("com.atomikos.icatch.rest_port_url", "http://example.com/atomikos");
		InventoryService inventoryService = inventoryService();
		PaymentService paymentService = paymentService();
		UserTransactionManager utm = new UserTransactionManager();
		utm.init();

		callBothInJtaTransaction(paymentService, inventoryService);
		utm.close();
	}


	private static PaymentService paymentService() {
		HttpInvokerProxyFactoryBean paymentInvoker = new HttpInvokerProxyFactoryBean();
		paymentInvoker.setServiceUrl("http://localhost:9092/payment");
		paymentInvoker.setServiceInterface(PaymentService.class);
		paymentInvoker.setHttpInvokerRequestExecutor(new TransactionalHttpInvokerRequestExecutor());
		paymentInvoker.afterPropertiesSet();
		PaymentService paymentService =  (PaymentService)paymentInvoker.getObject();
		return paymentService;
	}


	private static InventoryService inventoryService() {
		HttpInvokerProxyFactoryBean inventoryinvoker = new HttpInvokerProxyFactoryBean();
		inventoryinvoker.setServiceUrl("http://localhost:9091/inventory");
		inventoryinvoker.setServiceInterface(InventoryService.class);
		inventoryinvoker.setHttpInvokerRequestExecutor(new TransactionalHttpInvokerRequestExecutor());
		inventoryinvoker.afterPropertiesSet();
		InventoryService inventoryService =  (InventoryService)inventoryinvoker.getObject();
		return inventoryService;
	}


	private static void callBothInJtaTransaction(PaymentService paymentService, InventoryService inventoryService) throws Exception {
		
		UserTransactionManager utm = new UserTransactionManager();
		try {			
			utm.begin();
			Payment payment = new Payment(cardno, 10);
			paymentService.pay(payment);
			inventoryService.update("20", 2);
			utm.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (utm != null) {
				utm.rollback();
			}
		}
		
	}
	
	
}
