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