From: Roland Haeder Date: Fri, 9 Oct 2015 07:57:11 +0000 (+0200) Subject: This lib should only contain remote EJB interfaces for the addressbook application... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=bd6efb10bc21bc9f6e9e08f705fd333e488ffa9a;p=jfinancials-swing.git This lib should only contain remote EJB interfaces for the addressbook application and no other classes. A better place for them is maybe addressbook-swing Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/addressbook/BaseAddressbookSystem.java b/src/org/mxchange/addressbook/BaseAddressbookSystem.java new file mode 100644 index 0000000..772922b --- /dev/null +++ b/src/org/mxchange/addressbook/BaseAddressbookSystem.java @@ -0,0 +1,85 @@ +/* + * 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 . + */ +package org.mxchange.addressbook; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.jcore.BaseFrameworkSystem; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; + +/** + * A general addressbook class + *

+ * @author Roland Haeder + */ +public abstract class BaseAddressbookSystem extends BaseFrameworkSystem { + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal logger; + + /** + * Protected constructor + */ + protected BaseAddressbookSystem () { + try { + // Get initial context + Context context = new InitialContext(); + + // Lookup logger + this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new RuntimeException(ex); + } + } + + /** + * Log exception and abort program. + *

+ * @param throwable Throwable + */ + protected void abortProgramWithException (final Throwable throwable) { + // Log exception + this.logException(throwable); + + // Abort here + System.exit(1); + } + + /** + * Getter for logger instance + *

+ * @return Logger instance + */ + protected LoggerBeanLocal getLogger () { + return this.logger; + } + + /** + * Logs given exception + *

+ * @param exception Throwable + */ + protected void logException (final Throwable exception) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } +} diff --git a/src/org/mxchange/addressbook/client/AddressbookClient.java b/src/org/mxchange/addressbook/client/AddressbookClient.java new file mode 100644 index 0000000..ff8442b --- /dev/null +++ b/src/org/mxchange/addressbook/client/AddressbookClient.java @@ -0,0 +1,173 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.client; + +import org.mxchange.addressbook.menu.item.SelectableMenuItem; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcontacts.contact.gender.Gender; +import org.mxchange.jcore.client.Client; +import org.mxchange.jcore.exceptions.MenuInitializationException; +import org.mxchange.jcore.exceptions.UnhandledUserChoiceException; + +/** + * A special client interface for addressbook applications. + *

+ * @author Roland Haeder + */ +public interface AddressbookClient extends Client { + + /** + * Displays a "box" for the address + *

+ * @param contact Contact to show address from + */ + public void displayAddressBox (final Contact contact); + + /** + * Displays a "box" for the name + *

+ * @param contact Contact to show name from + */ + public void displayNameBox (final Contact contact); + + /** + * Displays a "box" for other data + *

+ * @param contact Contact to show other data from + */ + public void displayOtherDataBox (final Contact contact); + + /** + * Shows given contact instamce + * + * @param contact Contact instance + */ + public void show (final Contact contact); + + /** + * The user changes own address data + *

+ * @param contact Contact instance to change + */ + public void doChangeOwnAddressData (final Contact contact); + + /** + * The user changes own name data + *

+ * @param contact + */ + public void doChangeOwnNameData (final Contact contact); + + /** + * The user changes own other data + *

+ * @param contact Constact instance to change + */ + public void doChangeOwnOtherData (final Contact contact); + + /** + * Allows the user to enter own data + *

+ * @return Finished Contact instance + */ + public Contact doEnterOwnData (); + + /** + * Asks the user for a choice and proceeds accordingly + *

+ * @throws UnhandledUserChoiceException If choice is not supported + * @throws org.mxchange.jcore.exceptions.MenuInitializationException If the menu cannot be initialized + */ + public void doUserMenuChoice () throws UnhandledUserChoiceException, MenuInitializationException; + + /** + * Asks the the user to enter a single character which must match validChars + *

+ * @param validChars Valid chars that are accepted + * @param message Message to user + * @return Allowed character + */ + public char enterChar (final char[] validChars, final String message); + + /** + * Asks the user to enter his/her gender (M=Male, F=Female, C=Company) + *

+ * @param message Message to output + * @return Gender enum + */ + public Gender enterGender (final String message); + + /** + * Reads an integer (int) from the user + *

+ * @param minimum Minimum allowed number + * @param maximum Maximum allowed number + * @param message Message to user + * @return Entered string by user or null if empty string is allowed + */ + public int enterInt (final int minimum, final int maximum, final String message); + + /** + * Reads a string of minimum and maximum length from the user. An empty + * string should be generally not allowed, but might be okay for e.g. + * company name. + *

+ * @param minLength Minimum length of the string to read + * @param maxLength Maximum length of the string to read + * @param message Message to user + * @param allowEmpty Whether empty strings are allowed + * @return Entered string by user or null if empty string is allowed + */ + public String enterString (final int minLength, final int maxLength, final String message, final boolean allowEmpty); + + /** + * Setter for current menu choice + *

+ * @param currentMenu Current menu choice + */ + public void setCurrentMenu (final String currentMenu); + + /** + * Some "Getter" for menu item + *

+ * @param accessKey Key to press to access this menu + * @param text Text to show to user + * @return + */ + public SelectableMenuItem getMenuItem (final char accessKey, final String text); + + /** + * Shows current menu selection to the user + */ + public void showCurrentMenu (); + + /** + * Shows given menu entry in client + *

+ * @param item Menu item to show + */ + public void showEntry (final SelectableMenuItem item); + + /** + * Let the user choose what to change on the address: [n]ame, [a]ddress, + * [o]ther + *

+ * @param contact Contact instance to let the user change data + * @throws UnhandledUserChoiceException If choice is not supported + */ + public void userChooseChangeContactData (final Contact contact) throws UnhandledUserChoiceException; +} diff --git a/src/org/mxchange/addressbook/client/BaseAddressbookClient.java b/src/org/mxchange/addressbook/client/BaseAddressbookClient.java new file mode 100644 index 0000000..69b631d --- /dev/null +++ b/src/org/mxchange/addressbook/client/BaseAddressbookClient.java @@ -0,0 +1,208 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.client; + +import java.sql.SQLException; +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Map; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.addressbook.facade.contact.AddressbookContactFacade; +import org.mxchange.addressbook.facade.contact.ContactFacade; +import org.mxchange.addressbook.menu.Menu; +import org.mxchange.jcore.client.BaseClient; +import org.mxchange.jcore.client.Client; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; + +/** + * A general addressbook client + *

+ * @author Roland Haeder TODO: Find better name + */ +public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient { + + /** + * Current menu choice + */ + private String currentMenu; + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal logger; + + /** + * Menu system + */ + private final Map menus; + + /** + * No instances can be created of this class + */ + protected BaseAddressbookClient () { + // Init menu map + this.menus = new HashMap<>(10); + + // Try it + try { + // Get context + Context context = new InitialContext(); + + // Lookup logger + this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new RuntimeException(ex); + } + } + + /** + * Current menu choice + *

+ * @return the currentMenu + */ + public final String getCurrentMenu () { + return this.currentMenu; + } + + @Override + public final void setCurrentMenu (final String currentMenu) { + this.currentMenu = currentMenu; + } + + /** + * "Getter" for given menu type + *

+ * @param menuType Menu type instance to return + * @return Menu or null if not found + */ + private Menu getMenu (final String menuType) { + // Default is not found + Menu menu = null; + + // Check array + if (this.getMenus().containsKey(menuType)) { + // Found! + menu = this.getMenus().get(menuType); + } + + // Return it + return menu; + } + + /** + * Logs exception and exits program + *

+ * @param throwable Throwable + */ + protected void abortProgramWithException (final Throwable throwable) { + // Log exception + this.logException(throwable); + + // Abort here + System.exit(1); + } + + /** + * Fills menu map with swing menus + */ + protected abstract void fillMenuMap (); + + /** + * Getter for logger instance + *

+ * @return Logger instance + */ + protected LoggerBeanLocal getLogger () { + return this.logger; + } + + /** + * Getter for menus map + *

+ * @return Map of all menus + */ + protected final Map getMenus () { + return this.menus; + } + + /** + * Initializes contact manager + *

+ * @throws java.sql.SQLException If any SQL error occurs + */ + protected void initContactManager () throws SQLException { + // Trace message + this.getLogger().logTrace("CALLED!"); //NOI18N + + // Debug message + this.getLogger().logDebug("Initializing contact manager ..."); //NOI18N + + // Init contact facade with console client + // TODO Static initial amount of contacts + ContactFacade facade = new AddressbookContactFacade((Client) this); + + // Set it here + this.setFacade(facade); + + // Debug message + this.getLogger().logDebug("Contact manager has been initialized."); //NOI18N + + // Trace message + this.getLogger().logTrace("EXIT!"); //NOI18N + } + + /** + * Logs an exception + *

+ * @param throwable Throwable + */ + protected void logException (final Throwable throwable) { + // Deligate to logger + this.getLogger().logException(throwable); + } + + /** + * Shows given menu + *

+ * @param menuType Given menu to show + */ + protected void showMenu (final String menuType) { + // Trace message + this.getLogger().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N + + // Get menu from type + Menu menu = this.getMenu(menuType); + + // Is the menu set? + if (!(menu instanceof Menu)) { + // Not found + // TODO Own exception? + throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N + } + + // Show menu + menu.show(this); + + // Trace message + this.getLogger().logTrace("EXIT!"); //NOI18N + } +} diff --git a/src/org/mxchange/addressbook/facade/contact/AddressbookContactFacade.java b/src/org/mxchange/addressbook/facade/contact/AddressbookContactFacade.java new file mode 100644 index 0000000..592d5b7 --- /dev/null +++ b/src/org/mxchange/addressbook/facade/contact/AddressbookContactFacade.java @@ -0,0 +1,643 @@ +/* + * 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 . + */ +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. + *

+ * @author Roland Haeder + * @version 0.0 + */ +public class AddressbookContactFacade extends BaseFacade implements ContactFacade { + + /** + * Column name list + */ + private final List columnNames; + + /** + * Entity manager + */ + @PersistenceContext + private EntityManager entityManager; + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal logger; + + /** + * Translated column name list + */ + private final List translatedColumnNames; + + /** + * Constructor which accepts maxContacts for maximum (initial) contacts and + * a client instance. + *

+ * @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 + *

+ * @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 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 + *

+ * @return Logger instance + */ + private LoggerBeanLocal getLogger () { + return this.logger; + } + + /** + * "Getter" for own contact instance or null if not found + *

+ * @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 + *

+ * @param exception Throwable + */ + protected void logException (final Throwable exception) { + this.getLogger().logException(exception); + } +} diff --git a/src/org/mxchange/addressbook/facade/contact/ContactFacade.java b/src/org/mxchange/addressbook/facade/contact/ContactFacade.java new file mode 100644 index 0000000..aac08dd --- /dev/null +++ b/src/org/mxchange/addressbook/facade/contact/ContactFacade.java @@ -0,0 +1,241 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.facade.contact; + +import java.io.IOException; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcontacts.contact.gender.Gender; +import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException; +import org.mxchange.jcore.facade.Facade; + +/** + * An interface for addressbook contact manager + *

+ * @author Roland Haeder + */ +public interface ContactFacade extends Facade { + + /** + * Adds given Contact instance to list + *

+ * @param contact Contact instance to add + * @throws org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException If the contact is already added + */ + public void addContact (final Contact contact) throws ContactAlreadyAddedException; + + /** + * Let the user add a new other address + */ + public void doAddOtherAddress (); + + /** + * The user can change address data, like street, ZIP code, city and country + * of given Contact instance. + *

+ * @param contact Instance to change data + */ + public void doChangeAddressData (final Contact contact); + + /** + * The user can change name data, like gender, surname, family name and + * company name (if business contact). + *

+ * @param contact Instance to change data + */ + public void doChangeNameData (final Contact contact); + + /** + * Let the user change other address + */ + public void doChangeOtherAddress (); + + /** + * The user can change other data, like phone numbers or comments. + *

+ * @param contact Instance to change data + */ + public void doChangeOtherData (final Contact contact); + + /** + * Let the user change own data + *

+ */ + public void doChangeOwnData (); + + /** + * Let the user delete other address + */ + public void doDeleteOtherAddress (); + + /** + * Asks user for own data + *

+ * @throws org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException If own contact is already added + * @throws java.io.IOException If an IO error was found + */ + public void doEnterOwnData () throws ContactAlreadyAddedException; + + /** + * List all contacts + */ + public void doListContacts (); + + /** + * Searches address book for a contact + */ + public void doSearchContacts (); + + /** + * Allows the user to enter own cellphone number. + *

+ * @return Cellphone number + */ + public String enterOwnCellNumber (); + + /** + * Allows the user to enter own city name. + *

+ * @return City name + */ + public String enterOwnCity (); + + /** + * Allows the user to enter comment for own entry. + *

+ * @return Comment + */ + public String enterOwnComment (); + + /** + * Allows the user to enter own company name. + *

+ * @return Company name + */ + public String enterOwnCompanyName (); + + /** + * Allows the user to enter own country code. + *

+ * @return Country code + */ + public String enterOwnCountryCode (); + + /** + * Allows the user to enter own email address. + *

+ * @return Email address + */ + public String enterOwnEmailAddress (); + + /** + * Allows the user to enter own family name. + *

+ * @return Family name + */ + public String enterOwnFamilyName (); + + /** + * Allows the user to enter own fax number. + *

+ * @return Fax number + */ + public String enterOwnFaxNumber (); + + /** + * Allows the user to enter own surname. + *

+ * @return Surname + */ + public String enterOwnFirstName (); + + /** + * Allows the user to enter own gender. + *

+ * @return Gender + */ + public Gender enterOwnGender (); + + /** + * Allows the user to enter own phone number. + *

+ * @return Phone number + */ + public String enterOwnPhoneNumber (); + + /** + * Allows the user to enter own street and house number. + *

+ * @return Street and house number + */ + public String enterOwnStreet (); + + /** + * Allows the user to enter own ZIP code. + *

+ * @return ZIP code + */ + public int enterOwnZipCode (); + + /** + * Getter for column count + *

+ * @return Column count TODO: This is needed for TableModel in Swing + */ + public int getColumnCount (); + + /** + * Getter for column name at given index. + *

+ * @param columnIndex Column index + * @return Database column name TODO: This is needed for TableModel in Swing + */ + public String getColumnName (final int columnIndex); + + /** + * Getter for translated column name at given index. + *

+ * @param columnIndex Column index + * @return Human-readable column name TODO: This is needed for TableModel in + * Swing + */ + public String getTranslatedColumnName (final int columnIndex); + + /** + * Somewhat "getter" for value from given row and column index + *

+ * @param rowIndex Row index + * @param columnIndex Column index + * @return Value from given row/column + */ + public Object getValueFromRowColumn (final int rowIndex, final int columnIndex); + + /** + * Checks whether own contact is already added by checking all entries for + * isOwnContact flag + *

+ * @return Whether own contact is already added + * @throws java.io.IOException If an IO error occurs + */ + public boolean isOwnContactAdded () throws IOException; + + /** + * Adds given contact to address book + *

+ * @param contact Contact being added TODO Add check for book size + */ + public void registerContact (final Contact contact); +} diff --git a/src/org/mxchange/addressbook/menu/AddressbookMenu.java b/src/org/mxchange/addressbook/menu/AddressbookMenu.java new file mode 100644 index 0000000..8ef43f4 --- /dev/null +++ b/src/org/mxchange/addressbook/menu/AddressbookMenu.java @@ -0,0 +1,95 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.menu; + +import java.text.MessageFormat; +import java.util.List; +import org.mxchange.addressbook.client.AddressbookClient; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; +import org.mxchange.jcore.client.Client; + +/** + * Utility class for menu structure + *

+ * @author Roland Haeder + */ +public class AddressbookMenu extends BaseMenu { + + /** + * Copies entries for given type into the menu list + *

+ * @param menuList Menu list for later showing + * @param menuType Type of menu + * @param client Client instance to call back + */ + public static void addItemsToList (final List menuList, final String menuType, final Client client) { + // Some instances must be set + if (null == menuList) { + // Abort here + throw new NullPointerException("menuList is null"); //NOI18N + } else if (null == client) { + // Abort here + throw new NullPointerException("contact is null"); //NOI18N + } else if (!(client instanceof AddressbookClient)) { + // Not correct instance + throw new IllegalArgumentException(MessageFormat.format("client{0} must implement AddressbookClient", client)); + } + + // Cast client to proper interface + AddressbookClient c = (AddressbookClient) client; + + // Get list size + int size = menuList.size(); + + // Depends on type + switch (menuType) { + case "main": // Main menu //NOI18N + // Enter own data + menuList.add(c.getMenuItem('1', "Eigene Adresse anlegen")); + + // Change own data + menuList.add(c.getMenuItem('2', "Eigene Adresse ändern")); + + // Add new addess + menuList.add(c.getMenuItem('3', "Neue Adresse hinzufügen")); + + // List entries + menuList.add(c.getMenuItem('4', "Adressbuch anzeigen")); + + // Address search + menuList.add(c.getMenuItem('5', "Adresse suchen")); + + // Change other address + menuList.add(c.getMenuItem('6', "Adresse ändern")); + + // Delete other address + menuList.add(c.getMenuItem('7', "Adresse löschen")); + + // Always last line: Exit program + menuList.add(c.getMenuItem('0', "Programm verlassen")); + break; + + default: // Not supported + System.err.println(MessageFormat.format("Menu type '{0}' ont supported", menuType)); //NOI18N + System.exit(1); + } + + // Size must have changed to more entries than before + assert (menuList.size() > size); + } + +} diff --git a/src/org/mxchange/addressbook/menu/BaseMenu.java b/src/org/mxchange/addressbook/menu/BaseMenu.java new file mode 100644 index 0000000..cf5b916 --- /dev/null +++ b/src/org/mxchange/addressbook/menu/BaseMenu.java @@ -0,0 +1,110 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.menu; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import org.mxchange.addressbook.BaseAddressbookSystem; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; +import org.mxchange.jcore.client.Client; + +/** + * A general menu class + *

+ * @author Roland Haeder + */ +public abstract class BaseMenu extends BaseAddressbookSystem implements Menu { + + /** + * Menu list + */ + private List menuList; + + /** + * No instance from this object + */ + protected BaseMenu () { + } + + @Override + public int getMenuItemsCount () { + return this.menuList.size(); + } + + @Override + public Iterator getMenuItemsIterator () { + return this.menuList.iterator(); + } + + @Override + public void show (final Client client) { + // Trace message + this.getLogger().logTrace(MessageFormat.format("client={0} CALLED!", client)); //NOI18N + + // Client must not be null + if (null == client) { + // Abort here + throw new NullPointerException("client is null"); //NOI18N + } + + // Get values + Iterator iterator = this.menuList.iterator(); + + // Debug message + this.getLogger().logDebug("Showing menu with '" + this.menuList.size() + "' entries."); + + // Output all menus + while (iterator.hasNext()) { + // Get item + SelectableMenuItem item = iterator.next(); + + // Show this item + item.show(client); + } + + // Trace message + this.getLogger().logTrace("EXIT!"); //NOI18N + } + + /** + * Getter for menu list + *

+ * @return menuList List of menu entries + */ + protected final List getMenuList () { + return this.menuList; + } + + /** + * Initializes menu + *

+ * @param menuType Menu type to initialize + * @param client CLient to call back + */ + protected void initMenu (final String menuType, final Client client) { + // Trace message + this.getLogger().logTrace(MessageFormat.format("menuType={0},client={1} - CALLED!", menuType, client)); //NOI18N + + // Init menu list + this.menuList = new ArrayList<>(5); + + // Trace message + this.getLogger().logTrace("EXIT!"); //NOI18N + } +} diff --git a/src/org/mxchange/addressbook/menu/Menu.java b/src/org/mxchange/addressbook/menu/Menu.java new file mode 100644 index 0000000..a0ba62f --- /dev/null +++ b/src/org/mxchange/addressbook/menu/Menu.java @@ -0,0 +1,50 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.menu; + +import java.util.Iterator; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; +import org.mxchange.jcore.client.Client; + +/** + * An interface for menus + *

+ * @author Roland Haeder TODO find better name + */ +public interface Menu { + + /** + * Size of all menu items + *

+ * @return + */ + public int getMenuItemsCount (); + + /** + * "Getter" for an iterator on all menu items of the current menu + *

+ * @return Iterator on all menu items + */ + public Iterator getMenuItemsIterator (); + + /** + * Shows this menu + *

+ * @param client Client instance + */ + public void show (final Client client); +} diff --git a/src/org/mxchange/addressbook/menu/MenuTools.java b/src/org/mxchange/addressbook/menu/MenuTools.java new file mode 100644 index 0000000..87f8c11 --- /dev/null +++ b/src/org/mxchange/addressbook/menu/MenuTools.java @@ -0,0 +1,75 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.menu; + +import java.util.Iterator; +import java.util.Map; +import org.mxchange.addressbook.BaseAddressbookSystem; +import org.mxchange.addressbook.menu.item.SelectableMenuItem; +import org.mxchange.jcore.exceptions.MenuInitializationException; + +/** + * Menu utilities + *

+ * @author Roland Haeder + */ +public class MenuTools extends BaseAddressbookSystem { + + /** + * Gets an array with all available access keys back from given menu map. + * This can later be handle to the client's enterChar() method. + *

+ * @param menus A Map with all menus and their entries + * @param menuType Menu type + * @return An array with available access chars + * @throws org.mxchange.jcore.exceptions.MenuInitializationException If the menu cannot be initialized + */ + public static char[] getAccessKeysFromMenuMap (final Map menus, final String menuType) throws MenuInitializationException { + // First search for the proper menu class + Menu menu = menus.get(menuType); + + // Is it there? + if (!(menu instanceof Menu)) { + // Not found + throw new MenuInitializationException(menu, menuType); + } + + // Get iterator + Iterator iterator = menu.getMenuItemsIterator(); + + // Init return array and counter 'i' + char[] accessKeys = new char[menu.getMenuItemsCount()]; + int i = 0; + + // Now "walk" through all menu entries + while (iterator.hasNext()) { + // Get item + SelectableMenuItem item = iterator.next(); + //* NOISY-DEBUG: */ logger.logDebug("item=" + item); + + // Get access key from item and add it to the array + accessKeys[i] = item.getAccessKey(); + //* NOISY-DEBUG: */ logger.logDebug("accessKeys[" + i + "]=" + accessKeys[i]); + + // Increment counter + i++; + } + + // Return finished array + return accessKeys; + } +} diff --git a/src/org/mxchange/addressbook/menu/item/BaseMenuItem.java b/src/org/mxchange/addressbook/menu/item/BaseMenuItem.java new file mode 100644 index 0000000..473e9e4 --- /dev/null +++ b/src/org/mxchange/addressbook/menu/item/BaseMenuItem.java @@ -0,0 +1,28 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.menu.item; + +import org.mxchange.addressbook.BaseAddressbookSystem; + +/** + * A general menu item class + *

+ * @author Roland Haeder + */ +public abstract class BaseMenuItem extends BaseAddressbookSystem { + +} diff --git a/src/org/mxchange/addressbook/menu/item/SelectableMenuItem.java b/src/org/mxchange/addressbook/menu/item/SelectableMenuItem.java new file mode 100644 index 0000000..59758f8 --- /dev/null +++ b/src/org/mxchange/addressbook/menu/item/SelectableMenuItem.java @@ -0,0 +1,49 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.menu.item; + +import org.mxchange.jcore.FrameworkInterface; +import org.mxchange.jcore.client.Client; + +/** + * A selectable menu item intereface + *

+ * @author Roland Haeder + */ +public interface SelectableMenuItem extends FrameworkInterface { + + /** + * Access key + *

+ * @return the accessKey + */ + public char getAccessKey (); + + /** + * Text to user + *

+ * @return the text + */ + public String getText (); + + /** + * Shows this menu item + *

+ * @param client Client instance + */ + public void show (final Client client); +}