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