-/*
- * Copyright (C) 2015 Roland Haeder
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.addressbook.facade.contact;
-
-import java.io.IOException;
-import java.sql.SQLException;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import org.mxchange.addressbook.client.AddressbookClient;
-import org.mxchange.jcontacts.contact.Contact;
-import org.mxchange.jcontacts.contact.gender.Gender;
-import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
-import org.mxchange.jcore.client.Client;
-import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
-import org.mxchange.jcore.facade.BaseFacade;
-import org.mxchange.jcoreeelogger.beans.local.logger.Log;
-import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
-
-/**
- * A facade for contacts.
- * <p>
- * @author Roland Haeder
- * @version 0.0
- */
-public class AddressbookContactFacade extends BaseFacade implements ContactFacade {
-
- /**
- * Column name list
- */
- private final List<String> columnNames;
-
- /**
- * Entity manager
- */
- @PersistenceContext
- private EntityManager entityManager;
-
- /**
- * Logger instance
- */
- @Log
- private LoggerBeanLocal logger;
-
- /**
- * Translated column name list
- */
- private final List<String> translatedColumnNames;
-
- /**
- * Constructor which accepts maxContacts for maximum (initial) contacts and
- * a client instance.
- * <p>
- * @param client Client instance to use
- * @throws java.sql.SQLException If an SQL error occurs
- */
- public AddressbookContactFacade (final Client client) throws SQLException {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
-
- // Make sure all parameters are set correctly
- if (null == client) {
- // Abort here
- throw new NullPointerException("client is null"); //NOI18N
- }
-
- // Set client instance
- this.setClient(client);
-
- // Initialize list
- this.columnNames = new ArrayList<>(15);
- this.translatedColumnNames = new ArrayList<>(15);
-
- // And fill it
- this.fillColumnNamesFromBundle();
-
- // Debug message
- //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
- }
-
- @Override
- public void addContact (final Contact contact) throws ContactAlreadyAddedException {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
-
- // Contact instance must not be null
- if (null == contact) {
- // Abort here
- throw new NullPointerException("contact is null"); //NOI18N
- }
-
- // Add it
- this.entityManager.persist(contact);
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public void doAddOtherAddress () {
- throw new UnsupportedOperationException("Not supported yet."); //NOI18N
- }
-
- @Override
- public void doChangeAddressData (final Contact contact) {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
-
- // Contact must not be null
- if (null == contact) {
- // Abort here
- throw new NullPointerException("contact is null"); //NOI18N
- }
- if (!(this.getClient() instanceof AddressbookClient)) {
- // Cannot cast
- throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
- }
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- // First display it again
- client.displayAddressBox(contact);
-
- // Is it own data?
- if (contact.isOwnContact()) {
- // Deligate to client
- client.doChangeOwnAddressData(contact);
- } else {
- // Other contact's address data to change
- throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
- }
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public void doChangeNameData (final Contact contact) {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
-
- // Contact must not be null
- if (null == contact) {
- // Abort here
- throw new NullPointerException("contact is null"); //NOI18N
- }
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- // First display them again
- client.displayNameBox(contact);
-
- // Is this own data?
- if (contact.isOwnContact()) {
- // Re-ask own data
- client.doChangeOwnNameData(contact);
- } else {
- // Then re-ask them ...
- throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
- }
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public void doChangeOtherAddress () {
- throw new UnsupportedOperationException("Not supported yet."); //NOI18N
- }
-
- @Override
- public void doChangeOtherData (final Contact contact) {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
-
- // Contact must not be null
- if (null == contact) {
- // Abort here
- throw new NullPointerException("contact is null"); //NOI18N
- }
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- // First display them again
- client.displayOtherDataBox(contact);
-
- // Is this own data?
- if (contact.isOwnContact()) {
- // Re-ask own data
- client.doChangeOwnOtherData(contact);
- } else {
- // Then re-ask them ...
- throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
- }
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public void doChangeOwnData () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- /*
- * First check if the user has registered own contact, before that
- * nothing can be changed.
- */
- if (!this.isOwnContactAdded()) {
- // Not added
- this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
-
- // Skip any below code
- return;
- }
-
- // Instance
- Contact contact = this.getOwnContact();
-
- // It must be found
- assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- // Display contact
- client.show(contact);
-
- try {
- // Ask user what to change
- client.userChooseChangeContactData(contact);
- } catch (final UnhandledUserChoiceException ex) {
- this.getLogger().logException(ex);
- }
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public void doDeleteOtherAddress () {
- throw new UnsupportedOperationException("Not supported yet."); //NOI18N
- }
-
- @Override
- public void doEnterOwnData () throws ContactAlreadyAddedException {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Is own contact already added?
- if (this.isOwnContactAdded()) {
- // Don't continue here
- throw new ContactAlreadyAddedException();
- }
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- // Deligate this call to the client
- Contact contact = client.doEnterOwnData();
-
- // Is it set?
- if (contact instanceof Contact) {
- // Add it to contact "book"
- this.registerContact(contact);
- }
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public void doListContacts () {
- throw new UnsupportedOperationException("Not supported yet."); //NOI18N
- }
-
- @Override
- public void doSearchContacts () {
- throw new UnsupportedOperationException("Not supported yet."); //NOI18N
- }
-
- @Override
- public void doShutdown () throws SQLException, IOException {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Shut down the database layer
- this.entityManager.close();
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public String enterOwnCellNumber () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
- }
-
- @Override
- public String enterOwnCity () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(3, 50, "Bitte geben Sie Ihren Wohnort ein: ", false);
- }
-
- @Override
- public String enterOwnComment () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
- }
-
- @Override
- public String enterOwnCompanyName () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
- }
-
- @Override
- public String enterOwnCountryCode () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
- }
-
- @Override
- public String enterOwnEmailAddress () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
- }
-
- @Override
- public String enterOwnFamilyName () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
- }
-
- @Override
- public String enterOwnFaxNumber () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
- }
-
- @Override
- public String enterOwnFirstName () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
- }
-
- @Override
- public Gender enterOwnGender () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
- }
-
- @Override
- public String enterOwnPhoneNumber () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
- }
-
- @Override
- public String enterOwnStreet () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
- }
-
- @Override
- public int enterOwnZipCode () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Get and cast client instance
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
- }
-
- @Override
- public final int getColumnCount () {
- assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
-
- return this.columnNames.size();
- }
-
- @Override
- public String getColumnName (final int columnIndex) {
- assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
-
- // Get column name at index
- return this.columnNames.get(columnIndex);
- }
-
- @Override
- public String getTranslatedColumnName (final int columnIndex) {
- assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
-
- // Get column name at index
- return this.translatedColumnNames.get(columnIndex);
- }
-
- @Override
- public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
-
- // Convert column index -> name
- String columnName = this.getColumnName(columnIndex);
-
- // Debug message
- this.getLogger().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
-
- // Init value
- Object value = null;
-
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
-
- // Return it
- return value;
- }
-
- @Override
- public boolean isOwnContactAdded () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Init variable
- boolean isAdded = false;
-
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
-
- // Return result
- return isAdded;
- }
-
- @Override
- public void registerContact (final Contact contact) {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
-
- // Sanity check
- if (null == contact) {
- // Abort here
- throw new NullPointerException("contact is null"); //NOI18N
- }
- try {
- // Check if contact is found
- if (this.entityManager.contains(contact)) {
- // Contact already added
- // TODO Do something here
- } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
- // Own contact already added
- // TODO Do something
- }
-
- // Add contact to internal list
- this.addContact(contact);
- } catch (final ContactAlreadyAddedException ex) {
- // Abort here
- this.abortProgramWithException(ex);
- }
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- /**
- * Logs given exception and exits program
- * <p>
- * @param throwable Throwable
- */
- private void abortProgramWithException (Throwable throwable) {
- // Log exception
- this.logException(throwable);
-
- // Abort here
- System.exit(1);
- }
-
- /**
- * Fills the column names array with strings from bundle
- */
- private void fillColumnNamesFromBundle () {
- assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
- assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
-
- // Debug message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // First get an iterator from key set to iterate over
- Iterator<String> iterator = this.getBundle().keySet().iterator();
-
- // Then iterate over all
- while (iterator.hasNext()) {
- // Get next element
- String key = iterator.next().toLowerCase();
-
- // Does the key start with AddressbookContactFacade.columnName ?
- if (key.startsWith("ContactManager.columnName")) { //NOI18N
- // This is the wanted entry.
- this.getLogger().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
-
- // Convert string to array based on delimiter '.'
- String[] tokens = this.getArrayFromString(key, "."); //NOI18N
-
- // Token array must contain 4 elements (AddressbookContactFacade.columnName.foo.text)
- assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
-
- // Get pre-last element
- String columnName = tokens[tokens.length - 2];
-
- // Debug message
- this.getLogger().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
-
- // So add it
- this.columnNames.add(columnName);
- this.translatedColumnNames.add(this.getBundle().getString(key));
- }
- }
-
- // Debug message
- this.getLogger().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
- }
-
- /**
- * Getter for logger instance
- * <p>
- * @return Logger instance
- */
- private LoggerBeanLocal getLogger () {
- return this.logger;
- }
-
- /**
- * "Getter" for own contact instance or null if not found
- * <p>
- * @return Contact instance or null
- */
- private Contact getOwnContact () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Deligate this call to database frontend
- Contact contact = null;
- //Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
-
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
-
- // Return instance or null
- return contact;
- }
-
- /**
- * Logs given exception
- * <p>
- * @param exception Throwable
- */
- protected void logException (final Throwable exception) {
- this.getLogger().logException(exception);
- }
-}