2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.database.frontend;
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;
28 * General database frontend class
30 * @author Roland Haeder
32 public abstract class BaseDatabaseFrontend extends BaseFrameworkSystem implements DatabaseFrontend {
35 * Instance for database backend
37 private DatabaseBackend backend;
40 * No instances from this class
42 protected BaseDatabaseFrontend () {
46 * Instance for database backend
50 protected final DatabaseBackend getBackend () {
55 * Instance for database backend
57 * @param backend the backend to set
59 protected final void setBackend (final DatabaseBackend backend) {
60 this.backend = backend;
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
69 protected void initBackend () throws UnsupportedDatabaseBackendException, SQLException {
71 this.getLogger().trace("CALLED!"); //NOI18N
74 // @TODO rewrite this to own properties file
75 String backendType = this.getProperty("database.backendType"); //NOI18N
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());
85 case "base64csv": // BASE64-encoded CSV rows //NOI18N
86 this.backend = new Base64CsvDatabaseBackend(this.getTableName(), this);
89 default: // Unsupported
90 throw new UnsupportedDatabaseBackendException(backendType);
93 // Try to run a connect on it
94 this.backend.connectToDatabase();
95 } catch (final UnsupportedDatabaseDriverException ex) {
97 throw new UnsupportedDatabaseBackendException(backendType, ex);
101 this.getLogger().trace("EXIT!"); //NOI18N