|
18 | 18 |
|
19 | 19 | import java.io.File; |
20 | 20 | import java.io.IOException; |
| 21 | +import java.net.URL; |
| 22 | +import java.nio.charset.Charset; |
21 | 23 | import java.nio.file.Paths; |
22 | 24 | import java.sql.SQLException; |
23 | | - |
| 25 | +import java.time.LocalDate; |
| 26 | +import java.util.List; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +import de.doubleslash.keeptime.Main; |
| 30 | +import de.doubleslash.keeptime.model.Project; |
| 31 | +import de.doubleslash.keeptime.model.Work; |
| 32 | +import javafx.application.Platform; |
| 33 | +import javafx.scene.control.*; |
| 34 | +import org.h2.tools.RunScript; |
24 | 35 | import org.h2.tools.Script; |
25 | 36 | import org.slf4j.Logger; |
26 | 37 | import org.slf4j.LoggerFactory; |
|
39 | 50 | import javafx.fxml.FXMLLoader; |
40 | 51 | import javafx.scene.Parent; |
41 | 52 | import javafx.scene.Scene; |
42 | | -import javafx.scene.control.Alert; |
43 | 53 | import javafx.scene.control.Alert.AlertType; |
44 | | -import javafx.scene.control.Button; |
45 | | -import javafx.scene.control.CheckBox; |
46 | | -import javafx.scene.control.ColorPicker; |
47 | | -import javafx.scene.control.Label; |
48 | 54 | import javafx.scene.layout.AnchorPane; |
49 | 55 | import javafx.scene.layout.Region; |
50 | 56 | import javafx.scene.paint.Color; |
@@ -107,6 +113,9 @@ public class SettingsController { |
107 | 113 | @FXML |
108 | 114 | private Button exportButton; |
109 | 115 |
|
| 116 | + @FXML |
| 117 | + private Button importButton; |
| 118 | + |
110 | 119 | @FXML |
111 | 120 | private Button aboutButton; |
112 | 121 |
|
@@ -156,6 +165,7 @@ private void initialize() { |
156 | 165 | } |
157 | 166 |
|
158 | 167 | initExportButton(); |
| 168 | + initImportButton(); |
159 | 169 |
|
160 | 170 | LOG.debug("saveButton.setOnAction"); |
161 | 171 | saveButton.setOnAction(ae -> { |
@@ -239,6 +249,63 @@ private void initialize() { |
239 | 249 | aboutStage.show(); |
240 | 250 | }); |
241 | 251 | } |
| 252 | + private void initImportButton(){ |
| 253 | + LOG.debug("Initialize importButton."); |
| 254 | + importButton.setOnAction(event ->{ |
| 255 | + |
| 256 | + try { |
| 257 | + Alert confirmationAlert = new Alert(AlertType.CONFIRMATION , "", ButtonType.YES, ButtonType.NO); |
| 258 | + confirmationAlert.setTitle("Import"); |
| 259 | + confirmationAlert.setHeaderText("Do you want to Override current Data ?"); |
| 260 | + confirmationAlert.setContentText("Import previously exported .sql file. This will overwrite the currently used database contents - all current data will be lost!\n" + |
| 261 | + "\n" + |
| 262 | + "If you do not have a .sql file yet you need to open the previous version of KeepTime and in the settings dialog press \"Export\".\n" + |
| 263 | + "\n" + |
| 264 | + "You will need to restart the application after this action. If you proceed you need to select the previouls exported .sql file."); |
| 265 | + confirmationAlert.showAndWait(); |
| 266 | + |
| 267 | + if(confirmationAlert.getResult()==ButtonType.NO){ |
| 268 | + LOG.info("User canceled import"); |
| 269 | + return; |
| 270 | + } |
| 271 | + |
| 272 | + final FileChooser fileChooser = new FileChooser(); |
| 273 | + fileChooser.setTitle("Open Exported SQl Script"); |
| 274 | + fileChooser.getExtensionFilters().add(new ExtensionFilter("SQL script files.", "*.sql")); |
| 275 | + File file = fileChooser.showOpenDialog(thisStage); |
| 276 | + if (file == null) { |
| 277 | + LOG.info("User canceled import."); |
| 278 | + return; |
| 279 | + } |
| 280 | + |
| 281 | + final String url = applicationProperties.getSpringDataSourceUrl(); |
| 282 | + final String username = applicationProperties.getSpringDataSourceUserName(); |
| 283 | + final String password = applicationProperties.getSpringDataSourcePassword(); |
| 284 | + |
| 285 | + new RunScript().runTool("-url", url, "-user",username,"-password",password,"-script",file.toString(),"-options", "FROM_1X"); |
| 286 | + |
| 287 | + Alert informationDialog = new Alert(AlertType.INFORMATION); |
| 288 | + informationDialog.setTitle("Import done"); |
| 289 | + informationDialog.setHeaderText("The data was imported."); |
| 290 | + informationDialog.setContentText("KeepTime will now be CLOSED! You have to RESTART it again to see the changes"); |
| 291 | + informationDialog.showAndWait(); |
| 292 | + Platform.exit(); |
| 293 | + |
| 294 | + |
| 295 | + } catch (SQLException e) { |
| 296 | + LOG.error("Could not import script file to db.", e); |
| 297 | + |
| 298 | + Alert errorDialog = new Alert(AlertType.ERROR); |
| 299 | + errorDialog.setTitle("Import failed"); |
| 300 | + errorDialog.setHeaderText("The current data could not be imported."); |
| 301 | + errorDialog.setContentText("Please inform a developer and provide your log file."); |
| 302 | + |
| 303 | + errorDialog.showAndWait(); |
| 304 | + } |
| 305 | + |
| 306 | + }); |
| 307 | + |
| 308 | + } |
242 | 309 |
|
243 | 310 | private void initExportButton() { |
244 | 311 | LOG.debug("Initialize exportButton."); |
@@ -266,6 +333,8 @@ private void initExportButton() { |
266 | 333 | Script.process(url, username, password, fileToSave.getAbsolutePath(), "DROP", ""); |
267 | 334 | LOG.info("Export done."); |
268 | 335 |
|
| 336 | + |
| 337 | + |
269 | 338 | Alert informationDialog = new Alert(AlertType.INFORMATION); |
270 | 339 | informationDialog.setTitle("Export done"); |
271 | 340 | informationDialog.setHeaderText("The current data was exported."); |
|
0 commit comments