]> git.mxchange.org Git - jfinancials-lib.git/blob - src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java
Continued rewriting:
[jfinancials-lib.git] / src / org / mxchange / addressbook / manager / contact / AddressbookContactManager.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.manager.contact;
18
19 import java.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import java.util.List;
27 import org.mxchange.addressbook.client.AddressbookClient;
28 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jcontacts.contact.gender.Gender;
31 import org.mxchange.jcore.client.Client;
32 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
33 import org.mxchange.jcore.manager.BaseManager;
34 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
35 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
36
37 /**
38  * A manager for contacts.
39  * <p>
40  * @author Roland Haeder
41  * @version 0.0
42  */
43 public class AddressbookContactManager extends BaseManager implements ManageableContactAddressbook {
44
45         /**
46          * Column name list
47          */
48         private final List<String> columnNames;
49
50         /**
51          * Logger instance
52          */
53         @Log
54         private LoggerBeanLocal logger;
55
56         /**
57          * Translated column name list
58          */
59         private final List<String> translatedColumnNames;
60
61         /**
62          * Constructor which accepts maxContacts for maximum (initial) contacts and
63          * a client instance.
64          * <p>
65          * @param client Client instance to use
66          * @throws java.sql.SQLException If an SQL error occurs
67          */
68         public AddressbookContactManager (final Client client) throws SQLException {
69                 // Trace message
70                 this.getLogger().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
71
72                 // Make sure all parameters are set correctly
73                 if (null == client) {
74                         // Abort here
75                         throw new NullPointerException("client is null"); //NOI18N
76                 }
77
78                 // Set client instance
79                 this.setClient(client);
80
81                 // Init database connection
82                 DatabaseFrontend frontend = new AddressbookContactDatabaseFrontend(this);
83                 this.setFrontend(frontend);
84
85                 // Initialize list
86                 this.columnNames = new ArrayList<>(15);
87                 this.translatedColumnNames = new ArrayList<>(15);
88
89                 // And fill it
90                 this.fillColumnNamesFromBundle();
91
92                 // Debug message
93                 //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
94         }
95
96         @Override
97         public void addContact (final Contact contact) throws ContactAlreadyAddedException {
98                 // Trace message
99                 this.getLogger().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
100
101                 // Contact instance must not be null
102                 if (null == contact) {
103                         // Abort here
104                         throw new NullPointerException("contact is null"); //NOI18N
105                 }
106
107                 // Add it
108                 ((AddressbookContactFrontend) this.getFrontend()).addContact(contact);
109
110                 // Trace message
111                 this.getLogger().logTrace("EXIT!"); //NOI18N
112         }
113
114         @Override
115         public void doAddOtherAddress () {
116                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
117         }
118
119         @Override
120         public void doChangeAddressData (final Contact contact) {
121                 // Trace message
122                 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
123
124                 // Contact must not be null
125                 if (null == contact) {
126                         // Abort here
127                         throw new NullPointerException("contact is null"); //NOI18N
128                 }
129                 if (!(this.getClient() instanceof AddressbookClient)) {
130                         // Cannot cast
131                         throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
132                 }
133
134                 // Get and cast client instance
135                 AddressbookClient client = (AddressbookClient) this.getClient();
136
137                 // First display it again
138                 client.displayAddressBox(contact);
139
140                 // Is it own data?
141                 if (contact.isOwnContact()) {
142                         // Deligate to client
143                         client.doChangeOwnAddressData(contact);
144                 } else {
145                         // Other contact's address data to change
146                         throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
147                 }
148
149                 // Trace message
150                 this.getLogger().logTrace("EXIT!"); //NOI18N
151         }
152
153         @Override
154         public void doChangeNameData (final Contact contact) {
155                 // Trace message
156                 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
157
158                 // Contact must not be null
159                 if (null == contact) {
160                         // Abort here
161                         throw new NullPointerException("contact is null"); //NOI18N
162                 }
163
164                 // Get and cast client instance
165                 AddressbookClient client = (AddressbookClient) this.getClient();
166
167                 // First display them again
168                 client.displayNameBox(contact);
169
170                 // Is this own data?
171                 if (contact.isOwnContact()) {
172                         // Re-ask own data
173                         client.doChangeOwnNameData(contact);
174                 } else {
175                         // Then re-ask them ...
176                         throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
177                 }
178
179                 // Trace message
180                 this.getLogger().logTrace("EXIT!"); //NOI18N
181         }
182
183         @Override
184         public void doChangeOtherAddress () {
185                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
186         }
187
188         @Override
189         public void doChangeOtherData (final Contact contact) {
190                 // Trace message
191                 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
192
193                 // Contact must not be null
194                 if (null == contact) {
195                         // Abort here
196                         throw new NullPointerException("contact is null"); //NOI18N
197                 }
198
199                 // Get and cast client instance
200                 AddressbookClient client = (AddressbookClient) this.getClient();
201
202                 // First display them again
203                 client.displayOtherDataBox(contact);
204
205                 // Is this own data?
206                 if (contact.isOwnContact()) {
207                         // Re-ask own data
208                         client.doChangeOwnOtherData(contact);
209                 } else {
210                         // Then re-ask them ...
211                         throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
212                 }
213
214                 // Trace message
215                 this.getLogger().logTrace("EXIT!"); //NOI18N
216         }
217
218         @Override
219         public void doChangeOwnData () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
220                 // Trace message
221                 this.getLogger().logTrace("CALLED!"); //NOI18N
222
223                 /*
224                  * First check if the user has registered own contact, before that
225                  * nothing can be changed.
226                  */
227                 if (!this.isOwnContactAdded()) {
228                         // Not added
229                         this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
230
231                         // Skip any below code
232                         return;
233                 }
234
235                 // Instance
236                 Contact contact = this.getOwnContact();
237
238                 // It must be found
239                 assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
240
241                 // Get and cast client instance
242                 AddressbookClient client = (AddressbookClient) this.getClient();
243
244                 // Display contact
245                 client.show(contact);
246
247                 try {
248                         // Ask user what to change
249                         client.userChooseChangeContactData(contact);
250                 } catch (final UnhandledUserChoiceException ex) {
251                         this.getLogger().logException(ex);
252                 }
253
254                 // Trace message
255                 this.getLogger().logTrace("EXIT!"); //NOI18N
256         }
257
258         @Override
259         public void doDeleteOtherAddress () {
260                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
261         }
262
263         @Override
264         public void doEnterOwnData () throws ContactAlreadyAddedException, IOException {
265                 // Trace message
266                 this.getLogger().logTrace("CALLED!"); //NOI18N
267
268                 // Is own contact already added?
269                 if (this.isOwnContactAdded()) {
270                         // Don't continue here
271                         throw new ContactAlreadyAddedException();
272                 }
273
274                 // Get and cast client instance
275                 AddressbookClient client = (AddressbookClient) this.getClient();
276
277                 // Deligate this call to the client
278                 Contact contact = client.doEnterOwnData();
279
280                 // Is it set?
281                 if (contact instanceof Contact) {
282                         // Add it to contact "book"
283                         this.registerContact(contact);
284                 }
285
286                 // Trace message
287                 this.getLogger().logTrace("EXIT!"); //NOI18N
288         }
289
290         @Override
291         public void doListContacts () {
292                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
293         }
294
295         @Override
296         public void doSearchContacts () {
297                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
298         }
299
300         @Override
301         public void doShutdown () throws SQLException, IOException {
302                 // Trace message
303                 this.getLogger().logTrace("CALLED!"); //NOI18N
304
305                 // Shut down the database layer
306                 ((AddressbookContactFrontend) this.getFrontend()).doShutdown();
307
308                 // Trace message
309                 this.getLogger().logTrace("EXIT!"); //NOI18N
310         }
311
312         @Override
313         public String enterOwnCellNumber () {
314                 // Trace message
315                 this.getLogger().logTrace("CALLED!"); //NOI18N
316
317                 // Get and cast client instance
318                 AddressbookClient client = (AddressbookClient) this.getClient();
319
320                 return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
321         }
322
323         @Override
324         public String enterOwnCity () {
325                 // Trace message
326                 this.getLogger().logTrace("CALLED!"); //NOI18N
327
328                 // Get and cast client instance
329                 AddressbookClient client = (AddressbookClient) this.getClient();
330
331                 return client.enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
332         }
333
334         @Override
335         public String enterOwnComment () {
336                 // Trace message
337                 this.getLogger().logTrace("CALLED!"); //NOI18N
338
339                 // Get and cast client instance
340                 AddressbookClient client = (AddressbookClient) this.getClient();
341
342                 return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
343         }
344
345         @Override
346         public String enterOwnCompanyName () {
347                 // Trace message
348                 this.getLogger().logTrace("CALLED!"); //NOI18N
349
350                 // Get and cast client instance
351                 AddressbookClient client = (AddressbookClient) this.getClient();
352
353                 return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
354         }
355
356         @Override
357         public String enterOwnCountryCode () {
358                 // Trace message
359                 this.getLogger().logTrace("CALLED!"); //NOI18N
360
361                 // Get and cast client instance
362                 AddressbookClient client = (AddressbookClient) this.getClient();
363
364                 return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
365         }
366
367         @Override
368         public String enterOwnEmailAddress () {
369                 // Trace message
370                 this.getLogger().logTrace("CALLED!"); //NOI18N
371
372                 // Get and cast client instance
373                 AddressbookClient client = (AddressbookClient) this.getClient();
374
375                 return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
376         }
377
378         @Override
379         public String enterOwnFamilyName () {
380                 // Trace message
381                 this.getLogger().logTrace("CALLED!"); //NOI18N
382
383                 // Get and cast client instance
384                 AddressbookClient client = (AddressbookClient) this.getClient();
385
386                 return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
387         }
388
389         @Override
390         public String enterOwnFaxNumber () {
391                 // Trace message
392                 this.getLogger().logTrace("CALLED!"); //NOI18N
393
394                 // Get and cast client instance
395                 AddressbookClient client = (AddressbookClient) this.getClient();
396
397                 return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
398         }
399
400         @Override
401         public String enterOwnFirstName () {
402                 // Trace message
403                 this.getLogger().logTrace("CALLED!"); //NOI18N
404
405                 // Get and cast client instance
406                 AddressbookClient client = (AddressbookClient) this.getClient();
407
408                 return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
409         }
410
411         @Override
412         public Gender enterOwnGender () {
413                 // Trace message
414                 this.getLogger().logTrace("CALLED!"); //NOI18N
415
416                 // Get and cast client instance
417                 AddressbookClient client = (AddressbookClient) this.getClient();
418
419                 return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
420         }
421
422         @Override
423         public String enterOwnPhoneNumber () {
424                 // Trace message
425                 this.getLogger().logTrace("CALLED!"); //NOI18N
426
427                 // Get and cast client instance
428                 AddressbookClient client = (AddressbookClient) this.getClient();
429
430                 return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
431         }
432
433         @Override
434         public String enterOwnStreet () {
435                 // Trace message
436                 this.getLogger().logTrace("CALLED!"); //NOI18N
437
438                 // Get and cast client instance
439                 AddressbookClient client = (AddressbookClient) this.getClient();
440
441                 return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
442         }
443
444         @Override
445         public int enterOwnZipCode () {
446                 // Trace message
447                 this.getLogger().logTrace("CALLED!"); //NOI18N
448
449                 // Get and cast client instance
450                 AddressbookClient client = (AddressbookClient) this.getClient();
451
452                 return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
453         }
454
455         @Override
456         public final int getColumnCount () {
457                 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
458
459                 return this.columnNames.size();
460         }
461
462         @Override
463         public String getColumnName (final int columnIndex) {
464                 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
465
466                 // Get column name at index
467                 return this.columnNames.get(columnIndex);
468         }
469
470         @Override
471         public String getTranslatedColumnName (final int columnIndex) {
472                 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
473
474                 // Get column name at index
475                 return this.translatedColumnNames.get(columnIndex);
476         }
477
478         @Override
479         public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
480                 // Trace message
481                 this.getLogger().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
482
483                 // Then get specific row from database which is a Contact instance
484                 Storable storable = this.getFrontend().getStorableAtRow(rowIndex);
485
486                 // Debug message
487                 this.getLogger().logDebug(MessageFormat.format("storable={0}", storable)); //NOI18N
488
489                 // It may return null
490                 if (null == storable) {
491                         // Nothing found
492                         this.getLogger().logWarning("contact is null - returning null ..."); //NOI18N
493                         return null;
494                 }
495
496                 // Convert column index -> name
497                 String columnName = this.getColumnName(columnIndex);
498
499                 // Debug message
500                 this.getLogger().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
501
502                 // Now get that column
503                 Object value = null;
504                 try {
505                         value = storable.getValueFromColumn(columnName);
506                 } catch (final IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
507                         this.abortProgramWithException(ex);
508                 }
509
510                 // Trace message
511                 this.getLogger().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
512
513                 // Return it
514                 return value;
515         }
516
517         @Override
518         public boolean isOwnContactAdded () throws IOException {
519                 // Trace message
520                 this.getLogger().logTrace("CALLED!"); //NOI18N
521
522                 // Init variable
523                 boolean isAdded = false;
524
525                 try {
526                         // Deligate this call to frontend
527                         isAdded = ((AddressbookContactFrontend) this.getFrontend()).isOwnContactFound();
528                 } catch (final SQLException | IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
529                         // Something bad happened
530                         this.abortProgramWithException(ex);
531                 }
532
533                 // Trace message
534                 this.getLogger().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
535
536                 // Return result
537                 return isAdded;
538         }
539
540         @Override
541         public void registerContact (final Contact contact) {
542                 // Trace message
543                 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
544
545                 // Sanity check
546                 if (null == contact) {
547                         // Abort here
548                         throw new NullPointerException("contact is null"); //NOI18N
549                 }
550                 try {
551                         // Check if contact is found
552                         if (((AddressbookContactFrontend) this.getFrontend()).isContactFound(contact)) {
553                                 // Contact already added
554                                 // TODO Do something here
555                         } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
556                                 // Own contact already added
557                                 // TODO Do something
558                         }
559
560                         // Add contact to internal list
561                         this.addContact(contact);
562                 } catch (final ContactAlreadyAddedException | SQLException | IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
563                         // Abort here
564                         this.abortProgramWithException(ex);
565                 }
566
567                 // Trace message
568                 this.getLogger().logTrace("EXIT!"); //NOI18N
569         }
570
571         /**
572          * Logs given exception and exits program
573          *
574          * @param throwable Throwable
575          */
576         private void abortProgramWithException (Throwable throwable) {
577                 // Log exception
578                 this.logException(throwable);
579
580                 // Abort here
581                 System.exit(1);
582         }
583
584         /**
585          * Fills the column names array with strings from bundle
586          */
587         private void fillColumnNamesFromBundle () {
588                 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
589                 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
590
591                 // Debug message
592                 this.getLogger().logTrace("CALLED!"); //NOI18N
593
594                 // First get an iterator from key set to iterate over
595                 Iterator<String> iterator = this.getBundle().keySet().iterator();
596
597                 // Then iterate over all
598                 while (iterator.hasNext()) {
599                         // Get next element
600                         String key = iterator.next().toLowerCase();
601
602                         // Does the key start with AddressbookContactManager.columnName ?
603                         if (key.startsWith("ContactManager.columnName")) { //NOI18N
604                                 // This is the wanted entry.
605                                 this.getLogger().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
606
607                                 // Convert string to array based on delimiter '.'
608                                 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
609
610                                 // Token array must contain 4 elements (AddressbookContactManager.columnName.foo.text)
611                                 assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
612
613                                 // Get pre-last element
614                                 String columnName = tokens[tokens.length - 2];
615
616                                 // Debug message
617                                 this.getLogger().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
618
619                                 // So add it
620                                 this.columnNames.add(columnName);
621                                 this.translatedColumnNames.add(this.getBundle().getString(key));
622                         }
623                 }
624
625                 // Debug message
626                 this.getLogger().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
627         }
628
629         /**
630          * Getter for logger instance
631          *
632          * @return Logger instance
633          */
634         private LoggerBeanLocal getLogger () {
635                 return this.logger;
636         }
637
638         /**
639          * "Getter" for own contact instance or null if not found
640          * <p>
641          * @return Contact instance or null
642          * @throws java.sql.SQLException If an SQL error occurs
643          * @throws java.io.IOException If an IO error occurs
644          * @throws java.lang.NoSuchMethodException If a method cannot be found
645          * @throws java.lang.IllegalAccessException If a method is not accessible
646          * @throws java.lang.reflect.InvocationTargetException Any other problems?
647          */
648         private Contact getOwnContact () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
649                 // Trace message
650                 this.getLogger().logTrace("CALLED!"); //NOI18N
651
652                 // Deligate this call to database frontend
653                 Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
654
655                 // Trace message
656                 this.getLogger().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
657
658                 // Return instance or null
659                 return contact;
660         }
661
662         /**
663          * Logs given exception
664          * <p>
665          * @param exception Throwable
666          */
667         protected void logException (final Throwable exception) {
668                 this.getLogger().logException(exception);
669         }
670 }