2 * Copyright (C) 2015 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.lang.reflect.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import java.util.List;
27 import org.mxchange.addressbook.client.AddressbookClient;
28 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jcontacts.contact.gender.Gender;
31 import org.mxchange.jcore.client.Client;
32 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
33 import org.mxchange.jcore.manager.BaseManager;
34 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
35 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
38 * A facade for contacts.
40 * @author Roland Haeder
43 public class AddressbookContactFacade extends BaseManager implements ContactFacade {
48 private final List<String> columnNames;
54 private LoggerBeanLocal logger;
57 * Translated column name list
59 private final List<String> translatedColumnNames;
62 * Constructor which accepts maxContacts for maximum (initial) contacts and
65 * @param client Client instance to use
66 * @throws java.sql.SQLException If an SQL error occurs
68 public AddressbookContactFacade (final Client client) throws SQLException {
70 this.getLogger().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
72 // Make sure all parameters are set correctly
75 throw new NullPointerException("client is null"); //NOI18N
78 // Set client instance
79 this.setClient(client);
81 // Init database connection
82 DatabaseFrontend frontend = new AddressbookContactDatabaseFrontend(this);
83 this.setFrontend(frontend);
86 this.columnNames = new ArrayList<>(15);
87 this.translatedColumnNames = new ArrayList<>(15);
90 this.fillColumnNamesFromBundle();
93 //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
97 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
99 this.getLogger().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
101 // Contact instance must not be null
102 if (null == contact) {
104 throw new NullPointerException("contact is null"); //NOI18N
108 ((AddressbookContactFrontend) this.getFrontend()).addContact(contact);
111 this.getLogger().logTrace("EXIT!"); //NOI18N
115 public void doAddOtherAddress () {
116 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
120 public void doChangeAddressData (final Contact contact) {
122 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
124 // Contact must not be null
125 if (null == contact) {
127 throw new NullPointerException("contact is null"); //NOI18N
129 if (!(this.getClient() instanceof AddressbookClient)) {
131 throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
134 // Get and cast client instance
135 AddressbookClient client = (AddressbookClient) this.getClient();
137 // First display it again
138 client.displayAddressBox(contact);
141 if (contact.isOwnContact()) {
142 // Deligate to client
143 client.doChangeOwnAddressData(contact);
145 // Other contact's address data to change
146 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
150 this.getLogger().logTrace("EXIT!"); //NOI18N
154 public void doChangeNameData (final Contact contact) {
156 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
158 // Contact must not be null
159 if (null == contact) {
161 throw new NullPointerException("contact is null"); //NOI18N
164 // Get and cast client instance
165 AddressbookClient client = (AddressbookClient) this.getClient();
167 // First display them again
168 client.displayNameBox(contact);
171 if (contact.isOwnContact()) {
173 client.doChangeOwnNameData(contact);
175 // Then re-ask them ...
176 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
180 this.getLogger().logTrace("EXIT!"); //NOI18N
184 public void doChangeOtherAddress () {
185 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
189 public void doChangeOtherData (final Contact contact) {
191 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
193 // Contact must not be null
194 if (null == contact) {
196 throw new NullPointerException("contact is null"); //NOI18N
199 // Get and cast client instance
200 AddressbookClient client = (AddressbookClient) this.getClient();
202 // First display them again
203 client.displayOtherDataBox(contact);
206 if (contact.isOwnContact()) {
208 client.doChangeOwnOtherData(contact);
210 // Then re-ask them ...
211 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
215 this.getLogger().logTrace("EXIT!"); //NOI18N
219 public void doChangeOwnData () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
221 this.getLogger().logTrace("CALLED!"); //NOI18N
224 * First check if the user has registered own contact, before that
225 * nothing can be changed.
227 if (!this.isOwnContactAdded()) {
229 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
231 // Skip any below code
236 Contact contact = this.getOwnContact();
239 assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
241 // Get and cast client instance
242 AddressbookClient client = (AddressbookClient) this.getClient();
245 client.show(contact);
248 // Ask user what to change
249 client.userChooseChangeContactData(contact);
250 } catch (final UnhandledUserChoiceException ex) {
251 this.getLogger().logException(ex);
255 this.getLogger().logTrace("EXIT!"); //NOI18N
259 public void doDeleteOtherAddress () {
260 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
264 public void doEnterOwnData () throws ContactAlreadyAddedException, IOException {
266 this.getLogger().logTrace("CALLED!"); //NOI18N
268 // Is own contact already added?
269 if (this.isOwnContactAdded()) {
270 // Don't continue here
271 throw new ContactAlreadyAddedException();
274 // Get and cast client instance
275 AddressbookClient client = (AddressbookClient) this.getClient();
277 // Deligate this call to the client
278 Contact contact = client.doEnterOwnData();
281 if (contact instanceof Contact) {
282 // Add it to contact "book"
283 this.registerContact(contact);
287 this.getLogger().logTrace("EXIT!"); //NOI18N
291 public void doListContacts () {
292 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
296 public void doSearchContacts () {
297 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
301 public void doShutdown () throws SQLException, IOException {
303 this.getLogger().logTrace("CALLED!"); //NOI18N
305 // Shut down the database layer
306 ((AddressbookContactFrontend) this.getFrontend()).doShutdown();
309 this.getLogger().logTrace("EXIT!"); //NOI18N
313 public String enterOwnCellNumber () {
315 this.getLogger().logTrace("CALLED!"); //NOI18N
317 // Get and cast client instance
318 AddressbookClient client = (AddressbookClient) this.getClient();
320 return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
324 public String enterOwnCity () {
326 this.getLogger().logTrace("CALLED!"); //NOI18N
328 // Get and cast client instance
329 AddressbookClient client = (AddressbookClient) this.getClient();
331 return client.enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
335 public String enterOwnComment () {
337 this.getLogger().logTrace("CALLED!"); //NOI18N
339 // Get and cast client instance
340 AddressbookClient client = (AddressbookClient) this.getClient();
342 return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
346 public String enterOwnCompanyName () {
348 this.getLogger().logTrace("CALLED!"); //NOI18N
350 // Get and cast client instance
351 AddressbookClient client = (AddressbookClient) this.getClient();
353 return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
357 public String enterOwnCountryCode () {
359 this.getLogger().logTrace("CALLED!"); //NOI18N
361 // Get and cast client instance
362 AddressbookClient client = (AddressbookClient) this.getClient();
364 return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
368 public String enterOwnEmailAddress () {
370 this.getLogger().logTrace("CALLED!"); //NOI18N
372 // Get and cast client instance
373 AddressbookClient client = (AddressbookClient) this.getClient();
375 return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
379 public String enterOwnFamilyName () {
381 this.getLogger().logTrace("CALLED!"); //NOI18N
383 // Get and cast client instance
384 AddressbookClient client = (AddressbookClient) this.getClient();
386 return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
390 public String enterOwnFaxNumber () {
392 this.getLogger().logTrace("CALLED!"); //NOI18N
394 // Get and cast client instance
395 AddressbookClient client = (AddressbookClient) this.getClient();
397 return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
401 public String enterOwnFirstName () {
403 this.getLogger().logTrace("CALLED!"); //NOI18N
405 // Get and cast client instance
406 AddressbookClient client = (AddressbookClient) this.getClient();
408 return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
412 public Gender enterOwnGender () {
414 this.getLogger().logTrace("CALLED!"); //NOI18N
416 // Get and cast client instance
417 AddressbookClient client = (AddressbookClient) this.getClient();
419 return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
423 public String enterOwnPhoneNumber () {
425 this.getLogger().logTrace("CALLED!"); //NOI18N
427 // Get and cast client instance
428 AddressbookClient client = (AddressbookClient) this.getClient();
430 return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
434 public String enterOwnStreet () {
436 this.getLogger().logTrace("CALLED!"); //NOI18N
438 // Get and cast client instance
439 AddressbookClient client = (AddressbookClient) this.getClient();
441 return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
445 public int enterOwnZipCode () {
447 this.getLogger().logTrace("CALLED!"); //NOI18N
449 // Get and cast client instance
450 AddressbookClient client = (AddressbookClient) this.getClient();
452 return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
456 public final int getColumnCount () {
457 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
459 return this.columnNames.size();
463 public String getColumnName (final int columnIndex) {
464 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
466 // Get column name at index
467 return this.columnNames.get(columnIndex);
471 public String getTranslatedColumnName (final int columnIndex) {
472 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
474 // Get column name at index
475 return this.translatedColumnNames.get(columnIndex);
479 public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
481 this.getLogger().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
483 // Then get specific row from database which is a Contact instance
484 Storable storable = this.getFrontend().getStorableAtRow(rowIndex);
487 this.getLogger().logDebug(MessageFormat.format("storable={0}", storable)); //NOI18N
489 // It may return null
490 if (null == storable) {
492 this.getLogger().logWarning("contact is null - returning null ..."); //NOI18N
496 // Convert column index -> name
497 String columnName = this.getColumnName(columnIndex);
500 this.getLogger().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
502 // Now get that column
505 value = storable.getValueFromColumn(columnName);
506 } catch (final IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
507 this.abortProgramWithException(ex);
511 this.getLogger().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
518 public boolean isOwnContactAdded () throws IOException {
520 this.getLogger().logTrace("CALLED!"); //NOI18N
523 boolean isAdded = false;
526 // Deligate this call to frontend
527 isAdded = ((AddressbookContactFrontend) this.getFrontend()).isOwnContactFound();
528 } catch (final SQLException | IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
529 // Something bad happened
530 this.abortProgramWithException(ex);
534 this.getLogger().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
541 public void registerContact (final Contact contact) {
543 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
546 if (null == contact) {
548 throw new NullPointerException("contact is null"); //NOI18N
551 // Check if contact is found
552 if (((AddressbookContactFrontend) this.getFrontend()).isContactFound(contact)) {
553 // Contact already added
554 // TODO Do something here
555 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
556 // Own contact already added
560 // Add contact to internal list
561 this.addContact(contact);
562 } catch (final ContactAlreadyAddedException | SQLException | IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
564 this.abortProgramWithException(ex);
568 this.getLogger().logTrace("EXIT!"); //NOI18N
572 * Logs given exception and exits program
574 * @param throwable Throwable
576 private void abortProgramWithException (Throwable throwable) {
578 this.logException(throwable);
585 * Fills the column names array with strings from bundle
587 private void fillColumnNamesFromBundle () {
588 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
589 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
592 this.getLogger().logTrace("CALLED!"); //NOI18N
594 // First get an iterator from key set to iterate over
595 Iterator<String> iterator = this.getBundle().keySet().iterator();
597 // Then iterate over all
598 while (iterator.hasNext()) {
600 String key = iterator.next().toLowerCase();
602 // Does the key start with AddressbookContactFacade.columnName ?
603 if (key.startsWith("ContactManager.columnName")) { //NOI18N
604 // This is the wanted entry.
605 this.getLogger().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
607 // Convert string to array based on delimiter '.'
608 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
610 // Token array must contain 4 elements (AddressbookContactFacade.columnName.foo.text)
611 assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
613 // Get pre-last element
614 String columnName = tokens[tokens.length - 2];
617 this.getLogger().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
620 this.columnNames.add(columnName);
621 this.translatedColumnNames.add(this.getBundle().getString(key));
626 this.getLogger().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
630 * Getter for logger instance
632 * @return Logger instance
634 private LoggerBeanLocal getLogger () {
639 * "Getter" for own contact instance or null if not found
641 * @return Contact instance or null
642 * @throws java.sql.SQLException If an SQL error occurs
643 * @throws java.io.IOException If an IO error occurs
644 * @throws java.lang.NoSuchMethodException If a method cannot be found
645 * @throws java.lang.IllegalAccessException If a method is not accessible
646 * @throws java.lang.reflect.InvocationTargetException Any other problems?
648 private Contact getOwnContact () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
650 this.getLogger().logTrace("CALLED!"); //NOI18N
652 // Deligate this call to database frontend
653 Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
656 this.getLogger().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
658 // Return instance or null
663 * Logs given exception
665 * @param exception Throwable
667 protected void logException (final Throwable exception) {
668 this.getLogger().logException(exception);