From 9619cd0deb2dce30bb6cc92ea2c05b73c4ca892c Mon Sep 17 00:00:00 2001 From: Tharre Date: Tue, 1 May 2018 22:42:46 +0200 Subject: Add JDBCConnectionManager --- .../groupphase/util/JDBCConnectionManager.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util') diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java new file mode 100644 index 0000000..5494471 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java @@ -0,0 +1,45 @@ +package at.ac.tuwien.sepm.assignment.groupphase.util; + +import java.lang.invoke.MethodHandles; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class JDBCConnectionManager { + + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private static final String CONNECTION_URL = + "jdbc:h2:~/sepm;INIT=RUNSCRIPT FROM 'classpath:sql/database.sql'"; + + private Connection connection; + + public JDBCConnectionManager() { + try { + Class.forName("org.h2.Driver"); + } catch (ClassNotFoundException e) { + LOG.error("Failed to load H2 JDBC driver '{}'", e.getMessage(), e); + throw new IllegalStateException(e); + } + } + + public Connection getConnection() throws SQLException { + if (connection == null) connection = DriverManager.getConnection(CONNECTION_URL); + + return connection; + } + + public void closeConnection() { + if (connection == null) return; + + try { + connection.close(); + } catch (SQLException e) { + LOG.error("Failed to close connection '{}'", e.getMessage(), e); + } + connection = null; + } +} -- cgit v1.2.3-70-g09d2 From 0ab36507fffe98c9ec7d60f29a9c7b0391fc9532 Mon Sep 17 00:00:00 2001 From: Tharre Date: Fri, 4 May 2018 15:17:44 +0200 Subject: Add SpringFXMLLoader --- .../groupphase/util/SpringFXMLLoader.java | 212 +++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/SpringFXMLLoader.java (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util') diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/SpringFXMLLoader.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/SpringFXMLLoader.java new file mode 100644 index 0000000..cb1f510 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/SpringFXMLLoader.java @@ -0,0 +1,212 @@ +package at.ac.tuwien.sepm.assignment.groupphase.util; + +import java.io.IOException; +import java.io.InputStream; +import javafx.fxml.FXMLLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * A Spring Based FXMLLoader. + * + *

