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 java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.mxchange.addressbook.BaseAddressbookSystem;
25 import org.mxchange.addressbook.client.AddressbookClient;
26 import org.mxchange.addressbook.client.console.ConsoleClient;
27 import org.mxchange.addressbook.client.gui.SwingClient;
28 import org.mxchange.jcore.BaseFrameworkSystem;
29 import org.mxchange.jcore.application.Application;
30 import org.mxchange.jcore.client.Client;
31 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
32 import org.mxchange.jcore.manager.application.ApplicationManager;
35 * ============================================
36 * AddressbookApplication management:
37 * ============================================
39 * Inernet("public" service) and Intranet
42 * - Single-user local application
61 * - Categorization of contacts
64 * - Permanent storage in database
67 * - Multi-user web application
68 * - Local user registration / login / resend confirmation link / password
70 * - User groups (aka. teams)
71 * - Administration area (user role)
72 * + Create/edit/delete groups
73 * + Edit/delete/lock/unlock user
74 * + Assign user roles/rights
75 * - Allow other users / groups to view addressbook
77 * + Only some categories
79 * + Allow users/guests (not recommended)
80 * - XML export of addressbook and compressable (ZIP)
81 * - Form to contact other user/group without need of mail program
82 * + User can disabled this
83 * - Directory for ussers/groups (who allowed to be listed)
84 * + Simple click to add user to own addressbook
88 * - Multi-language support
90 * Version 2.2+:("socialized")
91 * - "Social login" (OpenID consumer)
92 * + Connect user account to social account
98 * ============================================
100 * ============================================
106 * + Initial tables: contacts, categories, contact_category
110 * + Additional tables: admins (?), admin_rights, groups,
111 * users, user_contacts, user_user_rights, user_category_rights,
115 * + Additional tables: languages (disable, enable language "pack" ?)
119 * + Additional tables: ???
121 * @author Roland Haeder
124 public class AddressbookApplication extends BaseAddressbookSystem implements Application {
129 public static final String APP_TITLE = "Adressbuch"; //NOI18N
132 * Application version
134 public static final String APP_VERSION = "0.0"; //NOI18N
137 * Console client is enabled by default
139 private boolean consoleClient = true;
142 * GUI client is disabled by default
144 private boolean guiClient = false;
147 * Protected constructor
148 * @throws java.io.IOException If any IO error occurs
150 protected AddressbookApplication () throws IOException {
151 // Init properties file
152 this.initProperties();
154 // Is the bundle initialized?
155 if (!isBundledInitialized()) {
156 // Temporary initialize default bundle
157 // TODO The enum Gender uses this
163 * Bootstraps application
166 public void doBootstrap () {
167 this.getLogger().debug("Initializing application ..."); //NOI18N
169 // Init client variable
172 // Is console or Swing choosen?
173 if (this.isConsole()) {
175 this.getLogger().debug("Initializing console client ..."); //NOI18N
177 // Init console client instance
178 client = new ConsoleClient(this);
179 } else if (this.isGui()) {
181 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
183 // Init console instance
184 client = new SwingClient(this);
186 // Not client choosen
187 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
190 } catch (final SQLException | IOException ex) {
192 this.abortProgramWithException(ex);
202 // Set client instance
203 this.setClient(client);
205 // The application is running at this point
206 this.getClient().enableIsRunning();
209 this.getLogger().trace("EXIT!"); //NOI18N
213 * Main loop of the application
216 public void doMainLoop () {
217 // Get client and cast it
218 AddressbookClient client = (AddressbookClient) this.getClient();
221 this.getLogger().trace("CALLED!"); //NOI18N
223 // TODO The application should be running now
224 // Output introduction
227 // Set current menu to main
228 client.setCurrentMenu("main"); //NOI18N
230 // --- Main loop starts here ---
231 while (this.getClient().isRunning()) {
232 // The application is still active, show menu selection
233 client.showCurrentMenu();
236 // Ask for user input and run proper method
237 client.doUserMenuChoice();
238 } catch (final UnhandledUserChoiceException ex) {
239 this.getLogger().catching(ex);
243 // Sleep a little to reduce system load
245 } catch (final InterruptedException ex) {
249 // --- Main loop ends here ---
252 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
256 * Shuts down the application.
259 public void doShutdown () throws SQLException, IOException {
261 this.getLogger().trace("CALLED!"); //NOI18N
264 this.getClient().doShutdown();
266 this.getLogger().info("End of program (last line)"); //NOI18N
271 * Enables console client by setting propper flag
273 private void enableConsoleClient () {
274 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
275 this.consoleClient = true;
276 this.guiClient = false;
280 * Enables GUI client by setting propper flag
282 private void enableGuiClient () {
283 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
284 this.consoleClient = false;
285 this.guiClient = true;
289 * Checks whether the client shoule be console client should be launched by
290 * checking if -console is set.
292 * @return Whether console client should be taken
294 private boolean isConsole () {
295 return this.consoleClient;
299 * Checks whether the client shoule be GUI client should be launched by
300 * checking if -gui is set.
302 * @return Whether GUI client should be taken
304 private boolean isGui () {
305 return this.guiClient;
309 * Parses all given arguments
311 * @param args Arguments from program launch
313 private void parseArguments (final String[] args) {
315 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
318 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
320 for (final String arg : args) {
323 case "-console": //NOI18N
324 enableConsoleClient();
327 case "-gui": //NOI18N
335 * Show introduction which depends on client
337 private void showIntro () {
339 this.getLogger().trace("CALLED!"); //NOI18N
341 // Let the client show it
342 this.getClient().showWelcome();
345 this.getLogger().trace("EXIT!"); //NOI18N
349 * Launches the application
351 * @param args Arguments handled to program
353 private void start (final String args[]) {
354 this.getLogger().info("Program is started."); //NOI18N
356 // Init properties file
357 this.initProperties();
358 } catch (final IOException ex) {
359 // Something bad happened
360 this.abortProgramWithException(ex);
364 this.parseArguments(args);
366 // Launch application
367 ApplicationManager.getSingeltonManager(this).start();
369 // Good bye, but this should not be reached ...
370 this.getLogger().warn("Unusual exit reached."); //NOI18N
373 } catch (final SQLException | IOException ex) {
374 this.abortProgramWithException(ex);
379 * Main method (entry point)
381 * @param args the command line arguments
383 public static void main (String[] args) {
386 new AddressbookApplication().start(args);
387 } catch (final IOException ex) {
389 BaseFrameworkSystem.getInstance().getLogger().catching(ex);
395 * Getter for printable application name
397 * @return A printable name
399 public static String printableTitle () {
400 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N