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