-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic class structure for the data exporters, both continuous and int…
…egrated.
- Loading branch information
1 parent
e65898b
commit 67c0e48
Showing
32 changed files
with
627 additions
and
168 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
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
2 changes: 1 addition & 1 deletion
2
...bf/excel/Records/OldDimensionsRecord.java → ...bf/excel/records/OldDimensionsRecord.java
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,117 @@ | ||
package com.dbf.naps.data.db; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.DefaultParser; | ||
import org.apache.commons.cli.ParseException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.dbf.naps.data.BaseOptions; | ||
|
||
public class DBOptions extends BaseOptions { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(DBOptions.class); | ||
|
||
private String dbHost = "localhost"; | ||
private int dbPort = 5432; | ||
private String dbName = "naps"; | ||
private String dbUser = "postgres"; | ||
private String dbPass = "password"; | ||
|
||
static { | ||
getOptions().addOption("dbh","dbHost", true, "Hostname for the PostgreSQL database. Default: localhost"); | ||
getOptions().addOption("dbt","dbPort", true, "Port for the PostgreSQL database. Default: 5432"); | ||
getOptions().addOption("dbn","dbName", true, "Database name for the PostgreSQL database. Default: naps"); | ||
getOptions().addOption("dbu","dbUser", true, "Database user name for the PostgreSQL database. Default: postgres"); | ||
getOptions().addOption("dbp","dbPass", true, "Database password for the PostgreSQL database. Default: password"); | ||
} | ||
|
||
public DBOptions(String[] args) throws IllegalArgumentException { | ||
super(args); | ||
loadFromArgs(args); | ||
} | ||
|
||
private void loadFromArgs(String[] args) throws IllegalArgumentException { | ||
CommandLine cmd = null; | ||
try { | ||
cmd = (new DefaultParser()).parse(getOptions(), args); | ||
} | ||
catch(ParseException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
|
||
loadDBHost(cmd); | ||
loadDBPort(cmd); | ||
loadDBName(cmd); | ||
loadDBUser(cmd); | ||
loadDBPass(cmd); | ||
} | ||
|
||
private void loadDBPort(CommandLine cmd) { | ||
if(cmd.hasOption("dbPort")) { | ||
dbPort = Integer.parseInt(cmd.getOptionValue("dbPort")); | ||
if (dbPort < 0 || dbPort > 65535) { | ||
throw new IllegalArgumentException("DB port number: " + dbPort); | ||
} | ||
log.info("Using DB port number: " + dbPort); | ||
} else { | ||
log.info("Using default DB port number: " + dbPort); | ||
} | ||
} | ||
|
||
private void loadDBHost(CommandLine cmd) { | ||
if(cmd.hasOption("dbHost")) { | ||
dbHost = cmd.getOptionValue("dbHost"); | ||
log.info("Using DB hostname: " + dbHost); | ||
} else { | ||
log.info("Using default DB hostname: " + dbHost); | ||
} | ||
} | ||
|
||
private void loadDBUser(CommandLine cmd) { | ||
if(cmd.hasOption("dbUser")) { | ||
dbUser = cmd.getOptionValue("dbUser"); | ||
log.info("Using DB user name: " + dbUser); | ||
} else { | ||
log.info("Using default DB user name: " + dbUser); | ||
} | ||
} | ||
|
||
private void loadDBPass(CommandLine cmd) { | ||
if(cmd.hasOption("dbPass")) { | ||
dbPass = cmd.getOptionValue("dbPass"); | ||
log.info("Using DB password: " + dbPass); | ||
} else { | ||
log.info("Using default DB password: " + dbPass); | ||
} | ||
} | ||
|
||
private void loadDBName(CommandLine cmd) { | ||
if(cmd.hasOption("dbName")) { | ||
dbName = cmd.getOptionValue("dbName"); | ||
log.info("Using DB name: " + dbName); | ||
} else { | ||
log.info("Using default DB name: " + dbName); | ||
} | ||
} | ||
|
||
public String getDbHost() { | ||
return dbHost; | ||
} | ||
|
||
public int getDbPort() { | ||
return dbPort; | ||
} | ||
|
||
public String getDbName() { | ||
return dbName; | ||
} | ||
|
||
public String getDbUser() { | ||
return dbUser; | ||
} | ||
|
||
public String getDbPass() { | ||
return dbPass; | ||
} | ||
} |
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,76 @@ | ||
package com.dbf.naps.data.db; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.jdbc.ScriptRunner; | ||
import org.apache.ibatis.mapping.Environment; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.dbf.naps.data.NAPSActionBase; | ||
import com.dbf.naps.data.db.mappers.DataMapper; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
|
||
public abstract class NAPSDBAction<O extends DBOptions> extends NAPSActionBase<O> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(NAPSDBAction.class); | ||
|
||
private HikariDataSource dbDataSource; | ||
private SqlSessionFactory sqlSessionFactory; | ||
|
||
public NAPSDBAction(String[] args) { | ||
super(args); | ||
} | ||
|
||
protected void run() { | ||
try | ||
{ | ||
initDB(); | ||
} catch (Throwable t) { | ||
log.error("Unexpected failure initializing the DB.", t); | ||
throw new RuntimeException(t); | ||
} | ||
} | ||
|
||
private void initDB() throws IOException, SQLException { | ||
dbDataSource = new HikariDataSource(); | ||
dbDataSource.setUsername(getOptions().getDbUser()); | ||
dbDataSource.setPassword(getOptions().getDbPass()); | ||
dbDataSource.setSchema(getOptions().getDbName()); | ||
dbDataSource.setDriverClassName("org.postgresql.Driver"); | ||
dbDataSource.setMaximumPoolSize(getOptions().getThreadCount() + 1); | ||
dbDataSource.setMinimumIdle(getOptions().getThreadCount()); | ||
dbDataSource.setJdbcUrl("jdbc:postgresql://" + getOptions().getDbHost() + ":" + getOptions().getDbPort() + "/"); | ||
dbDataSource.setAutoCommit(true); | ||
|
||
JdbcTransactionFactory transactionFactory = new JdbcTransactionFactory(); | ||
Environment environment = new Environment("local", transactionFactory, dbDataSource); | ||
|
||
Configuration configuration = new Configuration(environment); | ||
configuration.addMapper(DataMapper.class); | ||
|
||
for(Class<?> clazz : getDBMappers()) { | ||
configuration.addMapper(clazz); | ||
} | ||
|
||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); | ||
|
||
try (Reader reader = Resources.getResourceAsReader(NAPSDBAction.class.getClassLoader(),"schema/schema.sql")) { | ||
ScriptRunner scriptRunner = new ScriptRunner(dbDataSource.getConnection()); | ||
scriptRunner.runScript(reader); | ||
} | ||
} | ||
|
||
protected abstract List<Class<?>> getDBMappers(); | ||
|
||
public SqlSessionFactory getSqlSessionFactory() { | ||
return sqlSessionFactory; | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
...ader/continuous/ContinuousDataMapper.java → ...data/db/mappers/ContinuousDataMapper.java
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
6 changes: 3 additions & 3 deletions
6
.../com/dbf/naps/data/loader/DataMapper.java → .../dbf/naps/data/db/mappers/DataMapper.java
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
4 changes: 3 additions & 1 deletion
4
...ader/integrated/IntegratedDataMapper.java → ...data/db/mappers/IntegratedDataMapper.java
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.