2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.facade.contact;
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;
43 * A facade for contacts.
45 * @author Roland Haeder<roland@mxchange.org>
48 public class AddressbookContactFacade extends BaseFacade implements ContactFacade {
53 private final List<String> columnNames;
59 private EntityManager entityManager;
65 private LoggerBeanLocal loggerBeanLocal;
68 * Translated column name list
70 private final List<String> translatedColumnNames;
73 * Constructor which accepts maxContacts for maximum (initial) contacts and
76 * @param client Client instance to use
78 * @throws java.sql.SQLException If an SQL error occurs
80 public AddressbookContactFacade (final Client client) throws SQLException {
82 this.getLoggerBeanLocal().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
84 // Make sure all parameters are set correctly
87 throw new NullPointerException("client is null"); //NOI18N
90 // Set client instance
91 this.setClient(client);
94 this.columnNames = new ArrayList<>(15);
95 this.translatedColumnNames = new ArrayList<>(15);
98 this.fillColumnNamesFromBundle();
101 //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
105 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
107 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
109 // Contact instance must not be null
110 if (null == contact) {
112 throw new NullPointerException("contact is null"); //NOI18N
116 this.entityManager.persist(contact);
119 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
123 public void doAddOtherAddress () {
124 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
128 public void doChangeAddressData (final Contact contact) {
130 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
132 // Contact must not be null
133 if (null == contact) {
135 throw new NullPointerException("contact is null"); //NOI18N
137 if (!(this.getClient() instanceof AddressbookClient)) {
139 throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
142 // Get and cast client instance
143 AddressbookClient client = (AddressbookClient) this.getClient();
145 // First display it again
146 client.displayAddressBox(contact);
149 if (contact.isOwnContact()) {
150 // Deligate to client
151 client.doChangeOwnAddressData(contact);
153 // Other contact's address data to change
154 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
158 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
162 public void doChangeNameData (final Contact contact) {
164 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
166 // Contact must not be null
167 if (null == contact) {
169 throw new NullPointerException("contact is null"); //NOI18N
172 // Get and cast client instance
173 AddressbookClient client = (AddressbookClient) this.getClient();
175 // First display them again
176 client.displayNameBox(contact);
179 if (contact.isOwnContact()) {
181 client.doChangeOwnNameData(contact);
183 // Then re-ask them ...
184 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
188 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
192 public void doChangeOtherAddress () {
193 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
197 public void doChangeOtherData (final Contact contact) {
199 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
201 // Contact must not be null
202 if (null == contact) {
204 throw new NullPointerException("contact is null"); //NOI18N
207 // Get and cast client instance
208 AddressbookClient client = (AddressbookClient) this.getClient();
210 // First display them again
211 client.displayOtherDataBox(contact);
214 if (contact.isOwnContact()) {
216 client.doChangeOwnOtherData(contact);
218 // Then re-ask them ...
219 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
223 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
227 public void doChangeOwnData () {
229 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
232 * First check if the user has registered own contact, before that
233 * nothing can be changed.
235 if (!this.isOwnContactAdded()) {
237 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
239 // Skip any below code
244 Contact contact = this.getOwnContact();
247 assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
249 // Get and cast client instance
250 AddressbookClient client = (AddressbookClient) this.getClient();
253 client.show(contact);
256 // Ask user what to change
257 client.userChooseChangeContactData(contact);
258 } catch (final UnhandledUserChoiceException ex) {
259 this.getLoggerBeanLocal().logException(ex);
263 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
267 public void doDeleteOtherAddress () {
268 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
272 public void doEnterOwnData () throws ContactAlreadyAddedException {
274 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
276 // Is own contact already added?
277 if (this.isOwnContactAdded()) {
278 // Don't continue here
279 throw new ContactAlreadyAddedException();
282 // Get and cast client instance
283 AddressbookClient client = (AddressbookClient) this.getClient();
285 // Deligate this call to the client
286 Contact contact = client.doEnterOwnData();
289 if (contact instanceof Contact) {
290 // Add it to contact "book"
291 this.registerContact(contact);
295 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
299 public void doListContacts () {
300 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
304 public void doSearchContacts () {
305 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
309 public void doShutdown () throws SQLException, IOException {
311 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
313 // Shut down the database layer
314 this.entityManager.close();
317 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
321 public DialableCellphoneNumber enterOwnCellNumber () {
323 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
325 // Get and cast client instance
326 AddressbookClient client = (AddressbookClient) this.getClient();
328 return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
332 public String enterOwnCity () {
334 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
336 // Get and cast client instance
337 AddressbookClient client = (AddressbookClient) this.getClient();
339 return client.enterString(3, 50, "Bitte geben Sie Ihren Wohnort ein: ", false);
343 public String enterOwnComment () {
345 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
347 // Get and cast client instance
348 AddressbookClient client = (AddressbookClient) this.getClient();
350 return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
354 public String enterOwnCompanyName () {
356 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
358 // Get and cast client instance
359 AddressbookClient client = (AddressbookClient) this.getClient();
361 return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
365 public Country enterOwnCountryCode () {
367 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
369 // Get and cast client instance
370 AddressbookClient client = (AddressbookClient) this.getClient();
372 return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
376 public String enterOwnEmailAddress () {
378 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
380 // Get and cast client instance
381 AddressbookClient client = (AddressbookClient) this.getClient();
383 return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
387 public String enterOwnFamilyName () {
389 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
391 // Get and cast client instance
392 AddressbookClient client = (AddressbookClient) this.getClient();
394 return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
398 public DialableFaxNumber enterOwnFaxNumber () {
400 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
402 // Get and cast client instance
403 AddressbookClient client = (AddressbookClient) this.getClient();
405 return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
409 public String enterOwnFirstName () {
411 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
413 // Get and cast client instance
414 AddressbookClient client = (AddressbookClient) this.getClient();
416 return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
420 public Gender enterOwnGender () {
422 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
424 // Get and cast client instance
425 AddressbookClient client = (AddressbookClient) this.getClient();
427 return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
431 public DialableLandLineNumber enterOwnPhoneNumber () {
433 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
435 // Get and cast client instance
436 AddressbookClient client = (AddressbookClient) this.getClient();
438 return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
442 public String enterOwnStreet () {
444 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
446 // Get and cast client instance
447 AddressbookClient client = (AddressbookClient) this.getClient();
449 return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
453 public int enterOwnZipCode () {
455 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
457 // Get and cast client instance
458 AddressbookClient client = (AddressbookClient) this.getClient();
460 return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
464 public int getColumnCount () {
465 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
467 return this.columnNames.size();
471 public String getColumnName (final int columnIndex) {
472 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
474 // Get column name at index
475 return this.columnNames.get(columnIndex);
479 public String getTranslatedColumnName (final int columnIndex) {
480 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
482 // Get column name at index
483 return this.translatedColumnNames.get(columnIndex);
487 public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) {
489 this.getLoggerBeanLocal().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
491 // Convert column index -> name
492 String columnName = this.getColumnName(columnIndex);
495 this.getLoggerBeanLocal().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
501 this.getLoggerBeanLocal().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
508 public boolean isOwnContactAdded () {
510 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
513 boolean isAdded = false;
516 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
523 public void registerContact (final Contact contact) {
525 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
528 if (null == contact) {
530 throw new NullPointerException("contact is null"); //NOI18N
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
542 // Add contact to internal list
543 this.addContact(contact);
544 } catch (final ContactAlreadyAddedException ex) {
546 this.abortProgramWithException(ex);
550 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
554 * Logs given exception and exits program
556 * @param throwable Throwable
558 private void abortProgramWithException (Throwable throwable) {
560 this.logException(throwable);
567 * Fills the column names array with strings from bundle
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
574 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
576 // First get an iterator from key set to iterate over
577 Iterator<String> iterator = this.getBundle().keySet().iterator();
579 // Then iterate over all
580 while (iterator.hasNext()) {
582 String key = iterator.next().toLowerCase();
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
589 // Convert string to array based on delimiter '.'
590 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
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
595 // Get pre-last element
596 String columnName = tokens[tokens.length - 2];
599 this.getLoggerBeanLocal().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
602 this.columnNames.add(columnName);
603 this.translatedColumnNames.add(this.getBundle().getString(key));
608 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
612 * Getter for loggerBeanLocal instance
614 * @return Logger instance
616 private LoggerBeanLocal getLoggerBeanLocal () {
617 return this.loggerBeanLocal;
621 * "Getter" for own contact instance or null if not found
623 * @return Contact instance or null
625 private Contact getOwnContact () {
627 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
629 // Deligate this call to database frontend
630 Contact contact = null;
631 //Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
634 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
636 // Return instance or null
641 * Logs given exception
643 * @param exception Throwable
645 protected void logException (final Throwable exception) {
646 this.getLoggerBeanLocal().logException(exception);