Skip to content

Commit b0c07af

Browse files
authored
Merge 'task-4_import_and_export' into 'version_5_0_0'
task-4 import and export
2 parents e1d11b9 + f7b062a commit b0c07af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2486
-535
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| 2.0.0 | db chooser |
99
| 3.0.0 | save data after push button |
1010
| 4.0.0 | improve and refactoring |
11+
| 5.0.0 | java_17, import and export |
1112

1213
## Plans
1314

pom.xml

+16
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@
3030
<artifactId>sqlite-jdbc</artifactId>
3131
<version>3.34.0</version>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.yaml</groupId>
35+
<artifactId>snakeyaml</artifactId>
36+
<version>1.30</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.fasterxml.jackson.core</groupId>
40+
<artifactId>jackson-databind</artifactId>
41+
<version>2.13.3</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.opencsv</groupId>
45+
<artifactId>opencsv</artifactId>
46+
<version>5.6</version>
47+
</dependency>
3348
<dependency>
3449
<groupId>org.swinglabs.swingx</groupId>
3550
<artifactId>swingx-all</artifactId>
@@ -96,4 +111,5 @@
96111
</plugin>
97112
</plugins>
98113
</build>
114+
99115
</project>

pom_(libs_in_jar).xml

+1
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@
9696
</plugin>
9797
</plugins>
9898
</build>
99+
99100
</project>

src/main/java/home/Data.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package home;
2+
3+
import java.io.IOException;
4+
import java.sql.SQLException;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import home.db.DbInitializer;
10+
import home.db.dao.DaoSQLite;
11+
import home.gui.DataActionInGui;
12+
import home.gui.Gui;
13+
import home.gui.component.CustomJFileChooserDb;
14+
import home.gui.component.CustomJFileChooserDb.ChooserDbOperation;
15+
import home.utils.ThreadUtil;
16+
import home.utils.LogUtils;
17+
18+
final class Data {
19+
20+
private static final Logger LOG = LoggerFactory.getLogger(Data.class);
21+
22+
static void initDb() {
23+
if (Settings.hasPathToDbFile()) {
24+
readDataFromDb();
25+
} else {
26+
try {
27+
CustomJFileChooserDb.createAndShowChooser(null,
28+
ChooserDbOperation.CREATE_OR_OPEN);
29+
readDataFromDb();
30+
Gui.INSTANCE.setDbLabel(Settings.getDbFilePath());
31+
} catch (IOException e) {
32+
throw new IllegalStateException("Error while create/open DB file.", e);
33+
}
34+
}
35+
}
36+
37+
private static void readDataFromDb() {
38+
ThreadUtil.runInThread(() -> {
39+
Thread.currentThread().setName("-> read data from database");
40+
try {
41+
DbInitializer.createTableIfNotExists();
42+
DataActionInGui.init(DaoSQLite.getInstance().readAll());
43+
} catch (SQLException e) {
44+
String errorMsg = "Error while read data from database: " + e.getMessage();
45+
LogUtils.logAndShowError(LOG, null, errorMsg, "Data reading error", e);
46+
throw new IllegalStateException(errorMsg, e);
47+
}
48+
});
49+
}
50+
}

src/main/java/home/Main.java

+8-50
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
package home;
22

3-
import java.io.IOException;
43
import java.lang.Thread.UncaughtExceptionHandler;
5-
import java.sql.SQLException;
64
import java.util.function.BiFunction;
75

86
import org.slf4j.Logger;
97
import org.slf4j.LoggerFactory;
108

11-
import home.db.DbInitializer;
12-
import home.db.dao.DaoSQLite;
139
import home.gui.Gui;
14-
import home.gui.component.CustomJFileChooser;
15-
import home.gui.component.CustomJFileChooser.ChooserOperation;
16-
import home.utils.Utils;
10+
import home.utils.LogUtils;
1711

