|
@ -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,12 +61,12 @@ 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"; |
|
|
|
|
|
|
|
@ -144,27 +145,42 @@ public class DBInitializerService { |
|
|
public void doSomethingAfterStartup() throws SQLException, IOException { |
|
|
public void doSomethingAfterStartup() throws SQLException, IOException { |
|
|
// setProperties(); |
|
|
// setProperties(); |
|
|
|
|
|
|
|
|
System.out.println("Hello world, I have just started up"); |
|
|
|
|
|
|
|
|
connectDB(); |
|
|
|
|
|
|
|
|
List<ApplicationStatus> applicationStatusList = getStatusList(); |
|
|
|
|
|
Map<String, Boolean> statusMap = listToMap(applicationStatusList); |
|
|
|
|
|
|
|
|
System.out.println("Hello world, I have just started up"); |
|
|
|
|
|
|
|
|
// 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(); |
|
@ -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<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> listToMap(List<ApplicationStatus> applicationStatusList) { |
|
|
Map<String, Boolean> statusMap = new HashMap<>(); |
|
|
Map<String, Boolean> statusMap = new HashMap<>(); |
|
@ -252,8 +278,7 @@ public class DBInitializerService { |
|
|
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); |
|
|