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