diff --git a/src/main/java/de/geofroggerfx/dao/jdbc/DatabaseUpdater.java b/src/main/java/de/geofroggerfx/dao/jdbc/DatabaseUpdater.java new file mode 100644 index 0000000..a38dfde --- /dev/null +++ b/src/main/java/de/geofroggerfx/dao/jdbc/DatabaseUpdater.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) Andreas Billmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package de.geofroggerfx.dao.jdbc; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Created by Andreas on 23.03.2015. + */ +@Service +public class DatabaseUpdater { + + private static final Logger LOGGER = Logger.getLogger("DatabaseUpdater"); + private static final String UPDATE_FOLDER = "/de/geofroggerfx/dao/jdbc/updates/"; + private static final String SQL_EXTENSION = ".sql"; + private JdbcTemplate jdbcTemplate; + + @Autowired + public void setDataConfig(DataConfig dataConfig) { + this.jdbcTemplate = new JdbcTemplate(dataConfig.dataSource()); + } + + public void update() { + Integer version = 1; + + try { + version = jdbcTemplate.queryForObject("SELECT VERSION FROM VERSION", Integer.class); + version++; + } catch (DataAccessException e) { + LOGGER.info("no database version found"); + } + + try { + URL url = getUpdateFileUrl(version); + while(url != null) { + executeSQL(url); + version++; + url = getUpdateFileUrl(version); + } + } catch (URISyntaxException e) { + LOGGER.log(Level.SEVERE, "file error", e); + throw new RuntimeException(e); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "file error", e); + throw new RuntimeException(e); + } + } + + private URL getUpdateFileUrl(Integer version) { + return DatabaseUpdater.class.getResource(UPDATE_FOLDER+version+SQL_EXTENSION); + } + + private void executeSQL(URL url) throws URISyntaxException, IOException { + List lines = Files.readAllLines(Paths.get(url.toURI()), Charset.defaultCharset()); + List statements = extractStatements(lines); + for (String statement: statements) { + jdbcTemplate.execute(statement); + } + } + + private List extractStatements(List lines) { + List statements = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + for (String line: lines) { + sb.append(line.trim()); + if (line.indexOf(';') > -1) { + statements.add(sb.toString()); + sb = new StringBuilder(); + } + } + return statements; + } + + +} diff --git a/src/main/java/de/geofroggerfx/ui/GeoFroggerFX.java b/src/main/java/de/geofroggerfx/ui/GeoFroggerFX.java index 64f53f7..83dd7c0 100644 --- a/src/main/java/de/geofroggerfx/ui/GeoFroggerFX.java +++ b/src/main/java/de/geofroggerfx/ui/GeoFroggerFX.java @@ -25,6 +25,7 @@ */ package de.geofroggerfx.ui; +import de.geofroggerfx.dao.jdbc.DatabaseUpdater; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Parent; @@ -46,28 +47,19 @@ public class GeoFroggerFX extends Application { @Override public void start(Stage primaryStage) throws Exception { - - loadCustomFonts(); - appContext = new AnnotationConfigApplicationContext(GeoFroggerFX.class); String name = appContext.getEnvironment().getProperty("application.name"); String version = appContext.getEnvironment().getProperty("application.version"); - GeoFroggerFXController geoFroggerFXController = appContext.getBean(GeoFroggerFXController.class); - Scene scene = new Scene((Parent) geoFroggerFXController.getView()); + DatabaseUpdater databaseUpdater = appContext.getBean(DatabaseUpdater.class); + try { + databaseUpdater.update(); + loadCustomFonts(); + showUI(primaryStage, name, version); + } catch (Exception e) { + e.printStackTrace(); + } - scene.setOnKeyPressed(new EventHandler() { - - @Override - public void handle(KeyEvent event) { - if (isScenicViewShortcutPressed(event)) { -// ScenicView.show(scene); - } - } - }); - primaryStage.setScene(scene); - primaryStage.setTitle(String.format("%s %s", name, version)); - primaryStage.show(); } @Bean @@ -88,8 +80,12 @@ public class GeoFroggerFX extends Application { launch(args); } - private static boolean isScenicViewShortcutPressed(final KeyEvent keyEvent) { - return keyEvent.isAltDown() && keyEvent.isControlDown() && keyEvent.getCode().equals(KeyCode.V); + private void showUI(Stage primaryStage, String name, String version) { + GeoFroggerFXController geoFroggerFXController = appContext.getBean(GeoFroggerFXController.class); + Scene scene = new Scene((Parent) geoFroggerFXController.getView()); + primaryStage.setScene(scene); + primaryStage.setTitle(String.format("%s %s", name, version)); + primaryStage.show(); } private void loadCustomFonts() { diff --git a/src/main/resources/de/geofroggerfx/dao/jdbc/database.properties b/src/main/resources/de/geofroggerfx/dao/jdbc/database.properties index 933590a..cd3020b 100644 --- a/src/main/resources/de/geofroggerfx/dao/jdbc/database.properties +++ b/src/main/resources/de/geofroggerfx/dao/jdbc/database.properties @@ -1,3 +1,3 @@ -jdbc.url=jdbc:h2:file:C:/billmann/projects/GeoFroggerFX2/geofroggerfx +jdbc.url=jdbc:h2:file:./geofroggerfx jdbc.username = sa jdbc.password = sa \ No newline at end of file