]> git.mxchange.org Git - jfinancials-swing.git/blob - Addressbook/src/org/mxchange/addressbook/database/backend/csv/Base64CsvDatabaseBackend.java
Added a lot trace messages + sanity checks for null references and such things
[jfinancials-swing.git] / Addressbook / src / org / mxchange / addressbook / database / backend / csv / Base64CsvDatabaseBackend.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.database.backend.csv;
18
19 import java.io.DataOutput;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.RandomAccessFile;
23 import java.text.MessageFormat;
24 import java.util.ArrayList;
25 import java.util.Base64;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.StringTokenizer;
29 import org.mxchange.addressbook.contact.Contact;
30 import org.mxchange.addressbook.contact.Gender;
31 import org.mxchange.addressbook.contact.book.BookContact;
32 import org.mxchange.addressbook.contact.user.UserContact;
33 import org.mxchange.addressbook.database.backend.BaseDatabaseBackend;
34 import org.mxchange.addressbook.database.storage.Storeable;
35 import org.mxchange.addressbook.database.storage.csv.StoreableCsv;
36 import org.mxchange.addressbook.exceptions.BadTokenException;
37
38 /**
39  * A database backend with CSV file as storage implementation
40  *
41  * @author Roland Haeder
42  */
43 public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBackend {
44
45         /**
46          * Output stream for this storage engine
47          */
48         private RandomAccessFile storageFile;
49
50         /**
51          * Constructor with table name
52          *
53          * @param tableName Name of "table"
54          */
55         public Base64CsvDatabaseBackend (final String tableName) {
56                 // Debug message
57                 this.getLogger().debug(MessageFormat.format("Trying to initialize table {0} ...", tableName)); //NOI18N
58
59                 // Set table name here, too
60                 this.setTableName(tableName);
61
62                 // Construct file name
63                 String fileName = String.format("data/table_%s.b64", tableName); //NOI18N
64
65                 // Debug message
66                 this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName)); //NOI18N
67
68                 try {
69                         // Try to initialize the storage (file instance)
70                         this.storageFile = new RandomAccessFile(fileName, "rw"); //NOI18N
71                 } catch (final FileNotFoundException ex) {
72                         // Did not work
73                         this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString())); //NOI18N
74                         System.exit(1);
75                 }
76
77                 // Output message
78                 this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName)); //NOI18N
79         }
80
81         /**
82          * Gets an iterator for contacts
83          *
84          * @return Iterator for contacts
85          * @throws org.mxchange.addressbook.exceptions.BadTokenException If the
86          * underlaying method has found an invalid token
87          */
88         @Override
89         public Iterator<Contact> contactIterator () throws BadTokenException {
90                 // Trace message
91                 this.getLogger().trace("CALLED!"); //NOI18N
92
93                 /*
94                  * Then read the file into RAM (yes, not perfect for >1000 entries ...)
95                  * and get a List back.
96                  */
97                 List<Contact> list = this.readContactList();
98
99                 // List must be set
100                 assert (list instanceof List) : "list has not been set."; //NOI18N
101
102                 // Trace message
103                 this.getLogger().trace(MessageFormat.format("list.iterator()={0} - EXIT!", list.iterator())); //NOI18N
104
105                 // Get iterator from list and return it
106                 return list.iterator();
107         }
108
109         /**
110          * Shuts down this backend
111          */
112         @Override
113         public void doShutdown () {
114                 // Trace message
115                 this.getLogger().trace("CALLED!"); //NOI18N
116
117                 try {
118                         // Close file
119                         this.getStorageFile().close();
120                 } catch (final IOException ex) {
121                         this.getLogger().catching(ex);
122                         System.exit(1);
123                 }
124
125                 // Trace message
126                 this.getLogger().trace("EXIT!"); //NOI18N
127         }
128
129         /**
130          * Get length of underlaying file
131          *
132          * @return Length of underlaying file
133          */
134         @Override
135         public long length () {
136                 long length = 0;
137
138                 try {
139                         length = this.getStorageFile().length();
140                         this.getLogger().debug(MessageFormat.format("length={0}", length)); //NOI18N
141                 } catch (final IOException ex) {
142                         // Length cannot be determined
143                         this.getLogger().catching(ex);
144                         System.exit(1);
145                 }
146
147                 // Return result
148                 this.getLogger().trace(MessageFormat.format("length={0} : EXIT!", length)); //NOI18N
149                 return length;
150         }
151
152         /**
153          * Rewinds backend
154          */
155         @Override
156         public void rewind () {
157                 // Trace message
158                 this.getLogger().trace("CALLED!"); //NOI18N
159
160                 try {
161                         // Rewind underlaying database file
162                         this.getStorageFile().seek(0);
163                 } catch (final IOException ex) {
164                         this.getLogger().catching(ex);
165                         System.exit(1);
166                 }
167
168                 // Trace message
169                 this.getLogger().trace("EXIT!"); //NOI18N
170         }
171
172         /**
173          * Stores given object by "visiting" it
174          *
175          * @param object An object implementing Storeable
176          * @throws java.io.IOException From "inner" class
177          */
178         @Override
179         public void store (final Storeable object) throws IOException {
180                 // Trace message
181                 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
182
183                 // Object must not be null
184                 if (object == null) {
185                         // Abort here
186                         throw new NullPointerException("object is null");
187                 }
188
189                 // Make sure the instance is there (DataOutput flawor)
190                 assert (this.storageFile instanceof DataOutput);
191
192                 // Try to cast it, this will fail if the interface is not implemented
193                 StoreableCsv csv = (StoreableCsv) object;
194
195                 // Now get a string from the object that needs to be stored
196                 String str = csv.getCsvStringFromStoreableObject();
197
198                 // Debug message
199                 this.getLogger().debug(MessageFormat.format("str({0})={1}", str.length(), str)); //NOI18N
200
201                 // Encode line in BASE-64
202                 byte[] encoded = Base64.getEncoder().encode(str.getBytes());
203
204                 // The string is now a valid CSV string
205                 this.getStorageFile().write(encoded);
206
207                 // Trace message
208                 this.getLogger().trace("EXIT!"); //NOI18N
209         }
210
211         /**
212          * Adds given contact to list
213          *
214          * @param contact Contact instance to add
215          * @param list List instance
216          */
217         private void addContactToList (final Contact contact, final List<Contact> list) {
218                 // Trace message
219                 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
220
221                 // No null here
222                 if (contact == null) {
223                         // Throw exception
224                         throw new NullPointerException("contact is null"); //NOI18N
225                 } else if (list == null) {
226                         // Throw exception
227                         throw new NullPointerException("list is null"); //NOI18N
228                 }
229
230                 // Debug message
231                 this.getLogger().debug(MessageFormat.format("contact={0}", contact)); //NOI18N
232
233                 // Is the contact read?
234                 if (contact instanceof Contact) {
235                         // Then add it
236                         boolean added = list.add(contact);
237
238                         // Debug message
239                         this.getLogger().debug(MessageFormat.format("contact={0} added={1}", contact, added)); //NOI18N
240
241                         // Has it been added?
242                         if (!added) {
243                                 // Not added
244                                 this.getLogger().warn("Contact object has not been added."); //NOI18N
245                         }
246                 }
247
248                 // Trace message
249                 this.getLogger().trace("EXIT!"); //NOI18N
250         }
251
252         /**
253          * Returns storage file
254          *
255          * @return Storage file instance
256          */
257         private RandomAccessFile getStorageFile () {
258                 return this.storageFile;
259         }
260
261         /**
262          * Checks whether end of file has been reached
263          *
264          * @return Whether lines are left to read
265          */
266         private boolean isEndOfFile () {
267                 // Default is EOF
268                 boolean isEof = true;
269
270                 try {
271                         isEof = (this.getStorageFile().getFilePointer() >= this.length());
272                 } catch (final IOException ex) {
273                         // Length cannot be determined
274                         this.getLogger().catching(ex);
275                 }
276
277                 // Return status
278                 this.getLogger().trace(MessageFormat.format("isEof={0} : EXIT!", isEof)); //NOI18N
279                 return isEof;
280         }
281
282         /**
283          * Reads the database file, if available, and adds all read lines into the
284          * list.
285          *
286          * @return A list with Contact instances
287          */
288         private List<Contact> readContactList () throws BadTokenException {
289                 this.getLogger().trace("CALLED!"); //NOI18N
290
291                 // First rewind
292                 this.rewind();
293
294                 // Get file size and divide it by 140 (possible average length of one line)
295                 int lines = Math.round(this.length() / 140 + 0.5f);
296
297                 // Debug message
298                 this.getLogger().debug(MessageFormat.format("lines={0}", lines)); //NOI18N
299
300                 // Instance list
301                 // @TODO The maximum length could be guessed from file size?
302                 List<Contact> list = new ArrayList<>(lines);
303
304                 // Init variables
305                 StringTokenizer tokenizer;
306                 String line;
307
308                 // Init A lot variables
309                 Long num = null;
310                 Boolean bool = null;
311                 Gender gender = null;
312                 int count = 0;
313                 Contact contact = null;
314
315                 // Read all lines
316                 while (!this.isEndOfFile()) {
317                         // Then read a line
318                         line = this.readLine();
319
320                         // Debug message
321                         this.getLogger().debug(MessageFormat.format("line={0}", line)); //NOI18N
322
323                         // Then tokenize it
324                         // @TODO Move this into separate method
325                         tokenizer = new StringTokenizer(line, ";"); //NOI18N
326
327                         // Reset variables
328                         count = 0;
329                         contact = null;
330
331                         // The tokens are now available, so get all
332                         while (tokenizer.hasMoreElements()) {
333                                 // Reset variables
334                                 num = null;
335                                 bool = null;
336
337                                 // Get next token
338                                 String token = tokenizer.nextToken();
339
340                                 // If char " is at pos 2 (0,1,2), then cut it of there
341                                 if ((token.charAt(0) != '"') && (token.charAt(2) == '"')) {
342                                         // UTF-8 writer characters found
343                                         token = token.substring(2);
344                                 }
345
346                                 // Debug message
347                                 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
348
349                                 // Verify token, it must have double-quotes on each side
350                                 if ((!token.startsWith("\"")) || (!token.endsWith("\""))) { //NOI18N
351                                         // Something bad was read
352                                         throw new BadTokenException(MessageFormat.format("Token {0} at position {1} has not double-quotes on both ends.", token, count)); //NOI18N
353                                 }
354
355                                 // All fine, so remove it
356                                 String strippedToken = token.substring(1, token.length() - 1);
357
358                                 // Is the string's content "null"?
359                                 if (strippedToken.equals("null")) { //NOI18N
360                                         // Debug message
361                                         this.getLogger().debug(MessageFormat.format("strippedToken={0} - NULL!", strippedToken)); //NOI18N
362
363                                         // This needs to be set to null
364                                         strippedToken = null;
365                                 }
366
367                                 // Debug message
368                                 this.getLogger().debug(MessageFormat.format("strippedToken={0}", strippedToken)); //NOI18N
369
370                                 // Now, let's try a number check, if no null
371                                 if (strippedToken != null) {
372                                         // Okay, no null, maybe the string bears a decimal number?
373                                         try {
374                                                 num = Long.valueOf(strippedToken);
375
376                                                 // Debug message
377                                                 this.getLogger().debug(MessageFormat.format("strippedToken={0} - NUMBER!", strippedToken)); //NOI18N
378                                         } catch (final NumberFormatException ex) {
379                                                 // No number, then set default
380                                                 num = null;
381                                         }
382                                 }
383
384                                 // Now, let's try a boolean check, if no null
385                                 if ((strippedToken != null) && (num == null) && ((strippedToken.equals("true")) || (strippedToken.equals("false")))) { //NOI18N
386                                         // Debug message
387                                         this.getLogger().debug(MessageFormat.format("strippedToken={0} - BOOLEAN!", strippedToken)); //NOI18N
388
389                                         // parseBoolean() is relaxed, so no exceptions
390                                         bool = Boolean.valueOf(strippedToken);
391                                 }
392
393                                 // Debug message
394                                 this.getLogger().debug(MessageFormat.format("strippedToken={0},num={1},bool={2}", strippedToken, num, bool)); //NOI18N
395
396                                 // Now, let's try a gender check, if no null
397                                 if ((strippedToken != null) && (num == null) && (bool == null) && ((strippedToken.equals("M")) || (strippedToken.equals("F")) || (strippedToken.equals("C")))) { //NOI18N
398                                         // Get first character
399                                         gender = Gender.fromChar(strippedToken.charAt(0));
400
401                                         // Debug message
402                                         this.getLogger().debug(MessageFormat.format("strippedToken={0},gender={1}", strippedToken, gender)); //NOI18N
403
404                                         // This instance must be there
405                                         assert (gender instanceof Gender) : "gender is not set by Gender.fromChar(" + strippedToken + ")"; //NOI18N
406                                 }
407
408                                 // Now it depends on the counter which position we need to check
409                                 switch (count) {
410                                         case 0: // isOwnContact
411                                                 assert ((bool instanceof Boolean));
412
413                                                 // Debug message
414                                                 this.getLogger().debug(MessageFormat.format("bool={0}", bool)); //NOI18N
415
416                                                 // Is it own contact?
417                                                 if (true == bool) {
418                                                         // Debug message
419                                                         this.getLogger().debug("Creating UserContact object ..."); //NOI18N
420
421                                                         // Own entry
422                                                         contact = new UserContact();
423                                                 } else {
424                                                         // Debug message
425                                                         this.getLogger().debug("Creating BookContact object ..."); //NOI18N
426
427                                                         // Other contact
428                                                         contact = new BookContact();
429                                                 }
430                                                 break;
431
432                                         case 1: // Gender
433                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
434
435                                                 // Update data
436                                                 contact.updateNameData(gender, null, null, null);
437                                                 break;
438
439                                         case 2: // Surname
440                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
441                                                 assert (gender instanceof Gender) : "gender instance is not set"; //NOI18N
442
443                                                 // Update data
444                                                 contact.updateNameData(gender, strippedToken, null, null);
445                                                 break;
446
447                                         case 3: // Family name
448                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
449                                                 assert (gender instanceof Gender) : "gender instance is not set"; //NOI18N
450
451                                                 // Update data
452                                                 contact.updateNameData(gender, null, strippedToken, null);
453                                                 break;
454
455                                         case 4: // Company name
456                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
457                                                 assert (gender instanceof Gender) : "gender instance is not set"; //NOI18N
458
459                                                 // Update data
460                                                 contact.updateNameData(gender, null, null, strippedToken);
461                                                 break;
462
463                                         case 5: // Street number
464                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
465
466                                                 // Update data
467                                                 contact.updateAddressData(strippedToken, 0, null, null);
468                                                 break;
469
470                                         case 6: // ZIP code
471                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
472
473                                                 // Update data
474                                                 contact.updateAddressData(null, num, null, null);
475                                                 break;
476
477                                         case 7: // City name
478                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
479
480                                                 // Update data
481                                                 contact.updateAddressData(null, 0, strippedToken, null);
482                                                 break;
483
484                                         case 8: // Country code
485                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
486
487                                                 // Update data
488                                                 contact.updateAddressData(null, 0, null, strippedToken);
489                                                 break;
490
491                                         case 9: // Phone number
492                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
493
494                                                 // Update data
495                                                 contact.updateOtherData(strippedToken, null, null, null, null, null);
496                                                 break;
497
498                                         case 10: // Fax number
499                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
500
501                                                 // Update data
502                                                 contact.updateOtherData(null, strippedToken, null, null, null, null);
503                                                 break;
504
505                                         case 11: // Cellphone number
506                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
507
508                                                 // Update data
509                                                 contact.updateOtherData(null, null, strippedToken, null, null, null);
510                                                 break;
511
512                                         case 12: // Email address
513                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
514
515                                                 // Update data
516                                                 contact.updateOtherData(null, null, null, strippedToken, null, null);
517                                                 break;
518
519                                         case 13: // Birthday
520                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
521
522                                                 // Update data
523                                                 contact.updateOtherData(null, null, null, null, strippedToken, null);
524                                                 break;
525
526                                         case 14: // Birthday
527                                                 assert (contact instanceof Contact) : "First token was not boolean"; //NOI18N
528
529                                                 // Update data
530                                                 contact.updateOtherData(null, null, null, null, null, strippedToken);
531                                                 break;
532
533                                         default: // New data entry
534                                                 this.getLogger().warn(MessageFormat.format("Will not handle unknown data {0} at index {1}", strippedToken, count)); //NOI18N
535                                                 break;
536                                 }
537
538                                 // Increment counter for next round
539                                 count++;
540                         }
541
542                         // The contact instance should be there now
543                         assert (contact instanceof Contact) : "contact is not set: " + contact; //NOI18N
544
545                         // Add contact
546                         this.addContactToList(contact, list);
547                 }
548
549                 // Return finished list
550                 this.getLogger().trace(MessageFormat.format("list.size()={0} : EXIT!", list.size())); //NOI18N
551                 return list;
552         }
553
554         /**
555          * Reads a line from file base
556          *
557          * @return Read line from file
558          */
559         private String readLine () {
560                 // Trace message
561                 this.getLogger().trace("CALLED!"); //NOI18N
562
563                 // Init input
564                 String input = null;
565
566                 try {
567                         // Read single line
568                         String base64 = this.getStorageFile().readLine();
569
570                         // Decode BASE-64
571                         byte[] decoded = Base64.getDecoder().decode(base64);
572
573                         // Convert to string
574                         input = new String(decoded);
575                 } catch (final IOException ex) {
576                         this.getLogger().catching(ex);
577                 }
578
579                 // Trace message
580                 this.getLogger().trace(MessageFormat.format("input={0} - EXIT!", input)); //NOI18N
581
582                 // Return read string or null
583                 return input;
584         }
585 }