org.bitbucket.bradleysmithllc.etlunit.feature.database.DatabaseConnectionImpl Maven / Gradle / Ivy
package org.bitbucket.bradleysmithllc.etlunit.feature.database;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jackson.JsonLoader;
import org.bitbucket.bradleysmithllc.etlunit.feature.database.db.Database;
import org.bitbucket.bradleysmithllc.etlunit.feature.database.json.DatabaseDefinitionsProperty;
import org.bitbucket.bradleysmithllc.etlunit.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DatabaseConnectionImpl implements DatabaseConnection {
private final String implementationId;
private final String id;
private final String projectVersion;
private final String projectName;
private final String projectUid;
private final String userName;
private final String serverName;
private final int serverPort;
private final String adminUserName;
private final String adminPassword;
private final List ddlScripts = new ArrayList();
private final DatabaseDefinitionsProperty databaseConfiguration;
private final Map implementationProperties;
private Database database;
public DatabaseConnectionImpl(String id, DatabaseDefinitionsProperty databaseConfiguration, DatabaseConfiguration configuration, String projectName, String projectVersion, String userName, String projectUid)
{
this.projectName = projectName;
this.projectVersion = projectVersion;
this.userName = userName;
this.projectUid = projectUid;
this.id = id;
this.databaseConfiguration = databaseConfiguration;
String eImplementationId = databaseConfiguration.getImplementationId();
if (eImplementationId == null)
{
implementationId = configuration.getDefaultImplementationId();
if (implementationId == null)
{
throw new IllegalArgumentException("implementation-id is not specified and no default is provided");
}
}
else
{
implementationId = eImplementationId;
}
implementationProperties = configuration.getEffectiveImplementationProperties(databaseConfiguration);
serverName = configuration.getEffectiveServerName(databaseConfiguration);
serverPort = configuration.getEffectiveServerPort(databaseConfiguration);
List ddl = databaseConfiguration.getSchemaScripts();
if (ddl != null)
{
for (String script : ddl)
{
ddlScripts.add(script);
}
}
adminUserName = configuration.getEffectiveUserName(databaseConfiguration);
adminPassword = configuration.getEffectivePassword(databaseConfiguration);
}
public String getAdminUserName()
{
return adminUserName;
}
public String getAdminPassword()
{
return adminPassword;
}
public String getId()
{
return id;
}
@Override
public void setDatabase(Database database) {
if (this.database != null)
{
throw new IllegalStateException("Database already set");
}
this.database = database;
}
@Override
public Database getDatabase() {
if (database == null)
{
throw new IllegalStateException("Database not yet available");
}
return database;
}
@Override
public boolean hasDatabase() {
return database != null;
}
public String getImplementationId() {
return implementationId;
}
public String getDatabaseName(String mode)
{
return
getUserName() +
"_" + projectName +
"_" + projectVersion +
"_" + StringUtils.sanitize(getId(), '_') +
(mode == null ? "" : ("_" + StringUtils.sanitize(mode, '_'))) +
"_" + projectUid;
}
private String getUserName() {
return userName;
}
public String getLoginName(String mode)
{
return getDatabaseName(mode);
}
public String getPassword(String mode)
{
return getDatabaseName(mode);
}
public String getServerName() {
return serverName;
}
public int getServerPort() {
return serverPort;
}
public List getSchemaScripts() {
return ddlScripts;
}
public DatabaseDefinitionsProperty getDatabaseConfiguration() {
return databaseConfiguration;
}
@Override
public JsonNode getRawDatabaseConfiguration() {
// write the value back to a string and reload
ObjectMapper objectMapper = new ObjectMapper();
String rawJson = null;
try {
rawJson = objectMapper.writeValueAsString(databaseConfiguration);
return JsonLoader.fromString(rawJson);
} catch (Exception e) {
throw new RuntimeException("Error roundtripping json configuration", e);
}
}
@Override
public Map getDatabaseProperties() {
return implementationProperties;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy