]> git.mxchange.org Git - jfinancials-swing.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java
Basic support for properties file
[jfinancials-swing.git] / Addressbook / src / org / mxchange / addressbook / database / frontend / BaseDatabaseFrontend.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.database.frontend;
18
19 import org.mxchange.addressbook.BaseFrameworkSystem;
20 import org.mxchange.addressbook.database.backend.DatabaseBackend;
21 import org.mxchange.addressbook.database.backend.csv.Base64CsvDatabaseBackend;
22 import org.mxchange.addressbook.database.backend.mysql.MySqlDatabaseBackend;
23 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException;
24 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException;
25
26 /**
27  * General database frontend class
28  *
29  * @author Roland Haeder
30  */
31 public class BaseDatabaseFrontend extends BaseFrameworkSystem {
32
33         /**
34          * Instance for database backend
35          */
36         private DatabaseBackend backend;
37
38         /**
39          * No instances from this class
40          */
41         protected BaseDatabaseFrontend () {
42         }
43
44         /**
45          * Instance for database backend
46          *
47          * @return the backend
48          */
49         protected final DatabaseBackend getBackend () {
50                 return this.backend;
51         }
52
53         /**
54          * Instance for database backend
55          *
56          * @param backend the backend to set
57          */
58         protected final void setBackend (final DatabaseBackend backend) {
59                 this.backend = backend;
60         }
61
62         /**
63          * Initialize backend
64          * 
65          * @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException If the backend is not supported
66          */
67         protected void initBackend () throws UnsupportedDatabaseBackendException {
68                 // Trace message
69                 this.getLogger().trace("CALLED!"); //NOI18N
70
71                 // Read property
72                 // @TODO rewrite this to own properties file
73                 String backendType = this.getProperty("org.mxchange.addressbook.database.backendType"); //NOI18N
74
75                 // Try it
76                 try {
77                         // Switch on backend type
78                         switch (backendType) {
79                                 case "mysql": // MySQL backend, this requires more information //NOI18N
80                                         this.backend = new MySqlDatabaseBackend(this.getTableName());
81                                         break;
82                                         
83                                 case "base64csv": // BASE64-encoded CSV rows //NOI18N
84                                         this.backend = new Base64CsvDatabaseBackend(this.getTableName());
85                                         break;
86                                         
87                                 default: // Unsupported
88                                         throw new UnsupportedDatabaseBackendException(backendType);
89                         }
90
91                         // Try to run a connect on it
92                         this.backend.connectToDatabase();
93                 } catch (final UnsupportedDatabaseDriverException ex) {
94                         // Continue to throw
95                         throw new UnsupportedDatabaseBackendException(backendType, ex);
96                 }
97
98                 // Trace message
99                 this.getLogger().trace("EXIT!"); //NOI18N
100         }
101 }