A self hosted chat application with end-to-end encrypted messaging.
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.

133 lines
3.9 KiB

  1. package org.ros.chatto.service;
  2. import java.io.IOException;
  3. import java.nio.charset.StandardCharsets;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.stream.Collectors;
  13. import org.ros.chatto.model.ApplicationStatus;
  14. import org.ros.chatto.model.UserSession;
  15. import org.ros.chatto.repository.UserSessionRepository;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.boot.context.event.ApplicationReadyEvent;
  19. import org.springframework.context.annotation.PropertySource;
  20. import org.springframework.context.event.EventListener;
  21. import org.springframework.core.io.ClassPathResource;
  22. import org.springframework.core.io.support.EncodedResource;
  23. import org.springframework.jdbc.datasource.init.ScriptUtils;
  24. import org.springframework.stereotype.Service;
  25. @Service
  26. @PropertySource(value = "classpath:queries.properties")
  27. public class DBInitializerService {
  28. @Value("${spring.datasource.url}")
  29. private String url;
  30. @Value("${spring.datasource.username}")
  31. private String userName;
  32. @Value("${spring.datasource.password}")
  33. private String password;
  34. @Value("${database-name}")
  35. private String dbName;
  36. @Value("${num-tables}")
  37. private String numTablesQuery;
  38. @Value("${test.bindAddress}")
  39. private String bindAddress;
  40. private Connection connection;
  41. @Autowired
  42. private UserSessionRepository userSessionRepository;
  43. public void connectDB() throws SQLException {
  44. connection = DriverManager.getConnection(url, userName, password);
  45. }
  46. public int getNumTables() throws SQLException {
  47. PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery);
  48. // preparedStatement.get
  49. preparedStatement.setString(1, dbName);
  50. ResultSet resultSet = preparedStatement.executeQuery();
  51. // while(resultSet.next())
  52. // {
  53. // System.out.println(resultSet.get);
  54. // }
  55. resultSet.next();
  56. int numTables = resultSet.getInt("num_tables");
  57. // System.out.println(numTables);
  58. return numTables;
  59. }
  60. @EventListener(ApplicationReadyEvent.class)
  61. public void doSomethingAfterStartup() throws SQLException, IOException {
  62. // setProperties();
  63. connectDB();
  64. System.out.println("Hello world, I have just started up");
  65. // System.out.println("Checking database and application state");
  66. //
  67. if (getNumTables() == 0)
  68. populateDB();
  69. closeConnection();
  70. resetAllUserSessions(userSessionRepository.findAll());
  71. }
  72. public void populateDB() throws SQLException, IOException {
  73. ScriptUtils.executeSqlScript(connection,
  74. new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
  75. ScriptUtils.executeSqlScript(connection,
  76. new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8));
  77. }
  78. private void populateTables() {
  79. ScriptUtils.executeSqlScript(connection,
  80. new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
  81. }
  82. private void populateRoles() {
  83. ScriptUtils.executeSqlScript(connection,
  84. new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8));
  85. }
  86. Map<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) {
  87. Map<String, Boolean> statusMap = new HashMap<>();
  88. for (ApplicationStatus applicationStatus : applicationStatusList) {
  89. statusMap.put(applicationStatus.getName(), applicationStatus.isDone());
  90. }
  91. return statusMap;
  92. }
  93. public void closeConnection() throws SQLException {
  94. connection.close();
  95. }
  96. private void resetAllUserSessions(List<UserSession> userSessionsList) {
  97. List<UserSession> userSessionsResetList = userSessionsList.stream().map(us -> {
  98. us.setNumSessions(0);
  99. us.setOnline(false);
  100. return us;
  101. }).collect(Collectors.toList());
  102. userSessionRepository.saveAll(userSessionsResetList);
  103. }
  104. }