]> git.mxchange.org Git - addressbook-swing.git/blob - src/org/mxchange/addressbook/client/BaseAddressbookClient.java
Updated copyright year
[addressbook-swing.git] / src / org / mxchange / addressbook / client / BaseAddressbookClient.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.client;
18
19 import java.sql.SQLException;
20 import java.text.MessageFormat;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
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.jcore.client.Client;
32 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
33 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
34
35 /**
36  * A general addressbook client
37  * <p>
38  * @author Roland Haeder TODO: Find better name
39  */
40 public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient {
41
42         /**
43          * Current menu choice
44          */
45         private String currentMenu;
46
47         /**
48          * Logger instance
49          */
50         @Log
51         private LoggerBeanLocal loggerBeanLocal;
52
53         /**
54          * Menu system
55          */
56         private final Map<String, Menu> menus;
57
58         /**
59          * No instances can be created of this class
60          */
61         protected BaseAddressbookClient () {
62                 // Init menu map
63                 this.menus = new HashMap<>(10);
64
65                 // Try it
66                 try {
67                         // Get context
68                         Context context = new InitialContext();
69
70                         // Lookup loggerBeanLocal
71                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
72                 } catch (final NamingException ex) {
73                         // Continue to throw
74                         throw new RuntimeException(ex);
75                 }
76         }
77
78         /**
79          * Current menu choice
80          * <p>
81          * @return the currentMenu
82          */
83         public String getCurrentMenu () {
84                 return this.currentMenu;
85         }
86
87         @Override
88         public void setCurrentMenu (final String currentMenu) {
89                 this.currentMenu = currentMenu;
90         }
91
92         /**
93          * "Getter" for given menu type
94          * <p>
95          * @param menuType Menu type instance to return
96          * <p>
97          * @return Menu or null if not found
98          */
99         private Menu getMenu (final String menuType) {
100                 // Default is not found
101                 Menu menu = null;
102
103                 // Check array
104                 if (this.getMenus().containsKey(menuType)) {
105                         // Found!
106                         menu = this.getMenus().get(menuType);
107                 }
108
109                 // Return it
110                 return menu;
111         }
112
113         /**
114          * Logs exception and exits program
115          * <p>
116          * @param throwable Throwable
117          */
118         protected void abortProgramWithException (final Throwable throwable) {
119                 // Log exception
120                 this.logException(throwable);
121
122                 // Abort here
123                 System.exit(1);
124         }
125
126         /**
127          * Fills menu map with swing menus
128          */
129         protected abstract void fillMenuMap ();
130
131         /**
132          * Getter for loggerBeanLocal instance
133          * <p>
134          * @return Logger instance
135          */
136         protected LoggerBeanLocal getLoggerBeanLocal () {
137                 return this.loggerBeanLocal;
138         }
139
140         /**
141          * Getter for menus map
142          * <p>
143          * @return Map of all menus
144          */
145         protected Map<String, Menu> getMenus () {
146                 return Collections.unmodifiableMap(this.menus);
147         }
148
149         /**
150          * Initializes contact manager
151          * <p>
152          * @throws java.sql.SQLException If any SQL error occurs
153          */
154         protected void initContactManager () throws SQLException {
155                 // Trace message
156                 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
157
158                 // Debug message
159                 this.getLoggerBeanLocal().logDebug("Initializing contact manager ..."); //NOI18N
160
161                 // Init contact facade with console client
162                 // TODO Static initial amount of contacts
163                 ContactFacade facade = new AddressbookContactFacade((Client) this);
164
165                 // Set it here
166                 this.setFacade(facade);
167
168                 // Debug message
169                 this.getLoggerBeanLocal().logDebug("Contact manager has been initialized."); //NOI18N
170
171                 // Trace message
172                 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
173         }
174
175         /**
176          * Logs an exception
177          * <p>
178          * @param throwable Throwable
179          */
180         protected void logException (final Throwable throwable) {
181                 // Deligate to loggerBeanLocal
182                 this.getLoggerBeanLocal().logException(throwable);
183         }
184
185         /**
186          * Shows given menu
187          * <p>
188          * @param menuType Given menu to show
189          */
190         protected void showMenu (final String menuType) {
191                 // Trace message
192                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
193
194                 // Get menu from type
195                 Menu menu = this.getMenu(menuType);
196
197                 // Is the menu set?
198                 if (!(menu instanceof Menu)) {
199                         // Not found
200                         // TODO Own exception?
201                         throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
202                 }
203
204                 // Show menu
205                 menu.show(this);
206
207                 // Trace message
208                 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
209         }
210 }