Skip to content

Commit 1ed6f8d

Browse files
authored
Merge pull request #81 from doubleSlashde/feature/IPTE-22_exportH2DatabaseToSqlScript
Feature/ipte 22 export h2 database to sql script
2 parents dbbca1c + dc30a9b commit 1ed6f8d

File tree

12 files changed

+241
-20
lines changed

12 files changed

+241
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
66

77
### Added
88

9+
- export functionality of database
910
- search functionality
1011
- functionality to edit past work items
1112
- possibility to save position on screen

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Create projects and choose if they are counted as 'work time'. Select the projec
2626
+ Use Hotkey (`Strg`+`Win`): Change the project by using the Hotkey feature. A popup will appear at the mouse cursor.
2727
+ Save Position on Screen: Remembers the last position of the Main UI on application start.
2828
+ Ask for notes when switching project (if empty): Pops up a dialog to add notes if no notes are given and you try to switch projects
29+
+ Export: export database for backup and later import (import currently not yet implemented)
2930

3031
### Reports:
3132
![Report Screen](readme/images/reportDescription.png?raw=true "Report")
@@ -44,6 +45,14 @@ It is recommended to run the application at computer start so you do not forget
4445

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

48+
### Migrate from old version
49+
50+
1. Download new version and replace the .jar file.
51+
2. Start new version of KeepTime. Notice that your old data is not available.
52+
3. Stop KeepTime
53+
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).
54+
5. Start KeepTime again. Notice that your data is available again.
55+
4756
## Requirements
4857

4958
* Windows 7, 10

pom.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>de.doubleslash</groupId>
77
<artifactId>keeptime</artifactId>
8-
<version>1.1.0-SNAPSHOT</version>
8+
<version>1.2.0-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

1111
<name>KeepTime</name>
@@ -55,6 +55,8 @@
5555
<maven.compiler.source>1.8</maven.compiler.source>
5656
<maven.compiler.target>1.8</maven.compiler.target>
5757

58+
<h2.version>1.4.197</h2.version>
59+
5860
<maven-dependency-check.version>6.0.5</maven-dependency-check.version>
5961
<!-- USING HTML,XML (comma-separated list) did not work with plugin version 5.1.0 -->
6062
<maven-dependency-check.format>ALL</maven-dependency-check.format>
@@ -72,8 +74,7 @@
7274
<dependency>
7375
<groupId>com.h2database</groupId>
7476
<artifactId>h2</artifactId>
75-
<scope>runtime</scope>
76-
<version>1.4.197</version>
77+
<version>${h2.version}</version>
7778
</dependency>
7879
<dependency>
7980
<groupId>org.springframework.boot</groupId>
@@ -130,6 +131,14 @@
130131
<artifactId>spring-boot-maven-plugin</artifactId>
131132
</plugin>
132133

134+
<plugin>
135+
<groupId>pl.project13.maven</groupId>
136+
<artifactId>git-commit-id-plugin</artifactId>
137+
<configuration>
138+
<generateGitPropertiesFile>false</generateGitPropertiesFile>
139+
</configuration>
140+
</plugin>
141+
133142
<plugin>
134143
<artifactId>maven-assembly-plugin</artifactId>
135144
<executions>

readme/images/report.png

16 KB
Loading
7.18 KB
Loading

readme/images/settings.png

-4.88 KB
Loading
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package de.doubleslash.keeptime;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class ApplicationProperties {
8+
9+
private String buildVersion;
10+
private String buildTimestamp;
11+
12+
private String h2Version;
13+
14+
private String springDataSourceUrl;
15+
private String springDataSourceUserName;
16+
private String springDataSourcePassword;
17+
18+
private String gitCommitId;
19+
private String gitCommitTime;
20+
private String gitBranch;
21+
private String gitDirty;
22+
23+
public String getBuildVersion() {
24+
return buildVersion;
25+
}
26+
27+
@Value("${build.version}")
28+
private void setBuildVersion(String projectVersion) {
29+
this.buildVersion = projectVersion;
30+
}
31+
32+
public String getBuildTimestamp() {
33+
return buildTimestamp;
34+
}
35+
36+
@Value("${build.timestamp}")
37+
public void setBuildTimestamp(String buildTimestamp) {
38+
this.buildTimestamp = buildTimestamp;
39+
}
40+
41+
public String getH2Version() {
42+
return this.h2Version;
43+
}
44+
45+
@Value("${h2.version}")
46+
private void setH2Version(final String h2Version) {
47+
this.h2Version = h2Version;
48+
}
49+
50+
public String getSpringDataSourceUrl() {
51+
return springDataSourceUrl;
52+
}
53+
54+
@Value("${spring.datasource.url}")
55+
private void setSpringDataSourceUrl(String springDataSourceUrl) {
56+
this.springDataSourceUrl = springDataSourceUrl;
57+
}
58+
59+
public String getSpringDataSourceUserName() {
60+
return springDataSourceUserName;
61+
}
62+
63+
@Value("${spring.datasource.username}")
64+
private void setSpringDataSourceUserName(String springDataSourceUserName) {
65+
this.springDataSourceUserName = springDataSourceUserName;
66+
}
67+
68+
public String getSpringDataSourcePassword() {
69+
return springDataSourcePassword;
70+
}
71+
72+
@Value("${spring.datasource.password}")
73+
private void setSpringDataSourcePassword(String springDataSourcePassword) {
74+
this.springDataSourcePassword = springDataSourcePassword;
75+
}
76+
77+
public String getGitCommitId() {
78+
return gitCommitId;
79+
}
80+
81+
@Value("${git.commit.id}")
82+
private void setGitCommitId(String gitCommitId) {
83+
this.gitCommitId = gitCommitId;
84+
}
85+
86+
public String getGitCommitTime() {
87+
return gitCommitTime;
88+
}
89+
90+
@Value("${git.commit.time}")
91+
private void setGitCommitTime(String gitCommitTime) {
92+
this.gitCommitTime = gitCommitTime;
93+
}
94+
95+
public String getGitBranch() {
96+
return gitBranch;
97+
}
98+
99+
@Value("${git.branch}")
100+
private void setGitBranch(String gitBranch) {
101+
this.gitBranch = gitBranch;
102+
}
103+
104+
public String getGitDirty() {
105+
return gitDirty;
106+
}
107+
108+
@Value("${git.dirty}")
109+
private void setGitDirty(String gitDirty) {
110+
this.gitDirty = gitDirty;
111+
}
112+
}

