You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
8.3 KiB
246 lines
8.3 KiB
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;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Properties;
|
|
|
|
import javax.persistence.EntityManager;
|
|
import javax.persistence.PersistenceContext;
|
|
|
|
import org.ros.chatto.model.ApplicationStatus;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
import org.springframework.context.annotation.PropertySource;
|
|
import org.springframework.context.event.EventListener;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
import org.springframework.core.io.support.EncodedResource;
|
|
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;
|
|
|
|
@Value("${spring.datasource.username}")
|
|
private String userName;
|
|
|
|
@Value("${spring.datasource.password}")
|
|
private String password;
|
|
|
|
@Value("${database-name}")
|
|
private String dbName;
|
|
|
|
@Value("${num-tables}")
|
|
private String numTablesQuery;
|
|
|
|
@Value("${test.bindAddress}")
|
|
private String bindAddress;
|
|
|
|
private Connection connection;
|
|
|
|
@PersistenceContext
|
|
EntityManager entityManager;
|
|
|
|
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);
|
|
}
|
|
|
|
public int getNumTables() throws SQLException {
|
|
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);
|
|
return numTables;
|
|
}
|
|
|
|
@EventListener(ApplicationReadyEvent.class)
|
|
public void doSomethingAfterStartup() throws SQLException, IOException {
|
|
// setProperties();
|
|
|
|
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");
|
|
* }
|
|
*/
|
|
|
|
if (getNumTables() == 0)
|
|
populateDB();
|
|
closeConnection();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
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<>();
|
|
for (ApplicationStatus applicationStatus : applicationStatusList) {
|
|
statusMap.put(applicationStatus.getName(), applicationStatus.isDone());
|
|
}
|
|
return statusMap;
|
|
}
|
|
|
|
public void closeConnection() throws SQLException {
|
|
connection.close();
|
|
}
|
|
|
|
}
|