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
73 * @throws java.sql.SQLException If an SQL error occurs
75 public AddressbookContactFacade (final Client client) throws SQLException {
77 this.getLogger().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
79 // Make sure all parameters are set correctly
82 throw new NullPointerException("client is null"); //NOI18N
85 // Set client instance
86 this.setClient(client);
89 this.columnNames = new ArrayList<>(15);
90 this.translatedColumnNames = new ArrayList<>(15);
93 this.fillColumnNamesFromBundle();
96 //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
100 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
102 this.getLogger().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
104 // Contact instance must not be null
105 if (null == contact) {
107 throw new NullPointerException("contact is null"); //NOI18N
111 this.entityManager.persist(contact);
114 this.getLogger().logTrace("EXIT!"); //NOI18N
118 public void doAddOtherAddress () {
119 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
123 public void doChangeAddressData (final Contact contact) {
125 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
127 // Contact must not be null
128 if (null == contact) {
130 throw new NullPointerException("contact is null"); //NOI18N
132 if (!(this.getClient() instanceof AddressbookClient)) {
134 throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
137 // Get and cast client instance
138 AddressbookClient client = (AddressbookClient) this.getClient();
140 // First display it again
141 client.displayAddressBox(contact);
144 if (contact.isOwnContact()) {
145 // Deligate to client
146 client.doChangeOwnAddressData(contact);
148 // Other contact's address data to change
149 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
153 this.getLogger().logTrace("EXIT!"); //NOI18N
157 public void doChangeNameData (final Contact contact) {
159 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
161 // Contact must not be null
162 if (null == contact) {
164 throw new NullPointerException("contact is null"); //NOI18N
167 // Get and cast client instance
168 AddressbookClient client = (AddressbookClient) this.getClient();
170 // First display them again
171 client.displayNameBox(contact);
174 if (contact.isOwnContact()) {
176 client.doChangeOwnNameData(contact);
178 // Then re-ask them ...
179 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
183 this.getLogger().logTrace("EXIT!"); //NOI18N
187 public void doChangeOtherAddress () {
188 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
192 public void doChangeOtherData (final Contact contact) {
194 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
196 // Contact must not be null
197 if (null == contact) {
199 throw new NullPointerException("contact is null"); //NOI18N
202 // Get and cast client instance
203 AddressbookClient client = (AddressbookClient) this.getClient();
205 // First display them again
206 client.displayOtherDataBox(contact);
209 if (contact.isOwnContact()) {
211 client.doChangeOwnOtherData(contact);
213 // Then re-ask them ...
214 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
218 this.getLogger().logTrace("EXIT!"); //NOI18N
222 public void doChangeOwnData () {
224 this.getLogger().logTrace("CALLED!"); //NOI18N
227 * First check if the user has registered own contact, before that
228 * nothing can be changed.
230 if (!this.isOwnContactAdded()) {
232 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
234 // Skip any below code
239 Contact contact = this.getOwnContact();
242 assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
244 // Get and cast client instance
245 AddressbookClient client = (AddressbookClient) this.getClient();
248 client.show(contact);
251 // Ask user what to change
252 client.userChooseChangeContactData(contact);
253 } catch (final UnhandledUserChoiceException ex) {
254 this.getLogger().logException(ex);
258 this.getLogger().logTrace("EXIT!"); //NOI18N
262 public void doDeleteOtherAddress () {
263 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
267 public void doEnterOwnData () throws ContactAlreadyAddedException {
269 this.getLogger().logTrace("CALLED!"); //NOI18N
271 // Is own contact already added?
272 if (this.isOwnContactAdded()) {
273 // Don't continue here
274 throw new ContactAlreadyAddedException();
277 // Get and cast client instance
278 AddressbookClient client = (AddressbookClient) this.getClient();
280 // Deligate this call to the client
281 Contact contact = client.doEnterOwnData();
284 if (contact instanceof Contact) {
285 // Add it to contact "book"
286 this.registerContact(contact);
290 this.getLogger().logTrace("EXIT!"); //NOI18N
294 public void doListContacts () {
295 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
299 public void doSearchContacts () {
300 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
304 public void doShutdown () throws SQLException, IOException {
306 this.getLogger().logTrace("CALLED!"); //NOI18N
308 // Shut down the database layer
309 this.entityManager.close();
312 this.getLogger().logTrace("EXIT!"); //NOI18N
316 public String enterOwnCellNumber () {
318 this.getLogger().logTrace("CALLED!"); //NOI18N
320 // Get and cast client instance
321 AddressbookClient client = (AddressbookClient) this.getClient();
323 return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
327 public String enterOwnCity () {
329 this.getLogger().logTrace("CALLED!"); //NOI18N
331 // Get and cast client instance
332 AddressbookClient client = (AddressbookClient) this.getClient();
334 return client.enterString(3, 50, "Bitte geben Sie Ihren Wohnort ein: ", false);
338 public String enterOwnComment () {
340 this.getLogger().logTrace("CALLED!"); //NOI18N
342 // Get and cast client instance
343 AddressbookClient client = (AddressbookClient) this.getClient();
345 return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
349 public String enterOwnCompanyName () {
351 this.getLogger().logTrace("CALLED!"); //NOI18N
353 // Get and cast client instance
354 AddressbookClient client = (AddressbookClient) this.getClient();
356 return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
360 public String enterOwnCountryCode () {
362 this.getLogger().logTrace("CALLED!"); //NOI18N
364 // Get and cast client instance
365 AddressbookClient client = (AddressbookClient) this.getClient();
367 return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
371 public String enterOwnEmailAddress () {
373 this.getLogger().logTrace("CALLED!"); //NOI18N
375 // Get and cast client instance
376 AddressbookClient client = (AddressbookClient) this.getClient();
378 return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
382 public String enterOwnFamilyName () {
384 this.getLogger().logTrace("CALLED!"); //NOI18N
386 // Get and cast client instance
387 AddressbookClient client = (AddressbookClient) this.getClient();
389 return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
393 public String enterOwnFaxNumber () {
395 this.getLogger().logTrace("CALLED!"); //NOI18N
397 // Get and cast client instance
398 AddressbookClient client = (AddressbookClient) this.getClient();
400 return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
404 public String enterOwnFirstName () {
406 this.getLogger().logTrace("CALLED!"); //NOI18N
408 // Get and cast client instance
409 AddressbookClient client = (AddressbookClient) this.getClient();
411 return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
415 public Gender enterOwnGender () {
417 this.getLogger().logTrace("CALLED!"); //NOI18N
419 // Get and cast client instance
420 AddressbookClient client = (AddressbookClient) this.getClient();
422 return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
426 public String enterOwnPhoneNumber () {
428 this.getLogger().logTrace("CALLED!"); //NOI18N
430 // Get and cast client instance
431 AddressbookClient client = (AddressbookClient) this.getClient();
433 return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
437 public String enterOwnStreet () {
439 this.getLogger().logTrace("CALLED!"); //NOI18N
441 // Get and cast client instance
442 AddressbookClient client = (AddressbookClient) this.getClient();
444 return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
448 public int enterOwnZipCode () {
450 this.getLogger().logTrace("CALLED!"); //NOI18N
452 // Get and cast client instance
453 AddressbookClient client = (AddressbookClient) this.getClient();
455 return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
459 public final int getColumnCount () {
460 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
462 return this.columnNames.size();
466 public String getColumnName (final int columnIndex) {
467 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
469 // Get column name at index
470 return this.columnNames.get(columnIndex);
474 public String getTranslatedColumnName (final int columnIndex) {
475 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
477 // Get column name at index
478 return this.translatedColumnNames.get(columnIndex);
482 public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) {
484 this.getLogger().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
486 // Convert column index -> name
487 String columnName = this.getColumnName(columnIndex);
490 this.getLogger().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
496 this.getLogger().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
503 public boolean isOwnContactAdded () {
505 this.getLogger().logTrace("CALLED!"); //NOI18N
508 boolean isAdded = false;
511 this.getLogger().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
518 public void registerContact (final Contact contact) {
520 this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
523 if (null == contact) {
525 throw new NullPointerException("contact is null"); //NOI18N
528 // Check if contact is found
529 if (this.entityManager.contains(contact)) {
530 // Contact already added
531 // TODO Do something here
532 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
533 // Own contact already added
537 // Add contact to internal list
538 this.addContact(contact);
539 } catch (final ContactAlreadyAddedException ex) {
541 this.abortProgramWithException(ex);
545 this.getLogger().logTrace("EXIT!"); //NOI18N
549 * Logs given exception and exits program
551 * @param throwable Throwable
553 private void abortProgramWithException (Throwable throwable) {
555 this.logException(throwable);
562 * Fills the column names array with strings from bundle
564 private void fillColumnNamesFromBundle () {
565 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
566 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
569 this.getLogger().logTrace("CALLED!"); //NOI18N
571 // First get an iterator from key set to iterate over
572 Iterator<String> iterator = this.getBundle().keySet().iterator();
574 // Then iterate over all
575 while (iterator.hasNext()) {
577 String key = iterator.next().toLowerCase();
579 // Does the key start with AddressbookContactFacade.columnName ?
580 if (key.startsWith("ContactManager.columnName")) { //NOI18N
581 // This is the wanted entry.
582 this.getLogger().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
584 // Convert string to array based on delimiter '.'
585 String[] tokens = this.getArrayFromString(key, "."); //NOI18N
587 // Token array must contain 4 elements (AddressbookContactFacade.columnName.foo.text)
588 assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
590 // Get pre-last element
591 String columnName = tokens[tokens.length - 2];
594 this.getLogger().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
597 this.columnNames.add(columnName);
598 this.translatedColumnNames.add(this.getBundle().getString(key));
603 this.getLogger().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
607 * Getter for logger instance
609 * @return Logger instance
611 private LoggerBeanLocal getLogger () {
616 * "Getter" for own contact instance or null if not found
618 * @return Contact instance or null
620 private Contact getOwnContact () {
622 this.getLogger().logTrace("CALLED!"); //NOI18N
624 // Deligate this call to database frontend
625 Contact contact = null;
626 //Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
629 this.getLogger().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
631 // Return instance or null
636 * Logs given exception
638 * @param exception Throwable
640 protected void logException (final Throwable exception) {
641 this.getLogger().logException(exception);