/*
 * 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.core.yaml.sharding;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingsphere.api.config.rule.TableRuleConfiguration;
import io.shardingsphere.core.keygen.KeyGeneratorFactory;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * Yaml table rule configuration.
 *
 * @author caohao
 * @author panjuan
 */
@NoArgsConstructor
@Getter
@Setter
public class YamlTableRuleConfiguration {
    
    private String logicTable;
    
    private String actualDataNodes;
    
    private YamlShardingStrategyConfiguration databaseStrategy;
    
    private YamlShardingStrategyConfiguration tableStrategy;
    
    private String keyGeneratorColumnName;
    
    private String keyGeneratorClassName;
    
    private String logicIndex;
    
    public YamlTableRuleConfiguration(final TableRuleConfiguration tableRuleConfiguration) {
        logicTable = tableRuleConfiguration.getLogicTable();
        actualDataNodes = tableRuleConfiguration.getActualDataNodes();
        databaseStrategy = new YamlShardingStrategyConfiguration(tableRuleConfiguration.getDatabaseShardingStrategyConfig());
        tableStrategy = new YamlShardingStrategyConfiguration(tableRuleConfiguration.getTableShardingStrategyConfig());
        keyGeneratorColumnName = tableRuleConfiguration.getKeyGeneratorColumnName();
        keyGeneratorClassName = null == tableRuleConfiguration.getKeyGenerator()
                ? null : tableRuleConfiguration.getKeyGenerator().getClass().getName();
    }
    
    /**
     * Build table rule configuration.
     *
     * @return table rule configuration
     */
    public TableRuleConfiguration build() {
        Preconditions.checkNotNull(logicTable, "Logic table cannot be null.");
        TableRuleConfiguration result = new TableRuleConfiguration();
        result.setLogicTable(logicTable);
        result.setActualDataNodes(actualDataNodes);
        if (null != databaseStrategy) {
            result.setDatabaseShardingStrategyConfig(databaseStrategy.build());
        }
        if (null != tableStrategy) {
            result.setTableShardingStrategyConfig(tableStrategy.build());
        }
        if (!Strings.isNullOrEmpty(keyGeneratorClassName)) {
            result.setKeyGenerator(KeyGeneratorFactory.newInstance(keyGeneratorClassName));
        }
        result.setKeyGeneratorColumnName(keyGeneratorColumnName);
        result.setLogicIndex(logicIndex);
        return result;
    }
}

@@ -709,47 +709,8 @@
 ns;%0A
-import com.google.common.base.Strings;%0A
 impo
@@ -807,16 +807,23 @@
 .keygen.
+Builtin
 KeyGener
@@ -826,23 +826,20 @@
 enerator
-Factory
+Type
 ;%0Aimport
@@ -1294,32 +1294,29 @@
     private 
-String k
+YamlK
 eyGeneratorC
@@ -1320,41 +1320,19 @@
 orCo
-lumnName;%0A    %0A    private String
+nfiguration
  key
@@ -1340,25 +1340,16 @@
 enerator
-ClassName
 ;%0A    %0A 
@@ -1878,69 +1878,348 @@
 ator
-ColumnName = tableRuleConfiguration.getKeyGeneratorColumnName
+ = null == tableRuleConfiguration.getKeyGenerator() ? null : getYamlKeyGeneratorConfiguration(tableRuleConfiguration);%0A    %7D%0A    %0A    private YamlKeyGeneratorConfiguration getYamlKeyGeneratorConfiguration(final TableRuleConfiguration tableRuleConfiguration) %7B%0A        YamlKeyGeneratorConfiguration result = new YamlKeyGeneratorConfiguration
 ();%0A
@@ -2218,32 +2218,39 @@
 tion();%0A        
+String 
 keyGeneratorClas
@@ -2256,24 +2256,16 @@
 ssName =
- null ==
  tableRu
@@ -2301,95 +2301,555 @@
 or()
-%0A                ? null : tableRuleConfiguration.getKeyGenerator().getClass().getName()
+.getClass().getName();%0A        BuiltinKeyGeneratorType keyGeneratorType = BuiltinKeyGeneratorType.getBuiltinKeyGeneratorType(keyGeneratorClassName);%0A        if (null == keyGeneratorType) %7B%0A            result.setKeyGeneratorClassName(keyGeneratorClassName);%0A        %7D else %7B%0A            result.setKeyGeneratorType(keyGeneratorType.name());%0A        %7D%0A        result.setKeyGeneratorColumnName(tableRuleConfiguration.getKeyGeneratorColumnName());%0A        result.setProps(tableRuleConfiguration.getKeyGenerator().getProperties());%0A        return result
 ;%0A  
@@ -3501,73 +3501,8 @@
   %7D%0A
-        if (!Strings.isNullOrEmpty(keyGeneratorClassName)) %7B%0A    
     
@@ -3528,17 +3528,17 @@
 nerator(
-K
+k
 eyGenera
@@ -3544,62 +3544,28 @@
 ator
-Factory.newInstance(keyGeneratorClassName));%0A        %7D
+.getKeyGenerator());
 %0A   
@@ -3606,32 +3606,48 @@
 ame(keyGenerator
+.getKeyGenerator
 ColumnName);%0A   
@@ -3636,24 +3636,26 @@
 orColumnName
+()
 );%0A        r

MODIFY build()