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