]> git.mxchange.org Git - jfinancials-lib.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java
Method getProperty() now prefixes all keys with org.mxchange.addressbook.
[jfinancials-lib.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 java.sql.SQLException;
20 import org.mxchange.addressbook.BaseFrameworkSystem;
21 import org.mxchange.addressbook.database.backend.DatabaseBackend;
22 import org.mxchange.addressbook.database.backend.csv.Base64CsvDatabaseBackend;
23 import org.mxchange.addressbook.database.backend.mysql.MySqlDatabaseBackend;
24 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException;
25 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException;
26
27 /**
28  * General database frontend class
29  *
30  * @author Roland Haeder
31  */
32 public class BaseDatabaseFrontend extends BaseFrameworkSystem {
33
34         /**
35          * Instance for database backend
36          */
37         private DatabaseBackend backend;
38
39         /**
40          * No instances from this class
41          */
42         protected BaseDatabaseFrontend () {
43         }
44
45         /**
46          * Instance for database backend
47          *
48          * @return the backend
49          */
50         protected final DatabaseBackend getBackend () {
51                 return this.backend;
52         }
53
54         /**
55          * Instance for database backend
56          *
57          * @param backend the backend to set
58          */
59         protected final void setBackend (final DatabaseBackend backend) {
60                 this.backend = backend;
61         }
62
63         /**
64          * Initialize backend
65          * 
66          * @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException If the backend is not supported
67          * @throws java.sql.SQLException If a SQL database backend fails to connect
68          */
69         protected void initBackend () throws UnsupportedDatabaseBackendException, SQLException {
70                 // Trace message
71                 this.getLogger().trace("CALLED!"); //NOI18N
72
73                 // Read property
74                 // @TODO rewrite this to own properties file
75                 String backendType = this.getProperty("database.backendType"); //NOI18N
76
77                 // Try it
78                 try {
79                         // Switch on backend type
80                         switch (backendType) {
81                                 case "mysql": // MySQL backend, this requires more information //NOI18N
82                                         this.backend = new MySqlDatabaseBackend(this.getTableName());
83                                         break;
84                                         
85                                 case "base64csv": // BASE64-encoded CSV rows //NOI18N
86                                         this.backend = new Base64CsvDatabaseBackend(this.getTableName());
87                                         break;
88                                         
89                                 default: // Unsupported
90                                         throw new UnsupportedDatabaseBackendException(backendType);
91                         }
92
93                         // Try to run a connect on it
94                         this.backend.connectToDatabase();
95                 } catch (final UnsupportedDatabaseDriverException ex) {
96                         // Continue to throw
97                         throw new UnsupportedDatabaseBackendException(backendType, ex);
98                 }
99
100                 // Trace message
101                 this.getLogger().trace("EXIT!"); //NOI18N
102         }
103 }