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.client.AddressbookClient;
23 import org.mxchange.addressbook.client.console.ConsoleClient;
24 import org.mxchange.addressbook.client.gui.SwingClient;
25 import org.mxchange.jcore.BaseFrameworkSystem;
26 import org.mxchange.jcore.application.Application;
27 import org.mxchange.jcore.client.Client;
28 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
29 import org.mxchange.jcore.manager.application.ApplicationManager;
32 * ============================================
33 * AddressbookApplication management:
34 * ============================================
36 * Inernet("public" service) and Intranet
39 * - Single-user local application
58 * - Categorization of contacts
61 * - Permanent storage in database
64 * - Multi-user web application
65 * - Local user registration / login / resend confirmation link / password
67 * - User groups (aka. teams)
68 * - Administration area (user role)
69 * + Create/edit/delete groups
70 * + Edit/delete/lock/unlock user
71 * + Assign user roles/rights
72 * - Allow other users / groups to view addressbook
74 * + Only some categories
76 * + Allow users/guests (not recommended)
77 * - XML export of addressbook and compressable (ZIP)
78 * - Form to contact other user/group without need of mail program
79 * + User can disabled this
80 * - Directory for ussers/groups (who allowed to be listed)
81 * + Simple click to add user to own addressbook
85 * - Multi-language support
87 * Version 2.2+:("socialized")
88 * - "Social login" (OpenID consumer)
89 * + Connect user account to social account
95 * ============================================
97 * ============================================
103 * + Initial tables: contacts, categories, contact_category
107 * + Additional tables: admins (?), admin_rights, groups,
108 * users, user_contacts, user_user_rights, user_category_rights,
112 * + Additional tables: languages (disable, enable language "pack" ?)
116 * + Additional tables: ???
118 * @author Roland Haeder
121 public class AddressbookApplication extends BaseFrameworkSystem implements Application {
126 public static final String APP_TITLE = "Adressbuch"; //NOI18N
129 * Application version
131 public static final String APP_VERSION = "0.0"; //NOI18N
134 * Console client is enabled by default
136 private boolean consoleClient = true;
139 * GUI client is disabled by default
141 private boolean guiClient = false;
144 * Protected constructor
145 * @throws java.io.IOException If any IO error occurs
147 protected AddressbookApplication () throws IOException {
148 // Init properties file
149 this.initProperties();
151 // Is the bundle initialized?
152 if (!isBundledInitialized()) {
153 // Temporary initialize default bundle
154 // TODO The enum Gender uses this
160 * Bootstraps application
163 public void doBootstrap () {
164 this.getLogger().debug("Initializing application ..."); //NOI18N
166 // Init client variable
169 // Is console or Swing choosen?
170 if (this.isConsole()) {
172 this.getLogger().debug("Initializing console client ..."); //NOI18N
174 // Init console client instance
175 client = new ConsoleClient(this);
176 } else if (this.isGui()) {
178 this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
180 // Init console instance
181 client = new SwingClient(this);
183 // Not client choosen
184 this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
188 } catch (final SQLException | IOException ex) {
190 this.abortProgramWithException(ex);
200 // Set client instance
201 this.setClient(client);
203 // The application is running at this point
204 this.getClient().enableIsRunning();
207 this.getLogger().trace("EXIT!"); //NOI18N
211 * Main loop of the application
214 public void doMainLoop () {
215 // Get client and cast it
216 AddressbookClient client = (AddressbookClient) this.getClient();
219 this.getLogger().trace("CALLED!"); //NOI18N
221 // TODO The application should be running now
222 // Output introduction
225 // Set current menu to main
226 client.setCurrentMenu("main"); //NOI18N
228 // --- Main loop starts here ---
229 while (this.getClient().isRunning()) {
230 // The application is still active, show menu selection
231 client.showCurrentMenu();
234 // Ask for user input and run proper method
235 client.doUserMenuChoice();
236 } catch (final UnhandledUserChoiceException ex) {
237 this.getLogger().catching(ex);
241 // Sleep a little to reduce system load
243 } catch (final InterruptedException ex) {
247 // --- Main loop ends here ---
250 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
254 * Shuts down the application.
257 public void doShutdown () throws SQLException, IOException {
259 this.getLogger().trace("CALLED!"); //NOI18N
262 this.getClient().doShutdown();
264 this.getLogger().info("End of program (last line)"); //NOI18N
269 * Enables console client by setting propper flag
271 private void enableConsoleClient () {
272 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
273 this.consoleClient = true;
274 this.guiClient = false;
278 * Enables GUI client by setting propper flag
280 private void enableGuiClient () {
281 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
282 this.consoleClient = false;
283 this.guiClient = true;
287 * Checks whether the client shoule be console client should be launched by
288 * checking if -console is set.
290 * @return Whether console client should be taken
292 private boolean isConsole () {
293 return this.consoleClient;
297 * Checks whether the client shoule be GUI client should be launched by
298 * checking if -gui is set.
300 * @return Whether GUI client should be taken
302 private boolean isGui () {
303 return this.guiClient;
307 * Parses all given arguments
309 * @param args Arguments from program launch
311 private void parseArguments (final String[] args) {
313 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
316 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
318 for (final String arg : args) {
321 case "-console": //NOI18N
322 enableConsoleClient();
325 case "-gui": //NOI18N
333 * Show introduction which depends on client
335 private void showIntro () {
337 this.getLogger().trace("CALLED!"); //NOI18N
339 // Let the client show it
340 this.getClient().showWelcome();
343 this.getLogger().trace("EXIT!"); //NOI18N
347 * Launches the application
349 * @param args Arguments handled to program
351 private void start (final String args[]) {
352 this.getLogger().info("Program is started."); //NOI18N
354 // Init properties file
355 this.initProperties();
356 } catch (final IOException ex) {
357 // Something bad happened
358 this.abortProgramWithException(ex);
362 this.parseArguments(args);
364 // Launch application
365 ApplicationManager.getSingeltonManager(this).start();
367 // Good bye, but this should not be reached ...
368 this.getLogger().warn("Unusual exit reached."); //NOI18N
371 } catch (final SQLException | IOException ex) {
372 this.abortProgramWithException(ex);
377 * Main method (entry point)
379 * @param args the command line arguments
381 public static void main (String[] args) {
384 new AddressbookApplication().start(args);
385 } catch (final IOException ex) {
387 BaseFrameworkSystem.getInstance().getLogger().catching(ex);
393 * Getter for printable application name
395 * @return A printable name
397 public static String printableTitle () {
398 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N