/*
 * 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.orchestration.internal.registry.config.listener;

import io.shardingsphere.orchestration.internal.registry.config.event.MasterSlaveRuleChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.event.ShardingRuleChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.node.ConfigurationNode;
import io.shardingsphere.orchestration.internal.registry.config.service.ConfigurationService;
import io.shardingsphere.orchestration.internal.registry.listener.PostShardingOrchestrationEventListener;
import io.shardingsphere.orchestration.internal.registry.listener.ShardingOrchestrationEvent;
import io.shardingsphere.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.orchestration.reg.listener.DataChangedEvent;

/**
 * Rule changed listener.
 *
 * @author caohao
 * @author panjuan
 */
public final class RuleChangedListener extends PostShardingOrchestrationEventListener {
    
    private final String shardingSchemaName;
    
    private final ConfigurationService configService;
    
    public RuleChangedListener(final String name, final RegistryCenter regCenter, final String shardingSchemaName) {
        super(regCenter, new ConfigurationNode(name).getRulePath(shardingSchemaName));
        this.shardingSchemaName = shardingSchemaName;
        configService = new ConfigurationService(name, regCenter);
    }
    
    @Override
    protected ShardingOrchestrationEvent createShardingOrchestrationEvent(final DataChangedEvent event) {
        return configService.isShardingRule(shardingSchemaName) ? getShardingConfigurationChangedEvent() : getMasterSlaveConfigurationChangedEvent();
    }
    
    private ShardingRuleChangedEvent getShardingConfigurationChangedEvent() {
        return new ShardingRuleChangedEvent(shardingSchemaName, configService.loadShardingRuleConfiguration(shardingSchemaName));
    }
    
    private MasterSlaveRuleChangedEvent getMasterSlaveConfigurationChangedEvent() {
        return new MasterSlaveRuleChangedEvent(shardingSchemaName, configService.loadMasterSlaveRuleConfiguration(shardingSchemaName));
    }
}

@@ -976,102 +976,8 @@
 de;%0A
-import io.shardingsphere.orchestration.internal.registry.config.service.ConfigurationService;%0A
 impo
@@ -1304,16 +1304,88 @@
 edEvent;
+%0Aimport io.shardingsphere.orchestration.yaml.ConfigurationYamlConverter;
 %0A%0A/**%0A *
@@ -1599,67 +1599,8 @@
    %0A
-    private final ConfigurationService configService;%0A    %0A
     
@@ -1857,75 +1857,8 @@
 me;%0A
-        configService = new ConfigurationService(name, regCenter);%0A
     
@@ -2003,55 +2003,45 @@
 urn 
-configService.isShardingRule(shardingSchemaName
+event.getValue().contains(%22tables:%5Cn%22
 ) ? 
@@ -2077,16 +2077,21 @@
 edEvent(
+event
 ) : getM
@@ -2126,16 +2126,21 @@
 edEvent(
+event
 );%0A    %7D
@@ -2211,32 +2211,60 @@
 ionChangedEvent(
+final DataChangedEvent event
 ) %7B%0A        retu
@@ -2311,37 +2311,50 @@
 SchemaName, 
-c
+C
 onfig
-Service
+urationYamlConverter
 .loadShardin
@@ -2372,34 +2372,32 @@
 uration(
-shardingSchemaName
+event.getValue()
 ));%0A    
@@ -2483,16 +2483,44 @@
 edEvent(
+final DataChangedEvent event
 ) %7B%0A    
@@ -2586,21 +2586,34 @@
 me, 
-c
+C
 onfig
-Service
+urationYamlConverter
 .loa
@@ -2642,34 +2642,32 @@
 uration(
-shardingSchemaName
+event.getValue()
 ));%0A    

modify RuleChangedListener