/*
 * 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.example.common.mybatis.service;

import org.apache.shardingsphere.example.common.entity.Country;
import org.apache.shardingsphere.example.common.mybatis.repository.MybatisCountryRepository;
import org.apache.shardingsphere.example.common.repository.CountryRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class SpringCountryServiceImpl implements SpringCountryService {

    @Resource
    private CountryRepository countryRepository;

    @Override
    public void initEnvironment() {
        countryRepository.createTableIfNotExists();
        countryRepository.truncateTable();
    }

    @Override
    public void cleanEnvironment() {
        countryRepository.dropTable();
    }

    @Override
    @Transactional
    public void processSuccess() {
        System.out.println("-------------- Process Success Begin ---------------");
        List<String> countryCodes = insertData();
        printData();
        deleteData(countryCodes);
        printData();
        System.out.println("-------------- Process Success Finish --------------");
    }

    @Override
    @Transactional
    public void processFailure() {
        System.out.println("-------------- Process Failure Begin ---------------");
        insertData();
        System.out.println("-------------- Process Failure Finish --------------");
        throw new RuntimeException("Exception occur for transaction test.");
    }

    @Override
    public void printData() {
        System.out.println("---------------------------- Print Country Data -------------------");
        for (Object each : countryRepository.selectAll()) {
            System.out.println(each);
        }
    }

    private void deleteData(final List<String> countryCodes) {
        System.out.println("---------------------------- Delete Data ----------------------------");
        for (String each: countryCodes) {
            countryRepository.delete(each);
        }
    }

    private List<String> insertData() {
        System.out.println("---------------------------- Insert Data ----------------------------");
        MybatisCountryRepository repository = (MybatisCountryRepository) countryRepository;
        List<String> result = new ArrayList<>();
        Locale[] locales = Locale.getAvailableLocales();
        int i = 0;
        for (Locale l:locales) {
            final String country = l.getCountry();
            if (country == null || "".equals(country)) {
                continue;
            }
            Country currCountry = new Country();
            currCountry.setCode(l.getCountry());
            currCountry.setName(l.getDisplayCountry(l));
            currCountry.setLanguage(l.getLanguage());
            repository.insert(currCountry);
            result.add(currCountry.getCode());
            if (++i == 10) {
                break;
            }
        }
        return result;
    }
}

@@ -3247,26 +3247,26 @@
     
+for (
 Locale
-%5B%5D locales =
+ each :
  Loc
@@ -3294,58 +3294,8 @@
 es()
-;%0A        int i = 0;%0A        for (Locale l:locales
 ) %7B%0A
@@ -3329,17 +3329,20 @@
 untry = 
-l
+each
 .getCoun
@@ -3368,23 +3368,23 @@
 if (
-country == null
+null == country
  %7C%7C 
@@ -3465,26 +3465,21 @@
 Country 
-currCountr
+entit
 y = new 
@@ -3497,34 +3497,29 @@
             
-currCountr
+entit
 y.setCode(l.
@@ -3516,17 +3516,20 @@
 setCode(
-l
+each
 .getCoun
@@ -3544,34 +3544,29 @@
             
-currCountr
+entit
 y.setName(l.
@@ -3563,17 +3563,20 @@
 setName(
-l
+each
 .getDisp
@@ -3586,17 +3586,20 @@
 Country(
-l
+each
 ));%0A    
@@ -3606,26 +3606,21 @@
         
-currCountr
+entit
 y.setLan
@@ -3625,17 +3625,20 @@
 anguage(
-l
+each
 .getLang
@@ -3676,26 +3676,21 @@
 .insert(
-currCountr
+entit
 y);%0A    
@@ -3712,18 +3712,13 @@
 add(
-currCountr
+entit
 y.ge
@@ -3731,74 +3731,8 @@
 ));%0A
-            if (++i == 10) %7B%0A                break;%0A            %7D%0A
     

refactor SpringCountryServiceImpl.