/*
 * 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.dbtest.env.authority;

import io.shardingsphere.core.constant.DatabaseType;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * Authority environment manager.
 *
 * @author panjuan
 */
@Slf4j
public final class AuthorityEnvironmentManager {
    
    private final Authority authority;
    
    private final DataSource dataSource;
    
    private final DatabaseType databaseType;
    
    public AuthorityEnvironmentManager(final String path, final DataSource dataSource, final DatabaseType databaseType) throws IOException, JAXBException {
        try (FileReader reader = new FileReader(path)) {
            authority = (Authority) JAXBContext.newInstance(Authority.class).createUnmarshaller().unmarshal(reader);
        }
        this.dataSource = dataSource;
        this.databaseType = databaseType;
    }
    
    /**
     * Initialize data.
     * 
     * @throws SQLException SQL exception
     */
    public void initialize() throws SQLException {
        Collection<String> initSQLs = authority.getInitSQLs(databaseType);
        if (initSQLs.isEmpty()) {
            return;
        }
        try (Connection connection = dataSource.getConnection()) {
            for (String each : initSQLs) {
                try {
                    connection.createStatement().execute(each);
                } catch (final SQLException ex) {
                    log.warn("Init SQL: " + ex.getMessage());
                }
            }    
        }
    }
    
    /**
     * Clean data.
     * 
     * @throws SQLException SQL exception
     */
    public void clean() throws SQLException {
        Collection<String> cleanSQLs = authority.getCleanSQLs(databaseType);
        if (cleanSQLs.isEmpty()) {
            return;
        }
        try (Connection connection = dataSource.getConnection()) {
            for (String each : cleanSQLs) {
                try {
                    connection.createStatement().execute(each);
                } catch (final SQLException ex) {
                    log.warn("Clean SQL: " + ex.getMessage());
                }
            }    
        }
    }
}

@@ -992,16 +992,38 @@
 lection;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -1199,36 +1199,57 @@
 ivate final 
-DataSource d
+Map%3CString, DataSource%3E instanceD
 ataSource;%0A 
@@ -1241,24 +1241,27 @@
 ceDataSource
+Map
 ;%0A    %0A    p
@@ -1373,20 +1373,41 @@
 nal 
-DataSource d
+Map%3CString, DataSource%3E instanceD
 ataS
@@ -1411,16 +1411,19 @@
 taSource
+Map
 , final 
@@ -1686,22 +1686,41 @@
 his.
-dataSource = d
+instanceDataSourceMap = instanceD
 ataS
@@ -1724,16 +1724,19 @@
 taSource
+Map
 ;%0A      
@@ -2074,343 +2074,121 @@
     
-try (Connection connection = dataSource.getConnection()) %7B%0A            for (String each : initSQLs) %7B%0A                try %7B%0A                    connection.createStatement().execute(each);%0A                %7D catch (final SQLException ex) %7B%0A                    log.warn(%22I
+for (DataSource each : instanceDataSourceMap.values()) %7B%0A            executeOnInstanceDataSource(each, i
 nit
- 
 SQL
-: %22 + ex.getMessage());%0A                %7D%0A            %7D    
+s);
 %0A   
@@ -2470,32 +2470,303 @@
 turn;%0A        %7D%0A
+        for (DataSource each : instanceDataSourceMap.values()) %7B%0A            executeOnInstanceDataSource(each, cleanSQLs);%0A        %7D%0A    %7D%0A    %0A    private void executeOnInstanceDataSource(final DataSource dataSource, final Collection%3CString%3E sqls) throws SQLException %7B%0A
         try (Con
@@ -2847,24 +2847,19 @@
  each : 
-cleanSQL
+sql
 s) %7B%0A   
@@ -3021,21 +3021,25 @@
 g.warn(%22
-Clean
+Authority
  SQL: %22 
@@ -3089,20 +3089,16 @@
        %7D
-    
 %0A       

use instanceDataSourceMap.