/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.shardingsphere.shardingjdbc.spring.namespace.parser;

import com.google.common.base.Strings;
import org.apache.shardingsphere.api.config.encrypt.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.spring.datasource.SpringEncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.spring.namespace.constants.EncryptDataSourceBeanDefinitionParserTag;
import org.apache.shardingsphere.shardingjdbc.spring.namespace.constants.EncryptorRuleBeanDefinitionParserTag;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Encrypt data source parser for spring namespace.
 * 
 * @author panjuan
 */
public final class EncryptDataSourceBeanDefinitionParser extends AbstractBeanDefinitionParser {
    
    @Override
    protected AbstractBeanDefinition parseInternal(final Element element, final ParserContext parserContext) {
        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(SpringEncryptDataSource.class);
        factory.addConstructorArgValue(parseDataSource(element));
        factory.addConstructorArgValue(parseEncryptRuleConfiguration(element));
        factory.addConstructorArgValue(parseProperties(element, parserContext));
        factory.setDestroyMethodName("close");
        return factory.getBeanDefinition();
    }
    
    private RuntimeBeanReference parseDataSource(final Element element) {
        String dataSource = element.getAttribute(EncryptDataSourceBeanDefinitionParserTag.DATA_SOURCE_NAME_TAG);
        return new RuntimeBeanReference(dataSource);
    }
    
    private BeanDefinition parseEncryptRuleConfiguration(final Element element) {
        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptRuleConfiguration.class);
        factory.addConstructorArgValue(parseEncryptorRulesConfiguration(element));
        return factory.getBeanDefinition();
    }
    
    private Map<String, BeanDefinition> parseEncryptorRulesConfiguration(final Element element) {
        List<Element> encryptorRuleElements = DomUtils.getChildElementsByTagName(element, EncryptDataSourceBeanDefinitionParserTag.ENCRYPTOR_RULE_CONFIG_TAG);
        Map<String, BeanDefinition> result = new ManagedMap<>(encryptorRuleElements.size());
        for (Element each : encryptorRuleElements) {
            result.put(each.getAttribute(ID_ATTRIBUTE), parseEncryptorRuleConfiguration(each));
        }
        return result;
    }
    
    private AbstractBeanDefinition parseEncryptorRuleConfiguration(final Element element) {
        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptorRuleConfiguration.class);
        factory.addConstructorArgValue(element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_TYPE_ATTRIBUTE));
        factory.addConstructorArgValue(element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_QUALIFIED_COLUMNS_ATTRIBUTE));
        parseAssistedQueryColumns(element, factory);
        parseProperties(element, factory);
        return factory.getBeanDefinition();
    }
    
    private void parseAssistedQueryColumns(final Element element, final BeanDefinitionBuilder factory) {
        String assistedQueryColumns = element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_ASSISTED_QUERY_COLUMNS_ATTRIBUTE);
        if (!Strings.isNullOrEmpty(assistedQueryColumns)) {
            factory.addConstructorArgValue(assistedQueryColumns);
        }
    }
    
    private void parseProperties(final Element element, final BeanDefinitionBuilder factory) {
        String properties = element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_PROPERTY_REF_ATTRIBUTE);
        if (!Strings.isNullOrEmpty(properties)) {
            factory.addConstructorArgReference(properties);
        } else {
            factory.addConstructorArgValue(new Properties());
        }
    }
    
    private Properties parseProperties(final Element element, final ParserContext parserContext) {
        Element propsElement = DomUtils.getChildElementByTagName(element, EncryptDataSourceBeanDefinitionParserTag.PROPS_TAG);
        return null == propsElement ? new Properties() : parserContext.getDelegate().parsePropsElement(propsElement);
    }
}

@@ -873,205 +873,8 @@
 r;%0A%0A
-import com.google.common.base.Strings;%0Aimport org.apache.shardingsphere.api.config.encrypt.EncryptRuleConfiguration;%0Aimport org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration;%0A
 impo
@@ -1077,183 +1077,8 @@
 ag;%0A
-import org.apache.shardingsphere.shardingjdbc.spring.namespace.constants.EncryptorRuleBeanDefinitionParserTag;%0Aimport org.springframework.beans.factory.config.BeanDefinition;%0A
 impo
@@ -1292,69 +1292,8 @@
 er;%0A
-import org.springframework.beans.factory.support.ManagedMap;%0A
 impo
@@ -1502,53 +1502,8 @@
 t;%0A%0A
-import java.util.List;%0Aimport java.util.Map;%0A
 impo
@@ -2053,52 +2053,75 @@
 rArg
-Value(parseEncryptRuleConfiguration(element)
+Reference(EncryptDataSourceBeanDefinitionParserTag.ENCRYPT_RULE_TAG
 );%0A 
@@ -2557,2326 +2557,8 @@
    %0A
-    private BeanDefinition parseEncryptRuleConfiguration(final Element element) %7B%0A        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptRuleConfiguration.class);%0A        factory.addConstructorArgValue(parseEncryptorRulesConfiguration(element));%0A        return factory.getBeanDefinition();%0A    %7D%0A    %0A    private Map%3CString, BeanDefinition%3E parseEncryptorRulesConfiguration(final Element element) %7B%0A        List%3CElement%3E encryptorRuleElements = DomUtils.getChildElementsByTagName(element, EncryptDataSourceBeanDefinitionParserTag.ENCRYPTOR_RULE_CONFIG_TAG);%0A        Map%3CString, BeanDefinition%3E result = new ManagedMap%3C%3E(encryptorRuleElements.size());%0A        for (Element each : encryptorRuleElements) %7B%0A            result.put(each.getAttribute(ID_ATTRIBUTE), parseEncryptorRuleConfiguration(each));%0A        %7D%0A        return result;%0A    %7D%0A    %0A    private AbstractBeanDefinition parseEncryptorRuleConfiguration(final Element element) %7B%0A        BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(EncryptorRuleConfiguration.class);%0A        factory.addConstructorArgValue(element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_TYPE_ATTRIBUTE));%0A        factory.addConstructorArgValue(element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_QUALIFIED_COLUMNS_ATTRIBUTE));%0A        parseAssistedQueryColumns(element, factory);%0A        parseProperties(element, factory);%0A        return factory.getBeanDefinition();%0A    %7D%0A    %0A    private void parseAssistedQueryColumns(final Element element, final BeanDefinitionBuilder factory) %7B%0A        String assistedQueryColumns = element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_ASSISTED_QUERY_COLUMNS_ATTRIBUTE);%0A        if (!Strings.isNullOrEmpty(assistedQueryColumns)) %7B%0A            factory.addConstructorArgValue(assistedQueryColumns);%0A        %7D%0A    %7D%0A    %0A    private void parseProperties(final Element element, final BeanDefinitionBuilder factory) %7B%0A        String properties = element.getAttribute(EncryptorRuleBeanDefinitionParserTag.ENCRYPTOR_PROPERTY_REF_ATTRIBUTE);%0A        if (!Strings.isNullOrEmpty(properties)) %7B%0A            factory.addConstructorArgReference(properties);%0A        %7D else %7B%0A            factory.addConstructorArgValue(new Properties());%0A        %7D%0A    %7D%0A    %0A
     

use addConstructorArgReference