/*
 * 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.core.strategy.encrypt;

import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.api.config.encryptor.EncryptorRuleConfiguration;
import org.apache.shardingsphere.core.exception.ShardingConfigurationException;
import org.apache.shardingsphere.core.rule.ColumnNode;
import org.apache.shardingsphere.core.spi.algorithm.encrypt.ShardingEncryptorServiceLoader;
import org.apache.shardingsphere.spi.encrypt.ShardingEncryptor;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Sharding encryptor strategy.
 *
 * @author panjuan
 */
@Getter
public final class ShardingEncryptorStrategy {
    
    private final List<ColumnNode> columns;
    
    private final List<ColumnNode> assistedQueryColumns;
    
    private final ShardingEncryptor shardingEncryptor;
    
    public ShardingEncryptorStrategy(final EncryptorRuleConfiguration config) {
        this.columns = Splitter.on(",").trimResults().splitToList(config.getQualifiedColumns());
        this.assistedQueryColumns = Strings.isNullOrEmpty(config.getAssistedQueryColumns())
                ? Collections.<String>emptyList() : Splitter.on(",").trimResults().splitToList(config.getAssistedQueryColumns());
        checkEncryptorConfiguration(columns, assistedQueryColumns);
        shardingEncryptor = new ShardingEncryptorServiceLoader().newService(config.getType(), config.getProperties());
        shardingEncryptor.init();
    }
    
    private List<ColumnNode> createColumnNodes(final String columnNodeStr) {
        List<ColumnNode> result = new LinkedList<>();
        for (String each : Splitter.on(",").trimResults().splitToList(columnNodeStr)) {
            result.add(new ColumnNode(each));
        }
        return result;
    }
    
    private void checkEncryptorConfiguration(final List<String> columns, final List<String> assistedQueryColumns) {
        if (!assistedQueryColumns.isEmpty() && assistedQueryColumns.size() != columns.size()) {
            throw new ShardingConfigurationException("The size of `columns` and `assistedQueryColumns` is not same.");
        }
    }
    
    /**
     * Get assisted query column.
     * @param column column
     * @return assisted query column
     */
    public Optional<String> getAssistedQueryColumn(final String column) {
        return columns.contains(column) ? Optional.of(assistedQueryColumns.get(columns.indexOf(column))) : Optional.<String>absent();
    }
}

@@ -894,16 +894,102 @@
 tional;%0A
+import com.google.common.base.Preconditions;%0Aimport com.google.common.base.Predicate;%0A
 import c
@@ -1016,24 +1016,24 @@
 e.Splitter;%0A
-
 import com.g
@@ -1938,50 +1938,25 @@
 s = 
-Splitter.on(%22,%22).trimResults().splitToList
+createColumnNodes
 (con
@@ -2078,24 +2078,8 @@
 s())
-%0A               
  ? C
@@ -2090,22 +2090,26 @@
 ctions.%3C
-String
+ColumnNode
 %3EemptyLi
@@ -2119,50 +2119,25 @@
 ) : 
-Splitter.on(%22,%22).trimResults().splitToList
+createColumnNodes
 (con
@@ -2180,45 +2180,145 @@
     
-checkEncryptorConfiguration(columns, 
+Preconditions.checkArgument(assistedQueryColumns.isEmpty() %7C%7C assistedQueryColumns.size() == columns.size(), %22The size of %60columns%60 and %60
 assi
@@ -2333,16 +2333,31 @@
 yColumns
+%60 is not same.%22
 );%0A     
@@ -2786,24 +2786,24 @@
 ;%0A        %7D%0A
+
         retu
@@ -2828,360 +2828,8 @@
    %0A
-    private void checkEncryptorConfiguration(final List%3CString%3E columns, final List%3CString%3E assistedQueryColumns) %7B%0A        if (!assistedQueryColumns.isEmpty() && assistedQueryColumns.size() != columns.size()) %7B%0A            throw new ShardingConfigurationException(%22The size of %60columns%60 and %60assistedQueryColumns%60 is not same.%22);%0A        %7D%0A    %7D%0A    %0A
     

delete checkEncryptorConfiguration()