/**
 * BPEL Event Adaptor
 */
package com.zi.mwms.solution.workflow;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.ThreadContext;
import org.jdom.Document;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.cim.idm.flowpackage.core.activity.Activity;
import com.cim.idm.flowpackage.core.activity.BpelProcess;
//import com.cim.idm.framework.fos.nanoflow.BpelExecutionEventAdaptor;
import com.cim.idm.framework.fos.idmflow.BpelExecutionEventAdaptor;
import com.cim.idm.framework.fos.idmflow.BpelLogger;
import com.cim.idm.framework.util.xml.JdomUtils;

/**
 * @author gksong
 * 
 */
public class CustomEventAdaptor extends BpelExecutionEventAdaptor implements
		ApplicationContextAware {

	private static Log log = LogFactory.getLog(CustomEventAdaptor.class);
	/**
	 * @uml.property  name="applicationContext"
	 * @uml.associationEnd  
	 */
	private ApplicationContext applicationContext;
	private String rootBpelName = "dispatching.bpel";

	/**
	 * @param arg0
	 * @throws BeansException
	 * @uml.property  name="applicationContext"
	 */
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		applicationContext = arg0;
	}

	@Override
	public void onStart(Activity activity)
	{
		try 
		{
			if (activity instanceof BpelProcess) 
			{
				Object[] reqMessages = activity.getBpelExecutionContext().getReqMessages();
				if (reqMessages.length > 0)
				{
					if (reqMessages[0] instanceof Document)
					{
						Document doc = (Document) reqMessages[0];
						ThreadContext.put("MES.MSGNAME", JdomUtils.getNodeText(doc, "//Message/Header/MESSAGENAME"));
						ThreadContext.put("MES.TRXID", JdomUtils.getNodeText(doc, "//Message/Header/TRANSACTIONID"));
					}
				}
			}
			
			super.onStart(activity);
				
		} 
		catch (Exception e) 
		{
			log.error("", e);
		} 
	}

	
//	public void onException(Activity activity, Exception arg1) {
//		log.error(arg1);
//		TxDataSourceManager txDataSourceManager = IDMFrameServiceProxy
//				.getTxDataSourceManager();
//
//		if (txDataSourceManager.isAutoManaged()) {
//			txDataSourceManager.rollbackTransaction();
//		}
//	}

	public void onEnd(Activity activity)
	{
		if (activity instanceof BpelProcess)
		{
			BpelProcess process = (BpelProcess) activity;
			if (rootBpelName == null || rootBpelName.length() == 0)
			{
				// commit
				commitTransaction();

				if (process.getThrowable() == null)
				{
					log.info("===============================================================================================");
					log.info(BpelLogger.bpel_ended + summaryData(process, false));
					log.info("===============================================================================================");
				}
				else
				{
					log.info("===============================================================================================");
					log.info(BpelLogger.bpel_ended + summaryData(process, true));
					log.info("===============================================================================================");
				}
			} 
			else //not if (rootBpelName == null || rootBpelName.length() == 0)
			{
				if (!process.getName().equalsIgnoreCase(rootBpelName))
				{
					if (process.getThrowable() == null)
					{
						log.info("===============================================================================================");
						log.info(BpelLogger.bpel_ended + summaryData(process, false));
						log.info("===============================================================================================");
					}
					// Fail
					else
					{
						log.info("===============================================================================================");
						log.info(BpelLogger.bpel_ended + summaryData(process, true));
						log.info("===============================================================================================");
						
					}
				}
				else ////not if (rootBpelName == null || rootBpelName.length() == 0) && if (!process.getName().equalsIgnoreCase(rootBpelName))
				{
					// commit
					commitTransaction();
				}
			}

			/*
			 * else { try { HynixController HynixController =
			 * (HynixController)BaseController.getInstance
			 * ().getContext().getBean("HynixController"); HynixController.release(); } catch
			 * (Exception e) {} }
			 */
		}
		else // Activity not BpelProcess
		{
			if (activity.getName() != null && activity.getName().length() > 0)
			{
				
			}
		}
	}
	
	public void rollbackTransaction() {
		super.rollbackTransaction();
	}

	public void beginTransaction() {
		super.beginTransaction();
	}

	public void commitTransaction() {
		super.commitTransaction();
	}

}
