updated schema initilization script and switched back to jdbc based initialization
This commit is contained in:
parent
5a79c7026e
commit
9140885398
@ -20,6 +20,7 @@ import javax.persistence.PersistenceContext;
|
|||||||
|
|
||||||
import org.ros.chatto.model.ApplicationStatus;
|
import org.ros.chatto.model.ApplicationStatus;
|
||||||
import org.ros.chatto.model.UserSession;
|
import org.ros.chatto.model.UserSession;
|
||||||
|
import org.ros.chatto.repository.ApplicationStatusRepository;
|
||||||
import org.ros.chatto.repository.UserSessionRepository;
|
import org.ros.chatto.repository.UserSessionRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -60,11 +61,11 @@ public class DBInitializerService {
|
|||||||
|
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
|
||||||
@PersistenceContext
|
|
||||||
EntityManager entityManager;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserSessionRepository userSessionRepository;
|
private UserSessionRepository userSessionRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationStatusRepository applicationStatusRepository;
|
||||||
|
|
||||||
private final String tablesCreatedKey = "tables_created";
|
private final String tablesCreatedKey = "tables_created";
|
||||||
private final String rolesPopulatedKey = "roles_populated";
|
private final String rolesPopulatedKey = "roles_populated";
|
||||||
@ -143,33 +144,48 @@ public class DBInitializerService {
|
|||||||
@EventListener(ApplicationReadyEvent.class)
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
public void doSomethingAfterStartup() throws SQLException, IOException {
|
public void doSomethingAfterStartup() throws SQLException, IOException {
|
||||||
// setProperties();
|
// setProperties();
|
||||||
|
|
||||||
|
connectDB();
|
||||||
|
|
||||||
System.out.println("Hello world, I have just started up");
|
System.out.println("Hello world, I have just started up");
|
||||||
|
|
||||||
List<ApplicationStatus> applicationStatusList = getStatusList();
|
|
||||||
Map<String, Boolean> 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<ApplicationStatus> applicationStatusList = applicationStatusRepository.findAll();
|
||||||
|
// Map<String, Boolean> 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)
|
if (getNumTables() == 0)
|
||||||
populateDB();
|
populateDB();
|
||||||
closeConnection();
|
closeConnection();
|
||||||
|
|
||||||
resetAllUserSessions(userSessionRepository.findAll());
|
resetAllUserSessions(userSessionRepository.findAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,6 +217,16 @@ public class DBInitializerService {
|
|||||||
// connection.close();
|
// 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 {
|
public void setProperties() throws IOException {
|
||||||
// InputStream input = ChattoApplication.class.getClassLoader().getResourceAsStream("messages.properties");
|
// InputStream input = ChattoApplication.class.getClassLoader().getResourceAsStream("messages.properties");
|
||||||
OutputStream outputStream = new FileOutputStream("messages.properties");
|
OutputStream outputStream = new FileOutputStream("messages.properties");
|
||||||
@ -231,14 +257,14 @@ public class DBInitializerService {
|
|||||||
// prop.store(object, comments);
|
// prop.store(object, comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ApplicationStatus> getStatusList() {
|
// List<ApplicationStatus> getStatusList() {
|
||||||
// List<Object[]> persons = entityManager.createNativeQuery("SELECT * FROM Person" ).getResultList();
|
//// List<Object[]> persons = entityManager.createNativeQuery("SELECT * FROM Person" ).getResultList();
|
||||||
List<ApplicationStatus> applicationStatus = entityManager
|
// List<ApplicationStatus> applicationStatus = entityManager
|
||||||
.createQuery("from ApplicationStatus s", ApplicationStatus.class).getResultList();
|
// .createQuery("from ApplicationStatus s", ApplicationStatus.class).getResultList();
|
||||||
applicationStatus.stream().forEach(status -> status.getName());
|
// applicationStatus.stream().forEach(status -> status.getName());
|
||||||
// System.out.println(applicationStatus.get(0).getName() + applicationStatus.get(0).isDone());
|
//// System.out.println(applicationStatus.get(0).getName() + applicationStatus.get(0).isDone());
|
||||||
return applicationStatus;
|
// return applicationStatus;
|
||||||
}
|
// }
|
||||||
|
|
||||||
Map<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) {
|
Map<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) {
|
||||||
Map<String, Boolean> statusMap = new HashMap<>();
|
Map<String, Boolean> statusMap = new HashMap<>();
|
||||||
@ -251,9 +277,8 @@ public class DBInitializerService {
|
|||||||
public void closeConnection() throws SQLException {
|
public void closeConnection() throws SQLException {
|
||||||
connection.close();
|
connection.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetAllUserSessions(List<UserSession> userSessionsList)
|
private void resetAllUserSessions(List<UserSession> userSessionsList) {
|
||||||
{
|
|
||||||
List<UserSession> userSessionsResetList = userSessionsList.stream().map(us -> {
|
List<UserSession> userSessionsResetList = userSessionsList.stream().map(us -> {
|
||||||
us.setNumSessions(0);
|
us.setNumSessions(0);
|
||||||
us.setOnline(false);
|
us.setOnline(false);
|
||||||
|
@ -213,14 +213,14 @@
|
|||||||
--DROP TABLE IF EXISTS `chatmessage`;
|
--DROP TABLE IF EXISTS `chatmessage`;
|
||||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
/*!40101 SET character_set_client = utf8 */;
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
CREATE TABLE IF NOT EXISTS `chatmessage` (
|
-- CREATE TABLE IF NOT EXISTS `chatmessage` (
|
||||||
`Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
-- `Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
`Message` varchar(4000) NOT NULL,
|
-- `Message` varchar(4000) NOT NULL,
|
||||||
`userName` varchar(100) NOT NULL,
|
-- `userName` varchar(100) NOT NULL,
|
||||||
`MsgTime` varchar(45) NOT NULL,
|
-- `MsgTime` varchar(45) NOT NULL,
|
||||||
`colorSelected` varchar(45) NOT NULL,
|
-- `colorSelected` varchar(45) NOT NULL,
|
||||||
PRIMARY KEY (`Id`)
|
-- PRIMARY KEY (`Id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
-- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
|
|
||||||
--
|
--
|
||||||
@ -241,7 +241,7 @@ CREATE TABLE IF NOT EXISTS `message_ciphers` (
|
|||||||
`adata` varchar(11) NOT NULL,
|
`adata` varchar(11) NOT NULL,
|
||||||
`cipher` varchar(11) NOT NULL,
|
`cipher` varchar(11) NOT NULL,
|
||||||
`salt` varchar(100) NOT NULL,
|
`salt` varchar(100) NOT NULL,
|
||||||
`cipher_text` varchar(600) NOT NULL,
|
`cipher_text` varchar(2000) NOT NULL,
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
/*!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 */;
|
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||||
|
|
||||||
-- Dump completed on 2019-10-03 12:37:23
|
-- 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;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user