com.econcept.init.DAOConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.econcept.init.DAOConfig.java

Source

/**
 * 
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 Kai-Ting (Danil) Ko
 * 
 * Permission is hereby granted, free of charge, 
 * to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the 
 * following conditions:
 * 
 * The above copyright notice and this permission notice 
 * shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY 
 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

package com.econcept.init;

import javax.inject.Inject;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.econcept.dao.UserAccountDAO;
import com.econcept.dao.impl.base.UserAccountDAOBaseImpl;
import com.econcept.entity.UserAccount;
import com.econcept.entity.UserAuthority;
import com.econcept.entity.UserRole;
import com.econcept.webservice.rest.UserResource;
import com.jolbox.bonecp.BoneCPDataSource;

@Configuration
@PropertySource(value = "classpath:application.properties")
public class DAOConfig {
    // Inject PropertySource into environment instance
    @Inject
    private Environment mEnvironment;

    @Bean
    public UserResource getAccountService() {
        return new UserResource();
    } // AccountService getAccountService

    @Bean
    public UserAccountDAO getUserDAO() {
        return new UserAccountDAOBaseImpl();
    } // AccountDAO getUserDAO

    @Bean
    public UserAccount getUser() {
        return new UserAccount();
    } // Account getUser

    @Bean
    public UserAuthority getUserAuthority() {
        return new UserAuthority();
    } // Account getUser

    @Bean
    public UserRole getUserRole() {
        return new UserRole();
    } // Account getUser

    @Bean
    public JpaVendorAdapter getJpaVendorAdapter() {
        JpaVendorAdapter lVendorAdapter = new OpenJpaVendorAdapter();

        return lVendorAdapter;
    } // JpaVendorAdapter getJpaVendorAdapter

    @Bean
    public LocalContainerEntityManagerFactoryBean getEntityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean lBean = new LocalContainerEntityManagerFactoryBean();

        lBean.setJpaVendorAdapter(getJpaVendorAdapter());

        lBean.setDataSource(getDataSource());

        lBean.setPackagesToScan(new String[] { "com.econcept.dao", "com.econcept.entities" });

        lBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());

        return lBean;
    } // LocalContainerEntityManagerFactoryBean getEntityManagerFactoryBean

    @Bean
    public PlatformTransactionManager getTransactionManager() {
        JpaTransactionManager lTransactionManager = new JpaTransactionManager();
        lTransactionManager.setEntityManagerFactory(getEntityManagerFactoryBean().getObject());
        return lTransactionManager;
    } // 

    @Bean
    public DataSource getDataSource() {
        BoneCPDataSource lDataSource = new BoneCPDataSource();
        lDataSource.setDriverClass(mEnvironment.getProperty("jdbc.driverClassName"));
        lDataSource.setJdbcUrl(mEnvironment.getProperty("jdbc.url"));
        lDataSource.setUsername(mEnvironment.getProperty("jdbc.username"));
        lDataSource.setPassword(mEnvironment.getProperty("jdbc.password"));
        lDataSource.setPassword(mEnvironment.getProperty("jdbc.password"));
        lDataSource.setMaxConnectionsPerPartition(
                Integer.parseInt(mEnvironment.getProperty("jdbc.maxConnectionsPerPartition")));
        lDataSource.setMinConnectionsPerPartition(
                Integer.parseInt(mEnvironment.getProperty("jdbc.minConnectionsPerPartition")));

        return lDataSource;
    } // DriverManagerDataSource getDataSource

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    } // PersistenceExceptionTranslationPostProcessor exceptionTranslation
} // DAOConfig