From e2e242841096bfba65902fb01411a11f812bac3d Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Thu, 21 Nov 2019 23:39:27 +0530 Subject: [PATCH] removed unneeded idb init code and moved sessions reset query to query.prop --- .../chatto/service/DBInitializerService.java | 110 +----------------- chatto/src/main/resources/queries.properties | 3 +- 2 files changed, 7 insertions(+), 106 deletions(-) diff --git a/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java b/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java index da936bb..a3ebbba 100644 --- a/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java +++ b/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java @@ -3,26 +3,17 @@ 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 javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.Transactional; import org.hibernate.Session; -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; @@ -37,77 +28,34 @@ import org.springframework.stereotype.Service; 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; + + @Value("${reset-sessions}") + private String resetSessionsQuery; private final Logger logger = LoggerFactory.getLogger(DBInitializerService.class); @PersistenceContext private EntityManager entityManager; - 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; - } public int getNumTables(Connection connection) 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) @Transactional public void doSomethingAfterStartup() throws SQLException, IOException { -// setProperties(); - -// connectDB(); - logger.info("Application Started - running initializer service"); -// System.out.println("Checking database and application state"); Session session = entityManager.unwrap(Session.class); @@ -123,25 +71,10 @@ public class DBInitializerService { } }); -// if (getNumTables() == 0) { -// logger.info("Database is empty. Populating tables and roles"); -// populateDB(); -// } -// closeConnection(); - session.doWork(connection -> { + logger.info("Resetting all user sessions"); resetAllUserSessions(connection); }); - - resetAllUserSessions(userSessionRepository.findAll()); - } - - private 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 populateDB(Connection connection) throws SQLException, IOException { @@ -152,43 +85,10 @@ public class DBInitializerService { } - 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 listToMap(List applicationStatusList) { - Map 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 userSessionsList) { - List userSessionsResetList = userSessionsList.stream().map(us -> { - us.setNumSessions(0); - us.setOnline(false); - return us; - }).collect(Collectors.toList()); - userSessionRepository.saveAll(userSessionsResetList); - } private void resetAllUserSessions(Connection connection) throws SQLException { - String sql = "update user_sessions set online=0, num_sessions=0"; - - PreparedStatement preparedStatement = connection.prepareStatement(sql); - + PreparedStatement preparedStatement = connection.prepareStatement(resetSessionsQuery); preparedStatement.executeUpdate(); } diff --git a/chatto/src/main/resources/queries.properties b/chatto/src/main/resources/queries.properties index 2c8e761..78e0ce1 100644 --- a/chatto/src/main/resources/queries.properties +++ b/chatto/src/main/resources/queries.properties @@ -1,2 +1,3 @@ -num-tables = SELECT COUNT(*) as num_tables FROM information_schema.tables WHERE table_schema = ? and TABLE_TYPE='BASE TABLE' +num-tables = SELECT COUNT(*) as num_tables FROM information_schema.tables WHERE table_schema = ? and TABLE_TYPE='BASE TABLE' +reset-sessions = update user_sessions set online=0, num_sessions=0 #tables-created = \ No newline at end of file