2 * Copyright (C) 2015 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.application;
19 import java.io.IOException;
20 import java.sql.SQLException;
21 import java.text.MessageFormat;
22 import org.mxchange.addressbook.BaseAddressbookSystem;
23 import org.mxchange.addressbook.client.AddressbookClient;
24 import org.mxchange.addressbook.client.console.ConsoleClient;
25 import org.mxchange.addressbook.client.gui.SwingClient;
26 import org.mxchange.jcore.BaseFrameworkSystem;
27 import org.mxchange.jcore.application.Application;
28 import org.mxchange.jcore.client.Client;
29 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
30 import org.mxchange.jcore.manager.application.ApplicationManager;
33 * ============================================
34 * AddressbookApplication management:
35 * ============================================
37 * Inernet("public" service) and Intranet
40 * - Single-user local application
59 * - Categorization of contacts
62 * - Permanent storage in database
65 * - Multi-user web application
66 * - Local user registration / login / resend confirmation link / password
68 * - User groups (aka. teams)
69 * - Administration area (user role)
70 * + Create/edit/delete groups
71 * + Edit/delete/lock/unlock user
72 * + Assign user roles/rights
73 * - Allow other users / groups to view addressbook
75 * + Only some categories
77 * + Allow users/guests (not recommended)
78 * - XML export of addressbook and compressable (ZIP)
79 * - Form to contact other user/group without need of mail program
80 * + User can disabled this
81 * - Directory for ussers/groups (who allowed to be listed)
82 * + Simple click to add user to own addressbook
86 * - Multi-language support
88 * Version 2.2+:("socialized")
89 * - "Social login" (OpenID consumer)
90 * + Connect user account to social account
96 * ============================================
98 * ============================================
104 * + Initial tables: contacts, categories, contact_category
108 * + Additional tables: admins (?), admin_rights, groups,
109 * users, user_contacts, user_user_rights, user_category_rights,
113 * + Additional tables: languages (disable, enable language "pack" ?)
117 * + Additional tables: ???
119 * @author Roland Haeder
122 public class AddressbookApplication extends BaseAddressbookSystem implements Application {
127 public static final String APP_TITLE = "Adressbuch"; //NOI18N
130 * Application version
132 public static final String APP_VERSION = "0.0"; //NOI18N
135 * Console client is enabled by default
137 private boolean consoleClient = true;
140 * GUI client is disabled by default
142 private boolean guiClient = false;
145 * Protected constructor
146 * @throws java.io.IOException If any IO error occurs
148 protected AddressbookApplication () throws IOException {
149 // Init properties file
150 this.initProperties();
152 // Is the bundle initialized?
153 if (!isBundledInitialized()) {
154 // Temporary initialize default bundle
155 // TODO The enum Gender uses this
161 * Bootstraps application
164 public void doBootstrap () {
165 this.getLogger().debug("Initializing application ..."); //NOI18N
167 // Init client variable
168 Client client = null;
170 // Is console or Swing choosen?
171 if (this.isConsole()) {
173 this.getLogger().debug("Initializing console client ..."); //NOI18N
175 // Init console client instance
176 client = new ConsoleClient(this);
177 } else if (this.isGui()) {
179 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
181 // Init console instance
182 client = new SwingClient(this);
184 // Not client choosen
185 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
192 // Set client instance
193 this.setClient(client);
195 // The application is running at this point
196 this.getClient().enableIsRunning();
199 this.getLogger().trace("EXIT!"); //NOI18N
203 * Main loop of the application
206 public void doMainLoop () {
207 // Get client and cast it
208 AddressbookClient client = (AddressbookClient) this.getClient();
211 this.getLogger().trace("CALLED!"); //NOI18N
213 // TODO The application should be running now
214 // Output introduction
217 // Set current menu to main
218 client.setCurrentMenu("main"); //NOI18N
220 // --- Main loop starts here ---
221 while (this.getClient().isRunning()) {
222 // The application is still active, show menu selection
223 client.showCurrentMenu();
226 // Ask for user input and run proper method
227 client.doUserMenuChoice();
228 } catch (final UnhandledUserChoiceException ex) {
229 this.getLogger().catching(ex);
233 // Sleep a little to reduce system load
235 } catch (final InterruptedException ex) {
239 // --- Main loop ends here ---
242 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
246 * Shuts down the application.
249 public void doShutdown () throws SQLException, IOException {
251 this.getLogger().trace("CALLED!"); //NOI18N
254 this.getClient().doShutdown();
256 this.getLogger().info("End of program (last line)"); //NOI18N
261 * Enables console client by setting propper flag
263 private void enableConsoleClient () {
264 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
265 this.consoleClient = true;
266 this.guiClient = false;
270 * Enables GUI client by setting propper flag
272 private void enableGuiClient () {
273 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
274 this.consoleClient = false;
275 this.guiClient = true;
279 * Checks whether the client shoule be console client should be launched by
280 * checking if -console is set.
282 * @return Whether console client should be taken
284 private boolean isConsole () {
285 return this.consoleClient;
289 * Checks whether the client shoule be GUI client should be launched by
290 * checking if -gui is set.
292 * @return Whether GUI client should be taken
294 private boolean isGui () {
295 return this.guiClient;
299 * Parses all given arguments
301 * @param args Arguments from program launch
303 private void parseArguments (final String[] args) {
305 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
308 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
310 for (final String arg : args) {
313 case "-console": //NOI18N
314 enableConsoleClient();
317 case "-gui": //NOI18N
325 * Show introduction which depends on client
327 private void showIntro () {
329 this.getLogger().trace("CALLED!"); //NOI18N
331 // Let the client show it
332 this.getClient().showWelcome();
335 this.getLogger().trace("EXIT!"); //NOI18N
339 * Launches the application
341 * @param args Arguments handled to program
343 private void start (final String args[]) {
344 this.getLogger().info("Program is started."); //NOI18N
346 // Init properties file
347 this.initProperties();
348 } catch (final IOException ex) {
349 // Something bad happened
350 this.abortProgramWithException(ex);
354 this.parseArguments(args);
356 // Launch application
357 ApplicationManager.getSingeltonManager(this).start();
359 // Good bye, but this should not be reached ...
360 this.getLogger().warn("Unusual exit reached."); //NOI18N
363 } catch (final SQLException | IOException ex) {
364 this.abortProgramWithException(ex);
369 * Main method (entry point)
371 * @param args the command line arguments
373 public static void main (String[] args) {
376 new AddressbookApplication().start(args);
377 } catch (final IOException ex) {
379 BaseFrameworkSystem.getInstance().getLogger().catching(ex);
385 * Getter for printable application name
387 * @return A printable name
389 public static String printableTitle () {
390 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N