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