|
@ -12,43 +12,31 @@ import javax.persistence.PersistenceContext; |
|
|
import javax.transaction.Transactional; |
|
|
import javax.transaction.Transactional; |
|
|
|
|
|
|
|
|
import org.hibernate.Session; |
|
|
import org.hibernate.Session; |
|
|
import org.slf4j.Logger; |
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
|
|
|
|
import org.ros.chatto.config.DBInitializerConfig; |
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent; |
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent; |
|
|
import org.springframework.context.annotation.PropertySource; |
|
|
|
|
|
import org.springframework.context.event.EventListener; |
|
|
import org.springframework.context.event.EventListener; |
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
import org.springframework.core.io.support.EncodedResource; |
|
|
import org.springframework.core.io.support.EncodedResource; |
|
|
import org.springframework.jdbc.datasource.init.ScriptUtils; |
|
|
import org.springframework.jdbc.datasource.init.ScriptUtils; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@PropertySource(value = "classpath:queries.properties") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RequiredArgsConstructor |
|
|
public class DBInitializerService { |
|
|
public class DBInitializerService { |
|
|
|
|
|
|
|
|
@Value("${database-name}") |
|
|
|
|
|
private String dbName; |
|
|
|
|
|
|
|
|
|
|
|
@Value("${num-tables}") |
|
|
|
|
|
private String numTablesQuery; |
|
|
|
|
|
|
|
|
|
|
|
@Value("${reset-sessions}") |
|
|
|
|
|
private String resetSessionsQuery; |
|
|
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(DBInitializerService.class); |
|
|
|
|
|
|
|
|
private final DBInitializerConfig dbInitializerConfig; |
|
|
|
|
|
|
|
|
@PersistenceContext |
|
|
@PersistenceContext |
|
|
private EntityManager entityManager; |
|
|
private EntityManager entityManager; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int getNumTables(final Connection connection) throws SQLException { |
|
|
public int getNumTables(final Connection connection) throws SQLException { |
|
|
final PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery); |
|
|
|
|
|
preparedStatement.setString(1, dbName); |
|
|
|
|
|
|
|
|
final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getNumTablesQuery()); |
|
|
|
|
|
preparedStatement.setString(1, dbInitializerConfig.getDbName()); |
|
|
final ResultSet resultSet = preparedStatement.executeQuery(); |
|
|
final ResultSet resultSet = preparedStatement.executeQuery(); |
|
|
resultSet.next(); |
|
|
resultSet.next(); |
|
|
final int numTables = resultSet.getInt("num_tables"); |
|
|
final int numTables = resultSet.getInt("num_tables"); |
|
@ -58,13 +46,13 @@ public class DBInitializerService { |
|
|
@EventListener(ApplicationReadyEvent.class) |
|
|
@EventListener(ApplicationReadyEvent.class) |
|
|
@Transactional |
|
|
@Transactional |
|
|
public void doSomethingAfterStartup() throws SQLException, IOException { |
|
|
public void doSomethingAfterStartup() throws SQLException, IOException { |
|
|
logger.info("Application Started - running initializer service"); |
|
|
|
|
|
|
|
|
log.info("Application Started - running initializer service"); |
|
|
|
|
|
|
|
|
final Session session = entityManager.unwrap(Session.class); |
|
|
final Session session = entityManager.unwrap(Session.class); |
|
|
|
|
|
|
|
|
session.doWork(connection -> { |
|
|
session.doWork(connection -> { |
|
|
if (getNumTables(connection) == 0) { |
|
|
if (getNumTables(connection) == 0) { |
|
|
logger.info("Database is empty. Populating tables and roles"); |
|
|
|
|
|
|
|
|
log.info("Database is empty. Populating tables and roles"); |
|
|
try { |
|
|
try { |
|
|
populateDB(connection); |
|
|
populateDB(connection); |
|
|
} catch (final IOException e) { |
|
|
} catch (final IOException e) { |
|
@ -74,7 +62,7 @@ public class DBInitializerService { |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
session.doWork(connection -> { |
|
|
session.doWork(connection -> { |
|
|
logger.info("Resetting all user sessions"); |
|
|
|
|
|
|
|
|
log.info("Resetting all user sessions"); |
|
|
resetAllUserSessions(connection); |
|
|
resetAllUserSessions(connection); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
@ -88,7 +76,7 @@ public class DBInitializerService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void resetAllUserSessions(final Connection connection) throws SQLException { |
|
|
private void resetAllUserSessions(final Connection connection) throws SQLException { |
|
|
final PreparedStatement preparedStatement = connection.prepareStatement(resetSessionsQuery); |
|
|
|
|
|
|
|
|
final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getResetSessionsQuery()); |
|
|
preparedStatement.executeUpdate(); |
|
|
preparedStatement.executeUpdate(); |
|
|
} |
|
|
} |
|
|
|
|
|
|