Provides the possibility to load FXML-Files and wrap them for convenient access to the loaded + * class as well as the spring loaded controller. + */ +@Component +public class SpringFXMLLoader { + + private Logger LOG = LoggerFactory.getLogger(SpringFXMLLoader.class); + + private ApplicationContext applicationContext; + + @Autowired + public SpringFXMLLoader(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + private FXMLLoader getFXMLLoader() { + final var fxmlLoader = new FXMLLoader(); + fxmlLoader.setControllerFactory(applicationContext::getBean); + return fxmlLoader; + } + + /** + * Load and return an object from an FXML-File. + * + * @param inputStream the input stream of the FXML-File + * @param loadType the class of the object to load + * @param the loaded object + * @return the loaded object + * @throws IOException if the resource could not be loaded + */ + public synchronized TLoad load(InputStream inputStream, Class loadType) + throws IOException { + LOG.trace( + "Loading object of type {} from fxml resource {}", + loadType.getCanonicalName(), + inputStream); + return this.getFXMLLoader().load(inputStream); + } + + /** + * Load and return an object from an FXML-File. + * + * @param pathToFXMLFile path to the FXML-File + * @param loadType the class of the object to load + * @param the loaded object + * @return the loaded object + * @throws IOException if the resource could not be loaded + */ + public TLoad load(String pathToFXMLFile, Class loadType) throws IOException { + return this.load(SpringFXMLLoader.class.getResourceAsStream(pathToFXMLFile), loadType); + } + + /** + * Load and return an object from an FXML-File. + * + * @param inputStream the input stream of the FXML-File + * @return the loaded object + * @throws IOException if the resource could not be loaded + */ + public Object load(InputStream inputStream) throws IOException { + return this.load(inputStream, Object.class); + } + + /** + * Load and return an object from an FXML-File. + * + * @param pathToFXMLFile path to the FXML-File + * @return the loaded object + * @throws IOException if the resource could not be loaded + */ + public Object load(String pathToFXMLFile) throws IOException { + return this.load(SpringFXMLLoader.class.getResourceAsStream(pathToFXMLFile)); + } + + /** + * Load and wrap an object and the declared controller from an FXML-File. + * + * @param inputStream the input stream of the FXML-File + * @param loadType the class of the object to load + * @param controllerType the class of the declared controller of the loaded object + * @param the loaded object + * @param the controller of the loaded object + * @return a wrapper object containing the loaded object and its declared controller + * @throws IOException if the resource could not be loaded + * @see FXMLWrapper + */ + public synchronized FXMLWrapper loadAndWrap( + InputStream inputStream, Class loadType, Class controllerType) + throws IOException { + final var fxmlLoader = this.getFXMLLoader(); + LOG.trace( + "Loading and wrapping object of type {} with controller of type {} from fxml resource {}", + loadType.getCanonicalName(), + controllerType.getCanonicalName(), + inputStream); + return new FXMLWrapper<>(fxmlLoader.load(inputStream), fxmlLoader.getController()); + } + + /** + * Load and wrap an object and the declared controller from an FXML-File. + * + * @param pathToFXMLFile path to the FXML-File + * @param loadType the class of the object to load + * @param controllerType the class of the declared controller of the loaded object + * @param the loaded object + * @param the controller of the loaded object + * @return a wrapper object containing the loaded object and its declared controller + * @throws IOException if the resource could not be loaded + * @see FXMLWrapper + */ + public synchronized FXMLWrapper loadAndWrap( + String pathToFXMLFile, Class loadType, Class controllerType) + throws IOException { + return this.loadAndWrap( + SpringFXMLLoader.class.getResourceAsStream(pathToFXMLFile), + loadType, + controllerType); + } + + /** + * Load and wrap an object and the declared controller from an FXML-File. + * + * @param inputStream the input stream of the FXML-File + * @param controllerType the class of the declared controller of the loaded object + * @param the controller of the loaded object + * @return a wrapper object containing the loaded object and its declared controller + * @throws IOException if the resource could not be loaded + * @see FXMLWrapper + */ + public FXMLWrapper loadAndWrap( + InputStream inputStream, Class controllerType) throws IOException { + return this.loadAndWrap(inputStream, Object.class, controllerType); + } + + /** + * Load and wrap an object and the declared controller from an FXML-File. + * + * @param pathToFXMLFile path to the FXML-File + * @param controllerType the class of the declared controller of the loaded object + * @param the controller of the loaded object + * @return a wrapper object containing the loaded object and its declared controller + * @throws IOException if the resource could not be loaded + * @see FXMLWrapper + */ + public FXMLWrapper loadAndWrap( + String pathToFXMLFile, Class controllerType) throws IOException { + return this.loadAndWrap(pathToFXMLFile, Object.class, controllerType); + } + + /** + * Load and wrap an object and the declared controller from an FXML-File. + * + * @param inputStream the input stream of the FXML-File + * @return a wrapper object containing the loaded object and its declared controller + * @throws IOException if the resource could not be loaded + * @see FXMLWrapper + */ + public FXMLWrapper loadAndWrap(InputStream inputStream) throws IOException { + return this.loadAndWrap(inputStream, Object.class, Object.class); + } + + /** + * Load and wrap an object and the declared controller from an FXML-File. + * + * @param pathToFXMLFile path to the FXML-File + * @return a wrapper object containing the loaded object and its declared controller + * @throws IOException if the resource could not be loaded + * @see FXMLWrapper + */ + public FXMLWrapper loadAndWrap(String pathToFXMLFile) throws IOException { + return this.loadAndWrap(pathToFXMLFile, Object.class, Object.class); + } + + /** + * A wrapper for loading FXML-files and wrapping the loaded object as well as the controller. + * + * @param the loaded object + * @param the controller of the loaded object + */ + public class FXMLWrapper { + + public TLoad getLoadedObject() { + return loadedObject; + } + + public TController getController() { + return controller; + } + + private final TLoad loadedObject; + private final TController controller; + + private FXMLWrapper(TLoad loadedObject, TController controller) { + this.loadedObject = loadedObject; + this.controller = controller; + } + } +} -- cgit v1.2.3-70-g09d2 From f4b5613fd3c6ce8e45fb7b99d10b33bec12ad43c Mon Sep 17 00:00:00 2001 From: Felix Kehrer Date: Sun, 6 May 2018 14:50:50 +0200 Subject: Copied changes from branch employee_list to allow for DAO Tests --- .../assignment/groupphase/util/JDBCConnectionManager.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util') diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java index 5494471..6eb15ec 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java @@ -12,12 +12,17 @@ import org.springframework.stereotype.Component; public class JDBCConnectionManager { private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private static final String CONNECTION_URL = + private static final String DEFAULT_CONNECTION_URL = "jdbc:h2:~/sepm;INIT=RUNSCRIPT FROM 'classpath:sql/database.sql'"; - + private String connectionUrl; private Connection connection; public JDBCConnectionManager() { + this(DEFAULT_CONNECTION_URL); + } + + public JDBCConnectionManager(String connectionUrl) { + this.connectionUrl = connectionUrl; try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { @@ -27,7 +32,7 @@ public class JDBCConnectionManager { } public Connection getConnection() throws SQLException { - if (connection == null) connection = DriverManager.getConnection(CONNECTION_URL); + if (connection == null) connection = DriverManager.getConnection(connectionUrl); return connection; } -- cgit v1.2.3-70-g09d2 From dab91fac74c2c51ad823dc53b68acf5aea5b3111 Mon Sep 17 00:00:00 2001 From: Felix Kehrer Date: Mon, 7 May 2018 17:43:55 +0200 Subject: Change database location to ./ --- .../tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util') diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java index 6eb15ec..0ee3319 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java @@ -13,7 +13,7 @@ public class JDBCConnectionManager { private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static final String DEFAULT_CONNECTION_URL = - "jdbc:h2:~/sepm;INIT=RUNSCRIPT FROM 'classpath:sql/database.sql'"; + "jdbc:h2:./sepm;INIT=RUNSCRIPT FROM 'classpath:sql/database.sql'"; private String connectionUrl; private Connection connection; -- cgit v1.2.3-70-g09d2