/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * 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.
 * </p>
 */

package io.shardingsphere.proxy.backend.jdbc.transaction;

import com.google.common.base.Optional;
import io.shardingsphere.core.constant.TCLType;
import io.shardingsphere.transaction.common.TransactionContext;
import io.shardingsphere.transaction.common.TransactionContextHolder;
import io.shardingsphere.transaction.common.event.XaTransactionEvent;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.proxy.config.RuleRegistry;

import javax.transaction.Status;
import java.sql.SQLException;

/**
 * Execute XA transaction intercept.
 *
 * @author zhaojun
 */
public final class XaTransactionEngine extends TransactionEngine {
    
    private static final RuleRegistry RULE_REGISTRY = RuleRegistry.getInstance();
    
    public XaTransactionEngine(final String sql) {
        super(sql);
    }
    
    @Override
    public boolean execute() throws SQLException {
        Optional<TCLType> tclType = parseSQL();
        if (tclType.isPresent() && isInTransaction(tclType.get())) {
            TransactionContextHolder.set(new TransactionContext(RULE_REGISTRY.getTransactionManager(), RULE_REGISTRY.getTransactionType(), XaTransactionEvent.class));
            EventBusInstance.getInstance().post(new XaTransactionEvent(tclType.get(), getSql()));
            return true;
        }
        return false;
    }
    
    private boolean isInTransaction(final TCLType tclType) throws SQLException {
        return TCLType.ROLLBACK != tclType || Status.STATUS_NO_TRANSACTION != RULE_REGISTRY.getTransactionManager().getStatus();
    }
}

@@ -789,45 +789,33 @@
 ere.
-transaction.common.TransactionContext
+proxy.config.RuleRegistry
 ;%0Aim
@@ -849,23 +849,16 @@
 saction.
-common.
 Transact
@@ -860,29 +860,32 @@
 nsaction
-ContextHolder
+EventBusInstance
 ;%0Aimport
@@ -926,16 +926,8 @@
 mon.
-event.Xa
 Tran
@@ -933,21 +933,30 @@
 nsaction
-Event
+ContextFactory
 ;%0Aimport
@@ -978,86 +978,121 @@
 ere.
-core.util.EventBusInstance;%0Aimport io.shardingsphere.proxy.config.RuleRegistry
+transaction.common.TransactionContextHolder;%0Aimport io.shardingsphere.transaction.common.event.XaTransactionEvent
 ;%0A%0Ai
@@ -1685,20 +1685,16 @@
 der.set(
-new 
 Transact
@@ -1703,16 +1703,36 @@
 nContext
+Factory.newXAContext
 (RULE_RE
@@ -1765,70 +1765,8 @@
 er()
-, RULE_REGISTRY.getTransactionType(), XaTransactionEvent.class
 ));%0A
@@ -1777,16 +1777,27 @@
         
+Transaction
 EventBus

refactor XaTransactionEngine using ProxyTransactionConfiguration.