Skip to content

Commit

Permalink
Adding MongoDB implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed Sep 3, 2017
1 parent a4e3ea3 commit 238c190
Show file tree
Hide file tree
Showing 28 changed files with 432 additions and 94 deletions.
3 changes: 1 addition & 2 deletions README
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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-<version>.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.

Expand Down
9 changes: 6 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
group 'excel2db'
version '2.0'


apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'

version '3.0'

buildscript {
repositories {
Expand All @@ -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}"
}


Expand All @@ -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')

Expand Down
43 changes: 43 additions & 0 deletions excel2db.properties
Original file line number Diff line number Diff line change
@@ -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


2 changes: 0 additions & 2 deletions src/main/java/excel2db/Excel2dbTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
32 changes: 21 additions & 11 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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());

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

import java.sql.SQLException;

public interface CloseConnection {

public void closeDBConnection() throws SQLException;

}
6 changes: 4 additions & 2 deletions src/main/java/excel2db/service/DBConnection.java
Original file line number Diff line number Diff line change
@@ -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 ;

}
6 changes: 4 additions & 2 deletions src/main/java/excel2db/service/GetFirstRow.java
Original file line number Diff line number Diff line change
@@ -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;

}
15 changes: 15 additions & 0 deletions src/main/java/excel2db/service/impl/CloseConnectionDerbyImpl.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
18 changes: 18 additions & 0 deletions src/main/java/excel2db/service/impl/CloseConnectionOracleImpl.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
20 changes: 20 additions & 0 deletions src/main/java/excel2db/service/impl/CreateTableMongoDBImpl.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
12 changes: 3 additions & 9 deletions src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -31,8 +24,9 @@ public void establishDBConnection() {

} catch (SQLException e) {
logger.error("Derby Connection Failed! Check output console");

return false;
}
}
return true;
}
}
Loading

0 comments on commit 238c190

Please sign in to comment.