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 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;
27 * General database frontend class
29 * @author Roland Haeder
31 public class BaseDatabaseFrontend extends BaseFrameworkSystem {
34 * Instance for database backend
36 private DatabaseBackend backend;
39 * No instances from this class
41 protected BaseDatabaseFrontend () {
45 * Instance for database backend
49 protected final DatabaseBackend getBackend () {
54 * Instance for database backend
56 * @param backend the backend to set
58 protected final void setBackend (final DatabaseBackend backend) {
59 this.backend = backend;
65 * @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException If the backend is not supported
67 protected void initBackend () throws UnsupportedDatabaseBackendException {
69 this.getLogger().trace("CALLED!"); //NOI18N
72 // @TODO rewrite this to own properties file
73 String backendType = System.getProperty("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N
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());
83 case "base64csv": // BASE64-encoded CSV rows //NOI18N
84 this.backend = new Base64CsvDatabaseBackend(this.getTableName());
87 default: // Unsupported
88 throw new UnsupportedDatabaseBackendException(backendType);
91 // Try to run a connect on it
92 this.backend.connectToDatabase();
93 } catch (final UnsupportedDatabaseDriverException ex) {
95 throw new UnsupportedDatabaseBackendException(backendType, ex);
99 this.getLogger().trace("EXIT!"); //NOI18N