1812
public final class Main {
1913

2014
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
2115

22-
private static final String START_LOG_MESSAGE = "Application {} v{} started successfully.";
23-
2416
private static final String DEFAULT_APP_NAME = "=VEHICLE_ACCOUNTING=";
2517
private static final String DEFAULT_APP_VERSION = "UNKNOWN";
2618

@@ -32,9 +24,9 @@ public static void main(String[] args) {
3224
try {
3325
startApplication();
3426
isStarted = true;
35-
LOG.info(START_LOG_MESSAGE, appName, appVersion);
27+
LOG.info("Application {} v{} started successfully.", appName, appVersion);
3628
} catch (Exception e) {
37-
Utils.logAndShowError(LOG, null, e.getMessage(), "Application start error", e);
29+
LogUtils.logAndShowError(LOG, null, e.getMessage(), "Application start error", e);
3830
} finally {
3931
if (!isStarted) {
4032
System.exit(1);
@@ -49,14 +41,14 @@ private static void startApplication() {
4941
Settings.readSettings();
5042
Gui.INSTANCE.buildGui();
5143

52-
initDb();
44+
Data.initDb();
5345
}
5446

5547
private static void setUncaughtExceptionProcessing() {
5648
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
5749
@Override
5850
public void uncaughtException(Thread t, Throwable e) {
59-
Utils.logAndShowError(LOG, null, e.getMessage(), "Error", e);
51+
LogUtils.logAndShowError(LOG, null, e.getMessage(), "Error", e);
6052
System.exit(1);
6153
}
6254
};
@@ -65,42 +57,8 @@ public void uncaughtException(Thread t, Throwable e) {
6557

6658
private static void initAppDescription() {
6759
BiFunction<String, String, String> getSafeVal = (val, def) -> val != null ? val : def;
68-
Package pacage = Main.class.getPackage();
69-
appName = getSafeVal.apply(pacage.getImplementationTitle(), DEFAULT_APP_NAME);
70-
appVersion = getSafeVal.apply(pacage.getImplementationVersion(), DEFAULT_APP_VERSION);
71-
}
72-
73-
private static void initDb() {
74-
if (Settings.hasPathToDbFile()) {
75-
readDataFromDb();
76-
} else {
77-
try {
78-
CustomJFileChooser.showChooser(null, ChooserOperation.CREATE_OR_OPEN);
79-
readDataFromDb();
80-
Gui.INSTANCE.setDbLabel(Settings.getDbFilePath());
81-
} catch (IOException e) {
82-
throw new IllegalStateException("Error while create/open DB file.", e);
83-
}
84-
}
85-
}
86-
87-
private static void readDataFromDb() {
88-
try {
89-
DbInitializer.createTableIfNotExists();
90-
91-
Utils.runInThread("-> read data from database", () -> {
92-
try {
93-
Storage.INSTANCE.refresh(DaoSQLite.getInstance().readAll());
94-
} catch (SQLException e) {
95-
Utils.logAndShowError(LOG, null,
96-
"Error while read data from database: " + e.getMessage(),
97-
"Data reading error", e);
98-
throw new IllegalStateException("Error while read data from database: "
99-
+ e.getMessage(), e);
100-
}
101-
});
102-
} catch (Exception e) {
103-
throw new IllegalStateException("Error while read data from database.", e);
104-
}
60+
Package pkg = Main.class.getPackage();
61+
appName = getSafeVal.apply(pkg.getImplementationTitle(), DEFAULT_APP_NAME);
62+
appVersion = getSafeVal.apply(pkg.getImplementationVersion(), DEFAULT_APP_VERSION);
10563
}
10664
}

src/main/java/home/Storage.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
package home;
22

3-
import java.util.ArrayList;
43
import java.util.HashSet;
4+
import java.util.LinkedList;
55
import java.util.List;
66
import java.util.Set;
77

8-
import home.gui.Gui;
9-
import home.models.AbstractVehicle;
8+
import home.model.AbstractVehicle;
109

1110
public enum Storage {
1211

1312
INSTANCE;
1413

1514
public static final int NO_ROW_IS_SELECTED = -1;
1615

17-
private final List<AbstractVehicle> dataObjStorage = new ArrayList<>();
16+
private final List<AbstractVehicle> dataObjStorage = new LinkedList<>();
1817
private final Set<Long> dataObjIdsForDelete = new HashSet<>();
1918
private final Set<Long> dataObjIdsForUpdate = new HashSet<>();
2019

21-
public void refresh(List<AbstractVehicle> dataObjs) {
20+
public void initDataObjs(List<AbstractVehicle> dataObjs) {
2221
dataObjIdsForDelete.clear();
2322
dataObjIdsForUpdate.clear();
2423
dataObjStorage.clear();
2524
dataObjStorage.addAll(dataObjs);
26-
Gui.INSTANCE.refreshTable();
25+
}
26+
27+
public void addDataObj(List<AbstractVehicle> dataObjs) {
28+
dataObjStorage.addAll(dataObjs);
2729
}
2830

2931
public List<AbstractVehicle> getAll() {
@@ -43,7 +45,7 @@ public Set<Long> getIdsForUpdate() {
4345
return dataObjIdsForUpdate;
4446
}
4547

46-
public void updateStorage(AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
48+
public void updateDataObj(AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
4749
if (NO_ROW_IS_SELECTED == tblRowOfSelectedDataObj) {
4850
dataObjStorage.add(dataObj);
4951
} else {
@@ -52,7 +54,7 @@ public void updateStorage(AbstractVehicle dataObj, int tblRowOfSelectedDataObj)
5254
}
5355
}
5456

55-
public void deleteObjects(List<AbstractVehicle> obsMarkedForDelete) {
57+
public void deleteDataObjs(List<AbstractVehicle> obsMarkedForDelete) {
5658
for (AbstractVehicle objForDel : obsMarkedForDelete) {
5759
long idObjForDel = objForDel.getId();
5860
if (idObjForDel > 0) {

src/main/java/home/db/Connector.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.slf4j.LoggerFactory;
1313

1414
import home.Settings;
15-
import home.utils.Utils;
15+
import home.utils.LogUtils;
1616

1717
public final class Connector {
1818

@@ -70,9 +70,9 @@ private static Connection getConnection(String url, Properties props, String jdb
7070

7171
return DriverManager.getConnection(url, props);
7272
} catch (ClassNotFoundException e) {
73-
throw Utils.logAndCreateSqlException("Database driver class not found.", LOG, e);
73+
throw LogUtils.logAndCreateSqlException("Database driver class not found.", LOG, e);
7474
} catch (SQLException e) {
75-
throw Utils.logAndCreateSqlException("Error while connecting to the database.", LOG, e);
75+
throw LogUtils.logAndCreateSqlException("Error while connecting to the database.", LOG, e);
7676
}
7777
}
7878

src/main/java/home/db/DbInitializer.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ public final class DbInitializer {
1414

1515
private static final Logger LOG = LoggerFactory.getLogger(DbInitializer.class);
1616

17-
private static final String CREATE_TBL_QUERY = "CREATE TABLE if not exists vehicle"
18-
+ " ('id' INTEGER PRIMARY KEY AUTOINCREMENT,"
19-
+ " 'type' TEXT, 'color' TEXT, 'number' TEXT,"
20-
+ " 'is_transports_cargo' INTEGER, 'is_transports_passengers' INTEGER,"
21-
+ " 'has_trailer' INTEGER, 'has_cradle' INTEGER, 'date_time' INTEGER);";
17+
private static final String CREATE_TBL_QUERY = """
18+
CREATE TABLE IF NOT EXISTS vehicle (
19+
'id' INTEGER PRIMARY KEY AUTOINCREMENT,
20+
'type' TEXT,
21+
'color' TEXT,
22+
'number' TEXT,
23+
'is_transports_cargo' INTEGER,
24+
'is_transports_passengers' INTEGER,
25+
'has_trailer' INTEGER,
26+
'has_cradle' INTEGER,
27+
'date_time' INTEGER);""";
2228

2329
public static void createDbFileIfNotExists(File file) throws IOException {
2430
try {

0 commit comments

Comments
 (0)