src/main/java/de/doubleslash/keeptime/Main.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public class Main extends Application {
6565

6666
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
6767

68-
public static final String VERSION = "v1.1.0";
69-
7068
private ConfigurableApplicationContext springContext;
7169

7270
private Stage popupViewStage;
@@ -81,12 +79,18 @@ public class Main extends Application {
8179

8280
@Override
8381
public void init() throws Exception {
84-
LOG.info("Starting KeepTime {}", VERSION);
82+
LOG.info("Starting KeepTime.");
8583
final DefaultExceptionHandler defaultExceptionHandler = new DefaultExceptionHandler();
8684
defaultExceptionHandler.register();
8785

8886
springContext = SpringApplication.run(Main.class);
89-
// TODO test if everywhere is used the same model
87+
ApplicationProperties applicationProperties = springContext.getBean(ApplicationProperties.class);
88+
LOG.info("KeepTime Version: '{}'.", applicationProperties.getBuildVersion());
89+
LOG.info("KeepTime Build Timestamp: '{}'.", applicationProperties.getBuildTimestamp());
90+
LOG.info("KeepTime Git Infos: id '{}', branch '{}', time '{}', dirty '{}'.",
91+
applicationProperties.getGitCommitId(), applicationProperties.getGitBranch(),
92+
applicationProperties.getGitCommitTime(), applicationProperties.getGitDirty());
93+
9094
model = springContext.getBean(Model.class);
9195
controller = springContext.getBean(Controller.class);
9296
model.setSpringContext(springContext);
@@ -137,8 +141,7 @@ private void initialiseApplication(final Stage primaryStage) throws Exception {
137141
FontProvider.loadFonts();
138142
readSettings();
139143

140-
final List<Work> todaysWorkItems = model.getWorkRepository()
141-
.findByStartDateOrderByStartTimeAsc(LocalDate.now());
144+
final List<Work> todaysWorkItems = model.getWorkRepository().findByStartDateOrderByStartTimeAsc(LocalDate.now());
142145
LOG.info("Found {} past work items", todaysWorkItems.size());
143146
model.getPastWorkItems().addAll(todaysWorkItems);
144147

@@ -153,7 +156,7 @@ private void initialiseApplication(final Stage primaryStage) throws Exception {
153156

154157
model.getAllProjects().addAll(projects);
155158
model.getAvailableProjects()
156-
.addAll(model.getAllProjects().stream().filter(Project::isEnabled).collect(Collectors.toList()));
159+
.addAll(model.getAllProjects().stream().filter(Project::isEnabled).collect(Collectors.toList()));
157160

158161
// set default project
159162
final Optional<Project> findAny = projects.stream().filter(Project::isDefault).findAny();

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.slf4j.LoggerFactory;
2323
import org.springframework.stereotype.Component;
2424

25+
import de.doubleslash.keeptime.ApplicationProperties;
2526
import de.doubleslash.keeptime.Main;
2627
import de.doubleslash.keeptime.common.BrowserHelper;
2728
import de.doubleslash.keeptime.common.FileOpenHelper;
@@ -67,10 +68,16 @@ public class AboutController {
6768

6869
private static final Logger LOG = LoggerFactory.getLogger(AboutController.class);
6970

71+
private final ApplicationProperties applicationProperties;
72+
73+
public AboutController (ApplicationProperties applicationProperties) {
74+
this.applicationProperties = applicationProperties;
75+
}
76+
7077
@FXML
7178
public void initialize() {
7279
LOG.debug("set version label");
73-
versionNumberLabel.setText(Main.VERSION);
80+
versionNumberLabel.setText(applicationProperties.getBuildVersion());
7481

7582
ourLicenseHyperLink.setFocusTraversable(false);
7683
ourLicenseHyperLink.setOnAction(ae -> showLicense(Licenses.GPLV3));

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

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
package de.doubleslash.keeptime.view;
1818

19+
import java.io.File;
1920
import java.io.IOException;
21+
import java.nio.file.Paths;
22+
import java.sql.SQLException;
2023

24+
import org.h2.tools.Script;
2125
import org.slf4j.Logger;
2226
import org.slf4j.LoggerFactory;
2327
import org.springframework.beans.factory.annotation.Autowired;
2428
import org.springframework.stereotype.Component;
2529

30+
import de.doubleslash.keeptime.ApplicationProperties;
2631
import de.doubleslash.keeptime.common.OS;
2732
import de.doubleslash.keeptime.common.Resources;
2833
import de.doubleslash.keeptime.common.Resources.RESOURCE;
@@ -43,6 +48,8 @@
4348
import javafx.scene.layout.AnchorPane;
4449
import javafx.scene.layout.Region;
4550
import javafx.scene.paint.Color;
51+
import javafx.stage.FileChooser;
52+
import javafx.stage.FileChooser.ExtensionFilter;
4653
import javafx.stage.Modality;
4754
import 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

Comments
 (0)