1616
1717package de .doubleslash .keeptime .view ;
1818
19+ import java .io .File ;
1920import java .io .IOException ;
21+ import java .nio .file .Paths ;
22+ import java .sql .SQLException ;
2023
24+ import org .h2 .tools .Script ;
2125import org .slf4j .Logger ;
2226import org .slf4j .LoggerFactory ;
2327import org .springframework .beans .factory .annotation .Autowired ;
2428import org .springframework .stereotype .Component ;
2529
30+ import de .doubleslash .keeptime .ApplicationProperties ;
2631import de .doubleslash .keeptime .common .OS ;
2732import de .doubleslash .keeptime .common .Resources ;
2833import de .doubleslash .keeptime .common .Resources .RESOURCE ;
4348import javafx .scene .layout .AnchorPane ;
4449import javafx .scene .layout .Region ;
4550import javafx .scene .paint .Color ;
51+ import javafx .stage .FileChooser ;
52+ import javafx .stage .FileChooser .ExtensionFilter ;
4653import javafx .stage .Modality ;
4754import javafx .stage .Stage ;
4855
@@ -91,6 +98,9 @@ public class SettingsController {
9198 @ FXML
9299 private Button cancelButton ;
93100
101+ @ FXML
102+ private Button exportButton ;
103+
94104 @ FXML
95105 private Button aboutButton ;
96106
@@ -106,6 +116,7 @@ public class SettingsController {
106116
107117 private final Controller controller ;
108118 private final Model model ;
119+ private final ApplicationProperties applicationProperties ;
109120
110121 private Stage thisStage ;
111122
@@ -115,9 +126,11 @@ public class SettingsController {
115126 ViewController mainscreen ;
116127
117128 @ Autowired
118- public SettingsController (final Model model , final Controller controller ) {
129+ public SettingsController (final Model model , final Controller controller ,
130+ ApplicationProperties applicationProperties ) {
119131 this .model = model ;
120132 this .controller = controller ;
133+ this .applicationProperties = applicationProperties ;
121134 }
122135
123136 @ FXML
@@ -136,6 +149,8 @@ private void initialize() {
136149 globalKeyloggerLabel .setDisable (true );
137150 }
138151
152+ initExportButton ();
153+
139154 LOG .debug ("saveButton.setOnAction" );
140155 saveButton .setOnAction (ae -> {
141156 LOG .info ("Save clicked" );
@@ -203,11 +218,11 @@ private void initialize() {
203218 });
204219
205220 LOG .debug ("resetButton.setOnAction" );
206- resetHoverBackgroundButton
207- . setOnAction ( ae -> hoverBackgroundColor .setValue (Model .ORIGINAL_HOVER_BACKGROUND_COLOR ));
221+ resetHoverBackgroundButton . setOnAction (
222+ ae -> hoverBackgroundColor .setValue (Model .ORIGINAL_HOVER_BACKGROUND_COLOR ));
208223 resetHoverFontButton .setOnAction (ae -> hoverFontColor .setValue (Model .ORIGINAL_HOVER_Font_COLOR ));
209- resetDefaultBackgroundButton
210- . setOnAction ( ae -> defaultBackgroundColor .setValue (Model .ORIGINAL_DEFAULT_BACKGROUND_COLOR ));
224+ resetDefaultBackgroundButton . setOnAction (
225+ ae -> defaultBackgroundColor .setValue (Model .ORIGINAL_DEFAULT_BACKGROUND_COLOR ));
211226 resetDefaultFontButton .setOnAction (ae -> defaultFontColor .setValue (Model .ORIGINAL_DEFAULT_FONT_COLOR ));
212227 resetTaskBarFontButton .setOnAction (ae -> taskBarColor .setValue (Model .ORIGINAL_TASK_BAR_FONT_COLOR ));
213228
@@ -218,6 +233,51 @@ private void initialize() {
218233 });
219234 }
220235
236+ private void initExportButton () {
237+ LOG .debug ("Initialize exportButton." );
238+ exportButton .setOnAction (actionEvent -> {
239+ LOG .info ("Button pressed: exportButton" );
240+
241+ try {
242+ final String h2Version = applicationProperties .getH2Version ();
243+
244+ final FileChooser fileChooser = new FileChooser ();
245+ fileChooser .setInitialDirectory (Paths .get ("." ).toFile ());
246+ fileChooser .setInitialFileName (String .format ("KeepTime_database-export_H2-version-%s.sql" , h2Version ));
247+ fileChooser .getExtensionFilters ().add (new ExtensionFilter ("SQL script files." , "*.sql" ));
248+ final File fileToSave = fileChooser .showSaveDialog (thisStage );
249+ if (fileToSave == null ) {
250+ LOG .info ("User canceled export." );
251+ return ;
252+ }
253+
254+ final String url = applicationProperties .getSpringDataSourceUrl ();
255+ final String username = applicationProperties .getSpringDataSourceUserName ();
256+ final String password = applicationProperties .getSpringDataSourcePassword ();
257+
258+ LOG .info ("Exporting database to '{}'." , fileToSave );
259+ Script .process (url , username , password , fileToSave .getAbsolutePath (), "DROP" , "" );
260+ LOG .info ("Export done." );
261+
262+ Alert informationDialog = new Alert (AlertType .INFORMATION );
263+ informationDialog .setTitle ("Export done" );
264+ informationDialog .setHeaderText ("The current data was exported." );
265+ informationDialog .setContentText ("The data was exported to '" + fileToSave + "'." );
266+
267+ informationDialog .showAndWait ();
268+ } catch (final SQLException e ) {
269+ LOG .error ("Could not export db to script file." , e );
270+
271+ Alert errorDialog = new Alert (AlertType .ERROR );
272+ errorDialog .setTitle ("Export failed" );
273+ errorDialog .setHeaderText ("The current data could not be exported." );
274+ errorDialog .setContentText ("Please inform a developer and provide your log file." );
275+
276+ errorDialog .showAndWait ();
277+ }
278+ });
279+ }
280+
221281 void update () {
222282 // needed to close stage on esc
223283 settingsRoot .requestFocus ();
0 commit comments