Skip to content

Commit

Permalink
Submitting the current version: additional implementations of Oracle …
Browse files Browse the repository at this point in the history
…classes (empty)
  • Loading branch information
palych-piter committed May 26, 2017
1 parent 2077eae commit 2d8383a
Show file tree
Hide file tree
Showing 33 changed files with 340 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gradle/2.13/taskArtifacts/cache.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#Sat Apr 01 20:46:15 MSK 2017
1 change: 1 addition & 0 deletions .gradle/3.1/taskArtifacts/cache.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#Mon Mar 27 14:28:08 MSK 2017
2 changes: 2 additions & 0 deletions .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Fri Mar 24 14:21:21 MSK 2017
gradle.version=3.4.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Tue Mar 28 13:08:49 MSK 2017
gradle.version=3.4.1
17 changes: 17 additions & 0 deletions build/resources/main/excel2db.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# names of input tables
input.files=test-input.xls,test-input1.xls

# db implementation classes

# postgres section
# dbConnectionImplenentation=excel2db.service.impl.DBConnectionPostgresImpl
# oracle section
dbConnectionImplenentation=excel2db.service.impl.DBConnectionOracleImpl

# postgres connection parameters
db.server=localhost
db.database=excel2db
db.port=5433
db.user=postgres
db.password=postgres

Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.ge.mdm.tools.common;

