]> git.mxchange.org Git - addressbook-lib.git/blob - src/org/mxchange/addressbook/client/BaseAddressbookClient.java
ignored for internationalization
[addressbook-lib.git] / src / org / mxchange / addressbook / client / BaseAddressbookClient.java
1 /*
2  * Copyright (C) 2015 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.HashMap;
22 import java.util.Map;
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import org.mxchange.addressbook.facade.contact.AddressbookContactFacade;
27 import org.mxchange.addressbook.facade.contact.ContactFacade;
28 import org.mxchange.addressbook.menu.Menu;
29 import org.mxchange.jcore.client.BaseClient;
30 import org.mxchange.jcore.client.Client;
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 Haeder 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 logger;
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 logger
70                         this.logger = (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 final String getCurrentMenu () {
83                 return this.currentMenu;
84         }
85
86         @Override
87         public final 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          * @return Menu or null if not found
96          */
97         private Menu getMenu (final String menuType) {
98                 // Default is not found
99                 Menu menu = null;
100
101                 // Check array
102                 if (this.getMenus().containsKey(menuType)) {
103                         // Found!
104                         menu = this.getMenus().get(menuType);
105                 }
106
107                 // Return it
108                 return menu;
109         }
110
111         /**
112          * Logs exception and exits program
113          * <p>
114          * @param throwable Throwable
115          */
116         protected void abortProgramWithException (final Throwable throwable) {
117                 // Log exception
118                 this.logException(throwable);
119                 
120                 // Abort here
121                 System.exit(1);
122         }
123
124         /**
125          * Fills menu map with swing menus
126          */
127         protected abstract void fillMenuMap ();
128
129         /**
130          * Getter for logger instance
131          * <p>
132          * @return Logger instance
133          */
134         protected LoggerBeanLocal getLogger () {
135                 return this.logger;
136         }
137
138         /**
139          * Getter for menus map
140          * <p>
141          * @return Map of all menus
142          */
143         protected final Map<String, Menu> getMenus () {
144                 return this.menus;
145         }
146
147         /**
148          * Initializes contact manager
149          * <p>
150          * @throws java.sql.SQLException If any SQL error occurs
151          */
152         protected void initContactManager () throws SQLException {
153                 // Trace message
154                 this.getLogger().logTrace("CALLED!"); //NOI18N
155
156                 // Debug message
157                 this.getLogger().logDebug("Initializing contact manager ..."); //NOI18N
158
159                 // Init contact facade with console client
160                 // TODO Static initial amount of contacts
161                 ContactFacade facade = new AddressbookContactFacade((Client) this);
162
163                 // Set it here
164                 this.setFacade(facade);
165
166                 // Debug message
167                 this.getLogger().logDebug("Contact manager has been initialized."); //NOI18N
168
169                 // Trace message
170                 this.getLogger().logTrace("EXIT!"); //NOI18N
171         }
172
173         /**
174          * Logs an exception
175          * <p>
176          * @param throwable Throwable
177          */
178         protected void logException (final Throwable throwable) {
179                 // Deligate to logger
180                 this.getLogger().logException(throwable);
181         }
182
183         /**
184          * Shows given menu
185          * <p>
186          * @param menuType Given menu to show
187          */
188         protected void showMenu (final String menuType) {
189                 // Trace message
190                 this.getLogger().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
191
192                 // Get menu from type
193                 Menu menu = this.getMenu(menuType);
194
195                 // Is the menu set?
196                 if (!(menu instanceof Menu)) {
197                         // Not found
198                         // TODO Own exception?
199                         throw new NullPointerException(MessageFormat.format("Menu '{0}' not found.", menuType)); //NOI18N
200                 }
201
202                 // Show menu
203                 menu.show(this);
204
205                 // Trace message
206                 this.getLogger().logTrace("EXIT!"); //NOI18N
207         }
208 }