+/*\r
+ * Copyright (C) 2015 Roland Haeder\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package org.mxchange.addressbook.database.backend.csv;\r
+\r
+import java.io.DataOutput;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.io.RandomAccessFile;\r
+import java.text.MessageFormat;\r
+import org.mxchange.addressbook.database.storage.Storeable;\r
+import org.mxchange.addressbook.database.backend.DatabaseBackend;\r
+import org.mxchange.addressbook.database.storage.csv.StoreableCsv;\r
+\r
+/**\r
+ * A database backend with CSV file as storage implementation\r
+ * \r
+ * @author Roland Haeder\r
+ */\r
+public class CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {\r
+ /**\r
+ * Output stream for this storage engine\r
+ */\r
+ private RandomAccessFile storageFile;\r
+\r
+ /**\r
+ * Constructor with table name\r
+ * \r
+ * @param tableName Name of "table"\r
+ */\r
+ public CsvDatabaseBackend (final String tableName) {\r
+ // Debug message\r
+ this.getLogger().debug(MessageFormat.format("Trying to initialize table {0} ...", tableName));\r
+\r
+ // Set table name here, too\r
+ this.setTableName(tableName);\r
+\r
+ // Construct file name\r
+ String fileName = String.format("data/table_%s.csv", tableName);\r
+\r
+ // Debug message\r
+ this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName));\r
+\r
+ try {\r
+ // Try to initialize the storage (file instance)\r
+ this.storageFile = new RandomAccessFile(fileName, "rw");\r
+ } catch (final FileNotFoundException ex) {\r
+ // Did not work\r
+ this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString()));\r
+ System.exit(1);\r
+ }\r
+\r
+ // Output message\r
+ this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName));\r
+ }\r
+\r
+ /**\r
+ * Stores given object by "visiting" it\r
+ *\r
+ * @param object An object implementing Storeable\r
+ * @throws java.io.IOException From "inner" class\r
+ */\r
+ @Override\r
+ public void store (final Storeable object) throws IOException{\r
+ // Make sure the instance is there (DataOutput flawor)\r
+ assert(this.storageFile instanceof DataOutput);\r
+\r
+ // Try to cast it, this will fail if the interface is not implemented\r
+ StoreableCsv csv = (StoreableCsv) object;\r
+\r
+ // Now get the object that needs to be stored\r
+ String str = csv.getCsvStringFromStoreableObject();\r
+\r
+ // Debug message\r
+ this.getLogger().debug(MessageFormat.format("str({0})={1}", str.length(), str));\r
+\r
+ // The string is now a valid CSV string\r
+ this.storageFile.writeUTF(str);\r
+ }\r
+}\r