Done ticket GEPAFINBE-3
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package net.gepafin.tendermanagement.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
|
||||
@Configuration
|
||||
public class MessageSourceConfig {
|
||||
@Bean(name = "defaultMessageSource")
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasenames("message");
|
||||
messageSource.setDefaultEncoding("UTF-8");
|
||||
messageSource.setUseCodeAsDefaultMessage(true);
|
||||
return messageSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package net.gepafin.tendermanagement.config;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import liquibase.change.DatabaseChange;
|
||||
import liquibase.integration.spring.SpringLiquibase;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ConditionalOnClass({ SpringLiquibase.class, DatabaseChange.class })
|
||||
@ConditionalOnProperty(prefix = "spring.liquibase", name = "enabled", matchIfMissing = true)
|
||||
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
|
||||
@Import({SchemaInit.SpringLiquibaseDependsOnPostProcessor.class})
|
||||
public class SchemaInit {
|
||||
|
||||
@Component
|
||||
@ConditionalOnProperty(prefix = "spring.liquibase", name = "enabled", matchIfMissing = true)
|
||||
public static class SchemaInitBean implements InitializingBean {
|
||||
|
||||
private final DataSource dataSource;
|
||||
private final String schemaName;
|
||||
|
||||
@Autowired
|
||||
public SchemaInitBean(DataSource dataSource, @Value("${spring.liquibase.default-schema}") String schemaName) {
|
||||
this.dataSource = dataSource;
|
||||
this.schemaName = schemaName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
try (Connection conn = dataSource.getConnection();
|
||||
Statement statement = conn.createStatement()) {
|
||||
log.info("Going to create DB schema '{}' if not exists.", schemaName);
|
||||
String query = "create schema if not exists " + schemaName;
|
||||
statement.execute(query);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed to create schema '" + schemaName + "'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ConditionalOnBean(SchemaInitBean.class)
|
||||
static class SpringLiquibaseDependsOnPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor {
|
||||
|
||||
SpringLiquibaseDependsOnPostProcessor() {
|
||||
// Configure the 3rd party SpringLiquibase bean to depend on our SchemaInitBean
|
||||
super(SpringLiquibase.class, SchemaInitBean.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package net.gepafin.tendermanagement.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Component
|
||||
public class Translator {
|
||||
|
||||
private static ResourceBundleMessageSource messageSource;
|
||||
|
||||
@Autowired
|
||||
Translator(ResourceBundleMessageSource messageSource) {
|
||||
Translator.messageSource = messageSource;
|
||||
}
|
||||
|
||||
public static String toLocale(String msgCode) {
|
||||
// LocaleContextHolder.setDefaultLocale(Locale.ITALIAN);
|
||||
Locale locale = LocaleContextHolder.getLocale();
|
||||
return messageSource.getMessage(msgCode, null, locale);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user