package fi.nls.oskari.map.analysis.service;

import fi.nls.oskari.db.DatasourceHelper;
import fi.nls.oskari.domain.map.analysis.AnalysisStyle;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.mybatis.MyBatisHelper;
import fi.nls.oskari.service.ServiceException;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import javax.sql.DataSource;

public class AnalysisStyleDbServiceMybatisImpl implements AnalysisStyleDbService {

    private static final Logger LOG = LogFactory.getLogger(AnalysisStyleDbServiceMybatisImpl.class);
    private static final Class<AnalysisStyleMapper> MAPPER_CLASS = AnalysisStyleMapper.class;

    private SqlSessionFactory factory = null;

    public AnalysisStyleDbServiceMybatisImpl() {
        // Try to load analysis datasource
        DatasourceHelper helper = DatasourceHelper.getInstance();
        String prefix = AnalysisDbServiceMybatisImpl.DATASOURCE_ANALYSIS;
        String dataSourceName = helper.getOskariDataSourceName(prefix);
        DataSource dataSource = helper.getDataSource(dataSourceName);
        if (dataSource == null) {
            // If we can't find analysis datasource use default datasource
            dataSource = helper.getDataSource();
        }
        if (dataSource == null) {
            LOG.error("Failed to start, could not get datasource");
        } else {
            factory = MyBatisHelper.initMyBatis(dataSource, MAPPER_CLASS);
        }
    }

    /**
     * Insert analysis style to the database
     * @return id of the row inserted
     * @throws ServiceException if service is not initialized properly
     *         or if something goes wrong inserting the analysis style
     */
    public long insertAnalysisStyleRow(AnalysisStyle analysisStyle)
            throws ServiceException {
        if (factory == null) {
            throw new ServiceException("Service not initialized");
        }
        try (SqlSession session = factory.openSession()) {
            session.getMapper(MAPPER_CLASS).insertAnalysisStyleRow(analysisStyle);
            session.commit();
            LOG.debug("Inserted analysis style - id", analysisStyle.getId());
            return analysisStyle.getId();
        } catch (Exception e) {
            LOG.warn(e, "Failed to insert analysis style", analysisStyle);
            throw new ServiceException("Failed to insert analysis style", e);
        }
    }

}

@@ -327,79 +327,356 @@
 tis.
-session.SqlSession;%0Aimport org.apache.ibatis.session.SqlSessionFactory;
+mapping.Environment;%0Aimport org.apache.ibatis.session.Configuration;%0Aimport org.apache.ibatis.session.SqlSession;%0Aimport org.apache.ibatis.session.SqlSessionFactory;%0Aimport org.apache.ibatis.session.SqlSessionFactoryBuilder;%0Aimport org.apache.ibatis.transaction.TransactionFactory;%0Aimport org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;%0A
 %0Aimp
@@ -890,102 +890,8 @@
 ss);
-%0A    private static final Class%3CAnalysisStyleMapper%3E MAPPER_CLASS = AnalysisStyleMapper.class;
 %0A%0A  
@@ -1347,83 +1347,8 @@
 ) %7B%0A
-            // If we can't find analysis datasource use default datasource%0A
     
@@ -1367,35 +1367,38 @@
 Source = helper.
-get
+create
 DataSource();%0A  
@@ -1550,69 +1550,601 @@
 y = 
-MyBatisHelper.initMyBatis(dataSource, MAPPER_CLASS
+initializeMyBatis(dataSource);%0A        %7D%0A    %7D%0A%0A    private SqlSessionFactory initializeMyBatis(final DataSource dataSource) %7B%0A        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(AnalysisStyle.class);%0A        configuration.setLazyLoadingEnabled(true);%0A        configuration.addMapper(AnalysisStyleMapper.class
 );%0A
+%0A
         
 %7D%0A  
@@ -2131,33 +2131,91 @@
 lass);%0A%0A        
-%7D
+return new SqlSessionFactoryBuilder().build(configuration);
 %0A    %7D%0A%0A    /**%0A
@@ -2754,20 +2754,33 @@
 per(
-MAPPER_CLASS
+AnalysisStyleMapper.class
 ).in

Undo these changes