-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ec413b
commit 74a3083
Showing
26 changed files
with
472 additions
and
349 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
95 changes: 35 additions & 60 deletions
95
src/main/java/ch/admin/bar/siardsuite/database/DatabaseConnectionFactory.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,50 @@ | ||
package ch.admin.bar.siardsuite.database; | ||
|
||
import ch.admin.bar.siard2.api.Archive; | ||
import ch.admin.bar.siardsuite.model.Model; | ||
import ch.admin.bar.siardsuite.util.UserPreferences; | ||
import ch.admin.bar.siardsuite.database.model.DbmsConnectionData; | ||
import ch.admin.bar.siardsuite.util.preferences.UserPreferences; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
import static ch.admin.bar.siardsuite.util.UserPreferences.KeyIndex.LOGIN_TIMEOUT; | ||
import static ch.admin.bar.siardsuite.util.UserPreferences.NodePath.OPTIONS; | ||
|
||
@Slf4j | ||
public class DatabaseConnectionFactory { | ||
private static DatabaseConnectionFactory instance; | ||
private static Connection connection; | ||
private static Model model; | ||
|
||
private DatabaseConnectionFactory(Model model) throws SQLException { | ||
DatabaseConnectionFactory.model = model; | ||
loadDriver(DatabaseConnectionFactory.model.getDatabaseProps().product()); | ||
DriverManager.setLoginTimeout(Integer.parseInt(UserPreferences.node(OPTIONS).get(LOGIN_TIMEOUT.name(), "0"))); | ||
connection = DriverManager.getConnection(model.getConnectionUrl().get(), | ||
model.getDatabaseUsername().get(), model.getDatabasePassword()); | ||
} | ||
|
||
public static DatabaseConnectionFactory getInstance(Model model) throws SQLException { | ||
if (instance == null || connection.isClosed()) { | ||
instance = new DatabaseConnectionFactory(model); | ||
} | ||
return instance; | ||
} | ||
|
||
public DatabaseLoadService createDatabaseLoader(final Archive archive, boolean onlyMetaData, boolean viewsAsTables) { | ||
return new DatabaseLoadService(connection, model, archive, onlyMetaData, viewsAsTables); | ||
} | ||
|
||
public DatabaseUploadService createDatabaseUploader() { | ||
return new DatabaseUploadService(connection, model); | ||
} | ||
|
||
public static void disconnect() { | ||
if (connection != null) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
public Connection createConnection(final DbmsConnectionData dbmsConnectionData) { | ||
val options = UserPreferences.getStoredOptions(); | ||
|
||
loadDriverByClassName(dbmsConnectionData.getDbms().getDriverClassName()); | ||
DriverManager.setLoginTimeout(options.getLoginTimeout()); | ||
|
||
try { | ||
return DriverManager.getConnection( | ||
dbmsConnectionData.getJdbcConnectionString(), | ||
dbmsConnectionData.getUser(), | ||
dbmsConnectionData.getPassword()); | ||
} catch (SQLException e) { | ||
throw new DatabaseConnectionException( | ||
String.format("Failed to create connection '%s'", dbmsConnectionData), | ||
e); | ||
} | ||
} | ||
} | ||
|
||
private void loadDriver(String product) { | ||
Properties properties = new Properties(); | ||
try (InputStream is = getClass().getResourceAsStream("driver.properties")) { | ||
properties.load(is); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
String jdbcDriverClass = properties.getProperty(product); | ||
if (jdbcDriverClass != null) { | ||
try { | ||
Class.forName(jdbcDriverClass); | ||
} catch (ClassNotFoundException var7) { | ||
throw new RuntimeException("Driver " + jdbcDriverClass + " could not be loaded!"); | ||
} | ||
} else { | ||
throw new RuntimeException("No driver found for sub scheme \"" + product + "\"!"); | ||
private static void loadDriverByClassName(String jdbcDriverClass) { | ||
try { | ||
Class.forName(jdbcDriverClass); | ||
} catch (ClassNotFoundException var7) { | ||
throw new DatabaseConnectionException("Driver " + jdbcDriverClass + " could not be loaded!"); | ||
} | ||
} | ||
|
||
} | ||
private static class DatabaseConnectionException extends RuntimeException { | ||
public DatabaseConnectionException(String message) { | ||
super(message); | ||
} | ||
|
||
public DatabaseConnectionException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/ch/admin/bar/siardsuite/database/DatabaseInteractionService.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package ch.admin.bar.siardsuite.database; | ||
|
||
import ch.admin.bar.siardsuite.database.model.LoadDatabaseInstruction; | ||
import ch.admin.bar.siardsuite.database.model.UploadDatabaseInstruction; | ||
import ch.admin.bar.siardsuite.model.Model; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class DatabaseInteractionService { | ||
|
||
private final DatabaseConnectionFactory connectionFactory; | ||
|
||
@Deprecated // TODO remove model | ||
private final Model model; | ||
|
||
public InstructionExecutionHandle execute(final LoadDatabaseInstruction instruction) { | ||
val tempArchive = model.initArchive(); | ||
val connection = connectionFactory.createConnection(instruction.getConnectionData()); | ||
|
||
val dbLoadService = new DatabaseLoadService( | ||
connection, | ||
model, | ||
tempArchive, | ||
instruction.isLoadOnlyMetadata(), | ||
instruction.isViewsAsTables()); | ||
|
||
dbLoadService.setOnSucceeded(event -> { | ||
instruction.getOnSuccess().handle(event); | ||
close(connection); | ||
}); | ||
dbLoadService.setOnFailed(event -> { | ||
instruction.getOnFailure().handle(event); | ||
close(connection); | ||
}); | ||
|
||
dbLoadService.start(); | ||
|
||
dbLoadService.valueProperty().addListener((observable, oldValue, newValue) -> { | ||
instruction.getOnStepCompleted().changed(observable, oldValue, newValue); | ||
}); | ||
|
||
dbLoadService.progressProperty().addListener((observable, oldValue, newValue) -> { | ||
instruction.getOnProgress().changed(observable, oldValue, newValue); | ||
}); | ||
|
||
return () -> { | ||
dbLoadService.cancel(); | ||
close(connection); | ||
}; | ||
} | ||
|
||
public InstructionExecutionHandle execute(final UploadDatabaseInstruction instruction) { | ||
val connection = connectionFactory.createConnection(instruction.getConnectionData()); | ||
|
||
val dbUploadService = new DatabaseUploadService( | ||
connection, | ||
model); | ||
|
||
dbUploadService.setOnSucceeded(event -> { | ||
instruction.getOnSuccess().handle(event); | ||
close(connection); | ||
}); | ||
dbUploadService.setOnFailed(event -> { | ||
instruction.getOnFailure().handle(event); | ||
close(connection); | ||
}); | ||
|
||
dbUploadService.start(); | ||
|
||
dbUploadService.valueProperty().addListener(instruction.getOnStepCompleted()); | ||
dbUploadService.progressProperty().addListener(instruction.getOnProgress()); | ||
|
||
return () -> { | ||
dbUploadService.cancel(); | ||
close(connection); | ||
}; | ||
} | ||
|
||
public interface InstructionExecutionHandle { | ||
void cancel(); | ||
} | ||
|
||
private static void close(final Connection connection) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException e) { | ||
log.error("Failed to close connection {} because: {}", connection, e); | ||
} | ||
} | ||
} |
Oops, something went wrong.