2 * Copyright (C) 2015 Roland Haeder
\r
4 * This program is free software: you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation, either version 3 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
17 package org.mxchange.addressbook.database.backend.csv;
\r
19 import java.io.DataOutput;
\r
20 import java.io.FileNotFoundException;
\r
21 import java.io.IOException;
\r
22 import java.io.RandomAccessFile;
\r
23 import java.text.MessageFormat;
\r
24 import org.mxchange.addressbook.database.storage.Storeable;
\r
25 import org.mxchange.addressbook.database.backend.DatabaseBackend;
\r
26 import org.mxchange.addressbook.database.storage.csv.StoreableCsv;
\r
29 * A database backend with CSV file as storage implementation
\r
31 * @author Roland Haeder
\r
33 public class CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {
\r
35 * Output stream for this storage engine
\r
37 private RandomAccessFile storageFile;
\r
40 * Constructor with table name
\r
42 * @param tableName Name of "table"
\r
44 public CsvDatabaseBackend (final String tableName) {
\r
46 this.getLogger().debug(MessageFormat.format("Trying to initialize table {0} ...", tableName));
\r
48 // Set table name here, too
\r
49 this.setTableName(tableName);
\r
51 // Construct file name
\r
52 String fileName = String.format("data/table_%s.csv", tableName);
\r
55 this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName));
\r
58 // Try to initialize the storage (file instance)
\r
59 this.storageFile = new RandomAccessFile(fileName, "rw");
\r
60 } catch (final FileNotFoundException ex) {
\r
62 this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString()));
\r
67 this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName));
\r
71 * Stores given object by "visiting" it
\r
73 * @param object An object implementing Storeable
\r
74 * @throws java.io.IOException From "inner" class
\r
77 public void store (final Storeable object) throws IOException{
\r
78 // Make sure the instance is there (DataOutput flawor)
\r
79 assert(this.storageFile instanceof DataOutput);
\r
81 // Try to cast it, this will fail if the interface is not implemented
\r
82 StoreableCsv csv = (StoreableCsv) object;
\r
84 // Now get the object that needs to be stored
\r
85 String str = csv.getCsvStringFromStoreableObject();
\r
88 this.getLogger().debug(MessageFormat.format("str({0})={1}", str.length(), str));
\r
90 // The string is now a valid CSV string
\r
91 this.storageFile.writeUTF(str);
\r