-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df8723c
commit 2077eae
Showing
19 changed files
with
389 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package excel2db; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
|
||
import com.ge.mdm.tools.common.ApplicationException; | ||
import org.apache.commons.io.FilenameUtils; | ||
|
||
public class Excel2dbTask extends Thread { | ||
|
||
public Excel2dbTask(excel2db app, File fileName) { | ||
this.app = app; | ||
this.fileName = fileName; | ||
} | ||
|
||
public excel2db app; | ||
public File fileName; | ||
|
||
String tableName; | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
app.initInputFiles.initInputFiles(fileName); | ||
|
||
tableName = FilenameUtils.removeExtension(fileName.getName()); | ||
app.createTable.createTable(tableName); | ||
app.populateTable.populateTable(tableName); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} catch (ApplicationException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package excel2db; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
|
||
public class InsertDBRecord extends Thread { | ||
|
||
public InsertDBRecord(PreparedStatement insertDBRecordStatement) { | ||
this.insertDBRecordStatement = insertDBRecordStatement; | ||
} | ||
|
||
public PreparedStatement insertDBRecordStatement; | ||
|
||
public static final Logger logger = LoggerFactory.getLogger(InsertDBRecord.class); | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
logger.info("Thread " + getName() + " started"); | ||
insertDBRecordStatement.execute(); | ||
logger.info("Thread " + getName() + " finished"); | ||
} catch (SQLException e) { | ||
//Log the error somehow | ||
} | ||
// catch (InterruptedException e) { | ||
// //Log the error somehow | ||
// } | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,186 +1,114 @@ | ||
|
||
package excel2db; | ||
|
||
import java.io.File; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
import com.ge.mdm.tools.common.ApplicationException; | ||
import com.ge.mdm.tools.common.SheetEntityManager; | ||
import excel2db.service.CreateTable; | ||
import excel2db.service.DBConnection; | ||
import excel2db.service.GenerateFileList; | ||
import excel2db.service.InitConstants; | ||
import excel2db.service.InitInputFiles; | ||
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.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
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.SQLException; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
|
||
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) { | ||
public excel2db(CreateTable createTable, PopulateTable populateTable) { | ||
this.createTable = createTable; | ||
this.populateTable = populateTable; | ||
} | ||
|
||
private Workbook workbook; | ||
|
||
private File inputSheetFile; | ||
private File outputSheetFile; | ||
private String fileExtension; | ||
|
||
//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); | ||
public static Short numberProcessedRecords = Short.valueOf((short) 0); | ||
|
||
|
||
// declaring variables to meet to the Spring framework convention | ||
public DBConnection dbConnection; | ||
public InitInputFiles initInputFiles; | ||
public GenerateFileList generateFileList; | ||
public CreateTable createTable; | ||
public PopulateTable populateTable; | ||
public InitConstants initConstants; | ||
|
||
|
||
public File getInputSheetFile() { return inputSheetFile; } | ||
|
||
public void setInputSheetFile(File inputSheetFile) | ||
{ | ||
this.inputSheetFile = inputSheetFile; | ||
} | ||
|
||
public File getOutputSheetFile() { | ||
return outputSheetFile; | ||
} | ||
|
||
public void setOutputSheetFile(File outputSheetFile) { | ||
this.outputSheetFile = outputSheetFile; | ||
} | ||
|
||
public static ThreadPoolTaskExecutor taskExecutor; | ||
|
||
// declaring setters to meet to the Spring framework convention. | ||
// Setter injection method (SI)) | ||
public void setDbConnection(DBConnection dbConnection) | ||
{ | ||
public void setDbConnection(DBConnection dbConnection) { | ||
this.dbConnection = dbConnection; | ||
} | ||
public void setInitInputFiles(InitInputFiles initInputFiles) { | ||
this.initInputFiles = initInputFiles; | ||
} | ||
public void setGenerateFileList(GenerateFileList generateFileList) { | ||
this.generateFileList = generateFileList; | ||
} | ||
public void setInitConstants(InitConstants initConstants) { | ||
this.initConstants = initConstants; | ||
} | ||
|
||
|
||
public static void main(String[] args) | ||
{ | ||
if (args.length < 1) { | ||
logger.error("Need file name as an argument."); | ||
System.exit(1); | ||
} | ||
|
||
logger.info("excel2db : Passing file name as an argument ; " + String.join(" ", args)); | ||
|
||
File inputFile = new File(args[0]); | ||
public static void main(String[] args) { | ||
|
||
try | ||
{ | ||
try { | ||
|
||
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); | ||
excel2db app = (excel2db)context.getBean("excel2db"); | ||
excel2db app = (excel2db) context.getBean("excel2db"); | ||
|
||
app.setInputSheetFile(inputFile); | ||
app.initAndValidate(); | ||
taskExecutor = (ThreadPoolTaskExecutor) context.getBean("taskExecutor"); | ||
|
||
//this way we call methods for objects that is already initialized by the Spring | ||
app.dbConnection.establishDBConnection(); | ||
app.createTable.createTable(); | ||
app.populateTable.populateTable(); | ||
|
||
// iterate through file name values inn properties file | ||
for (String fileNameValue : app.generateFileList.generateFileList()) { | ||
File fileName = new File(fileNameValue); | ||
taskExecutor.execute(new Excel2dbTask(app, fileName)); | ||
} | ||
|
||
// Wait until all threads are finished | ||
logger.info("Waiting until active threads exist"); | ||
while (excel2db.taskExecutor.getActiveCount() != 0) { | ||
} | ||
logger.info("All threads are inactive"); | ||
|
||
app.closeConnections(); | ||
|
||
} | ||
catch (Exception e) { | ||
} catch (Exception e) { | ||
logger.error("An exception occurred while running application", e); | ||
System.exit(1); | ||
} | ||
|
||
} | ||
|
||
|
||
public void initAndValidate() | ||
throws ApplicationException, IOException | ||
{ | ||
logger.info("Reading input file '{}'...", inputSheetFile.getAbsolutePath()); | ||
|
||
|
||
fileExtension = FilenameUtils.getExtension(inputSheetFile.getName()); | ||
if (fileExtension.equalsIgnoreCase("xlsx")) { | ||
workbook = createWorkbook(openOPCPackage(inputSheetFile)); | ||
} else { | ||
workbook = createWorkbook(openNPOIFSFileSystemPackage(inputSheetFile)); | ||
} | ||
|
||
sheet = workbook.getSheetAt(0); | ||
if (sheet == null) { | ||
throw new ApplicationException("Workbook does not contain a sheet with index 0"); | ||
} | ||
|
||
sheetEntityManager = new SheetEntityManager(sheet); | ||
} | ||
|
||
|
||
private static OPCPackage openOPCPackage(File file) | ||
throws ApplicationException | ||
{ | ||
try | ||
{ | ||
return OPCPackage.open(file); | ||
} catch (InvalidFormatException e) { | ||
throw new ApplicationException("Unable to open OPC package from " + file.getAbsolutePath(), e); | ||
} | ||
} | ||
|
||
private static NPOIFSFileSystem openNPOIFSFileSystemPackage(File file) throws ApplicationException | ||
{ | ||
try { | ||
return new NPOIFSFileSystem(file); | ||
} catch (IOException e) { | ||
throw new ApplicationException("Unable to open NPOIFSFileSystem package from " + file.getAbsolutePath(), e); | ||
} | ||
} | ||
|
||
private static Workbook createWorkbook(OPCPackage pkg) throws ApplicationException | ||
{ | ||
try { | ||
return new XSSFWorkbook(pkg); | ||
} catch (IOException e) { | ||
throw new ApplicationException("Unable to create workbook from OPC package", e); | ||
} | ||
} | ||
|
||
private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationException | ||
{ | ||
try { | ||
return new HSSFWorkbook(pkg); | ||
} catch (IOException e) { | ||
throw new ApplicationException("Unable to create workbook from NPOIFSFileSystem package", e); | ||
} | ||
} | ||
|
||
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()); | ||
|
||
logger.info("Closing task executor "); | ||
taskExecutor.shutdown(); | ||
logger.info("Task executor is stopped"); | ||
|
||
logger.info("The process is completed"); | ||
logger.info("Number of processed records: " + numberProcessedRecords.toString()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package excel2db.service; | ||
|
||
import java.util.HashSet; | ||
|
||
public interface GenerateFileList { | ||
public HashSet<String> generateFileList(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package excel2db.service; | ||
|
||
public class InitConstants { | ||
|
||
public final static String workingDir = System.getProperty("user.dir"); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package excel2db.service; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import com.ge.mdm.tools.common.ApplicationException; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
|
||
public interface InitInputFiles { | ||
public Sheet initInputFiles(File inputFile) throws IOException, ApplicationException; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.