/*
 * 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.Preconditions;
import com.google.common.base.Predicate;
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 = createColumnNodes(config.getQualifiedColumns());
        this.assistedQueryColumns = Strings.isNullOrEmpty(config.getAssistedQueryColumns()) ? Collections.<ColumnNode>emptyList() : createColumnNodes(config.getAssistedQueryColumns());
        Preconditions.checkArgument(assistedQueryColumns.isEmpty() || assistedQueryColumns.size() == columns.size(), "The size of `columns` and `assistedQueryColumns` is not same.");
        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;
    }
    
    /**
     * 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();
    }
}

@@ -1059,16 +1059,63 @@
 trings;%0A
+import com.google.common.collect.Collections2;%0A
 import l
@@ -1214,88 +1214,8 @@
 on;%0A
-import org.apache.shardingsphere.core.exception.ShardingConfigurationException;%0A
 impo
@@ -2814,30 +2814,80 @@
 Get 
-assisted query column.
+sharding encryptor.%0A     *%0A     * @param logicTableName logic table name
 %0A   
@@ -2903,23 +2903,32 @@
 m column
+Name
  column
+ name
 %0A     * 
@@ -2939,89 +2939,136 @@
 urn 
-assisted query column%0A     */%0A    public Optional%3CString%3E getAssistedQueryColumn(
+optional of sharding encryptor%0A     */%0A    public Optional%3CShardingEncryptor%3E getShardingEncryptor(final String logicTableName, 
 fina
@@ -3082,16 +3082,20 @@
 g column
+Name
 ) %7B%0A    
@@ -3109,99 +3109,278 @@
 urn 
-columns.contains(column) ? Optional.of(assistedQueryColumns.get(columns.indexOf(column))) :
+Collections2.filter(columns, new Predicate%3CColumnNode%3E() %7B%0A            %0A            @Override%0A            public boolean apply(final ColumnNode input) %7B%0A                return input.equals(new ColumnNode(logicTableName, columnName));%0A            %7D%0A        %7D).isEmpty() ?
  Opt
@@ -3391,21 +3391,65 @@
 l.%3CS
-tring%3Eabsent(
+hardingEncryptor%3Eabsent() : Optional.of(shardingEncryptor
 );%0A 

add getShardingEncryptor()