]> git.mxchange.org Git - jfinancials-swing.git/blob - src/org/mxchange/addressbook/facade/contact/AddressbookContactFacade.java
The persistence will happen in the EJBs. So all these calls needs to be rewritten...
[jfinancials-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 org.mxchange.addressbook.client.AddressbookClient;
27 import org.mxchange.jcontacts.contact.Contact;
28 import org.mxchange.jcontacts.contact.gender.Gender;
29 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
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.jcountry.data.Country;
36 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
37 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
38 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
39
40 /**
41  * A facade for contacts.
42  * <p>
43  * @author Roland Haeder<roland@mxchange.org>
44  * @version 0.0
45  */
46 public class AddressbookContactFacade extends BaseFacade implements ContactFacade {
47
48         /**
49          * Column name list
50          */
51         private final List<String> columnNames;
52
53         /**
54          * Logger instance
55          */
56         @Log
57         private LoggerBeanLocal loggerBeanLocal;
58
59         /**
60          * Translated column name list
61          */
62         private final List<String> translatedColumnNames;
63
64         /**
65          * Constructor which accepts maxContacts for maximum (initial) contacts and
66          * a client instance.
67          * <p>
68          * @param client Client instance to use
69          * <p>
70          * @throws java.sql.SQLException If an SQL error occurs
71          */
72         public AddressbookContactFacade (final Client client) throws SQLException {
73                 // Trace message
74                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
75
76                 // Make sure all parameters are set correctly
77                 if (null == client) {
78                         // Abort here
79                         throw new NullPointerException("client is null"); //NOI18N
80                 }
81
82                 // Set client instance
83                 this.setClient(client);
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.getLoggerBeanLocal().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                 this.entityManager.persist(contact);
109
110                 // Trace message
111                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
151         }
152
153         @Override
154         public void doChangeNameData (final Contact contact) {
155                 // Trace message
156                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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.getLoggerBeanLocal().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.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
216         }
217
218         @Override
219         public void doChangeOwnData () {
220                 // Trace message
221                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().logException(ex);
252                 }
253
254                 // Trace message
255                 this.getLoggerBeanLocal().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 {
265                 // Trace message
266                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
304
305                 // Shut down the database layer
306                 this.entityManager.close();
307
308                 // Trace message
309                 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
310         }
311
312         @Override
313         public DialableCellphoneNumber enterOwnCellNumber () {
314                 // Trace message
315                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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 Ihren Wohnort ein: ", false);
332         }
333
334         @Override
335         public String enterOwnComment () {
336                 // Trace message
337                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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 Country enterOwnCountryCode () {
358                 // Trace message
359                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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.getLoggerBeanLocal().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 DialableFaxNumber enterOwnFaxNumber () {
391                 // Trace message
392                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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.getLoggerBeanLocal().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 DialableLandLineNumber enterOwnPhoneNumber () {
424                 // Trace message
425                 this.getLoggerBeanLocal().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.getLoggerBeanLocal().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.getLoggerBeanLocal().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 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) {
480                 // Trace message
481                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
482
483                 // Convert column index -> name
484                 String columnName = this.getColumnName(columnIndex);
485
486                 // Debug message
487                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
488
489                 // Init value
490                 Object value = null;
491
492                 // Trace message
493                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
494
495                 // Return it
496                 return value;
497         }
498
499         @Override
500         public boolean isOwnContactAdded () {
501                 // Trace message
502                 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
503
504                 // Init variable
505                 boolean isAdded = false;
506
507                 // Trace message
508                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
509
510                 // Return result
511                 return isAdded;
512         }
513
514         @Override
515         public void registerContact (final Contact contact) {
516                 // Trace message
517                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
518
519                 // Sanity check
520                 if (null == contact) {
521                         // Abort here
522                         throw new NullPointerException("contact is null"); //NOI18N
523                 }
524                 try {
525                         // Check if contact is found
526                         if (this.entityManager.contains(contact)) {
527                                 // Contact already added
528                                 // TODO Do something here
529                         } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
530                                 // Own contact already added
531                                 // TODO Do something
532                         }
533
534                         // Add contact to internal list
535                         this.addContact(contact);
536                 } catch (final ContactAlreadyAddedException ex) {
537                         // Abort here
538                         this.abortProgramWithException(ex);
539                 }
540
541                 // Trace message
542                 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
543         }
544
545         /**
546          * Logs given exception and exits program
547          * <p>
548          * @param throwable Throwable
549          */
550         private void abortProgramWithException (Throwable throwable) {
551                 // Log exception
552                 this.logException(throwable);
553
554                 // Abort here
555                 System.exit(1);
556         }
557
558         /**
559          * Fills the column names array with strings from bundle
560          */
561         private void fillColumnNamesFromBundle () {
562                 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
563                 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
564
565                 // Debug message
566                 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
567
568                 // First get an iterator from key set to iterate over
569                 Iterator<String> iterator = this.getBundle().keySet().iterator();
570
571                 // Then iterate over all
572                 while (iterator.hasNext()) {
573                         // Get next element
574                         String key = iterator.next().toLowerCase();
575
576                         // Does the key start with AddressbookContactFacade.columnName ?
577                         if (key.startsWith("ContactManager.columnName")) { //NOI18N
578                                 // This is the wanted entry.
579                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
580
581                                 // Convert string to array based on delimiter '.'
582                                 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
583
584                                 // Token array must contain 4 elements (AddressbookContactFacade.columnName.foo.text)
585                                 assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
586
587                                 // Get pre-last element
588                                 String columnName = tokens[tokens.length - 2];
589
590                                 // Debug message
591                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
592
593                                 // So add it
594                                 this.columnNames.add(columnName);
595                                 this.translatedColumnNames.add(this.getBundle().getString(key));
596                         }
597                 }
598
599                 // Debug message
600                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
601         }
602
603         /**
604          * Getter for loggerBeanLocal instance
605          * <p>
606          * @return Logger instance
607          */
608         private LoggerBeanLocal getLoggerBeanLocal () {
609                 return this.loggerBeanLocal;
610         }
611
612         /**
613          * "Getter" for own contact instance or null if not found
614          * <p>
615          * @return Contact instance or null
616          */
617         private Contact getOwnContact () {
618                 // Trace message
619                 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
620
621                 // Deligate this call to database frontend
622                 Contact contact = null;
623                 //Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
624
625                 // Trace message
626                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
627
628                 // Return instance or null
629                 return contact;
630         }
631
632         /**
633          * Logs given exception
634          * <p>
635          * @param exception Throwable
636          */
637         protected void logException (final Throwable exception) {
638                 this.getLoggerBeanLocal().logException(exception);
639         }
640 }