Skip to content

Commit 793007c

Browse files
authored
Merge pull request #102 from DavidDamke/feature/import_sql_to_database
Feature/import sql to database
2 parents 7180549 + 26e066f commit 793007c

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,24 @@ It is recommended to run the application at computer start so you do not forget
4545

4646
You should put the .jar in an extra folder as a *logs* and a *db* folder will be created next to it.\
4747

48-
### Migrate from old version
48+
### Migrate from older version than v1.2.0
4949

5050
1. Download new version and replace the .jar file.
5151
2. Start new version of KeepTime. Notice that your old data is not available.
5252
3. Stop KeepTime
5353
4. Copy the files (not directories) of directory `db` (next to the .jar file) into `db/1.4.197/` (path now includes the database version).
5454
5. Start KeepTime again. Notice that your data is available again.
5555

56+
### Migrate from KeepTime v1.2.0
57+
58+
1. Start your current version of KeepTime (v1.2.0)
59+
2. Go to the settings and export your KeepTime data
60+
3. Download new version and replace the .jar file.
61+
4. Start new version of KeepTime. Notice that your old data is not available.
62+
5. Open the new version and import the exported sql script
63+
6. After the import KeepTime closes automatically
64+
7. To see the changes just start the new KeepTime again
65+
5666
## Requirements
5767

5868
* Windows 7, 10

src/main/java/de/doubleslash/keeptime/view/SettingsController.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.net.URL;
22+
import java.nio.charset.Charset;
2123
import java.nio.file.Paths;
2224
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;
2435
import org.h2.tools.Script;
2536
import org.slf4j.Logger;
2637
import org.slf4j.LoggerFactory;
@@ -39,12 +50,7 @@
3950
import javafx.fxml.FXMLLoader;
4051
import javafx.scene.Parent;
4152
import javafx.scene.Scene;
42-
import javafx.scene.control.Alert;
4353
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;
4854
import javafx.scene.layout.AnchorPane;
4955
import javafx.scene.layout.Region;
5056
import javafx.scene.paint.Color;
@@ -107,6 +113,9 @@ public class SettingsController {
107113
@FXML
108114
private Button exportButton;
109115

116+
@FXML
117+
private Button importButton;
118+
110119
@FXML
111120
private Button aboutButton;
112121

@@ -156,6 +165,7 @@ private void initialize() {
156165
}
157166

158167
initExportButton();
168+
initImportButton();
159169

160170
LOG.debug("saveButton.setOnAction");
161171
saveButton.setOnAction(ae -> {
@@ -239,6 +249,63 @@ private void initialize() {
239249
aboutStage.show();
240250
});
241251
}
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+
//TODO: add an option at the next release to make the "FROM_1X flag" configurable. E.g. if we upgrade (in the release after) from H2 version 2.x to 2.x we must not set the "FROM_1X flag".
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+
}
242309

243310
private void initExportButton() {
244311
LOG.debug("Initialize exportButton.");
@@ -266,6 +333,8 @@ private void initExportButton() {
266333
Script.process(url, username, password, fileToSave.getAbsolutePath(), "DROP", "");
267334
LOG.info("Export done.");
268335

336+
337+
269338
Alert informationDialog = new Alert(AlertType.INFORMATION);
270339
informationDialog.setTitle("Export done");
271340
informationDialog.setHeaderText("The current data was exported.");

src/main/resources/layouts/settings.fxml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@
344344
<Font name="Open Sans Regular" size="12.0" />
345345
</font>
346346
</Button>
347+
<Button fx:id="importButton" mnemonicParsing="false" text="Import">
348+
<font>
349+
<Font name="Open Sans Regular" size="12.0" />
350+
</font>
351+
</Button>
347352
<Button fx:id="aboutButton" mnemonicParsing="false" text="About">
348353
<font>
349354
<Font name="Open Sans Regular" size="12.0" />

0 commit comments

Comments
 (0)