You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.1 KiB
139 lines
4.1 KiB
package org.ros.chatto.service;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
import org.ros.chatto.model.ApplicationStatus;
|
|
import org.ros.chatto.model.UserSession;
|
|
import org.ros.chatto.repository.UserSessionRepository;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
import org.springframework.context.annotation.PropertySource;
|
|
import org.springframework.context.event.EventListener;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
import org.springframework.core.io.support.EncodedResource;
|
|
import org.springframework.jdbc.datasource.init.ScriptUtils;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@Service
|
|
@PropertySource(value = "classpath:queries.properties")
|
|
|
|
public class DBInitializerService {
|
|
|
|
@Value("${spring.datasource.url}")
|
|
private String url;
|
|
|
|
@Value("${spring.datasource.username}")
|
|
private String userName;
|
|
|
|
@Value("${spring.datasource.password}")
|
|
private String password;
|
|
|
|
@Value("${database-name}")
|
|
private String dbName;
|
|
|
|
@Value("${num-tables}")
|
|
private String numTablesQuery;
|
|
|
|
@Value("${test.bindAddress}")
|
|
private String bindAddress;
|
|
|
|
private Connection connection;
|
|
|
|
@Autowired
|
|
private UserSessionRepository userSessionRepository;
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(DBInitializerService.class);
|
|
|
|
|
|
public void connectDB() throws SQLException {
|
|
connection = DriverManager.getConnection(url, userName, password);
|
|
}
|
|
|
|
public int getNumTables() throws SQLException {
|
|
PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery);
|
|
// preparedStatement.get
|
|
preparedStatement.setString(1, dbName);
|
|
ResultSet resultSet = preparedStatement.executeQuery();
|
|
// while(resultSet.next())
|
|
// {
|
|
// System.out.println(resultSet.get);
|
|
// }
|
|
resultSet.next();
|
|
int numTables = resultSet.getInt("num_tables");
|
|
// System.out.println(numTables);
|
|
return numTables;
|
|
}
|
|
|
|
@EventListener(ApplicationReadyEvent.class)
|
|
public void doSomethingAfterStartup() throws SQLException, IOException {
|
|
// setProperties();
|
|
|
|
connectDB();
|
|
|
|
logger.info("Application Started - running initializer service");
|
|
// System.out.println("Checking database and application state");
|
|
//
|
|
|
|
if (getNumTables() == 0) {
|
|
logger.info("Database is empty. Populating tables and roles");
|
|
populateDB();
|
|
}
|
|
closeConnection();
|
|
|
|
resetAllUserSessions(userSessionRepository.findAll());
|
|
}
|
|
|
|
public void populateDB() throws SQLException, IOException {
|
|
ScriptUtils.executeSqlScript(connection,
|
|
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
|
|
ScriptUtils.executeSqlScript(connection,
|
|
new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8));
|
|
|
|
}
|
|
|
|
private void populateTables() {
|
|
ScriptUtils.executeSqlScript(connection,
|
|
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
|
|
}
|
|
|
|
private void populateRoles() {
|
|
ScriptUtils.executeSqlScript(connection,
|
|
new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8));
|
|
}
|
|
|
|
|
|
Map<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) {
|
|
Map<String, Boolean> statusMap = new HashMap<>();
|
|
for (ApplicationStatus applicationStatus : applicationStatusList) {
|
|
statusMap.put(applicationStatus.getName(), applicationStatus.isDone());
|
|
}
|
|
return statusMap;
|
|
}
|
|
|
|
public void closeConnection() throws SQLException {
|
|
connection.close();
|
|
}
|
|
|
|
private void resetAllUserSessions(List<UserSession> userSessionsList) {
|
|
List<UserSession> userSessionsResetList = userSessionsList.stream().map(us -> {
|
|
us.setNumSessions(0);
|
|
us.setOnline(false);
|
|
return us;
|
|
}).collect(Collectors.toList());
|
|
userSessionRepository.saveAll(userSessionsResetList);
|
|
}
|
|
|
|
}
|