From 238c190d6eea2c175a55f33c6f8fd381b967ef19 Mon Sep 17 00:00:00 2001 From: Andrey Bespalov Date: Sun, 3 Sep 2017 21:29:22 +0300 Subject: [PATCH] Adding MongoDB implementation --- README | 3 +- build.gradle | 9 ++- excel2db.properties | 43 +++++++++++++ src/main/java/excel2db/Excel2dbTask.java | 2 - src/main/java/excel2db/excel2db.java | 32 ++++++---- .../excel2db/service/CloseConnection.java | 9 +++ .../java/excel2db/service/DBConnection.java | 6 +- .../java/excel2db/service/GetFirstRow.java | 6 +- .../impl/CloseConnectionDerbyImpl.java | 15 +++++ .../impl/CloseConnectionMongoDBImpl.java | 16 +++++ .../impl/CloseConnectionOracleImpl.java | 18 ++++++ .../impl/CloseConnectionPostgresImpl.java | 18 ++++++ .../service/impl/CreateTableMongoDBImpl.java | 20 ++++++ .../service/impl/DBConnectionDerbyImpl.java | 12 +--- .../service/impl/DBConnectionMongoDBImpl.java | 51 ++++++++++++++++ .../service/impl/DBConnectionOracleImpl.java | 6 +- .../impl/DBConnectionPostgresImpl.java | 4 +- .../service/impl/GetFirstRowDerbyImpl.java | 20 +++++- .../service/impl/GetFirstRowMongoDBImpl.java | 30 +++++++++ .../service/impl/GetFirstRowOracleImpl.java | 42 +++++++++++++ .../service/impl/GetFirstRowPostgresImpl.java | 4 ++ .../service/impl/InitInputFilesImpl.java | 4 +- .../service/impl/PopulateTableDerbyImpl.java | 5 ++ .../impl/PopulateTableMongoDBImpl.java | 61 +++++++++++++++++++ .../service/impl/PopulateTableOracleImpl.java | 17 +----- src/main/resources/excel2db.properties | 15 ++++- src/main/resources/spring.xml | 23 ++----- src/test/java/PopulateCellsImplTest.java | 35 +++++------ 28 files changed, 432 insertions(+), 94 deletions(-) create mode 100644 excel2db.properties create mode 100644 src/main/java/excel2db/service/CloseConnection.java create mode 100644 src/main/java/excel2db/service/impl/CloseConnectionDerbyImpl.java create mode 100644 src/main/java/excel2db/service/impl/CloseConnectionMongoDBImpl.java create mode 100644 src/main/java/excel2db/service/impl/CloseConnectionOracleImpl.java create mode 100644 src/main/java/excel2db/service/impl/CloseConnectionPostgresImpl.java create mode 100644 src/main/java/excel2db/service/impl/CreateTableMongoDBImpl.java create mode 100644 src/main/java/excel2db/service/impl/DBConnectionMongoDBImpl.java create mode 100644 src/main/java/excel2db/service/impl/GetFirstRowMongoDBImpl.java create mode 100644 src/main/java/excel2db/service/impl/GetFirstRowOracleImpl.java create mode 100644 src/main/java/excel2db/service/impl/GetFirstRowPostgresImpl.java create mode 100644 src/main/java/excel2db/service/impl/PopulateTableDerbyImpl.java create mode 100644 src/main/java/excel2db/service/impl/PopulateTableMongoDBImpl.java diff --git a/README b/README index d57fe29..318971a 100644 --- a/README +++ b/README @@ -1,4 +1,3 @@ - How to clone and build the project from the GitHub 1. Create a folder you want to handle the project and clone the @@ -10,7 +9,7 @@ The Excel2Db subfolder should be created. Gradle pre-installed. 3. All is set. The "build/libs/" subfolder should have the newest jar file -generated. By default the subfolder contains the property file pointing to +generated in the excel2db-.jar format. By default the subfolder contains the property file pointing to the Derby database as the testing implementation and test.xlsx as the test Excel file. diff --git a/build.gradle b/build.gradle index c1c6aef..bbfdb32 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,9 @@ group 'excel2db' -version '2.0' - apply plugin: 'java' apply plugin: 'com.github.johnrengelman.shadow' +version '3.0' buildscript { repositories { @@ -30,6 +29,8 @@ shadowJar { // exclude properties file from jar exclude('excel2db.properties') mergeServiceFiles() + //here we set a name format of jar file + archiveName = "excel2db-${version}.${extension}" } @@ -55,8 +56,10 @@ dependencies { compile 'org.apache.directory.studio:org.apache.commons.io:2.4' compile 'org.postgresql:postgresql:42.0.0.jre7' + compile 'org.mongodb:mongodb-driver:3.5.0' + - //jdbc drivers -- oracle, the Oracle driver is officially NOT shared in the Maven Repo + //jdbc drivers -- oracle, the Oracle driver is officially NOT shared in the Maven Repo //because of license aspects compile files('libs/ojdbc6.jar') diff --git a/excel2db.properties b/excel2db.properties new file mode 100644 index 0000000..6349987 --- /dev/null +++ b/excel2db.properties @@ -0,0 +1,43 @@ +#db implementation: Postgres, Oracle, MongoDB, Derby +#use Derby if you run Unit tests and have no Postgres and Oracle installed on you laptop +#in this case the Derby In-Memory database will be created and used during +#the UnitTest execution +db.implementation=MongoDB + +# names of input files +input.files=test.xlsx + + +# postgres connection parameters +db.server=localhost +db.database=excel2db +db.port=5433 +db.user=postgres +db.password=postgres + +# oracle connection parameters +db.oracle.server=localhost +# specify sid or SERVICE NAME here +db.oracle.sid=xe +db.oracle.port=1521 +db.oracle.user=oracle +db.oracle.password=oracle + +# mongo connection parameters +db.mongo.server=localhost +db.mongo.database=test +db.mongo.port=27017 +db.mongo.user=mongo +db.mongo.password=mongo + + + +# DON'T TOUCH : +# set db implementation classes based on the db.implementation +dbConnectionImplenentation=excel2db.service.impl.DBConnection${db.implementation}Impl +closeConnectionImplenentation=excel2db.service.impl.CloseConnection${db.implementation}Impl +createTableImplenentation=excel2db.service.impl.CreateTable${db.implementation}Impl +populateTableImplenentation=excel2db.service.impl.PopulateTable${db.implementation}Impl +getFirstRowImplenentation=excel2db.service.impl.GetFirstRow${db.implementation}Impl + + diff --git a/src/main/java/excel2db/Excel2dbTask.java b/src/main/java/excel2db/Excel2dbTask.java index 9df62b6..4ddc079 100644 --- a/src/main/java/excel2db/Excel2dbTask.java +++ b/src/main/java/excel2db/Excel2dbTask.java @@ -4,8 +4,6 @@ import java.io.IOException; import java.sql.SQLException; -import excel2db.ApplicationException; -import excel2db.excel2db; import org.apache.commons.io.FilenameUtils; import org.apache.poi.ss.usermodel.Sheet; diff --git a/src/main/java/excel2db/excel2db.java b/src/main/java/excel2db/excel2db.java index 819ae75..f0020bf 100644 --- a/src/main/java/excel2db/excel2db.java +++ b/src/main/java/excel2db/excel2db.java @@ -6,6 +6,9 @@ import java.util.HashSet; import java.util.concurrent.TimeUnit; +import com.mongodb.MongoClient; +import com.mongodb.client.MongoDatabase; +import excel2db.service.CloseConnection; import excel2db.service.CreateTable; import excel2db.service.DBConnection; import excel2db.service.GenerateFileList; @@ -31,12 +34,17 @@ public excel2db(CreateTable createTable, PopulateTable populateTable) { this.populateTable = populateTable; } - //vars are used in interface implementations + + //for JDBC connections public static Connection connection = null; + //for MongoDB connection + public static MongoClient mongoClient = null; + public static MongoDatabase mongoDB; // declaring variables to meet to the Spring framework convention public DBConnection dbConnection; + public CloseConnection closeConnection; public InitInputFiles initInputFiles; public GenerateFileList generateFileList; public CreateTable createTable; @@ -53,6 +61,9 @@ public excel2db(CreateTable createTable, PopulateTable populateTable) { public void setDbConnection(DBConnection dbConnection) { this.dbConnection = dbConnection; } + public void setCloseConnection(CloseConnection closeConnection) { + this.closeConnection = closeConnection; + } public void setInitInputFiles(InitInputFiles initInputFiles) { this.initInputFiles = initInputFiles; } @@ -66,13 +77,14 @@ public void setGetFirstRow(GetFirstRow getFirstRow) { this.getFirstRow = getFirstRow; } + private static Boolean isEstablished; public static void main(String[] args) { try { //for profiling, to launch the VisualVM first, - // then start the application by pressing the Enter + //then start the application by pressing the Enter //System.in.read(); ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); @@ -88,13 +100,13 @@ public static void main(String[] args) { } //this way we call methods for objects that is already initialized by the Spring - app.dbConnection.establishDBConnection(); + isEstablished = app.dbConnection.establishDBConnection(); // in case the connection established // iterate through file name values in properties file, for each file start a new thread - if (connection != null) { - for (String fileNameValue : fileList) { + if (isEstablished) { + for (String fileNameValue : fileList) { File fileName = new File(fileNameValue); //check if the file really exists if (fileName.exists() == false) { @@ -114,7 +126,9 @@ public static void main(String[] args) { } logger.info("All threads are inactive"); - app.closeConnections(); + app.closeTasks(); + app.closeConnection.closeDBConnection(); + } } catch (Exception e) { logger.error("An exception occurred while running application", e); @@ -123,14 +137,10 @@ public static void main(String[] args) { } - public void closeConnections() throws ApplicationException, SQLException { - - connection.close(); - logger.info("Connections are closed"); + public void closeTasks() throws ApplicationException, SQLException { taskExecutor.shutdown(); logger.info("Task executor is stopped"); - logger.info("The process is completed. Number of processed records: " + Excel2dbTask.numberOfProcessedRows.toString()); } diff --git a/src/main/java/excel2db/service/CloseConnection.java b/src/main/java/excel2db/service/CloseConnection.java new file mode 100644 index 0000000..7859e1a --- /dev/null +++ b/src/main/java/excel2db/service/CloseConnection.java @@ -0,0 +1,9 @@ +package excel2db.service; + +import java.sql.SQLException; + +public interface CloseConnection { + + public void closeDBConnection() throws SQLException; + +} diff --git a/src/main/java/excel2db/service/DBConnection.java b/src/main/java/excel2db/service/DBConnection.java index bef12ee..9c4ab7f 100644 --- a/src/main/java/excel2db/service/DBConnection.java +++ b/src/main/java/excel2db/service/DBConnection.java @@ -1,10 +1,12 @@ package excel2db.service; -import java.sql.Connection; import java.sql.SQLException; +import com.mongodb.MongoException; +import com.mongodb.MongoWriteConcernException; + public interface DBConnection { - public void establishDBConnection() throws SQLException; + public Boolean establishDBConnection() throws SQLException ; } diff --git a/src/main/java/excel2db/service/GetFirstRow.java b/src/main/java/excel2db/service/GetFirstRow.java index ff9ebcc..f809fc5 100644 --- a/src/main/java/excel2db/service/GetFirstRow.java +++ b/src/main/java/excel2db/service/GetFirstRow.java @@ -1,10 +1,12 @@ package excel2db.service; -import java.sql.ResultSet; import java.sql.SQLException; +import org.json.JSONException; +import org.json.JSONObject; + public interface GetFirstRow { - public ResultSet getFirstRow (String tableName) throws SQLException; + public JSONObject getFirstRow (String tableName) throws SQLException, JSONException; } diff --git a/src/main/java/excel2db/service/impl/CloseConnectionDerbyImpl.java b/src/main/java/excel2db/service/impl/CloseConnectionDerbyImpl.java new file mode 100644 index 0000000..428bc39 --- /dev/null +++ b/src/main/java/excel2db/service/impl/CloseConnectionDerbyImpl.java @@ -0,0 +1,15 @@ +package excel2db.service.impl; + +import java.sql.SQLException; + +import excel2db.excel2db; +import excel2db.service.CloseConnection; + +public class CloseConnectionDerbyImpl implements CloseConnection { + + @Override + public void closeDBConnection() throws SQLException { + excel2db.connection.close(); + } + +} diff --git a/src/main/java/excel2db/service/impl/CloseConnectionMongoDBImpl.java b/src/main/java/excel2db/service/impl/CloseConnectionMongoDBImpl.java new file mode 100644 index 0000000..bb8a1dd --- /dev/null +++ b/src/main/java/excel2db/service/impl/CloseConnectionMongoDBImpl.java @@ -0,0 +1,16 @@ +package excel2db.service.impl; + +import excel2db.excel2db; +import excel2db.service.CloseConnection; + +/** + * Created by Andrey on 8/15/2017. + */ +public class CloseConnectionMongoDBImpl implements CloseConnection{ + + @Override + public void closeDBConnection() { + excel2db.mongoClient.close(); + } + +} diff --git a/src/main/java/excel2db/service/impl/CloseConnectionOracleImpl.java b/src/main/java/excel2db/service/impl/CloseConnectionOracleImpl.java new file mode 100644 index 0000000..8fa2e2e --- /dev/null +++ b/src/main/java/excel2db/service/impl/CloseConnectionOracleImpl.java @@ -0,0 +1,18 @@ +package excel2db.service.impl; + +import java.sql.SQLException; + +import excel2db.excel2db; +import excel2db.service.CloseConnection; + +/** + * Created by Andrey on 8/15/2017. + */ +public class CloseConnectionOracleImpl implements CloseConnection { + + @Override + public void closeDBConnection() throws SQLException { + excel2db.connection.close(); + } + +} diff --git a/src/main/java/excel2db/service/impl/CloseConnectionPostgresImpl.java b/src/main/java/excel2db/service/impl/CloseConnectionPostgresImpl.java new file mode 100644 index 0000000..33ed5db --- /dev/null +++ b/src/main/java/excel2db/service/impl/CloseConnectionPostgresImpl.java @@ -0,0 +1,18 @@ +package excel2db.service.impl; + +import java.sql.SQLException; + +import excel2db.excel2db; +import excel2db.service.CloseConnection; + +/** + * Created by Andrey on 8/15/2017. + */ +public class CloseConnectionPostgresImpl implements CloseConnection{ + + @Override + public void closeDBConnection() throws SQLException { + excel2db.connection.close(); + } + +} \ No newline at end of file diff --git a/src/main/java/excel2db/service/impl/CreateTableMongoDBImpl.java b/src/main/java/excel2db/service/impl/CreateTableMongoDBImpl.java new file mode 100644 index 0000000..2bc08a7 --- /dev/null +++ b/src/main/java/excel2db/service/impl/CreateTableMongoDBImpl.java @@ -0,0 +1,20 @@ +package excel2db.service.impl; + +import com.mongodb.DBCollection; +import com.mongodb.client.MongoDatabase; +import excel2db.excel2db; +import excel2db.service.CreateTable; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xwpf.usermodel.Document; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CreateTableMongoDBImpl implements CreateTable { + + public static final Logger logger = LoggerFactory.getLogger(CreateTableMongoDBImpl.class); + + public void createTable(Sheet sheet, String tableName) { + logger.info("MongoDB creates collections automatically while inserting documents"); + } + +} diff --git a/src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java b/src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java index 9a5528b..3315358 100644 --- a/src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java +++ b/src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java @@ -1,11 +1,5 @@ package excel2db.service.impl; -/** - * Created by Andrey on 6/21/2017. - */ - -import java.sql.Connection; -import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; @@ -14,14 +8,13 @@ import org.apache.derby.jdbc.EmbeddedDriver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; public class DBConnectionDerbyImpl implements DBConnection { public static final Logger logger = LoggerFactory.getLogger(DBConnectionDerbyImpl.class); - public void establishDBConnection() { + public Boolean establishDBConnection() { { try { //initialize an In-Memory Derby database @@ -31,8 +24,9 @@ public void establishDBConnection() { } catch (SQLException e) { logger.error("Derby Connection Failed! Check output console"); - + return false; } } + return true; } } diff --git a/src/main/java/excel2db/service/impl/DBConnectionMongoDBImpl.java b/src/main/java/excel2db/service/impl/DBConnectionMongoDBImpl.java new file mode 100644 index 0000000..9d8020e --- /dev/null +++ b/src/main/java/excel2db/service/impl/DBConnectionMongoDBImpl.java @@ -0,0 +1,51 @@ +package excel2db.service.impl; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import excel2db.excel2db; +import excel2db.service.DBConnection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; + + +public class DBConnectionMongoDBImpl implements DBConnection { + + // connection parameters + @Value("${db.mongo.server}") + String dbServer; + + @Value("${db.mongo.database}") + String dbDatabase; + + @Value("${db.mongo.user}") + String dbUser; + + @Value("${db.mongo.password}") + String dbPassword; + + @Value("${db.mongo.port}") + String dbPort; + + public static final Logger logger = LoggerFactory.getLogger(DBConnectionMongoDBImpl.class); + + public Boolean establishDBConnection() { + try { + MongoClientURI mongoClientURI = new MongoClientURI( + "mongodb://" + dbUser + ":" + dbPassword + "@" + dbServer + ":" + dbPort + "/" + dbDatabase); + excel2db.mongoClient = new MongoClient(mongoClientURI); + //point to the database + excel2db.mongoDB = excel2db.mongoClient.getDatabase(dbDatabase); + + //this way we initialize the lazy MongoDB connection which wasn't actually established, known feature + excel2db.mongoClient.getAddress(); + + logger.info("MongoDB connection is established"); + + } catch (Exception e ) { + logger.info("Error while connecting to MongoDB instance"); + return false; + } + return true; + } +} diff --git a/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java b/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java index dffa09f..0427a40 100644 --- a/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java +++ b/src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java @@ -33,18 +33,20 @@ public class DBConnectionOracleImpl implements DBConnection { public static final Logger logger = LoggerFactory.getLogger(DBConnectionOracleImpl.class); - public void establishDBConnection() { + public Boolean establishDBConnection() { { try { excel2db.connection = DriverManager.getConnection( "jdbc:oracle:thin:@//" + dbServer + ":" + dbPort + "/" + dbSid, dbUser, dbPassword); logger.info("Oracle connection is established"); + } catch (SQLException e) { logger.error("Oracle Connection Failed! Check output console"); + return false; } } - + return true; } } diff --git a/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java b/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java index 2b793e1..8b072a5 100644 --- a/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java +++ b/src/main/java/excel2db/service/impl/DBConnectionPostgresImpl.java @@ -31,7 +31,7 @@ public class DBConnectionPostgresImpl implements DBConnection { public static final Logger logger = LoggerFactory.getLogger(DBConnectionPostgresImpl.class); - public void establishDBConnection() throws SQLException { + public Boolean establishDBConnection() throws SQLException { //TODO : try to use the try-catch-resource construction, connection object should be closed as soon as the program stops working with this object try { @@ -40,8 +40,10 @@ public void establishDBConnection() throws SQLException { logger.info("Postgres connection is established"); } catch (SQLException e) { logger.error("Postgres Connection Failed! Check output console."); + return false; } + return true; } } diff --git a/src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java b/src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java index 3a8d63e..976e636 100644 --- a/src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java +++ b/src/main/java/excel2db/service/impl/GetFirstRowDerbyImpl.java @@ -2,23 +2,37 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import excel2db.excel2db; import excel2db.service.GetFirstRow; +import org.json.JSONException; +import org.json.JSONObject; public class GetFirstRowDerbyImpl implements GetFirstRow { ResultSet firstRow ; + JSONObject jsonResultSet = new JSONObject(); + int numColumns; @Override - public ResultSet getFirstRow(String tableName) throws SQLException { + public JSONObject getFirstRow(String tableName) throws SQLException, JSONException { String sqlSelectFirstRow = "SELECT * FROM " + tableName + " FETCH FIRST ROW ONLY"; PreparedStatement pstmtSelectFirstRow = excel2db.connection.prepareStatement(sqlSelectFirstRow); firstRow = pstmtSelectFirstRow.executeQuery(); - return firstRow; - } + if (firstRow.next()) { + ResultSetMetaData rsmd = firstRow.getMetaData(); + numColumns = rsmd.getColumnCount(); + + for (int i=numColumns; i>0; i--) { + String column_name = rsmd.getColumnName(i); + jsonResultSet.put(column_name, firstRow.getObject(column_name)); + } + } + return jsonResultSet; + } } diff --git a/src/main/java/excel2db/service/impl/GetFirstRowMongoDBImpl.java b/src/main/java/excel2db/service/impl/GetFirstRowMongoDBImpl.java new file mode 100644 index 0000000..086b67e --- /dev/null +++ b/src/main/java/excel2db/service/impl/GetFirstRowMongoDBImpl.java @@ -0,0 +1,30 @@ +package excel2db.service.impl; + +import com.mongodb.client.MongoCollection; +import excel2db.service.GetFirstRow; +import org.bson.Document; +import org.json.JSONArray; +import com.mongodb.util.JSON; +import org.json.JSONException; +import org.json.JSONObject; + +import static excel2db.excel2db.mongoDB; + +public class GetFirstRowMongoDBImpl implements GetFirstRow { + + public JSONObject getFirstRow(String tableName) throws JSONException { + + //get the collection + MongoCollection mongoCollection = mongoDB.getCollection("test"); + //get the first document + Document firstDoc = mongoCollection.find().first(); + + JSON json =new JSON(); + String convertedJson = json.serialize(firstDoc); + + JSONObject returnValue = new JSONObject(convertedJson); + + return returnValue; + + } +} diff --git a/src/main/java/excel2db/service/impl/GetFirstRowOracleImpl.java b/src/main/java/excel2db/service/impl/GetFirstRowOracleImpl.java new file mode 100644 index 0000000..dcddbf9 --- /dev/null +++ b/src/main/java/excel2db/service/impl/GetFirstRowOracleImpl.java @@ -0,0 +1,42 @@ +package excel2db.service.impl; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import excel2db.excel2db; +import excel2db.service.GetFirstRow; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class GetFirstRowOracleImpl implements GetFirstRow { + + ResultSet firstRow ; + JSONObject jsonResultSet = new JSONObject(); + int numColumns; + + + public JSONObject getFirstRow(String tableName) throws SQLException, JSONException { + + String sqlSelectFirstRow = "SELECT * FROM " + tableName + " WHERE ROWNUM = 1"; + PreparedStatement pstmtSelectFirstRow = excel2db.connection.prepareStatement(sqlSelectFirstRow); + firstRow = pstmtSelectFirstRow.executeQuery(); + + if (firstRow.next()) { + ResultSetMetaData rsmd = firstRow.getMetaData(); + numColumns = rsmd.getColumnCount(); + + for (int i=numColumns; i>0; i--) { + String column_name = rsmd.getColumnName(i); + jsonResultSet.put(column_name, firstRow.getObject(column_name)); + } + } + + return jsonResultSet; + + + } + + +} diff --git a/src/main/java/excel2db/service/impl/GetFirstRowPostgresImpl.java b/src/main/java/excel2db/service/impl/GetFirstRowPostgresImpl.java new file mode 100644 index 0000000..cb77e05 --- /dev/null +++ b/src/main/java/excel2db/service/impl/GetFirstRowPostgresImpl.java @@ -0,0 +1,4 @@ +package excel2db.service.impl; + +public class GetFirstRowPostgresImpl extends GetFirstRowDerbyImpl { +} diff --git a/src/main/java/excel2db/service/impl/InitInputFilesImpl.java b/src/main/java/excel2db/service/impl/InitInputFilesImpl.java index e306529..24ab39a 100644 --- a/src/main/java/excel2db/service/impl/InitInputFilesImpl.java +++ b/src/main/java/excel2db/service/impl/InitInputFilesImpl.java @@ -42,7 +42,7 @@ public void setInputSheetFile(File inputSheetFile) { public static final Logger logger = LoggerFactory.getLogger(InitInputFilesImpl.class); - //fabric method for + //fabric method pattern, we don't know which object should be created //in advance and define the object type based on an extension @Override @@ -124,7 +124,7 @@ public static Map readSheetHeader(Sheet sheet) { if(cell.getCellType() == Cell.CELL_TYPE_STRING) { String name = cell.getStringCellValue(); if(! name.equals("")) { - name = name.toUpperCase(); + //name = name.toUpperCase(); if(! header.containsKey(name)) { header.put(name, cell.getColumnIndex()); } else { diff --git a/src/main/java/excel2db/service/impl/PopulateTableDerbyImpl.java b/src/main/java/excel2db/service/impl/PopulateTableDerbyImpl.java new file mode 100644 index 0000000..34f65f8 --- /dev/null +++ b/src/main/java/excel2db/service/impl/PopulateTableDerbyImpl.java @@ -0,0 +1,5 @@ +package excel2db.service.impl; + +public class PopulateTableDerbyImpl extends PopulateTablePostgresImpl { + //Derby is the same as Postgres +} diff --git a/src/main/java/excel2db/service/impl/PopulateTableMongoDBImpl.java b/src/main/java/excel2db/service/impl/PopulateTableMongoDBImpl.java new file mode 100644 index 0000000..d18ed83 --- /dev/null +++ b/src/main/java/excel2db/service/impl/PopulateTableMongoDBImpl.java @@ -0,0 +1,61 @@ +package excel2db.service.impl; + +import java.util.ArrayList; +import java.util.Iterator; + +import com.mongodb.client.MongoCollection; +import excel2db.service.PopulateTable; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.bson.Document; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static excel2db.excel2db.mongoDB; + + +public class PopulateTableMongoDBImpl implements PopulateTable { + + public static final Logger logger = LoggerFactory.getLogger(PopulateTableMongoDBImpl.class); + + private Integer numOfProcessedRows = 0; + + + public Integer populateTable(Sheet sheet, String tableName){ + + Iterator rowIterator = InitInputFilesImpl.sheet.rowIterator(); + + Row row = (Row) rowIterator.next(); + + //get column names from a header + short minColIdx = row.getFirstCellNum(); + short maxColIdx = row.getLastCellNum(); + + ArrayList coulumnNameList = new ArrayList(); + String columnName; + + logger.info("The table {} is being populated", tableName); + + //populate a list of column names + for (short colIdx = minColIdx; colIdx < maxColIdx; colIdx = (short) (colIdx + 1)) { + coulumnNameList.add(row.getCell(colIdx) == null? "": row.getCell(colIdx).toString()); + } + + while (rowIterator.hasNext()) { + + Document document = new Document(); + Row rowData = (Row) rowIterator.next(); + + numOfProcessedRows++; + for (short colIdx = minColIdx; colIdx < maxColIdx; colIdx = (short) (colIdx + 1)) { + document.put(coulumnNameList.get(colIdx), rowData.getCell(colIdx).toString()); + } + + //save the document into a collection, point to the database + MongoCollection mongoCollection = mongoDB.getCollection(tableName); + mongoCollection.insertOne(document); + + } + return numOfProcessedRows; + } +} diff --git a/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java b/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java index 023d4d9..8bee772 100644 --- a/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java +++ b/src/main/java/excel2db/service/impl/PopulateTableOracleImpl.java @@ -6,19 +6,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class PopulateTableOracleImpl implements PopulateTable { - - public static final Logger logger = LoggerFactory.getLogger(PopulateTableOracleImpl.class); - - private Integer numOfProcessedRows = 0; - - public Integer populateTable(Sheet sheet, String tableName) - throws SQLException { - - logger.info("Populate table method : Oracle implementation is not covered yet"); - - return numOfProcessedRows; - - } - +public class PopulateTableOracleImpl extends PopulateTablePostgresImpl { + //implementation is the same as Postgres implementation } diff --git a/src/main/resources/excel2db.properties b/src/main/resources/excel2db.properties index 1196c0d..b54b515 100644 --- a/src/main/resources/excel2db.properties +++ b/src/main/resources/excel2db.properties @@ -1,12 +1,13 @@ -#db implementation: Postgres, Oracle, Derby +#db implementation: Postgres, Oracle, MongoDB, Derby #use Derby if you run Unit tests and have no Postgres and Oracle installed on you laptop #in this case the Derby In-Memory database will be created and used during #the UnitTest execution db.implementation=Derby -# names of input tables +# names of input files input.files=test.xlsx + # postgres connection parameters db.server=localhost db.database=excel2db @@ -22,11 +23,21 @@ db.oracle.port=1521 db.oracle.user=oracle db.oracle.password=oracle +# mongo connection parameters +db.mongo.server=localhost +db.mongo.database=test +db.mongo.port=27017 +db.mongo.user=mongo +db.mongo.password=mongo + + # DON'T TOUCH : # set db implementation classes based on the db.implementation dbConnectionImplenentation=excel2db.service.impl.DBConnection${db.implementation}Impl +closeConnectionImplenentation=excel2db.service.impl.CloseConnection${db.implementation}Impl createTableImplenentation=excel2db.service.impl.CreateTable${db.implementation}Impl +populateTableImplenentation=excel2db.service.impl.PopulateTable${db.implementation}Impl getFirstRowImplenentation=excel2db.service.impl.GetFirstRow${db.implementation}Impl diff --git a/src/main/resources/spring.xml b/src/main/resources/spring.xml index 5285dae..adf16a0 100644 --- a/src/main/resources/spring.xml +++ b/src/main/resources/spring.xml @@ -15,8 +15,8 @@ - + @@ -28,32 +28,21 @@ - - - + - - - + - - - - - - - + + + - - - diff --git a/src/test/java/PopulateCellsImplTest.java b/src/test/java/PopulateCellsImplTest.java index 3dd076d..ec83d9a 100644 --- a/src/test/java/PopulateCellsImplTest.java +++ b/src/test/java/PopulateCellsImplTest.java @@ -1,9 +1,8 @@ import java.io.File; -import java.io.IOException; -import java.sql.ResultSet; import excel2db.excel2db; import org.apache.poi.ss.usermodel.Sheet; +import org.json.JSONObject; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; @@ -23,7 +22,7 @@ public class PopulateCellsImplTest { private Sheet sheet; - private String resultSetValue; + private String resultSetValue = ""; @Test public void testPopulateCellsImpl() throws Exception { @@ -40,26 +39,20 @@ public void testPopulateCellsImpl() throws Exception { app.dbConnection.establishDBConnection(); app.createTable.createTable(sheet, "TEST"); app.populateTable.populateTable(sheet, "TEST"); - ResultSet rs = app.getFirstRow.getFirstRow("TEST"); - - //concatenate values in the result set row - while (rs.next()){ - resultSetValue = - rs.getString("Column1") + " " + - rs.getString("Column2") + " " + - rs.getString("Column3") + " " + - rs.getString("Column4") + " " + - rs.getString("Column5") + " " + - rs.getString("Column6") + " " + - rs.getString("Column7") + " " + - rs.getString("Column8") + " " + - rs.getString("Column9") + " " + - rs.getString("Column10") - ; - } + JSONObject jsonResultSet = app.getFirstRow.getFirstRow("TEST"); //compare with the expected result - assertEquals("Value 11 Value 12 Value 13 Value 14 Value 15 Value 16 Value 17 Value 18 Value 19 Value 110", resultSetValue); + assertEquals( "Value 11", jsonResultSet.getString("Column1")); + assertEquals( "Value 12", jsonResultSet.getString("Column2")); + assertEquals( "Value 13", jsonResultSet.getString("Column3")); + assertEquals( "Value 14", jsonResultSet.getString("Column4")); + assertEquals( "Value 15", jsonResultSet.getString("Column5")); + assertEquals( "Value 16", jsonResultSet.getString("Column6")); + assertEquals( "Value 17", jsonResultSet.getString("Column7")); + assertEquals( "Value 18", jsonResultSet.getString("Column8")); + assertEquals( "Value 19", jsonResultSet.getString("Column9")); + assertEquals( "Value 110", jsonResultSet.getString("Column10")); + }