Skip to content

Commit 3710190

Browse files
committed
base commit
0 parents  commit 3710190

35 files changed

+2039
-0
lines changed

.gitignore

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Eclipse and IntelliJ IDEA folders/files
2+
3+
/target/
4+
/.idea/
5+
*.iml
6+
/.classpath
7+
/.project
8+
/.settings/
9+
10+
##########
11+
12+
# Application files
13+
14+
/logs/
15+
/*.db
16+
/*.log
17+
/*.properties
18+
/*.sqlite
19+
/*.sqlite3
20+
21+
##########
22+
23+
# Temp files
24+
25+
*.txt
26+
*.doc
27+
*.docx

pom.xml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>home</groupId>
8+
<artifactId>vehicle</artifactId>
9+
<version>1.0.0</version>
10+
11+
<packaging>jar</packaging>
12+
13+
<name>vehicle accounting</name>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
18+
<maven.compiler.source>11</maven.compiler.source>
19+
<maven.compiler.target>11</maven.compiler.target>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.xerial</groupId>
25+
<artifactId>sqlite-jdbc</artifactId>
26+
<version>3.34.0</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.swinglabs.swingx</groupId>
30+
<artifactId>swingx-all</artifactId>
31+
<version>1.6.5-1</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>log4j</groupId>
35+
<artifactId>log4j</artifactId>
36+
<version>1.2.17</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter-engine</artifactId>
41+
<version>5.0.0</version>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-dependency-plugin</artifactId>
51+
<executions>
52+
<execution>
53+
<id>copy-dependencies</id>
54+
<phase>prepare-package</phase>
55+
<goals>
56+
<goal>copy-dependencies</goal>
57+
</goals>
58+
<configuration>
59+
<outputDirectory>${project.build.directory}/lib</outputDirectory>
60+
<overWriteReleases>false</overWriteReleases>
61+
<overWriteSnapshots>false</overWriteSnapshots>
62+
<overWriteIfNewer>true</overWriteIfNewer>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-jar-plugin</artifactId>
70+
<configuration>
71+
<archive>
72+
<manifest>
73+
<addClasspath>true</addClasspath>
74+
<classpathPrefix>lib/</classpathPrefix>
75+
<mainClass>home.Main</mainClass>
76+
</manifest>
77+
</archive>
78+
</configuration>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
83+
</project>

src/main/java/home/Main.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package home;
2+
3+
import java.sql.SQLException;
4+
5+
import javax.swing.JOptionPane;
6+
7+
import org.apache.log4j.Logger;
8+
9+
import home.db.DbInitializer;
10+
import home.db.dao.DaoSQLite;
11+
import home.gui.Gui;
12+
import home.utils.Utils;
13+
14+
public class Main {
15+
16+
private static final Logger LOG = Logger.getLogger(Main.class);
17+
18+
public static void main(String[] args) {
19+
initDb();
20+
initGui();
21+
readDataFromDb();
22+
}
23+
24+
private static void initDb() {
25+
try {
26+
DbInitializer.createTableIfNotExists();
27+
} catch (Exception e) {
28+
LOG.error(e);
29+
JOptionPane.showMessageDialog(null,
30+
e.getLocalizedMessage(),
31+
"DB initialization error",
32+
JOptionPane.ERROR_MESSAGE);
33+
System.exit(1);
34+
}
35+
}
36+
37+
private static void initGui() {
38+
try {
39+
Gui.getInstance().buildGui();
40+
} catch (Exception e) {
41+
LOG.error(e);
42+
JOptionPane.showMessageDialog(null,
43+
e.getLocalizedMessage(),
44+
"GUI initialization error",
45+
JOptionPane.ERROR_MESSAGE);
46+
System.exit(1);
47+
}
48+
}
49+
50+
private static void readDataFromDb() {
51+
Utils.runInThread(() -> {
52+
try {
53+
Storage.getInstance().refresh(DaoSQLite.getInstance().readAll());
54+
} catch (SQLException e) {
55+
LOG.error(e);
56+
JOptionPane.showMessageDialog(null,
57+
"Error while read data from database: "
58+
+ e.getLocalizedMessage(),
59+
"Data reading error",
60+
JOptionPane.ERROR_MESSAGE);
61+
System.exit(1);
62+
}
63+
});
64+
}
65+
}

src/main/java/home/Storage.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package home;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import home.gui.Gui;
7+
import home.models.AbstractVehicle;
8+
9+
public class Storage {
10+
11+
private static final List<AbstractVehicle> DATA_OBJS = new ArrayList<>();
12+
13+
private static Storage instance;
14+
15+
private Storage() {
16+
}
17+
18+
public static Storage getInstance() {
19+
if (instance == null) {
20+
instance = new Storage();
21+
}
22+
return instance;
23+
}
24+
25+
public void refresh(List<AbstractVehicle> dataObjs) {
26+
DATA_OBJS.clear();
27+
DATA_OBJS.addAll(dataObjs);
28+
Gui.getInstance().refreshTable();
29+
}
30+
31+
public List<AbstractVehicle> getAll() {
32+
return DATA_OBJS;
33+
}
34+
35+
public AbstractVehicle get(int row) {
36+
return DATA_OBJS.get(row);
37+
}
38+
}

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

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
package home.db;
3+
4+
import java.io.IOException;
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.SQLException;
8+
9+
import org.apache.log4j.Logger;
10+
11+
public class Connector {
12+
13+
private static final Logger LOG = Logger.getLogger(Connector.class);
14+
15+
private static final String JDBC_DRIVER_SQLITE = "org.sqlite.JDBC";
16+
private static final String CONNECTION_URL_SQLITE = "jdbc:sqlite:%s";
17+
18+
private static String dbFileAbsolutePath;
19+
private static Connection connection;
20+
21+
public static Connection getConnection() throws SQLException {
22+
if (connection == null || connection.isClosed()) {
23+
try {
24+
if (dbFileAbsolutePath == null) {
25+
dbFileAbsolutePath = DbCreator.dbFileAbsolutePath();
26+
}
27+
28+
Class.forName(JDBC_DRIVER_SQLITE);
29+
30+
// Driver driver = (Driver) Class.forName(JDBC_DRIVER_SQLITE).newInstance();
31+
// DriverManager.registerDriver(driver);
32+
33+
connection = DriverManager.getConnection(
34+
String.format(CONNECTION_URL_SQLITE, dbFileAbsolutePath));
35+
} catch (ClassNotFoundException | IOException e) {
36+
String errorMsg = null;
37+
if (e instanceof ClassNotFoundException) {
38+
errorMsg = "Database driver class not found.";
39+
} else if (e instanceof IOException) {
40+
errorMsg = "Error while creating the database file.";
41+
}
42+
LOG.error(errorMsg);
43+
SQLException ex = new SQLException(errorMsg);
44+
ex.addSuppressed(e);
45+
throw ex;
46+
} catch (SQLException e) {
47+
LOG.error("Error while connecting to the database.");
48+
throw e;
49+
}
50+
}
51+
return connection;
52+
}
53+
54+
public static void closeConnection() throws SQLException {
55+
try {
56+
if (connection != null) {
57+
connection.close();
58+
}
59+
} catch (SQLException e) {
60+
LOG.error("Error while closing DB connection.");
61+
throw e;
62+
}
63+
}
64+
65+
private Connector() {
66+
}
67+
}

src/main/java/home/db/DbConsts.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package home.db;
2+
3+
public interface DbConsts {
4+
5+
// Table name
6+
String VEHICLE = "vehicle";
7+
8+
// The names of the columns in the table
9+
String ID = "id";
10+
String TYPE = "type";
11+
String COLOR = "color";
12+
String NUMBER = "number";
13+
String IS_TRANSPORTS_CARGO = "is_transports_cargo";
14+
String IS_TRANSPORTS_PASSENGERS = "is_transports_passengers";
15+
String HAS_TRAILER = "has_trailer";
16+
String HAS_CRADLE = "has_cradle";
17+
String DATE_TIME = "date_time";
18+
19+
// Data types
20+
String INTEGER = "INTEGER";
21+
String TEXT = "TEXT";
22+
}

src/main/java/home/db/DbCreator.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package home.db;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import org.apache.log4j.Logger;
7+
8+
public class DbCreator {
9+
10+
private static final Logger LOG = Logger.getLogger(DbCreator.class);
11+
12+
private static final String DB_FILE_PATH = "database.db";
13+
14+
/**
15+
* @return returns the absolute path to the created SQLite database file, if
16+
* this file does not exist, then creates it
17+
*/
18+
public static String dbFileAbsolutePath() throws IOException {
19+
try {
20+
File file = new File(DB_FILE_PATH);
21+
if (!file.exists()) {
22+
file.createNewFile();
23+
}
24+
return file.getAbsolutePath();
25+
} catch (IOException e) {
26+
LOG.error("Ошибка при создании файла базы данных.", e);
27+
throw e;
28+
}
29+
}
30+
31+
private DbCreator() {
32+
}
33+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
package home.db;
3+
4+
import java.sql.SQLException;
5+
6+
public class DbInitializer {
7+
8+
private static final String CREATE_TBL_QUERY = "CREATE TABLE if not exists vehicle"
9+
+ " ('id' INTEGER PRIMARY KEY AUTOINCREMENT,"
10+
+ " 'type' TEXT, 'color' TEXT, 'number' TEXT,"
11+
+ " 'is_transports_cargo' INTEGER, 'is_transports_passengers' INTEGER,"
12+
+ " 'has_trailer' INTEGER, 'has_cradle' INTEGER, 'date_time' INTEGER);";
13+
14+
public static void createTableIfNotExists() throws SQLException {
15+
try (var stmt = Connector.getConnection().createStatement()) {
16+
stmt.execute(CREATE_TBL_QUERY);
17+
} finally {
18+
Connector.closeConnection();
19+
}
20+
}
21+
22+
private DbInitializer() {
23+
}
24+
}

src/main/java/home/db/dao/Dao.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package home.db.dao;
2+
3+
import java.sql.SQLException;
4+
import java.util.ArrayList;
5+
6+
import home.models.AbstractVehicle;
7+
8+
public interface Dao {
9+
10+
AbstractVehicle readOne(long id) throws SQLException;
11+
12+
ArrayList<AbstractVehicle> readAll() throws SQLException;
13+
14+
void create(AbstractVehicle dataObj) throws SQLException;
15+
16+
void update(AbstractVehicle dataObj) throws SQLException;
17+
18+
void delete(Long[] ids) throws SQLException;
19+
}

0 commit comments

Comments
 (0)