added database update mechanism
This commit is contained in:
112
src/main/java/de/geofroggerfx/dao/jdbc/DatabaseUpdater.java
Normal file
112
src/main/java/de/geofroggerfx/dao/jdbc/DatabaseUpdater.java
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Andreas Billmann <abi@geofroggerfx.de>
|
||||||
|
* 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<String> lines = Files.readAllLines(Paths.get(url.toURI()), Charset.defaultCharset());
|
||||||
|
List<String> statements = extractStatements(lines);
|
||||||
|
for (String statement: statements) {
|
||||||
|
jdbcTemplate.execute(statement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> extractStatements(List<String> lines) {
|
||||||
|
List<String> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.geofroggerfx.ui;
|
package de.geofroggerfx.ui;
|
||||||
|
|
||||||
|
import de.geofroggerfx.dao.jdbc.DatabaseUpdater;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
@@ -46,28 +47,19 @@ public class GeoFroggerFX extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) throws Exception {
|
public void start(Stage primaryStage) throws Exception {
|
||||||
|
|
||||||
loadCustomFonts();
|
|
||||||
|
|
||||||
appContext = new AnnotationConfigApplicationContext(GeoFroggerFX.class);
|
appContext = new AnnotationConfigApplicationContext(GeoFroggerFX.class);
|
||||||
String name = appContext.getEnvironment().getProperty("application.name");
|
String name = appContext.getEnvironment().getProperty("application.name");
|
||||||
String version = appContext.getEnvironment().getProperty("application.version");
|
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<KeyEvent>() {
|
|
||||||
|
|
||||||
@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
|
@Bean
|
||||||
@@ -88,8 +80,12 @@ public class GeoFroggerFX extends Application {
|
|||||||
launch(args);
|
launch(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isScenicViewShortcutPressed(final KeyEvent keyEvent) {
|
private void showUI(Stage primaryStage, String name, String version) {
|
||||||
return keyEvent.isAltDown() && keyEvent.isControlDown() && keyEvent.getCode().equals(KeyCode.V);
|
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() {
|
private void loadCustomFonts() {
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
jdbc.url=jdbc:h2:file:C:/billmann/projects/GeoFroggerFX2/geofroggerfx
|
jdbc.url=jdbc:h2:file:./geofroggerfx
|
||||||
jdbc.username = sa
|
jdbc.username = sa
|
||||||
jdbc.password = sa
|
jdbc.password = sa
|
||||||
Reference in New Issue
Block a user