Skip to content

Commit

Permalink
Implementing interface for table creation
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed Apr 25, 2017
1 parent 8fd408e commit 82723e4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 37 deletions.
52 changes: 15 additions & 37 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -34,19 +36,22 @@ public class excel2db

private Workbook workbook;

private SheetEntityManager sheetEntityManager;
private Sheet sheet;
private File inputSheetFile;
private File outputSheetFile;
private String cellStringValue;
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; }

Expand All @@ -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)
{
Expand All @@ -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();

}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/excel2db/service/CreateTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package excel2db.service;

import java.sql.SQLException;

public interface CreateTable {

public void createTable () throws SQLException;

}
3 changes: 3 additions & 0 deletions src/main/java/excel2db/service/DBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ public interface DBConnection {

public void establishDBConnection();

interface CreateTable {
public void createTable();
}
}
40 changes: 40 additions & 0 deletions src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java
Original file line number Diff line number Diff line change
@@ -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();

}


}
8 changes: 8 additions & 0 deletions src/main/resources/spring.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version = "1.0" encoding = "UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
Expand All @@ -14,8 +15,15 @@

<bean id = "excel2db" class = "excel2db.excel2db" scope="singleton">
<property name="dbConnection" ref="dbConnection"></property>
<property name="createTable" ref="createTable"></property>
<!--<constructor-arg type="excel2db.service.impl.CreateTablePostgresImpl">-->
<!--<ref bean="createTable"/>-->
<!--</constructor-arg>-->
</bean>

<bean id = "dbConnection" class = "excel2db.service.impl.DBConnectionPostgresImpl"></bean>

<bean id = "createTable" class = "excel2db.service.impl.CreateTablePostgresImpl"></bean>


</beans>

0 comments on commit 82723e4

Please sign in to comment.