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 javax.persistence.EntityManager;
28 import javax.persistence.PersistenceContext;
29 import org.mxchange.addressbook.client.AddressbookClient;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jcontacts.contact.gender.Gender;
32 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
33 import org.mxchange.jcore.client.Client;
34 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
35 import org.mxchange.jcore.facade.BaseFacade;
36 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
37 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
40 * A facade for contacts.
42 * @author Roland Haeder
45 public class AddressbookContactFacade extends BaseFacade implements ContactFacade {
50 private final List<String> columnNames;
56 private EntityManager entityManager;
62 private LoggerBeanLocal logger;
65 * Translated column name list
67 private final List<String> translatedColumnNames;
70 * Constructor which accepts maxContacts for maximum (initial) contacts and
73 * @param client Client instance to use
74 * @throws java.sql.SQLException If an SQL error occurs
76 public AddressbookContactFacade (final Client client) throws SQLException {
78 this.getLogger().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
80 // Make sure all parameters are set correctly
83 throw new NullPointerException("client is null"); //NOI18N
86 // Set client instance
87 this.setClient(client);
90 this.columnNames = new ArrayList<>(15);
91 this.translatedColumnNames = new ArrayList<>(15);
94 this.fillColumnNamesFromBundle();
97 //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
101 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
103 this.getLogger().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
105 // Contact instance must not be null
106 if (null == contact) {
108 throw new NullPointerException("contact is null"); //NOI18N
112 this.entityManager.persist(contact);
115 this.getLogger().logTrace("EXIT!"); //NOI18N
119 public void doAddOtherAddress () {
120 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
124 public void doChangeAddressData (final Contact contact) {
126 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
128 // Contact must not be null
129 if (null == contact) {
131 throw new NullPointerException("contact is null"); //NOI18N
133 if (!(this.getClient() instanceof AddressbookClient)) {
135 throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
138 // Get and cast client instance
139 AddressbookClient client = (AddressbookClient) this.getClient();
141 // First display it again
142 client.displayAddressBox(contact);
145 if (contact.isOwnContact()) {
146 // Deligate to client
147 client.doChangeOwnAddressData(contact);
149 // Other contact's address data to change
150 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
154 this.getLogger().logTrace("EXIT!"); //NOI18N
158 public void doChangeNameData (final Contact contact) {
160 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
162 // Contact must not be null
163 if (null == contact) {
165 throw new NullPointerException("contact is null"); //NOI18N
168 // Get and cast client instance
169 AddressbookClient client = (AddressbookClient) this.getClient();
171 // First display them again
172 client.displayNameBox(contact);
175 if (contact.isOwnContact()) {
177 client.doChangeOwnNameData(contact);
179 // Then re-ask them ...
180 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
184 this.getLogger().logTrace("EXIT!"); //NOI18N
188 public void doChangeOtherAddress () {
189 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
193 public void doChangeOtherData (final Contact contact) {
195 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
197 // Contact must not be null
198 if (null == contact) {
200 throw new NullPointerException("contact is null"); //NOI18N
203 // Get and cast client instance
204 AddressbookClient client = (AddressbookClient) this.getClient();
206 // First display them again
207 client.displayOtherDataBox(contact);
210 if (contact.isOwnContact()) {
212 client.doChangeOwnOtherData(contact);
214 // Then re-ask them ...
215 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
219 this.getLogger().logTrace("EXIT!"); //NOI18N
223 public void doChangeOwnData () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
225 this.getLogger().logTrace("CALLED!"); //NOI18N
228 * First check if the user has registered own contact, before that
229 * nothing can be changed.
231 if (!this.isOwnContactAdded()) {
233 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
235 // Skip any below code
240 Contact contact = this.getOwnContact();
243 assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
245 // Get and cast client instance
246 AddressbookClient client = (AddressbookClient) this.getClient();
249 client.show(contact);
252 // Ask user what to change
253 client.userChooseChangeContactData(contact);
254 } catch (final UnhandledUserChoiceException ex) {
255 this.getLogger().logException(ex);
259 this.getLogger().logTrace("EXIT!"); //NOI18N
263 public void doDeleteOtherAddress () {
264 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
268 public void doEnterOwnData () throws ContactAlreadyAddedException, IOException {
270 this.getLogger().logTrace("CALLED!"); //NOI18N
272 // Is own contact already added?
273 if (this.isOwnContactAdded()) {
274 // Don't continue here
275 throw new ContactAlreadyAddedException();
278 // Get and cast client instance
279 AddressbookClient client = (AddressbookClient) this.getClient();
281 // Deligate this call to the client
282 Contact contact = client.doEnterOwnData();
285 if (contact instanceof Contact) {
286 // Add it to contact "book"
287 this.registerContact(contact);
291 this.getLogger().logTrace("EXIT!"); //NOI18N
295 public void doListContacts () {
296 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
300 public void doSearchContacts () {
301 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
305 public void doShutdown () throws SQLException, IOException {
307 this.getLogger().logTrace("CALLED!"); //NOI18N
309 // Shut down the database layer
310 this.entityManager.close();
313 this.getLogger().logTrace("EXIT!"); //NOI18N
317 public String enterOwnCellNumber () {
319 this.getLogger().logTrace("CALLED!"); //NOI18N
321 // Get and cast client instance
322 AddressbookClient client = (AddressbookClient) this.getClient();
324 return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
328 public String enterOwnCity () {
330 this.getLogger().logTrace("CALLED!"); //NOI18N
332 // Get and cast client instance
333 AddressbookClient client = (AddressbookClient) this.getClient();
335 return client.enterString(3, 50, "Bitte geben Sie Ihren Wohnort ein: ", false);
339 public String enterOwnComment () {
341 this.getLogger().logTrace("CALLED!"); //NOI18N
343 // Get and cast client instance
344 AddressbookClient client = (AddressbookClient) this.getClient();
346 return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
350 public String enterOwnCompanyName () {
352 this.getLogger().logTrace("CALLED!"); //NOI18N
354 // Get and cast client instance
355 AddressbookClient client = (AddressbookClient) this.getClient();
357 return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
361 public String enterOwnCountryCode () {
363 this.getLogger().logTrace("CALLED!"); //NOI18N
365 // Get and cast client instance
366 AddressbookClient client = (AddressbookClient) this.getClient();
368 return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
372 public String enterOwnEmailAddress () {
374 this.getLogger().logTrace("CALLED!"); //NOI18N
376 // Get and cast client instance
377 AddressbookClient client = (AddressbookClient) this.getClient();
379 return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
383 public String enterOwnFamilyName () {
385 this.getLogger().logTrace("CALLED!"); //NOI18N
387 // Get and cast client instance
388 AddressbookClient client = (AddressbookClient) this.getClient();
390 return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
394 public String enterOwnFaxNumber () {
396 this.getLogger().logTrace("CALLED!"); //NOI18N
398 // Get and cast client instance
399 AddressbookClient client = (AddressbookClient) this.getClient();
401 return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
405 public String enterOwnFirstName () {
407 this.getLogger().logTrace("CALLED!"); //NOI18N
409 // Get and cast client instance
410 AddressbookClient client = (AddressbookClient) this.getClient();
412 return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
416 public Gender enterOwnGender () {
418 this.getLogger().logTrace("CALLED!"); //NOI18N
420 // Get and cast client instance
421 AddressbookClient client = (AddressbookClient) this.getClient();
423 return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
427 public String enterOwnPhoneNumber () {
429 this.getLogger().logTrace("CALLED!"); //NOI18N
431 // Get and cast client instance
432 AddressbookClient client = (AddressbookClient) this.getClient();
434 return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
438 public String enterOwnStreet () {
440 this.getLogger().logTrace("CALLED!"); //NOI18N
442 // Get and cast client instance
443 AddressbookClient client = (AddressbookClient) this.getClient();
445 return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
449 public int enterOwnZipCode () {
451 this.getLogger().logTrace("CALLED!"); //NOI18N
453 // Get and cast client instance
454 AddressbookClient client = (AddressbookClient) this.getClient();
456 return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
460 public final int getColumnCount () {
461 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
463 return this.columnNames.size();
467 public String getColumnName (final int columnIndex) {
468 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
470 // Get column name at index
471 return this.columnNames.get(columnIndex);
475 public String getTranslatedColumnName (final int columnIndex) {
476 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
478 // Get column name at index
479 return this.translatedColumnNames.get(columnIndex);
483 public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
485 this.getLogger().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
487 // Then get specific row from database which is a Contact instance
488 Storable storable = this.getFrontend().getStorableAtRow(rowIndex);
491 this.getLogger().logDebug(MessageFormat.format("storable={0}", storable)); //NOI18N
493 // It may return null
494 if (null == storable) {
496 this.getLogger().logWarning("contact is null - returning null ..."); //NOI18N
500 // Convert column index -> name
501 String columnName = this.getColumnName(columnIndex);
504 this.getLogger().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
506 // Now get that column
509 value = storable.getValueFromColumn(columnName);
510 } catch (final IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
511 this.abortProgramWithException(ex);
515 this.getLogger().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
522 public boolean isOwnContactAdded () throws IOException {
524 this.getLogger().logTrace("CALLED!"); //NOI18N
527 boolean isAdded = false;
530 // Deligate this call to frontend
531 isAdded = ((AddressbookContactFrontend) this.getFrontend()).isOwnContactFound();
532 } catch (final SQLException | IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
533 // Something bad happened
534 this.abortProgramWithException(ex);
538 this.getLogger().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
545 public void registerContact (final Contact contact) {
547 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
550 if (null == contact) {
552 throw new NullPointerException("contact is null"); //NOI18N
555 // Check if contact is found
556 if (this.entityManager.contains(contact)) {
557 // Contact already added
558 // TODO Do something here
559 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
560 // Own contact already added
564 // Add contact to internal list
565 this.addContact(contact);
566 } catch (final ContactAlreadyAddedException ex) {
568 this.abortProgramWithException(ex);
572 this.getLogger().logTrace("EXIT!"); //NOI18N
576 * Logs given exception and exits program
578 * @param throwable Throwable
580 private void abortProgramWithException (Throwable throwable) {
582 this.logException(throwable);
589 * Fills the column names array with strings from bundle
591 private void fillColumnNamesFromBundle () {
592 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
593 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
596 this.getLogger().logTrace("CALLED!"); //NOI18N
598 // First get an iterator from key set to iterate over
599 Iterator<String> iterator = this.getBundle().keySet().iterator();
601 // Then iterate over all
602 while (iterator.hasNext()) {
604 String key = iterator.next().toLowerCase();
606 // Does the key start with AddressbookContactFacade.columnName ?
607 if (key.startsWith("ContactManager.columnName")) { //NOI18N
608 // This is the wanted entry.
609 this.getLogger().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
611 // Convert string to array based on delimiter '.'
612 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
614 // Token array must contain 4 elements (AddressbookContactFacade.columnName.foo.text)
615 assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
617 // Get pre-last element
618 String columnName = tokens[tokens.length - 2];
621 this.getLogger().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
624 this.columnNames.add(columnName);
625 this.translatedColumnNames.add(this.getBundle().getString(key));
630 this.getLogger().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
634 * Getter for logger instance
636 * @return Logger instance
638 private LoggerBeanLocal getLogger () {
643 * "Getter" for own contact instance or null if not found
645 * @return Contact instance or null
647 private Contact getOwnContact () {
649 this.getLogger().logTrace("CALLED!"); //NOI18N
651 // Deligate this call to database frontend
652 Contact contact = null;
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);