1+ package me .axeno .hommr .managers ;
2+
3+ import com .j256 .ormlite .dao .Dao ;
4+ import com .j256 .ormlite .dao .DaoManager ;
5+ import com .j256 .ormlite .jdbc .JdbcConnectionSource ;
6+ import com .j256 .ormlite .misc .TransactionManager ;
7+ import com .j256 .ormlite .support .ConnectionSource ;
8+ import com .j256 .ormlite .table .TableUtils ;
9+ import lombok .Getter ;
10+ import me .axeno .hommr .Hommr ;
11+ import me .axeno .hommr .models .Home ;
12+
13+ import java .io .File ;
14+ import java .sql .SQLException ;
15+ import java .util .List ;
16+ import java .util .concurrent .Callable ;
17+
18+ public class DatabaseManager {
19+
20+ private ConnectionSource connectionSource ;
21+ @ Getter
22+ private Dao <Home , Integer > homeDao ;
23+
24+ /**
25+ * Set up the database connection and DAO for Home entities based on configuration.
26+ *
27+ * Initializes the plugin data folder if necessary, creates a JDBC connection source
28+ * (MySQL when `database.type` is `"mysql"`, otherwise SQLite using a `homes.db` file),
29+ * and creates a Dao<Home, Integer> for accessing Home records. Ensures the Home table
30+ * exists in the database; failures during folder creation, table creation, or overall
31+ * initialization are logged.
32+ */
33+ public void init () {
34+ try {
35+ File dataFolder = Hommr .getInstance ().getDataFolder ();
36+ if (!dataFolder .exists ()) {
37+ boolean created = dataFolder .mkdirs ();
38+ if (!created ) {
39+ Hommr .getInstance ().getLogger ().warning ("Could not create plugin data folder: " + dataFolder .getAbsolutePath ());
40+ }
41+ }
42+
43+ String type = Hommr .getInstance ().getConfig ().getString ("database.type" , "sqlite" ).toLowerCase ();
44+ String databaseUrl ;
45+ String username = null ;
46+ String password = null ;
47+
48+ if (type .equals ("mysql" )) {
49+ databaseUrl = Hommr .getInstance ().getConfig ().getString ("database.connection.url" );
50+ username = Hommr .getInstance ().getConfig ().getString ("database.connection.username" );
51+ password = Hommr .getInstance ().getConfig ().getString ("database.connection.password" );
52+ if (databaseUrl == null || databaseUrl .isEmpty ()) {
53+ throw new IllegalArgumentException ("MySQL database URL is required but not configured. Please check your config.yml." );
54+ }
55+ connectionSource = new JdbcConnectionSource (databaseUrl , username , password );
56+ } else {
57+ databaseUrl = "jdbc:sqlite:" + new File (dataFolder , "homes.db" ).getAbsolutePath ();
58+ connectionSource = new JdbcConnectionSource (databaseUrl );
59+ }
60+
61+ homeDao = DaoManager .createDao (connectionSource , Home .class );
62+
63+ TableUtils .createTableIfNotExists (connectionSource , Home .class );
64+
65+ } catch (SQLException e ) {
66+ Hommr .getInstance ().getLogger ().log (java .util .logging .Level .SEVERE , "Failed to initialize database" , e );
67+ throw new RuntimeException ("Database initialization failed" , e );
68+ }
69+ }
70+
71+ /**
72+ * Closes the underlying database connection source and releases related resources.
73+ *
74+ * If an error occurs while closing, the exception is caught and a warning is logged.
75+ */
76+ public void close () {
77+ if (connectionSource != null ) {
78+ try {
79+ connectionSource .close ();
80+ connectionSource = null ;
81+ } catch (Exception e ) {
82+ Hommr .getInstance ().getLogger ().log (java .util .logging .Level .WARNING , "Error closing database connection" , e );
83+ }
84+ }
85+ }
86+
87+ /**
88+ * Retrieve all Home records from the database.
89+ *
90+ * @return a list of all Home objects
91+ * @throws SQLException if a database access error occurs while querying for homes
92+ */
93+ public List <Home > getAllHomes () throws SQLException {
94+ return homeDao .queryForAll ();
95+ }
96+
97+ /**
98+ * Replaces all stored Home records with the provided list.
99+ *
100+ * Clears the Home table and inserts the given homes in a single batch operation.
101+ *
102+ * @param homes the list of Home objects to persist (may be empty)
103+ * @throws SQLException if an error occurs while clearing the table or saving records
104+ */
105+ public void saveAllHomes (List <Home > homes ) throws SQLException {
106+ TransactionManager .callInTransaction (connectionSource , () -> {
107+ TableUtils .clearTable (connectionSource , Home .class );
108+ for (Home home : homes ) {
109+ homeDao .create (home );
110+ }
111+ return null ;
112+ });
113+ }
114+ }
0 commit comments