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.ArrayList;
import java.util.List;

/**
 * Created by SMAKINEN on 27.4.2016.
 */
@Oskari
public class RegionSetServiceMybatisImpl extends RegionSetService {
    private static final Logger LOG = LogFactory.getLogger(RegionSetServiceMybatisImpl.class);
    private SqlSessionFactory factory = null;

    public RegionSetServiceMybatisImpl() {
        final DatasourceHelper helper = DatasourceHelper.getInstance();
        final DataSource dataSource = helper.getDataSource(helper.getOskariDataSourceName());
        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() {
        List regions = new ArrayList();

        try (SqlSession session = factory.openSession()) {
            List<RegionSet> layerMetadataRows = session.getMapper(RegionSetMapper.class).getRegionSets();
            for (RegionSet row : layerMetadataRows) {
                regions.add(row);
            }
            LOG.debug("Oskari stat regionset layers: ", regions);
        }
        return regions;
    }
    public RegionSet getRegionSet(long id) {
        try (SqlSession session = factory.openSession()) {
            return session.getMapper(RegionSetMapper.class).getRegionSet(id);
        }
    }
}

@@ -726,87 +726,14 @@
 til.
-Array
 List;%0A
-import java.util.List;%0A%0A/**%0A * Created by SMAKINEN on 27.4.2016.%0A */
 %0A@Os
@@ -801,24 +801,25 @@
 etService %7B%0A
+%0A
     private 
@@ -897,24 +897,25 @@
 mpl.class);%0A
+%0A
     private 
@@ -1014,27 +1014,25 @@
 Data
-s
+S
 ource
-Helper helper
+ dataSource
  = D
@@ -1064,101 +1064,23 @@
 ce()
-;%0A        final DataSource dataSource = helper.getDataSource(helper.getOskariDataSourceName()
+.getDataSource(
 );%0A 
@@ -1088,16 +1088,17 @@
       if
+ 
 (dataSou
@@ -1174,24 +1174,16 @@
        %7D
-%0A       
  else %7B%0A
@@ -1279,24 +1279,25 @@
     %7D%0A    %7D%0A
+%0A
     private 
@@ -1937,49 +1937,8 @@
 ) %7B%0A
-        List regions = new ArrayList();%0A%0A
     
@@ -2008,43 +2008,14 @@
     
-List%3CRegionSet%3E layerMetadataRows =
+return
  ses
@@ -2081,201 +2081,9 @@
     
-    for (RegionSet row : layerMetadataRows) %7B%0A                regions.add(row);%0A            %7D%0A            LOG.debug(%22Oskari stat regionset layers: %22, regions);%0A        %7D%0A        return regions;
+%7D
 %0A   

Refactor RegionSetServiceMybatisImpl