|
|
@ -1,8 +1,6 @@ |
|
|
|
package org.ros.chatto.service; |
|
|
|
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.DriverManager; |
|
|
@ -12,7 +10,6 @@ import java.sql.SQLException; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import org.ros.chatto.model.ApplicationStatus; |
|
|
@ -29,14 +26,10 @@ import org.springframework.jdbc.datasource.init.ScriptUtils; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
@Service |
|
|
|
//@PropertySource(name = "myProperties", value = "example.properties") |
|
|
|
//@PropertySource(name = "appProperties", value="classpath:myapp.properties") |
|
|
|
@PropertySource(value = "classpath:queries.properties") |
|
|
|
|
|
|
|
public class DBInitializerService { |
|
|
|
|
|
|
|
// @Autowired |
|
|
|
// private Environment environment; |
|
|
|
@Value("${spring.datasource.url}") |
|
|
|
private String url; |
|
|
|
|
|
|
@ -60,60 +53,6 @@ public class DBInitializerService { |
|
|
|
@Autowired |
|
|
|
private UserSessionRepository userSessionRepository; |
|
|
|
|
|
|
|
private final String tablesCreatedKey = "tables_created"; |
|
|
|
private final String rolesPopulatedKey = "roles_populated"; |
|
|
|
|
|
|
|
// public DBInitializerService(Connection connection) { |
|
|
|
// this.connection = connection; |
|
|
|
// // TODO Auto-generated constructor stub |
|
|
|
// } |
|
|
|
|
|
|
|
// Run this method when application started |
|
|
|
// @EventListener(ApplicationReadyEvent.class) |
|
|
|
// public ResultSet getConnection() |
|
|
|
// { |
|
|
|
//// String url = environment.getProperty("spring.datasource.url"); |
|
|
|
// System.out.println("URL = " + url); |
|
|
|
//// |
|
|
|
//// //Connect to Database |
|
|
|
//// Connection connection = null; |
|
|
|
//// String QUERY="your sql query"; |
|
|
|
//// try { |
|
|
|
//// DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); |
|
|
|
//// connection = DriverManager.getConnection(url, userName, password ); |
|
|
|
//// } catch (SQLException e) { |
|
|
|
//// } |
|
|
|
//// |
|
|
|
//// |
|
|
|
//// //Run your query |
|
|
|
//// Statement stmt = null; |
|
|
|
//// try { |
|
|
|
//// stmt = connection.createStatement(); |
|
|
|
//// } catch (SQLException e1) { |
|
|
|
//// e1.printStackTrace(); |
|
|
|
//// } |
|
|
|
//// ResultSet rs = null; |
|
|
|
//// try { |
|
|
|
//// rs = stmt.executeQuery(QUERY); |
|
|
|
//// } catch (SQLException e1) { |
|
|
|
//// e1.printStackTrace(); |
|
|
|
//// } |
|
|
|
//// |
|
|
|
//// return rs; |
|
|
|
// return null; |
|
|
|
// } |
|
|
|
|
|
|
|
// @BeforeClass |
|
|
|
// public static void initializeJiraDataBaseForSla() throws SQLException { |
|
|
|
// final DataSource datasource = new SimpleDriverDataSource(new JDBCDriver(), "jdbc:hsqldb:mem:dataSource", null, null); |
|
|
|
// final Connection connection = datasource.getConnection(); |
|
|
|
// try { |
|
|
|
// ScriptUtils.executeSqlScript(connection, new EncodedResource(new ClassPathResource("sql/sla/jira-create.sql"), StandardCharsets.UTF_8)); |
|
|
|
// ScriptUtils.executeSqlScript(connection, new EncodedResource(new ClassPathResource("sql/sla/jira.sql"), StandardCharsets.UTF_8)); |
|
|
|
// } finally { |
|
|
|
// connection.close(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
public void connectDB() throws SQLException { |
|
|
|
connection = DriverManager.getConnection(url, userName, password); |
|
|
@ -144,36 +83,6 @@ public class DBInitializerService { |
|
|
|
|
|
|
|
// 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(); |
|
|
@ -183,31 +92,11 @@ public class DBInitializerService { |
|
|
|
} |
|
|
|
|
|
|
|
public void populateDB() throws SQLException, IOException { |
|
|
|
// System.out.println("Database name = " + dbName); |
|
|
|
// String sql = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '" + dbName + "' and TABLE_TYPE='BASE TABLE' "; |
|
|
|
// String sql = numTablesQuery; |
|
|
|
// System.out.println(numTablesQuery); |
|
|
|
// System.out.println(sql); |
|
|
|
// connection = DriverManager.getConnection(url, userName, password); |
|
|
|
// 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); |
|
|
|
// if (numTables == 0) { |
|
|
|
ScriptUtils.executeSqlScript(connection, |
|
|
|
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8)); |
|
|
|
ScriptUtils.executeSqlScript(connection, |
|
|
|
new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
// } |
|
|
|
// connection.close(); |
|
|
|
} |
|
|
|
|
|
|
|
private void populateTables() { |
|
|
@ -220,44 +109,6 @@ public class DBInitializerService { |
|
|
|
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"); |
|
|
|
// FileInputStream in = new FileInputStream("First.properties"); |
|
|
|
// Properties props = new Properties(); |
|
|
|
// props.load(in); |
|
|
|
// in.close(); |
|
|
|
// |
|
|
|
// FileOutputStream out = new FileOutputStream("First.properties"); |
|
|
|
// props.setProperty("country", "america"); |
|
|
|
// props.store(out, null); |
|
|
|
// out.close(); |
|
|
|
Properties prop = new Properties(); |
|
|
|
System.out.println("Hello from setProperties"); |
|
|
|
|
|
|
|
prop.setProperty("test.bindAddress", bindAddress); |
|
|
|
prop.store(outputStream, null); |
|
|
|
// if (input == null) { |
|
|
|
// System.out.println("Sorry, unable to find messages.properties"); |
|
|
|
// return; |
|
|
|
// } |
|
|
|
|
|
|
|
// load a properties file from class path, inside static method |
|
|
|
// prop.load(input); |
|
|
|
// Object object = prop.setProperty("test.bindAddress", bindAddress); |
|
|
|
// input.close(); |
|
|
|
outputStream.close(); |
|
|
|
// 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; |
|
|
|
// } |
|
|
|
|
|
|
|
Map<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) { |
|
|
|
Map<String, Boolean> statusMap = new HashMap<>(); |
|
|
|