2 * Copyright (C) 2016 - 2022 Free Software Foundation
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 org.mxchange.addressbook.client.AddressbookClient;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
29 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicDataAlreadyAddedException;
30 import org.mxchange.jcore.client.Client;
31 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
32 import org.mxchange.jcore.facade.BaseFacade;
33 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
34 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
35 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
36 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
37 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
40 * A facade for contacts.
42 * @author Roland Häder<roland@mxchange.org>
45 public class AddressbookContactFacade extends BaseFacade implements ContactFacade {
50 private final List<String> columnNames;
56 private LoggerBeanLocal loggerBeanLocal;
59 * Translated column name list
61 private final List<String> translatedColumnNames;
64 * Constructor which accepts maxContacts for maximum (initial) contacts and
67 * @param client Client instance to use
69 * @throws java.sql.SQLException If an SQL error occurs
71 public AddressbookContactFacade (final Client client) throws SQLException {
73 this.getLoggerBeanLocal().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
75 // Make sure all parameters are set correctly
78 throw new NullPointerException("client is null"); //NOI18N
81 // Set client instance
82 this.setClient(client);
85 this.columnNames = new ArrayList<>(15);
86 this.translatedColumnNames = new ArrayList<>(15);
89 this.fillColumnNamesFromBundle();
92 //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
96 public void addContact (final Contact contact) throws BasicDataAlreadyAddedException {
98 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
100 // Contact instance must not be null
101 if (null == contact) {
103 throw new NullPointerException("contact is null"); //NOI18N
107 this.entityManager.persist(contact);
110 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
114 public void doAddOtherAddress () {
115 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
119 public void doChangeAddressData (final Contact contact) {
121 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
123 // Contact must not be null
124 if (null == contact) {
126 throw new NullPointerException("contact is null"); //NOI18N
128 if (!(this.getClient() instanceof AddressbookClient)) {
130 throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
133 // Get and cast client instance
134 AddressbookClient client = (AddressbookClient) this.getClient();
136 // First display it again
137 client.displayAddressBox(contact);
140 if (contact.isOwnContact()) {
141 // Deligate to client
142 client.doChangeOwnAddressData(contact);
144 // Other contact's address data to change
145 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
149 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
153 public void doChangeNameData (final Contact contact) {
155 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
157 // Contact must not be null
158 if (null == contact) {
160 throw new NullPointerException("contact is null"); //NOI18N
163 // Get and cast client instance
164 AddressbookClient client = (AddressbookClient) this.getClient();
166 // First display them again
167 client.displayNameBox(contact);
170 if (contact.isOwnContact()) {
172 client.doChangeOwnNameData(contact);
174 // Then re-ask them ...
175 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
179 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
183 public void doChangeOtherAddress () {
184 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
188 public void doChangeOtherData (final Contact contact) {
190 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
192 // Contact must not be null
193 if (null == contact) {
195 throw new NullPointerException("contact is null"); //NOI18N
198 // Get and cast client instance
199 AddressbookClient client = (AddressbookClient) this.getClient();
201 // First display them again
202 client.displayOtherDataBox(contact);
205 if (contact.isOwnContact()) {
207 client.doChangeOwnOtherData(contact);
209 // Then re-ask them ...
210 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
214 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
218 public void doChangeOwnData () {
220 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
223 * First check if the user has registered own contact, before that
224 * nothing can be changed.
226 if (!this.isOwnContactAdded()) {
228 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
230 // Skip any below code
235 Contact contact = this.getOwnContact();
238 assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
240 // Get and cast client instance
241 AddressbookClient client = (AddressbookClient) this.getClient();
244 client.show(contact);
247 // Ask user what to change
248 client.userChooseChangeContactData(contact);
249 } catch (final UnhandledUserChoiceException ex) {
250 this.getLoggerBeanLocal().logException(ex);
254 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
258 public void doDeleteOtherAddress () {
259 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
263 public void doEnterOwnData () throws BasicDataAlreadyAddedException {
265 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
267 // Is own contact already added?
268 if (this.isOwnContactAdded()) {
269 // Don't continue here
270 throw new BasicDataAlreadyAddedException();
273 // Get and cast client instance
274 AddressbookClient client = (AddressbookClient) this.getClient();
276 // Deligate this call to the client
277 Contact contact = client.doEnterOwnData();
280 if (contact instanceof Contact) {
281 // Add it to contact "book"
282 this.registerContact(contact);
286 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
290 public void doListContacts () {
291 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
295 public void doSearchContacts () {
296 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
300 public void doShutdown () throws SQLException, IOException {
302 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
304 // Shut down the database layer
305 this.entityManager.close();
308 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
312 public DialableMobileNumber enterOwnCellNumber () {
314 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
316 // Get and cast client instance
317 AddressbookClient client = (AddressbookClient) this.getClient();
319 return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
323 public String enterOwnCity () {
325 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
327 // Get and cast client instance
328 AddressbookClient client = (AddressbookClient) this.getClient();
330 return client.enterString(3, 50, "Bitte geben Sie Ihren Wohnort ein: ", false);
334 public String enterOwnComment () {
336 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
338 // Get and cast client instance
339 AddressbookClient client = (AddressbookClient) this.getClient();
341 return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
345 public String enterOwnCompanyName () {
347 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
349 // Get and cast client instance
350 AddressbookClient client = (AddressbookClient) this.getClient();
352 return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
356 public Country enterOwnCountryCode () {
358 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
360 // Get and cast client instance
361 AddressbookClient client = (AddressbookClient) this.getClient();
363 return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
367 public String enterOwnEmailAddress () {
369 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
371 // Get and cast client instance
372 AddressbookClient client = (AddressbookClient) this.getClient();
374 return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
378 public String enterOwnFamilyName () {
380 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
382 // Get and cast client instance
383 AddressbookClient client = (AddressbookClient) this.getClient();
385 return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
389 public DialableFaxNumber enterOwnFaxNumber () {
391 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
393 // Get and cast client instance
394 AddressbookClient client = (AddressbookClient) this.getClient();
396 return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
400 public String enterOwnFirstName () {
402 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
404 // Get and cast client instance
405 AddressbookClient client = (AddressbookClient) this.getClient();
407 return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
411 public PersonalTitle enterOwnPersonalTitle () {
413 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
415 // Get and cast client instance
416 AddressbookClient client = (AddressbookClient) this.getClient();
418 return client.enterPersonalTitle("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
422 public DialableLandLineNumber enterOwnPhoneNumber () {
424 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
426 // Get and cast client instance
427 AddressbookClient client = (AddressbookClient) this.getClient();
429 return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
433 public String enterOwnStreet () {
435 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
437 // Get and cast client instance
438 AddressbookClient client = (AddressbookClient) this.getClient();
440 return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
444 public int enterOwnZipCode () {
446 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
448 // Get and cast client instance
449 AddressbookClient client = (AddressbookClient) this.getClient();
451 return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
455 public int getColumnCount () {
456 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
458 return this.columnNames.size();
462 public String getColumnName (final int columnIndex) {
463 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
465 // Get column name at index
466 return this.columnNames.get(columnIndex);
470 public String getTranslatedColumnName (final int columnIndex) {
471 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
473 // Get column name at index
474 return this.translatedColumnNames.get(columnIndex);
478 public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) {
480 this.getLoggerBeanLocal().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
482 // Convert column index -> name
483 String columnName = this.getColumnName(columnIndex);
486 this.getLoggerBeanLocal().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
492 this.getLoggerBeanLocal().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
499 public boolean isOwnContactAdded () {
501 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
504 boolean isAdded = false;
507 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
514 public void registerContact (final Contact contact) {
516 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
519 if (null == contact) {
521 throw new NullPointerException("contact is null"); //NOI18N
524 // Check if contact is found
525 if (this.entityManager.contains(contact)) {
526 // Contact already added
527 // TODO Do something here
528 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
529 // Own contact already added
533 // Add contact to internal list
534 this.addContact(contact);
535 } catch (final BasicDataAlreadyAddedException ex) {
537 this.abortProgramWithException(ex);
541 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
545 * Logs given exception and exits program
547 * @param throwable Throwable
549 private void abortProgramWithException (Throwable throwable) {
551 this.logException(throwable);
558 * Fills the column names array with strings from bundle
560 private void fillColumnNamesFromBundle () {
561 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
562 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
565 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
567 // First get an iterator from key set to iterate over
568 Iterator<String> iterator = this.getBundle().keySet().iterator();
570 // Then iterate over all
571 while (iterator.hasNext()) {
573 String key = iterator.next().toLowerCase();
575 // Does the key start with AddressbookContactFacade.columnName ?
576 if (key.startsWith("ContactManager.columnName")) { //NOI18N
577 // This is the wanted entry.
578 this.getLoggerBeanLocal().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
580 // Convert string to array based on delimiter '.'
581 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
583 // Token array must contain 4 elements (AddressbookContactFacade.columnName.foo.text)
584 assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
586 // Get pre-last element
587 String columnName = tokens[tokens.length - 2];
590 this.getLoggerBeanLocal().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
593 this.columnNames.add(columnName);
594 this.translatedColumnNames.add(this.getBundle().getString(key));
599 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
603 * Getter for loggerBeanLocal instance
605 * @return Logger instance
607 private LoggerBeanLocal getLoggerBeanLocal () {
608 return this.loggerBeanLocal;
612 * "Getter" for own contact instance or null if not found
614 * @return Contact instance or null
616 private Contact getOwnContact () {
618 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
620 // Deligate this call to database frontend
621 Contact contact = null;
622 //Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
625 this.getLoggerBeanLocal().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
627 // Return instance or null
632 * Logs given exception
634 * @param exception Throwable
636 protected void logException (final Throwable exception) {
637 this.getLoggerBeanLocal().logException(exception);