From 82723e4863cc0f2976a6eda499f755d03ab23492 Mon Sep 17 00:00:00 2001 From: Andrey Bespalov Date: Wed, 26 Apr 2017 00:46:17 +0300 Subject: [PATCH] Implementing interface for table creation --- src/main/java/excel2db/excel2db.java | 52 ++++++------------- .../java/excel2db/service/CreateTable.java | 9 ++++ .../java/excel2db/service/DBConnection.java | 3 ++ .../service/impl/CreateTablePostgresImpl.java | 40 ++++++++++++++ src/main/resources/spring.xml | 8 +++ 5 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 src/main/java/excel2db/service/CreateTable.java create mode 100644 src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java diff --git a/src/main/java/excel2db/excel2db.java b/src/main/java/excel2db/excel2db.java index 350bcbd..6b36a2b 100644 --- a/src/main/java/excel2db/excel2db.java +++ b/src/main/java/excel2db/excel2db.java @@ -4,6 +4,7 @@ import com.ge.mdm.tools.common.ApplicationException; import com.ge.mdm.tools.common.SheetEntityManager; +import excel2db.service.CreateTable; import excel2db.service.DBConnection; import org.apache.commons.io.FilenameUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -20,6 +21,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; + import java.io.File; import java.io.IOException; import java.sql.Connection; @@ -34,7 +36,6 @@ public class excel2db private Workbook workbook; - private SheetEntityManager sheetEntityManager; private Sheet sheet; private File inputSheetFile; private File outputSheetFile; @@ -42,11 +43,15 @@ public class excel2db private String fileExtension; private static Short numberProcessedRecords = Short.valueOf((short)0); + //vars are used in interface implementations public static Connection connection = null; + public static SheetEntityManager sheetEntityManager; + + // declaring variables to meet to the Spring framework convention public DBConnection dbConnection; + public CreateTable createTable; - public excel2db() {} public File getInputSheetFile() { return inputSheetFile; } @@ -63,10 +68,14 @@ public void setOutputSheetFile(File outputSheetFile) { this.outputSheetFile = outputSheetFile; } + // declaring setters to meet to the Spring framework convention. + // Setter injection method (SI)) public void setDbConnection(DBConnection dbConnection) { this.dbConnection = dbConnection; } + public void setCreateTable(CreateTable createTable) { this.createTable = createTable; } + public static void main(String[] args) { @@ -83,20 +92,18 @@ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); - excel2db app = (excel2db)context.getBean("excel2db"); - //DBConnection dbConnection = (DBConnection) context.getBean("dbConnection"); - - app.setInputSheetFile(inputFile); - app.initAndValidate(); + //this way we call the method for the dbConnection object + //that is already initialized by the Spring Framework app.dbConnection.establishDBConnection(); - app.createTable(); + app.createTable.createTable(); app.populateTable(); + app.closeConnections(); } @@ -167,35 +174,6 @@ private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationE } } - public void createTable() - throws SQLException - { - String sqlTableDropStatement = "DROP TABLE IF EXISTS excel2db;"; - - - - StringBuilder sqlTableCreateStatement = new StringBuilder(); - - sqlTableCreateStatement.append("CREATE TABLE excel2db("); - - - for (String headerColumnName : sheetEntityManager.header.keySet()) { - sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " text, "); - } - - - sqlTableCreateStatement.setLength(sqlTableCreateStatement.length() - 2); - - sqlTableCreateStatement.append(") WITH (OIDS=FALSE);"); - - - PreparedStatement pstmtDrop = connection.prepareStatement(sqlTableDropStatement); - pstmtDrop.execute(); - - PreparedStatement pstmtCreate = connection.prepareStatement(sqlTableCreateStatement.toString()); - pstmtCreate.execute(); - } - public void populateTable() throws SQLException diff --git a/src/main/java/excel2db/service/CreateTable.java b/src/main/java/excel2db/service/CreateTable.java new file mode 100644 index 0000000..d73f319 --- /dev/null +++ b/src/main/java/excel2db/service/CreateTable.java @@ -0,0 +1,9 @@ +package excel2db.service; + +import java.sql.SQLException; + +public interface CreateTable { + + public void createTable () throws SQLException; + +} diff --git a/src/main/java/excel2db/service/DBConnection.java b/src/main/java/excel2db/service/DBConnection.java index 734ddc6..517f26a 100644 --- a/src/main/java/excel2db/service/DBConnection.java +++ b/src/main/java/excel2db/service/DBConnection.java @@ -5,4 +5,7 @@ public interface DBConnection { public void establishDBConnection(); + interface CreateTable { + public void createTable(); + } } diff --git a/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java b/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java new file mode 100644 index 0000000..5877a83 --- /dev/null +++ b/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java @@ -0,0 +1,40 @@ +package excel2db.service.impl; + +import excel2db.excel2db; +import excel2db.service.CreateTable; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +public class CreateTablePostgresImpl implements CreateTable { + + public void createTable() throws SQLException + { + String sqlTableDropStatement = "DROP TABLE IF EXISTS excel2db;"; + + + StringBuilder sqlTableCreateStatement = new StringBuilder(); + + sqlTableCreateStatement.append("CREATE TABLE excel2db("); + + + for (String headerColumnName : excel2db.sheetEntityManager.header.keySet()) { + sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " text, "); + } + + + sqlTableCreateStatement.setLength(sqlTableCreateStatement.length() - 2); + + sqlTableCreateStatement.append(") WITH (OIDS=FALSE);"); + + + PreparedStatement pstmtDrop = excel2db.connection.prepareStatement(sqlTableDropStatement); + pstmtDrop.execute(); + + PreparedStatement pstmtCreate = excel2db.connection.prepareStatement(sqlTableCreateStatement.toString()); + pstmtCreate.execute(); + + } + + +} diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index 8e00d33..ce4ab6f 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -1,5 +1,6 @@ + + + + + + + + \ No newline at end of file