diff --git a/src/main/java/excel2db/excel2db.java b/src/main/java/excel2db/excel2db.java index 6b36a2b..1ca87e5 100644 --- a/src/main/java/excel2db/excel2db.java +++ b/src/main/java/excel2db/excel2db.java @@ -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; @@ -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; } @@ -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) @@ -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]); @@ -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(); @@ -111,6 +112,7 @@ public static void main(String[] args) logger.error("An exception occurred while running application", e); System.exit(1); } + } @@ -174,54 +176,11 @@ private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationE } } - - public void populateTable() - throws SQLException - { - Iterator 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()); } + } \ No newline at end of file diff --git a/src/main/java/excel2db/service/PopulateTable.java b/src/main/java/excel2db/service/PopulateTable.java new file mode 100644 index 0000000..f27668e --- /dev/null +++ b/src/main/java/excel2db/service/PopulateTable.java @@ -0,0 +1,9 @@ +package excel2db.service; + +import java.sql.SQLException; + +public interface PopulateTable { + + public void populateTable () throws SQLException; + +} diff --git a/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java b/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java index 5877a83..554be34 100644 --- a/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java +++ b/src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java @@ -36,5 +36,4 @@ public void createTable() throws SQLException } - } diff --git a/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java b/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java new file mode 100644 index 0000000..13a5422 --- /dev/null +++ b/src/main/java/excel2db/service/impl/PopulateTablePostgresImpl.java @@ -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 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(); + + } + } + + +} diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index ce4ab6f..85fda6f 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -15,15 +15,20 @@ - - - - + + + + + + + + + \ No newline at end of file