Commit dbbe0f91 authored by liya.cookie's avatar liya.cookie
Browse files

Refactor

parent 5aeca5e3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -19,5 +19,5 @@ package org.apache.shardingsphere.example.common.repository;

import org.apache.shardingsphere.example.common.entity.Country;

public interface CountryRepository extends CommonRepository<Country> {
public interface CountryRepository extends CommonRepository<Country,String> {
}
+5 −5
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public class CountryRepositroyImpl implements CountryRepository {
    }

    @Override
    public Long insert(final Country country) {
    public String insert(final Country country) {
        String sql = "INSERT INTO t_country (name, code, language) VALUES (?, ?, ?)";
        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
@@ -82,15 +82,15 @@ public class CountryRepositroyImpl implements CountryRepository {
            }
        } catch (final SQLException ignored) {
        }
        return country.getId();
        return country.getCode();
    }

    @Override
    public void delete(final Long id) {
        String sql = "DELETE FROM t_country WHERE id =?";
    public void delete(final String code) {
        String sql = "DELETE FROM t_country WHERE code =?";
        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setLong(1, id);
            preparedStatement.setString(1, code);
            preparedStatement.executeUpdate();
        } catch (final SQLException ignored) {
        }
+4 −73
Original line number Diff line number Diff line
@@ -17,17 +17,14 @@

package org.apache.shardingsphere.example.common.jdbc.service;

import org.apache.shardingsphere.example.common.entity.Country;
import org.apache.shardingsphere.example.common.entity.Order;
import org.apache.shardingsphere.example.common.entity.OrderItem;
import org.apache.shardingsphere.example.common.repository.CountryRepository;
import org.apache.shardingsphere.example.common.repository.OrderItemRepository;
import org.apache.shardingsphere.example.common.repository.OrderRepository;
import org.apache.shardingsphere.example.common.service.CommonService;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public final class CommonServiceImpl implements CommonService {

@@ -35,14 +32,6 @@ public final class CommonServiceImpl implements CommonService {

    private OrderItemRepository orderItemRepository;

    private CountryRepository countryRepository;

    public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository, final CountryRepository countryRepository) {
        this.orderRepository = orderRepository;
        this.orderItemRepository = orderItemRepository;
        this.countryRepository = countryRepository;
    }

    public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository) {
        this.orderRepository = orderRepository;
        this.orderItemRepository = orderItemRepository;
@@ -52,25 +41,22 @@ public final class CommonServiceImpl implements CommonService {
    public void initEnvironment() {
        orderRepository.createTableIfNotExists();
        orderItemRepository.createTableIfNotExists();
        countryRepository.createTableIfNotExists();
        orderRepository.truncateTable();
        orderItemRepository.truncateTable();
        countryRepository.truncateTable();
    }

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

    @Override
    public void processSuccess() {
        System.out.println("-------------- Process Success Begin ---------------");
        InsertResult insertResult = insertData();
        List<Long> orderIds = insertData();
        printData();
        deleteData(insertResult.getOrderIds(), insertResult.getCountryIds());
        deleteData(orderIds);
        printData();
        System.out.println("-------------- Process Success Finish --------------");
    }
@@ -83,14 +69,8 @@ public final class CommonServiceImpl implements CommonService {
        throw new RuntimeException("Exception occur for transaction test.");
    }

    private InsertResult insertData() {
    private List<Long> insertData() {
        System.out.println("---------------------------- Insert Data ----------------------------");
        List<Long> orderIds = insertOrderData();
        List<Long> countryIds = insertCountryData();
        return new InsertResult(orderIds, countryIds);
    }

    private List<Long> insertOrderData() {
        List<Long> result = new ArrayList<>(10);
        for (int i = 1; i <= 10; i++) {
            Order order = new Order();
@@ -107,37 +87,12 @@ public final class CommonServiceImpl implements CommonService {
        return result;
    }

    private List<Long> insertCountryData() {
        List<Long> 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.setName(l.getDisplayCountry(l));
            currCountry.setLanguage(l.getLanguage());
            currCountry.setCode(l.getCountry());
            countryRepository.insert(currCountry);
            result.add(currCountry.getId());
            if (++i == 10) {
                break;
            }
        }
        return result;
    }

    private void deleteData(final List<Long> orderIds, final List<Long> countryIds) {
    private void deleteData(final List<Long> orderIds) {
        System.out.println("---------------------------- Delete Data ----------------------------");
        for (Long each : orderIds) {
            orderRepository.delete(each);
            orderItemRepository.delete(each);
        }
        for (Long each: countryIds) {
            countryRepository.delete(each);
        }
    }

    @Override
@@ -150,29 +105,5 @@ public final class CommonServiceImpl implements CommonService {
        for (Object each : orderItemRepository.selectAll()) {
            System.out.println(each);
        }
        System.out.println("---------------------------- Print Country Data -------------------");
        for (Object each : countryRepository.selectAll()) {
            System.out.println(each);
        }
    }

    private static class InsertResult {

        private List<Long> orderIds;

        private List<Long> countryIds;

        InsertResult(final List<Long> orderIds, final List<Long> countryIds) {
            this.orderIds = orderIds;
            this.countryIds = countryIds;
        }

        public List<Long> getOrderIds() {
            return orderIds;
        }

        public List<Long> getCountryIds() {
            return countryIds;
        }
    }
}
+102 −0
Original line number Diff line number Diff line
/*
 * 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.jdbc.service;

import org.apache.shardingsphere.example.common.entity.Country;
import org.apache.shardingsphere.example.common.repository.CountryRepository;
import org.apache.shardingsphere.example.common.service.CommonService;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CountryServiceImpl implements CommonService {

    private final CountryRepository countryRepository;

    public CountryServiceImpl(CountryRepository countryRepository) {
        this.countryRepository = countryRepository;
    }

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

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

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

    @Override
    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 ----------------------------");
        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.setName(l.getDisplayCountry(l));
            currCountry.setLanguage(l.getLanguage());
            currCountry.setCode(l.getCountry());
            countryRepository.insert(currCountry);
            result.add(currCountry.getCode());
            if (++i == 10) {
                break;
            }
        }
        return result;
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import org.apache.shardingsphere.example.common.entity.Country;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@@ -30,6 +32,7 @@ public class CountryEntity extends Country {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Override
    public long getId() {
        return super.getId();
Loading