From 9140885398e997eff62a23a197b9fb09a0d91a99 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Sun, 17 Nov 2019 02:52:04 +0530 Subject: [PATCH] updated schema initilization script and switched back to jdbc based initialization --- .../chatto/service/DBInitializerService.java | 95 ++++++++++++------- chatto/src/main/resources/scheme.sql | 49 ++++++++-- 2 files changed, 99 insertions(+), 45 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 9e5a827..660c9b1 100644 --- a/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java +++ b/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java @@ -20,6 +20,7 @@ import javax.persistence.PersistenceContext; import org.ros.chatto.model.ApplicationStatus; import org.ros.chatto.model.UserSession; +import org.ros.chatto.repository.ApplicationStatusRepository; import org.ros.chatto.repository.UserSessionRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -60,11 +61,11 @@ public class DBInitializerService { private Connection connection; - @PersistenceContext - EntityManager entityManager; - @Autowired private UserSessionRepository userSessionRepository; + + @Autowired + private ApplicationStatusRepository applicationStatusRepository; private final String tablesCreatedKey = "tables_created"; private final String rolesPopulatedKey = "roles_populated"; @@ -143,33 +144,48 @@ public class DBInitializerService { @EventListener(ApplicationReadyEvent.class) public void doSomethingAfterStartup() throws SQLException, IOException { // setProperties(); + + connectDB(); System.out.println("Hello world, I have just started up"); - - List applicationStatusList = getStatusList(); - Map statusMap = listToMap(applicationStatusList); - -// applicationStatus. - connectDB(); - /* - * if (statusMap.get(tablesCreatedKey) == null || - * !statusMap.get(tablesCreatedKey)) { - * System.out.println("Initializing database"); if (getNumTables() == 0) { - * populateDB(); System.out.println("Tables created"); } ApplicationStatus - * status = new ApplicationStatus(); status.setName(tablesCreatedKey); - * status.setDone(true); - * - * } - * - * if (statusMap.get(rolesPopulatedKey) == null || - * !statusMap.get(rolesPopulatedKey)) { System.out.println("Populating roles"); - * } - */ +// System.out.println("Checking database and application state"); +// +// List applicationStatusList = applicationStatusRepository.findAll(); +// Map statusMap = listToMap(applicationStatusList); +// +// if (statusMap.get(tablesCreatedKey) == null || !statusMap.get(tablesCreatedKey)) { +// if (getNumTables() == 0) { +// System.out.println("Creating tables"); +// populateTables(); +// System.out.println("Tables created"); +// } +// ApplicationStatus status = new ApplicationStatus(); +// status.setName(tablesCreatedKey); +// status.setDone(true); +// applicationStatusRepository.save(status); +// } +// else { +// System.out.println("Tables already created"); +// } +// +// if (statusMap.get(rolesPopulatedKey) == null || !statusMap.get(rolesPopulatedKey)) { +// System.out.println("Populating roles"); +// populateRoles(); +// ApplicationStatus status = new ApplicationStatus(); +// status.setName(rolesPopulatedKey); +// status.setDone(true); +// applicationStatusRepository.save(status); +// System.out.println("Roles populated"); +// } +// else { +// System.out.println("Roles already populated"); +// } + if (getNumTables() == 0) populateDB(); closeConnection(); - + resetAllUserSessions(userSessionRepository.findAll()); } @@ -201,6 +217,16 @@ public class DBInitializerService { // connection.close(); } + 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)); + } + public void setProperties() throws IOException { // InputStream input = ChattoApplication.class.getClassLoader().getResourceAsStream("messages.properties"); OutputStream outputStream = new FileOutputStream("messages.properties"); @@ -231,14 +257,14 @@ public class DBInitializerService { // prop.store(object, comments); } - List getStatusList() { -// List persons = entityManager.createNativeQuery("SELECT * FROM Person" ).getResultList(); - List applicationStatus = entityManager - .createQuery("from ApplicationStatus s", ApplicationStatus.class).getResultList(); - applicationStatus.stream().forEach(status -> status.getName()); -// System.out.println(applicationStatus.get(0).getName() + applicationStatus.get(0).isDone()); - return applicationStatus; - } +// List getStatusList() { +//// List persons = entityManager.createNativeQuery("SELECT * FROM Person" ).getResultList(); +// List applicationStatus = entityManager +// .createQuery("from ApplicationStatus s", ApplicationStatus.class).getResultList(); +// applicationStatus.stream().forEach(status -> status.getName()); +//// System.out.println(applicationStatus.get(0).getName() + applicationStatus.get(0).isDone()); +// return applicationStatus; +// } Map listToMap(List applicationStatusList) { Map statusMap = new HashMap<>(); @@ -251,9 +277,8 @@ public class DBInitializerService { public void closeConnection() throws SQLException { connection.close(); } - - private void resetAllUserSessions(List userSessionsList) - { + + private void resetAllUserSessions(List userSessionsList) { List userSessionsResetList = userSessionsList.stream().map(us -> { us.setNumSessions(0); us.setOnline(false); diff --git a/chatto/src/main/resources/scheme.sql b/chatto/src/main/resources/scheme.sql index 8aca1f2..2946b3a 100644 --- a/chatto/src/main/resources/scheme.sql +++ b/chatto/src/main/resources/scheme.sql @@ -213,14 +213,14 @@ --DROP TABLE IF EXISTS `chatmessage`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; -CREATE TABLE IF NOT EXISTS `chatmessage` ( - `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `Message` varchar(4000) NOT NULL, - `userName` varchar(100) NOT NULL, - `MsgTime` varchar(45) NOT NULL, - `colorSelected` varchar(45) NOT NULL, - PRIMARY KEY (`Id`) -) ENGINE=InnoDB DEFAULT CHARSET=latin1; +-- CREATE TABLE IF NOT EXISTS `chatmessage` ( +-- `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, +-- `Message` varchar(4000) NOT NULL, +-- `userName` varchar(100) NOT NULL, +-- `MsgTime` varchar(45) NOT NULL, +-- `colorSelected` varchar(45) NOT NULL, +-- PRIMARY KEY (`Id`) +-- ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -241,7 +241,7 @@ CREATE TABLE IF NOT EXISTS `message_ciphers` ( `adata` varchar(11) NOT NULL, `cipher` varchar(11) NOT NULL, `salt` varchar(100) NOT NULL, - `cipher_text` varchar(600) NOT NULL, + `cipher_text` varchar(2000) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; /*!40101 SET character_set_client = @saved_cs_client */; @@ -342,4 +342,33 @@ CREATE TABLE IF NOT EXISTS `chat_messages` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2019-10-03 12:37:23 \ No newline at end of file +-- Dump completed on 2019-10-03 12:37:23 + +CREATE TABLE IF NOT EXISTS `status` ( + `id` int(2) NOT NULL AUTO_INCREMENT, + `name` varchar(15) NOT NULL, + `value` tinyint(1) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `name` (`name`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `user_sessions` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `user_id` int(15) NOT NULL, + `online` tinyint(1) NOT NULL, + `num_sessions` int(11) NOT NULL, + `time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `user_name` (`user_id`), + CONSTRAINT `FOREIGN KEY USER ID` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `tokens` ( + `token_id` int(11) NOT NULL AUTO_INCREMENT, + `user_name` varchar(15) NOT NULL, + `token_content` varchar(256) NOT NULL, + `role` varchar(15) NOT NULL, + PRIMARY KEY (`token_id`), + UNIQUE KEY `user_name` (`user_name`) +) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4; +