2 * Copyright (C) 2016 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.client;
19 import java.sql.SQLException;
20 import java.text.MessageFormat;
21 import java.util.Collections;
22 import java.util.HashMap;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import org.mxchange.addressbook.facade.contact.AddressbookContactFacade;
28 import org.mxchange.addressbook.facade.contact.ContactFacade;
29 import org.mxchange.addressbook.menu.Menu;
30 import org.mxchange.jcore.client.BaseClient;
31 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
32 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
35 * A general addressbook client
37 * @author Roland Haeder<roland@mxchange.org> TODO: Find better name
39 public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient {
44 private String currentMenu;
50 private LoggerBeanLocal loggerBeanLocal;
55 private final Map<String, Menu> menus;
58 * No instances can be created of this class
60 protected BaseAddressbookClient () {
62 this.menus = new HashMap<>(10);
67 Context context = new InitialContext();
69 // Lookup loggerBeanLocal
70 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
71 } catch (final NamingException ex) {
73 throw new RuntimeException(ex);
80 * @return the currentMenu
82 public String getCurrentMenu () {
83 return this.currentMenu;
87 public void setCurrentMenu (final String currentMenu) {
88 this.currentMenu = currentMenu;
92 * "Getter" for given menu type
94 * @param menuType Menu type instance to return
96 * @return Menu or null if not found
98 private Menu getMenu (final String menuType) {
99 // Default is not found
103 if (this.getMenus().containsKey(menuType)) {
105 menu = this.getMenus().get(menuType);
113 * Logs exception and exits program
115 * @param throwable Throwable
117 protected void abortProgramWithException (final Throwable throwable) {
119 this.logException(throwable);
126 * Fills menu map with swing menus
128 protected abstract void fillMenuMap ();
131 * Getter for loggerBeanLocal instance
133 * @return Logger instance
135 protected LoggerBeanLocal getLoggerBeanLocal () {
136 return this.loggerBeanLocal;
140 * Getter for menus map
142 * @return Map of all menus
144 protected Map<String, Menu> getMenus () {
145 return Collections.unmodifiableMap(this.menus);
149 * Initializes contact manager
151 * @throws java.sql.SQLException If any SQL error occurs
153 protected void initContactManager () throws SQLException {
155 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
158 this.getLoggerBeanLocal().logDebug("Initializing contact manager ..."); //NOI18N
160 // Init contact facade with console client
161 // TODO Static initial amount of contacts
162 ContactFacade facade = new AddressbookContactFacade(this);
165 this.setFacade(facade);
168 this.getLoggerBeanLocal().logDebug("Contact manager has been initialized."); //NOI18N
171 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
177 * @param throwable Throwable
179 protected void logException (final Throwable throwable) {
180 // Deligate to loggerBeanLocal
181 this.getLoggerBeanLocal().logException(throwable);
187 * @param menuType Given menu to show
189 protected void showMenu (final String menuType) {
191 this.getLoggerBeanLocal().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
193 // Get menu from type
194 Menu menu = this.getMenu(menuType);
197 if (!(menu instanceof Menu)) {
199 // TODO Own exception?
200 throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
207 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N