/**
* @author Stanislav Mamontov
*/
public class ApplicationException extends Exception {
static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

import java.util.*;

/**
* @author Stanislav Mamontov
*/
public class AuthOutInterceptor extends AbstractPhaseInterceptor<Message> {

private static final Logger logger = LoggerFactory.getLogger(AuthOutInterceptor.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* Read Excel files in JPA way, considering sheet as a simple database table with columns and rows.
* First row is assumed to be header containing column names, which could be mapped to JPA @Entity.
*
* @author Stanislav Mamontov
*/
public class SheetEntityManager implements EntityManager {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* @author Stanislav Mamontov
*/

public class TaskExecutor<T extends Runnable> {

private int poolSize;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/excel2db/ApplicationContextUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package excel2db;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextUtils implements ApplicationContextAware {

private static ApplicationContext ctx;

@Override
public void setApplicationContext(ApplicationContext appContext)
throws BeansException {
ctx = appContext;

}

public static ApplicationContext getApplicationContext() {
return ctx;
}
}
25 changes: 25 additions & 0 deletions src/main/java/excel2db/ApplicationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package excel2db;

/**
* Created by Andrey on 5/26/2017.
*/

public class ApplicationException extends Exception {
static final long serialVersionUID = 1L;

public ApplicationException() {
super();
}

public ApplicationException(String message) {
super(message);
}

public ApplicationException(String message, Throwable cause) {
super(message, cause);
}

public ApplicationException(Throwable cause) {
super(cause);
}
}
13 changes: 9 additions & 4 deletions src/main/java/excel2db/Excel2dbTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.io.IOException;
import java.sql.SQLException;

import com.ge.mdm.tools.common.ApplicationException;
import excel2db.ApplicationException;
import excel2db.excel2db;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.Sheet;

public class Excel2dbTask extends Thread {

Expand All @@ -16,17 +18,20 @@ public Excel2dbTask(excel2db app, File fileName) {

public excel2db app;
public File fileName;
private Sheet sheet;

static public Integer numberOfProcessedRows = 0;

String tableName;

@Override
public void run() {
try {
app.initInputFiles.initInputFiles(fileName);

sheet = app.initInputFiles.initInputFiles(fileName);
tableName = FilenameUtils.removeExtension(fileName.getName());
app.createTable.createTable(tableName);
app.populateTable.populateTable(tableName);
app.createTable.createTable(sheet, tableName);
numberOfProcessedRows = app.populateTable.populateTable(sheet, tableName);

} catch (IOException e) {
e.printStackTrace();
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import java.sql.Connection;
import java.sql.SQLException;

import com.ge.mdm.tools.common.ApplicationException;
import excel2db.ApplicationException;
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 excel2db.Excel2dbTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
Expand All @@ -31,10 +32,8 @@ public excel2db(CreateTable createTable, PopulateTable populateTable) {
this.populateTable = populateTable;
}


//vars are used in interface implementations
public static Connection connection = null;
public static Short numberProcessedRecords = Short.valueOf((short) 0);


// declaring variables to meet to the Spring framework convention
Expand Down Expand Up @@ -68,6 +67,11 @@ public static void main(String[] args) {

try {


//for profiling, to launch the VisualVM first,
// then start the application by pressing the Enter
//System.in.read();

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
excel2db app = (excel2db) context.getBean("excel2db");

Expand All @@ -76,7 +80,8 @@ public static void main(String[] args) {
//this way we call methods for objects that is already initialized by the Spring
app.dbConnection.establishDBConnection();

// iterate through file name values inn properties file
// iterate through file name values in properties file,
// for each file start a new thread
for (String fileNameValue : app.generateFileList.generateFileList()) {
File fileName = new File(fileNameValue);
taskExecutor.execute(new Excel2dbTask(app, fileName));
Expand All @@ -97,17 +102,15 @@ public static void main(String[] args) {

}


public void closeConnections() throws ApplicationException, SQLException {
logger.info("Closing connections");

connection.close();
logger.info("Connections are closed");

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());
logger.info("The process is completed. Number of processed records: " + Excel2dbTask.numberOfProcessedRows.toString());

}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/excel2db/service/CreateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.sql.SQLException;

import org.apache.poi.ss.usermodel.Sheet;

public interface CreateTable {

public void createTable(String tableName) throws SQLException;
public void createTable(Sheet sheet, String tableName) throws SQLException;

}
2 changes: 1 addition & 1 deletion src/main/java/excel2db/service/InitInputFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.File;
import java.io.IOException;

import com.ge.mdm.tools.common.ApplicationException;
import excel2db.ApplicationException;
import org.apache.poi.ss.usermodel.Sheet;

public interface InitInputFiles {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/excel2db/service/PopulateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.sql.SQLException;

import org.apache.poi.ss.usermodel.Sheet;

public interface PopulateTable {

public Integer populateTable(String tableName) throws SQLException;
public Integer populateTable(Sheet sheet, String tableName) throws SQLException;

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

import java.sql.SQLException;
import excel2db.service.CreateTable;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateTableOracleImpl implements CreateTable {

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

public void createTable(Sheet sheet, String tableName) throws SQLException {

logger.info("Create table method : Oracle implementation is not covered yet");

}

}
17 changes: 12 additions & 5 deletions src/main/java/excel2db/service/impl/CreateTablePostgresImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

import com.ge.mdm.tools.common.SheetEntityManager;
//import com.ge.mdm.tools.common.SheetEntityManager;
import excel2db.excel2db;
import excel2db.service.CreateTable;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateTablePostgresImpl implements CreateTable {

public static final Logger logger = LoggerFactory.getLogger(CreateTablePostgresImpl.class);
private SheetEntityManager sheetEntityManager;
//private SheetEntityManager sheetEntityManager;

public void createTable(String tableName) throws SQLException {
public void createTable(Sheet sheet, String tableName) throws SQLException {

//sheetEntityManager = new SheetEntityManager(sheet);
//initializing var for a header
Map<String, Integer> header = new LinkedHashMap<>();
header = InitInputFilesImpl.readSheetHeader(sheet);

sheetEntityManager = new SheetEntityManager(InitInputFilesImpl.sheet);

//dropping statement
String sqlTableDropStatement = "DROP TABLE IF EXISTS \"" +
Expand All @@ -26,7 +33,7 @@ public void createTable(String tableName) throws SQLException {
StringBuilder sqlTableCreateStatement = new StringBuilder();
sqlTableCreateStatement.append("CREATE TABLE \"" +
tableName + "\"(");
for (String headerColumnName : sheetEntityManager.header.keySet()) {
for (String headerColumnName : header.keySet()) {
sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " text, ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class DBConnectionPostgresImpl implements DBConnection {
@Value("${db.database}")
String dbDatabase;


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

public void establishDBConnection() {
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/excel2db/service/impl/InitInputFilesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import com.ge.mdm.tools.common.ApplicationException;
import excel2db.ApplicationException;
import excel2db.service.InitInputFiles;
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 Down Expand Up @@ -99,5 +104,38 @@ private static Workbook createWorkbook(NPOIFSFileSystem pkg) throws ApplicationE
}
}

public static Map<String, Integer> readSheetHeader(Sheet sheet) {
// first row is always considered as header
Row firstRow = sheet.getRow(sheet.getFirstRowNum());
if(firstRow == null) {
throw new IllegalArgumentException("Sheet is empty");
}

Map<String, Integer> header = new LinkedHashMap<String, Integer>();

Iterator<Cell> it = firstRow.cellIterator();

while(it.hasNext()) {
Cell cell = it.next();

if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
String name = cell.getStringCellValue();
if(! name.equals("")) {
name = name.toUpperCase();
if(! header.containsKey(name)) {
header.put(name, cell.getColumnIndex());
} else {
logger.warn("Ignoring duplicate header name {} at ({}, {})",
name, cell.getRowIndex(), cell.getColumnIndex());
}
}
} else {
logger.warn("Ignoring header cell with non-string type {} at ({}, {})",
cell.getCellType(), cell.getRowIndex(), cell.getColumnIndex());
}

}
return header;
}

}
Loading

0 comments on commit 2d8383a

Please sign in to comment.