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.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;
39 * A facade for contacts.
41 * @author Roland Haeder
44 public class AddressbookContactFacade extends BaseFacade implements ContactFacade {
49 private final List<String> columnNames;
55 private EntityManager entityManager;
61 private LoggerBeanLocal logger;
64 * Translated column name list
66 private final List<String> translatedColumnNames;
69 * Constructor which accepts maxContacts for maximum (initial) contacts and
72 * @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 () {
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 {
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) {
485 this.getLogger().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
487 // Convert column index -> name
488 String columnName = this.getColumnName(columnIndex);
491 this.getLogger().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
497 this.getLogger().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
504 public boolean isOwnContactAdded () {
506 this.getLogger().logTrace("CALLED!"); //NOI18N
509 boolean isAdded = false;
512 this.getLogger().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
519 public void registerContact (final Contact contact) {
521 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
524 if (null == contact) {
526 throw new NullPointerException("contact is null"); //NOI18N
529 // Check if contact is found
530 if (this.entityManager.contains(contact)) {
531 // Contact already added
532 // TODO Do something here
533 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
534 // Own contact already added
538 // Add contact to internal list
539 this.addContact(contact);
540 } catch (final ContactAlreadyAddedException ex) {
542 this.abortProgramWithException(ex);
546 this.getLogger().logTrace("EXIT!"); //NOI18N
550 * Logs given exception and exits program
552 * @param throwable Throwable
554 private void abortProgramWithException (Throwable throwable) {
556 this.logException(throwable);
563 * Fills the column names array with strings from bundle
565 private void fillColumnNamesFromBundle () {
566 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
567 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
570 this.getLogger().logTrace("CALLED!"); //NOI18N
572 // First get an iterator from key set to iterate over
573 Iterator<String> iterator = this.getBundle().keySet().iterator();
575 // Then iterate over all
576 while (iterator.hasNext()) {
578 String key = iterator.next().toLowerCase();
580 // Does the key start with AddressbookContactFacade.columnName ?
581 if (key.startsWith("ContactManager.columnName")) { //NOI18N
582 // This is the wanted entry.
583 this.getLogger().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
585 // Convert string to array based on delimiter '.'
586 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
588 // Token array must contain 4 elements (AddressbookContactFacade.columnName.foo.text)
589 assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
591 // Get pre-last element
592 String columnName = tokens[tokens.length - 2];
595 this.getLogger().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
598 this.columnNames.add(columnName);
599 this.translatedColumnNames.add(this.getBundle().getString(key));
604 this.getLogger().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
608 * Getter for logger instance
610 * @return Logger instance
612 private LoggerBeanLocal getLogger () {
617 * "Getter" for own contact instance or null if not found
619 * @return Contact instance or null
621 private Contact getOwnContact () {
623 this.getLogger().logTrace("CALLED!"); //NOI18N
625 // Deligate this call to database frontend
626 Contact contact = null;
627 //Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
630 this.getLogger().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
632 // Return instance or null
637 * Logs given exception
639 * @param exception Throwable
641 protected void logException (final Throwable exception) {
642 this.getLogger().logException(exception);