1818
1919import java .io .File ;
2020import java .io .IOException ;
21- import java .io .InputStream ;
22- import java .nio .file .Files ;
2321import java .nio .file .Paths ;
2422import java .sql .SQLException ;
25- import java .util .Properties ;
2623
2724import org .h2 .tools .Script ;
2825import org .slf4j .Logger ;
2926import org .slf4j .LoggerFactory ;
3027import org .springframework .beans .factory .annotation .Autowired ;
3128import org .springframework .stereotype .Component ;
3229
30+ import de .doubleslash .keeptime .ApplicationProperties ;
3331import de .doubleslash .keeptime .common .OS ;
3432import de .doubleslash .keeptime .common .Resources ;
3533import de .doubleslash .keeptime .common .Resources .RESOURCE ;
@@ -118,6 +116,7 @@ public class SettingsController {
118116
119117 private final Controller controller ;
120118 private final Model model ;
119+ private final ApplicationProperties applicationProperties ;
121120
122121 private Stage thisStage ;
123122
@@ -127,9 +126,11 @@ public class SettingsController {
127126 ViewController mainscreen ;
128127
129128 @ Autowired
130- public SettingsController (final Model model , final Controller controller ) {
129+ public SettingsController (final Model model , final Controller controller ,
130+ ApplicationProperties applicationProperties ) {
131131 this .model = model ;
132132 this .controller = controller ;
133+ this .applicationProperties = applicationProperties ;
133134 }
134135
135136 @ FXML
@@ -217,11 +218,11 @@ private void initialize() {
217218 });
218219
219220 LOG .debug ("resetButton.setOnAction" );
220- resetHoverBackgroundButton
221- . setOnAction ( ae -> hoverBackgroundColor .setValue (Model .ORIGINAL_HOVER_BACKGROUND_COLOR ));
221+ resetHoverBackgroundButton . setOnAction (
222+ ae -> hoverBackgroundColor .setValue (Model .ORIGINAL_HOVER_BACKGROUND_COLOR ));
222223 resetHoverFontButton .setOnAction (ae -> hoverFontColor .setValue (Model .ORIGINAL_HOVER_Font_COLOR ));
223- resetDefaultBackgroundButton
224- . setOnAction ( ae -> defaultBackgroundColor .setValue (Model .ORIGINAL_DEFAULT_BACKGROUND_COLOR ));
224+ resetDefaultBackgroundButton . setOnAction (
225+ ae -> defaultBackgroundColor .setValue (Model .ORIGINAL_DEFAULT_BACKGROUND_COLOR ));
225226 resetDefaultFontButton .setOnAction (ae -> defaultFontColor .setValue (Model .ORIGINAL_DEFAULT_FONT_COLOR ));
226227 resetTaskBarFontButton .setOnAction (ae -> taskBarColor .setValue (Model .ORIGINAL_TASK_BAR_FONT_COLOR ));
227228
@@ -237,26 +238,42 @@ private void initExportButton() {
237238 exportButton .setOnAction (actionEvent -> {
238239 LOG .info ("Button pressed: exportButton" );
239240
240- final Properties properties = new Properties ();
241- try (InputStream resourceStream = SettingsController .class .getResourceAsStream ("/application.properties" )) {
242- properties .load (resourceStream );
241+ try {
242+ final String h2Version = applicationProperties .getH2Version ();
243243
244244 final FileChooser fileChooser = new FileChooser ();
245245 fileChooser .setInitialDirectory (Paths .get ("." ).toFile ());
246- final String h2Version = properties .getProperty ("h2.version" );
247246 fileChooser .setInitialFileName (String .format ("KeepTime_database-export_H2-version-%s.sql" , h2Version ));
248- fileChooser .getExtensionFilters ().add (new ExtensionFilter ("SQL scrip files." , "*.sql" ));
249-
247+ fileChooser .getExtensionFilters ().add (new ExtensionFilter ("SQL script files." , "*.sql" ));
250248 final File fileToSave = fileChooser .showSaveDialog (thisStage );
251- Files .deleteIfExists (Paths .get (fileToSave .getAbsolutePath ()));
249+ if (fileToSave == null ) {
250+ LOG .info ("User canceled export." );
251+ return ;
252+ }
252253
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" );
254+ final String url = applicationProperties . getSpringDataSourceUrl ( );
255+ final String username = applicationProperties . getSpringDataSourceUserName ( );
256+ final String password = applicationProperties . getSpringDataSourcePassword ( );
256257
258+ LOG .info ("Exporting database to '{}'." , fileToSave );
257259 Script .process (url , username , password , fileToSave .getAbsolutePath (), "" , "" );
258- } catch (final SQLException | IOException e ) {
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 ) {
259269 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 ();
260277 }
261278 });
262279 }
0 commit comments