|
16 | 16 |
|
17 | 17 | package de.doubleslash.keeptime.view; |
18 | 18 |
|
| 19 | +import java.io.File; |
19 | 20 | import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.util.Properties; |
20 | 26 |
|
| 27 | +import org.h2.tools.Script; |
21 | 28 | import org.slf4j.Logger; |
22 | 29 | import org.slf4j.LoggerFactory; |
23 | 30 | import org.springframework.beans.factory.annotation.Autowired; |
|
43 | 50 | import javafx.scene.layout.AnchorPane; |
44 | 51 | import javafx.scene.layout.Region; |
45 | 52 | import javafx.scene.paint.Color; |
| 53 | +import javafx.stage.FileChooser; |
| 54 | +import javafx.stage.FileChooser.ExtensionFilter; |
46 | 55 | import javafx.stage.Modality; |
47 | 56 | import javafx.stage.Stage; |
48 | 57 |
|
@@ -91,6 +100,9 @@ public class SettingsController { |
91 | 100 | @FXML |
92 | 101 | private Button cancelButton; |
93 | 102 |
|
| 103 | + @FXML |
| 104 | + private Button exportButton; |
| 105 | + |
94 | 106 | @FXML |
95 | 107 | private Button aboutButton; |
96 | 108 |
|
@@ -136,6 +148,8 @@ private void initialize() { |
136 | 148 | globalKeyloggerLabel.setDisable(true); |
137 | 149 | } |
138 | 150 |
|
| 151 | + initExportButton(); |
| 152 | + |
139 | 153 | LOG.debug("saveButton.setOnAction"); |
140 | 154 | saveButton.setOnAction(ae -> { |
141 | 155 | LOG.info("Save clicked"); |
@@ -218,6 +232,35 @@ private void initialize() { |
218 | 232 | }); |
219 | 233 | } |
220 | 234 |
|
| 235 | + private void initExportButton() { |
| 236 | + LOG.debug("Initialize exportButton."); |
| 237 | + exportButton.setOnAction(actionEvent -> { |
| 238 | + LOG.info("Button pressed: exportButton"); |
| 239 | + |
| 240 | + final Properties properties = new Properties(); |
| 241 | + try (InputStream resourceStream = SettingsController.class.getResourceAsStream("/application.properties")) { |
| 242 | + properties.load(resourceStream); |
| 243 | + |
| 244 | + final FileChooser fileChooser = new FileChooser(); |
| 245 | + fileChooser.setInitialDirectory(Paths.get(".").toFile()); |
| 246 | + final String h2Version = properties.getProperty("h2.version"); |
| 247 | + fileChooser.setInitialFileName(String.format("KeepTime_database-export_H2-version-%s.sql", h2Version)); |
| 248 | + fileChooser.getExtensionFilters().add(new ExtensionFilter("SQL scrip files.", "*.sql")); |
| 249 | + |
| 250 | + final File fileToSave = fileChooser.showSaveDialog(thisStage); |
| 251 | + Files.deleteIfExists(Paths.get(fileToSave.getAbsolutePath())); |
| 252 | + |
| 253 | + final String url = properties.getProperty("spring.datasource.url"); |
| 254 | + final String username = properties.getProperty("spring.datasource.username"); |
| 255 | + final String password = properties.getProperty("spring.datasource.password"); |
| 256 | + |
| 257 | + Script.process(url, username, password, fileToSave.getAbsolutePath(), "", ""); |
| 258 | + } catch (final SQLException | IOException e) { |
| 259 | + LOG.error("Could not export db to script file.", e); |
| 260 | + } |
| 261 | + }); |
| 262 | + } |
| 263 | + |
221 | 264 | void update() { |
222 | 265 | // needed to close stage on esc |
223 | 266 | settingsRoot.requestFocus(); |
|
0 commit comments