]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java
Rewrite start of storing contacts in database as the previous was just for demo ...
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / database / backend / csv / CsvDatabaseBackend.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\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
8  *\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
13  *\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
16  */\r
17 package org.mxchange.addressbook.database.backend.csv;\r
18 \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.backend.BaseDatabaseBackend;\r
25 import org.mxchange.addressbook.database.storage.Storeable;\r
26 import org.mxchange.addressbook.database.storage.csv.StoreableCsv;\r
27 \r
28 /**\r
29  * A database backend with CSV file as storage implementation\r
30  * \r
31  * @author Roland Haeder\r
32  */\r
33 public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBackend {\r
34     /**\r
35      * Output stream for this storage engine\r
36      */\r
37     private RandomAccessFile storageFile;\r
38 \r
39     /**\r
40      * Constructor with table name\r
41      * \r
42      * @param tableName Name of "table"\r
43      */\r
44     public CsvDatabaseBackend (final String tableName) {\r
45         // Debug message\r
46         this.getLogger().debug(MessageFormat.format("Trying to initialize table {0} ...", tableName));\r
47 \r
48         // Set table name here, too\r
49         this.setTableName(tableName);\r
50 \r
51         // Construct file name\r
52         String fileName = String.format("data/table_%s.csv", tableName);\r
53 \r
54         // Debug message\r
55         this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName));\r
56 \r
57         try {\r
58             // Try to initialize the storage (file instance)\r
59             this.storageFile = new RandomAccessFile(fileName, "rw");\r
60         } catch (final FileNotFoundException ex) {\r
61             // Did not work\r
62             this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString()));\r
63             System.exit(1);\r
64         }\r
65 \r
66         // Output message\r
67         this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName));\r
68     }\r
69 \r
70     @Override\r
71     public void rewind () {\r
72         try {\r
73             // Rewind underlaying database file\r
74             this.storageFile.seek(0);\r
75         } catch (final IOException ex) {\r
76             this.getLogger().catching(ex);\r
77             System.exit(1);\r
78         }\r
79     }\r
80 \r
81     /**\r
82      * Stores given object by "visiting" it\r
83      *\r
84      * @param object An object implementing Storeable\r
85      * @throws java.io.IOException From "inner" class\r
86      */\r
87     @Override\r
88     public void store (final Storeable object) throws IOException{\r
89         // Make sure the instance is there (DataOutput flawor)\r
90         assert(this.storageFile instanceof DataOutput);\r
91 \r
92         // Try to cast it, this will fail if the interface is not implemented\r
93         StoreableCsv csv = (StoreableCsv) object;\r
94 \r
95         // Now get a string from the object that needs to be stored\r
96         String str = csv.getCsvStringFromStoreableObject();\r
97 \r
98         // Debug message\r
99         this.getLogger().debug(MessageFormat.format("str({0})={1}", str.length(), str));\r
100 \r
101         // The string is now a valid CSV string\r
102         this.storageFile.writeUTF(str);\r
103     }\r
104 }\r