Skip to content

Commit

Permalink
implementing interface to populate table
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed Apr 26, 2017
1 parent 82723e4 commit 2199e69
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 64 deletions.
77 changes: 18 additions & 59 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import com.ge.mdm.tools.common.SheetEntityManager;
import excel2db.service.CreateTable;
import excel2db.service.DBConnection;
import excel2db.service.PopulateTable;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Expand All @@ -21,36 +20,40 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;


public class excel2db
{
public static final Logger logger = LoggerFactory.getLogger(excel2db.class);

// declaring the constructor to meet to the Spring framework convention
// constructor injection method (CI))
public excel2db (CreateTable createTable, PopulateTable populateTable) {
this.createTable = createTable;
this.populateTable = populateTable;
}

private Workbook workbook;

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;
public static Sheet sheet;
public static Short numberProcessedRecords = Short.valueOf((short)0);


// declaring variables to meet to the Spring framework convention
public DBConnection dbConnection;
public CreateTable createTable;
public PopulateTable populateTable;


public File getInputSheetFile() { return inputSheetFile; }
Expand All @@ -68,13 +71,13 @@ 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 @@ -84,7 +87,7 @@ public static void main(String[] args)
System.exit(1);
}

logger.info("excel2db : Passing file name as argument ; " + String.join(" ", args));
logger.info("excel2db : Passing file name as an argument ; " + String.join(" ", args));

File inputFile = new File(args[0]);

Expand All @@ -97,12 +100,10 @@ public static void main(String[] args)
app.setInputSheetFile(inputFile);
app.initAndValidate();

//this way we call the method for the dbConnection object
//that is already initialized by the Spring Framework
//this way we call methods for objects that is already initialized by the Spring
app.dbConnection.establishDBConnection();

app.createTable.createTable();
app.populateTable();
app.populateTable.populateTable();

app.closeConnections();

Expand All @@ -111,6 +112,7 @@ public static void main(String[] args)
logger.error("An exception occurred while running application", e);
System.exit(1);
}

}


Expand Down Expand Up @@ -174,54 +176,11 @@ private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationE
}
}


public void populateTable()
throws SQLException
{
Iterator<Row> rowIterator = sheet.rowIterator();

if (rowIterator.hasNext()) {
rowIterator.next();
}


while (rowIterator.hasNext())
{
Row row = (Row)rowIterator.next();

Short localShort1 = numberProcessedRecords;Short localShort2 = excel2db.numberProcessedRecords = Short.valueOf((short)(numberProcessedRecords.shortValue() + 1));

StringBuilder sqlTableInsertStatement = new StringBuilder();
sqlTableInsertStatement.append("INSERT INTO excel2db VALUES");

sqlTableInsertStatement.append("(");


short minColIdx = row.getFirstCellNum();
short maxColIdx = row.getLastCellNum();

for (short colIdx = minColIdx; colIdx < maxColIdx; colIdx = (short)(colIdx + 1)) {
cellStringValue = "";
Cell cell = row.getCell(colIdx);
if (cell == null) cellStringValue = ""; else cellStringValue = cell.toString();
sqlTableInsertStatement.append("'" + cellStringValue.replace("'", "''") + "'" + ",");
}

sqlTableInsertStatement.setLength(sqlTableInsertStatement.length() - 1);

sqlTableInsertStatement.append(")");



PreparedStatement pstmtInsertRow = connection.prepareStatement(sqlTableInsertStatement.toString());
pstmtInsertRow.execute();
}
}

public void closeConnections() throws ApplicationException, SQLException {
logger.info("Closing connections");
connection.close();
logger.info("The Process is Completed");
logger.info("Number of Processed Records: " + numberProcessedRecords.toString());
}

}
9 changes: 9 additions & 0 deletions src/main/java/excel2db/service/PopulateTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package excel2db.service;

import java.sql.SQLException;

public interface PopulateTable {

public void populateTable () throws SQLException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ public void createTable() throws SQLException

}


}
60 changes: 60 additions & 0 deletions src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package excel2db.service.impl;

import excel2db.excel2db;
import excel2db.service.PopulateTable;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;

public class PopulateTablePostgresImpl implements PopulateTable {

private String cellStringValue;


public void populateTable()
throws SQLException
{
Iterator<Row> rowIterator = excel2db.sheet.rowIterator();

if (rowIterator.hasNext()) {
rowIterator.next();
}


while (rowIterator.hasNext())
{
Row row = (Row)rowIterator.next();

Short localShort1 = excel2db.numberProcessedRecords;Short localShort2 = excel2db.numberProcessedRecords = Short.valueOf((short)(excel2db.numberProcessedRecords.shortValue() + 1));

StringBuilder sqlTableInsertStatement = new StringBuilder();
sqlTableInsertStatement.append("INSERT INTO excel2db VALUES");

sqlTableInsertStatement.append("(");


short minColIdx = row.getFirstCellNum();
short maxColIdx = row.getLastCellNum();

for (short colIdx = minColIdx; colIdx < maxColIdx; colIdx = (short)(colIdx + 1)) {
cellStringValue = "";
Cell cell = row.getCell(colIdx);
if (cell == null) cellStringValue = ""; else cellStringValue = cell.toString();
sqlTableInsertStatement.append("'" + cellStringValue.replace("'", "''") + "'" + ",");
}

sqlTableInsertStatement.setLength(sqlTableInsertStatement.length() - 1);

sqlTableInsertStatement.append(")");

PreparedStatement pstmtInsertRow = excel2db.connection.prepareStatement(sqlTableInsertStatement.toString());
pstmtInsertRow.execute();

}
}


}
13 changes: 9 additions & 4 deletions src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@

<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>-->
<!--<property name="createTable" ref="createTable"></property>-->
<constructor-arg>
<ref bean="createTable"/>
</constructor-arg>
<constructor-arg>
<ref bean="populateTable"/>
</constructor-arg>
</bean>

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

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

<bean id = "populateTable" class = "excel2db.service.impl.PopulateTablePostgresImpl"></bean>


</beans>

0 comments on commit 2199e69

Please sign in to comment.