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.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,12 +61,12 @@ 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";
|
||||
|
||||
@ -144,27 +145,42 @@ public class DBInitializerService {
|
||||
public void doSomethingAfterStartup() throws SQLException, IOException {
|
||||
// setProperties();
|
||||
|
||||
connectDB();
|
||||
|
||||
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)
|
||||
populateDB();
|
||||
@ -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<ApplicationStatus> getStatusList() {
|
||||
// List<Object[]> persons = entityManager.createNativeQuery("SELECT * FROM Person" ).getResultList();
|
||||
List<ApplicationStatus> 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<ApplicationStatus> getStatusList() {
|
||||
//// List<Object[]> persons = entityManager.createNativeQuery("SELECT * FROM Person" ).getResultList();
|
||||
// List<ApplicationStatus> 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<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) {
|
||||
Map<String, Boolean> statusMap = new HashMap<>();
|
||||
@ -252,8 +278,7 @@ public class DBInitializerService {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
private void resetAllUserSessions(List<UserSession> userSessionsList)
|
||||
{
|
||||
private void resetAllUserSessions(List<UserSession> userSessionsList) {
|
||||
List<UserSession> userSessionsResetList = userSessionsList.stream().map(us -> {
|
||||
us.setNumSessions(0);
|
||||
us.setOnline(false);
|
||||
|
@ -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 */;
|
||||
@ -343,3 +343,32 @@ CREATE TABLE IF NOT EXISTS `chat_messages` (
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- 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