/*
 * 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.rewrite;

import org.apache.shardingsphere.core.optimize.result.insert.InsertOptimizeResult;
import org.apache.shardingsphere.core.optimize.result.insert.InsertOptimizeResultUnit;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Parameter builder.
 *
 * @author panjuan
 */
public final class ParameterBuilder {
    
    private final List<Object> originalParameters;
    
    private final Map<Integer, Object> assistedIndexAndParametersForUpdate;
    
    private final List<InsertParameterUnit> insertParameterUnits;
    
    public ParameterBuilder(final List<Object> parameters, final InsertOptimizeResult insertOptimizeResult) {
        originalParameters = parameters;
        assistedIndexAndParametersForUpdate = new HashMap<>();
        insertParameterUnits = createInsertParameterUnits(insertOptimizeResult);
    }
    
    private List<InsertParameterUnit> createInsertParameterUnits(final InsertOptimizeResult insertOptimizeResult) {
        List<InsertParameterUnit> result = new LinkedList<>();
        if (null == insertOptimizeResult) {
            return result;
        }
        for (InsertOptimizeResultUnit each : insertOptimizeResult.getUnits()) {
            result.add(new InsertParameterUnit(Arrays.asList(each.getParameters()), each.getDataNodes()));
        }
        return result;
    }
}

@@ -845,16 +845,38 @@
 write;%0A%0A
+import lombok.Getter;%0A
 import o
@@ -1036,16 +1036,131 @@
 ultUnit;
+%0Aimport org.apache.shardingsphere.core.route.type.RoutingUnit;%0Aimport org.apache.shardingsphere.core.rule.DataNode;
 %0A%0Aimport
@@ -1277,16 +1277,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0A/**%0A *
@@ -1359,16 +1359,24 @@
 uan%0A */%0A
+@Getter%0A
 public f
@@ -1757,16 +1757,33 @@
 eters = 
+new LinkedList%3C%3E(
 paramete
@@ -1780,24 +1780,25 @@
 %3E(parameters
+)
 ;%0A        as
@@ -2428,10 +2428,1364 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * Get parameters.%0A     * %0A     * @param routingUnit routing unit%0A     * @return parameters%0A     */%0A    public List%3CObject%3E getParameters(final RoutingUnit routingUnit) %7B%0A        List%3CObject%3E result = getInsertParameters(routingUnit);%0A        return result.isEmpty() ? getRevisedParameters() : result;%0A    %7D%0A    %0A    private List%3CObject%3E getInsertParameters(final RoutingUnit routingUnit) %7B%0A        List%3CObject%3E result = new LinkedList%3C%3E();%0A        for (InsertParameterUnit each : insertParameterUnits) %7B%0A            if (isAppendInsertParameter(each, routingUnit)) %7B%0A                result.addAll(each.getParameters());%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A    %0A    private boolean isAppendInsertParameter(final InsertParameterUnit unit, final RoutingUnit routingUnit) %7B%0A        for (DataNode each : unit.getDataNodes()) %7B%0A            if (routingUnit.getTableUnit(each.getDataSourceName(), each.getTableName()).isPresent()) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A    %0A    private List%3CObject%3E getRevisedParameters() %7B%0A        List%3CObject%3E result = new LinkedList%3C%3E(originalParameters);%0A        for (Entry%3CInteger, Object%3E entry : assistedIndexAndParametersForUpdate.entrySet()) %7B%0A            result.add(entry.getKey(), entry.getValue());%0A        %7D%0A        return result;%0A    %7D%0A
 %7D%0A

add getParameters()