/*
* Copyright (C) 2008-2017, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.juick.configuration;
import com.juick.service.search.SearchService;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import java.util.Collections;
import java.util.List;
/**
* Created by aalexeev on 11/11/16.
*/
@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:juick.conf"})
@ComponentScan(basePackages = {"com.juick.service"})
public class DataConfiguration implements TransactionManagementConfigurer {
@Value("${datasource_driver:com.mysql.jdbc.Driver}")
private String datasourceDriver;
@Value("${datasource_url:}")
private String datasourceUrl;
@Value("${datasource_user:}")
private String datasourceUser;
@Value("${datasource_password:}")
private String datasourcePassword;
// NOTE: The close() method will be called automatically with default @Bean settings
// But Datasource interface has no close() method
@Bean
public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(datasourceDriver);
dataSource.setUrl(datasourceUrl);
dataSource.setUsername(datasourceUser);
dataSource.setPassword(datasourcePassword);
dataSource.setValidationQuery("select 1");
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return transactionManager();
}
@Bean
@DependsOn("dataSource")
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public SearchService emptySearchService() {
return new SearchService() {
@Override
public void setMaxResult(int maxResult) {
}
@Override
public List searchInAllMessages(String searchString, int messageIdBefore) {
return Collections.emptyList();
}
@Override
public List searchByStringAndUser(String searchString, int userId, int messageIdBefore) {
return Collections.emptyList();
}
};
}
}