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 java.util.ArrayList;
\r
25 import java.util.Iterator;
\r
26 import java.util.List;
\r
27 import java.util.StringTokenizer;
\r
28 import org.mxchange.addressbook.BadTokenException;
\r
29 import org.mxchange.addressbook.contact.Contact;
\r
30 import org.mxchange.addressbook.contact.book.BookContact;
\r
31 import org.mxchange.addressbook.contact.user.UserContact;
\r
32 import org.mxchange.addressbook.database.backend.BaseDatabaseBackend;
\r
33 import org.mxchange.addressbook.database.storage.Storeable;
\r
34 import org.mxchange.addressbook.database.storage.csv.StoreableCsv;
\r
37 * A database backend with CSV file as storage implementation
\r
39 * @author Roland Haeder
\r
41 public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBackend {
\r
43 * Output stream for this storage engine
\r
45 private RandomAccessFile storageFile;
\r
48 * Constructor with table name
\r
50 * @param tableName Name of "table"
\r
52 public CsvDatabaseBackend (final String tableName) {
\r
54 this.getLogger().debug(MessageFormat.format("Trying to initialize table {0} ...", tableName));
\r
56 // Set table name here, too
\r
57 this.setTableName(tableName);
\r
59 // Construct file name
\r
60 String fileName = String.format("data/table_%s.csv", tableName);
\r
63 this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName));
\r
66 // Try to initialize the storage (file instance)
\r
67 this.storageFile = new RandomAccessFile(fileName, "rw");
\r
68 } catch (final FileNotFoundException ex) {
\r
70 this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString()));
\r
75 this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName));
\r
79 * Gets an iterator for contacts
\r
81 * @return Iterator for contacts
\r
82 * @throws org.mxchange.addressbook.BadTokenException If the underlaying method has found an invalid token
\r
85 public Iterator<Contact> contactIterator () throws BadTokenException {
\r
87 * Then read the file into RAM (yes, not perfect for >1000 entries ...)
\r
88 * and get a List back.
\r
90 List<Contact> list = this.readContactList();
\r
92 // Get iterator from list and return it
\r
93 return list.iterator();
\r
97 * Get length of underlaying file
\r
99 * @return Length of underlaying file
\r
102 public long length () {
\r
106 length = this.storageFile.length();
\r
107 this.getLogger().debug(MessageFormat.format("length={0}", length));
\r
108 } catch (final IOException ex) {
\r
109 // Length cannot be determined
\r
110 this.getLogger().catching(ex);
\r
115 this.getLogger().trace(MessageFormat.format("length={0} : EXIT!", length));
\r
123 public void rewind (){
\r
124 this.getLogger().trace("CALLED!");
\r
127 // Rewind underlaying database file
\r
128 this.storageFile.seek(0);
\r
129 } catch (final IOException ex) {
\r
130 this.getLogger().catching(ex);
\r
134 this.getLogger().trace("EXIT!");
\r
138 * Stores given object by "visiting" it
\r
140 * @param object An object implementing Storeable
\r
141 * @throws java.io.IOException From "inner" class
\r
144 public void store (final Storeable object) throws IOException {
\r
145 // Make sure the instance is there (DataOutput flawor)
\r
146 assert(this.storageFile instanceof DataOutput);
\r
148 // Try to cast it, this will fail if the interface is not implemented
\r
149 StoreableCsv csv = (StoreableCsv) object;
\r
151 // Now get a string from the object that needs to be stored
\r
152 String str = csv.getCsvStringFromStoreableObject();
\r
155 this.getLogger().debug(MessageFormat.format("str({0})={1}", str.length(), str));
\r
157 // The string is now a valid CSV string
\r
158 this.storageFile.writeChars(str);
\r
162 * Checks whether end of file has been reached
\r
164 * @return Whether lines are left to read
\r
166 private boolean isEndOfFile () {
\r
168 boolean isEof = true;
\r
171 isEof = (this.storageFile.getFilePointer() >= this.length());
\r
172 } catch (final IOException ex) {
\r
173 // Length cannot be determined
\r
174 this.getLogger().catching(ex);
\r
178 this.getLogger().trace(MessageFormat.format("isEof={0} : EXIT!", isEof));
\r
183 * Reads the database file, if available, and adds all read lines into
\r
186 * @return A list with Contact instances
\r
188 private List<Contact> readContactList () throws BadTokenException {
\r
189 this.getLogger().trace("CALLED!");
\r
194 // Get file size and divide it by 140 (possible average length of one line)
\r
195 int lines = Math.round(this.length() / 140 + 0.5f);
\r
198 this.getLogger().debug(MessageFormat.format("lines={0}", lines));
\r
201 // @TODO The maximum length could be guessed from file size?
\r
202 List<Contact> list = new ArrayList<>(lines);
\r
205 StringTokenizer tokenizer;
\r
209 while (!this.isEndOfFile()) {
\r
210 // Then read a line
\r
211 line = this.readLine();
\r
214 this.getLogger().debug(MessageFormat.format("line={0}", line));
\r
216 // Then tokenize it
\r
217 // @TODO Move this into separate method
\r
218 tokenizer = new StringTokenizer(line, ";");
\r
223 // Init contact object
\r
224 Contact contact = null;
\r
226 // The tokens are now available, so get all
\r
227 while (tokenizer.hasMoreElements()) {
\r
229 String token = tokenizer.nextToken();
\r
232 this.getLogger().debug(MessageFormat.format("token={0}", token));
\r
234 // Verify token, it must have double-quotes on each side
\r
235 if ((!token.startsWith("\"")) || (!token.endsWith("\""))) {
\r
236 // Something bad was read
\r
237 throw new BadTokenException(MessageFormat.format("Token {0} has not double-quotes on both ends.", token));
\r
240 // All fine, so remove it
\r
241 String strippedToken = token.substring(1, token.length() - 1);
\r
243 // Is the string's content "null"?
\r
244 if (strippedToken.equals("null")) {
\r
246 this.getLogger().debug(MessageFormat.format("strippedToken={0} - NULL!", strippedToken));
\r
248 // This needs to be set to null
\r
249 strippedToken = null;
\r
253 this.getLogger().debug(MessageFormat.format("strippedToken={0}", strippedToken));
\r
255 // Init number/string data values
\r
256 String strData = strippedToken;
\r
258 Boolean bool = null;
\r
261 // Now, let's try a number check, if no null
\r
262 if (strippedToken != null) {
\r
263 // Okay, no null, maybe the string bears a decimal number?
\r
265 num = Long.valueOf(strippedToken);
\r
268 this.getLogger().debug(MessageFormat.format("strippedToken={0} - NUMBER!", strippedToken));
\r
269 } catch (final NumberFormatException ex) {
\r
270 // No number, then set default
\r
275 // Now, let's try a boolean check, if no null
\r
276 if ((strippedToken != null) && (num == null) && ((strippedToken.equals("true")) || (strippedToken.equals("false")))) {
\r
278 this.getLogger().debug(MessageFormat.format("strippedToken={0} - BOOLEAN!", strippedToken));
\r
280 // parseBoolean() is relaxed, so no exceptions
\r
281 bool = Boolean.valueOf(strippedToken);
\r
284 // Now, let's try a boolean check, if no null
\r
285 if ((strippedToken != null) && (num == null) && (bool == null) && ((strippedToken.equals("M")) || (strippedToken.equals("F")) || (strippedToken.equals("C")))) {
\r
286 // Get first character
\r
287 gender = strippedToken.charAt(0);
\r
290 // Now it depends on the counter which position we need to check
\r
292 case 0: // isOwnContact
\r
293 assert((bool instanceof Boolean));
\r
296 this.getLogger().debug(MessageFormat.format("bool={0}", bool));
\r
298 // Is it own contact?
\r
299 if (true == bool) {
\r
301 this.getLogger().debug("Creating UserContact object ...");
\r
304 contact = new UserContact();
\r
307 this.getLogger().debug("Creating BookContact object ...");
\r
310 contact = new BookContact();
\r
315 assert(contact instanceof Contact) : "First token was not boolean";
\r
316 assert(gender != '?') : "Gender is not detected.";
\r
319 contact.updateNameData(gender, null, null, null);
\r
323 assert(contact instanceof Contact) : "First token was not boolean";
\r
324 assert(gender != '?') : "Gender is not detected.";
\r
327 contact.updateNameData(gender, strippedToken, null, null);
\r
330 case 3: // Family name
\r
331 assert(contact instanceof Contact) : "First token was not boolean";
\r
332 assert(gender != '?') : "Gender is not detected.";
\r
335 contact.updateNameData(gender, null, strippedToken, null);
\r
338 case 4: // Company name
\r
339 assert(contact instanceof Contact) : "First token was not boolean";
\r
340 assert(gender != '?') : "Gender is not detected.";
\r
343 contact.updateNameData(gender, null, null, strippedToken);
\r
346 case 5: // Street number
\r
347 assert(contact instanceof Contact) : "First token was not boolean";
\r
350 contact.updateAddressData(strippedToken, 0, null, null);
\r
353 case 6: // ZIP code
\r
354 assert(contact instanceof Contact) : "First token was not boolean";
\r
357 contact.updateAddressData(null, num, null, null);
\r
360 case 7: // City name
\r
361 assert(contact instanceof Contact) : "First token was not boolean";
\r
364 contact.updateAddressData(null, 0, strippedToken, null);
\r
367 case 8: // Country code
\r
368 assert(contact instanceof Contact) : "First token was not boolean";
\r
371 contact.updateAddressData(null, 0, null, strippedToken);
\r
374 case 9: // Phone number
\r
375 assert(contact instanceof Contact) : "First token was not boolean";
\r
378 contact.updateOtherData(strippedToken, null, null, null, null, null);
\r
381 case 10: // Fax number
\r
382 assert(contact instanceof Contact) : "First token was not boolean";
\r
385 contact.updateOtherData(null, strippedToken, null, null, null, null);
\r
388 case 11: // Cellphone number
\r
389 assert(contact instanceof Contact) : "First token was not boolean";
\r
392 contact.updateOtherData(null, null, strippedToken, null, null, null);
\r
395 case 12: // Email address
\r
396 assert(contact instanceof Contact) : "First token was not boolean";
\r
399 contact.updateOtherData(null, null, null, strippedToken, null, null);
\r
402 case 13: // Birthday
\r
403 assert(contact instanceof Contact) : "First token was not boolean";
\r
406 contact.updateOtherData(null, null, null, null, strippedToken, null);
\r
409 case 14: // Birthday
\r
410 assert(contact instanceof Contact) : "First token was not boolean";
\r
413 contact.updateOtherData(null, null, null, null, null, strippedToken);
\r
416 default: // New data entry
\r
417 this.getLogger().warn(MessageFormat.format("Will not handle unknown data {0} at index {1}", strippedToken, count));
\r
421 // Increment counter for next round
\r
426 this.getLogger().debug(MessageFormat.format("contact={0}", contact));
\r
428 // Is the contact read?
\r
429 if (contact instanceof Contact) {
\r
431 boolean added = list.add(contact);
\r
434 this.getLogger().debug(MessageFormat.format("contact={0} added={1}", contact, added));
\r
436 // Has it been added?
\r
439 this.getLogger().warn("Contact object has not been added.");
\r
444 // Return finished list
\r
445 this.getLogger().trace(MessageFormat.format("list.size()={0} : EXIT!", list.size()));
\r
450 * Reads a line from file base
\r
452 * @return Read line from file
\r
454 private String readLine () {
\r
456 String input = null;
\r
459 input = this.storageFile.readLine();
\r
460 } catch (final IOException ex) {
\r
461 this.getLogger().catching(ex);
\r
464 // Return read string or null
\r