package fi.nls.oskari.control.statistics;

import fi.nls.oskari.annotation.Oskari;
import fi.nls.oskari.control.statistics.db.RegionSet;
import fi.nls.oskari.control.statistics.db.RegionSetMapper;
import fi.nls.oskari.db.DatasourceHelper;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.util.List;

@Oskari
public class RegionSetServiceMybatisImpl extends RegionSetService {

    private static final Logger LOG = LogFactory.getLogger(RegionSetServiceMybatisImpl.class);

    private SqlSessionFactory factory = null;

    public RegionSetServiceMybatisImpl() {
        final DataSource dataSource = DatasourceHelper.getInstance().getDataSource();
        if (dataSource != null) {
            factory = initializeMyBatis(dataSource);
        } else {
            LOG.error("Couldn't get datasource for statistical regionsets service");
        }
    }

    private SqlSessionFactory initializeMyBatis(final DataSource dataSource) {
        final TransactionFactory transactionFactory = new JdbcTransactionFactory();
        final Environment environment = new Environment("development", transactionFactory, dataSource);

        final Configuration configuration = new Configuration(environment);
        configuration.getTypeAliasRegistry().registerAlias(RegionSet.class);
        configuration.setLazyLoadingEnabled(true);
        configuration.addMapper(RegionSetMapper.class);

        return new SqlSessionFactoryBuilder().build(configuration);
    }

    public List<RegionSet> getRegionSets() {
        try (SqlSession session = factory.openSession()) {
            return session.getMapper(RegionSetMapper.class).getRegionSets();
        }
    }
    public RegionSet getRegionSet(long id) {
        try (SqlSession session = factory.openSession()) {
            return session.getMapper(RegionSetMapper.class).getRegionSet(id);
        }
    }
}

@@ -313,47 +313,46 @@
 ort 
-org.apache.ibatis.mapping.Environment
+fi.nls.oskari.mybatis.MyBatisHelper
 ;%0A
+%0A
 impo
@@ -495,190 +495,8 @@
 ory;
-%0Aimport org.apache.ibatis.session.SqlSessionFactoryBuilder;%0Aimport org.apache.ibatis.transaction.TransactionFactory;%0Aimport org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
 %0A%0Aim
@@ -1192,324 +1192,74 @@
     
-final TransactionFactory transactionFactory = new JdbcTransactionFactory();%0A        final Environment environment = new Environment(%22development%22, transactionFactory, dataSource);%0A%0A        final Configuration configuration = new Configuration(environment);%0A        configuration.getTypeAliasRegistry().registerAlias(
+Configuration configuration = MyBatisHelper.getConfig(dataSource, 
 Regi
@@ -1263,16 +1263,22 @@
 egionSet
+Mapper
 .class);
@@ -1290,59 +1290,33 @@
     
-configuration.setLazyLoadingEnabled(true);%0A        
+MyBatisHelper.addAliases(
 conf
@@ -1328,19 +1328,10 @@
 tion
-.addMapper(
+, 
 Regi
@@ -1327,38 +1327,32 @@
 ation, RegionSet
-Mapper
 .class);%0A%0A      
@@ -1340,25 +1340,24 @@
 Set.class);%0A
-%0A
         retu
@@ -1363,38 +1363,21 @@
 urn 
-new SqlSessionFactoryBuilder()
+MyBatisHelper
 .bui

Use MyBatisHelper methods to initialize the SqlSessionFactory