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.text.MessageFormat;
20 import org.mxchange.addressbook.BaseFrameworkSystem;
21 import org.mxchange.addressbook.client.Client;
22 import org.mxchange.addressbook.client.console.ConsoleClient;
23 import org.mxchange.addressbook.client.gui.SwingClient;
24 import org.mxchange.addressbook.exceptions.UnhandledUserChoiceException;
25 import org.mxchange.addressbook.manager.application.ApplicationManager;
28 * ============================================ AddressbookApplication
29 * management: ============================================
31 * Inernet("public" service) and Intranet
33 * Version 1.0+: - Single-user local application - Fields: + Gender + Surname +
34 * Family name + Company name + Street + number + ZIP code + City + Landline
35 * number + Fax number + Cell phone number + Email address + Birth day + Comment
36 * (?) - Edit own data - Add new contact - Edit contacts - Delete contacts -
37 * Categorization of contacts
39 * Version 1.1+: - Permanent storage in database
41 * Version 2.0+: - Multi-user web application - Local user registration / login
42 * / resend confirmation link / password recovery - User groups (aka. teams) -
43 * Administration area (user role) + Create/edit/delete groups +
44 * Edit/delete/lock/unlock user + Assign user roles/rights - Allow other users /
45 * groups to view addressbook + Full addressbook + Only some categories - VCard
46 * export + Allow users/guests (not recommended) - XML export of addressbook and
47 * compressable (ZIP) - Form to contact other user/group without need of mail
48 * program + User can disabled this - Directory for ussers/groups (who allowed
49 * to be listed) + Simple click to add user to own addressbook + Search form?
51 * Version 2.1+: - Multi-language support
53 * Version 2.2+:("socialized") - "Social login" (OpenID consumer) + Connect user
54 * account to social account + Sync own data? - "Social profile" + OpenID
55 * provider + RSS/activity feed
57 * ============================================ Time esitmation:
58 * ============================================ 1.0 (console): + 2 days
60 * 1.1 (database): + 2 day + Initial tables: contacts, categories,
63 * 2.0 (web): + 3 days + Additional tables: admins (?), admin_rights, groups,
64 * users, user_contacts, user_user_rights, user_category_rights,
66 * 2.1 (language) + 1 day + Additional tables: languages (disable, enable
69 * 2.2 (social): + 3 days + Additional tables: ???
71 * @author Roland Haeder
75 public class AddressbookApplication extends BaseFrameworkSystem implements Application {
80 public static final String APP_TITLE = "Adressbuch";
85 public static final String APP_VERSION = "0.0";
90 private static Application selfInstance;
93 * Console client is enabled by default
95 private boolean consoleClient = true;
98 * GUI client is disabled by default
100 private boolean guiClient = false;
103 * Protected constructor
105 protected AddressbookApplication () {
111 * Getter for printable application name
113 * @return A printable name
115 public static String printableTitle () {
116 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION);
120 * Bootstraps application
123 public void doBootstrap () {
124 this.getLogger().debug("Initializing application ...");
126 // Init client variable
127 Client client = null;
129 // Is console or Swing choosen?
130 if (this.isConsole()) {
132 this.getLogger().debug("Initializing console client ...");
134 // Init console client instance
135 client = new ConsoleClient(this);
136 } else if (this.isGui()) {
138 this.getLogger().debug("Initializing GUI (Swing) client ...");
140 // Init console instance
141 client = new SwingClient(this);
143 // Not client choosen
144 this.getLogger().error("No client choosen. Cannot launch.");
151 // Set client instance
152 this.setClient(client);
154 // The application is running at this point
155 this.getClient().enableIsRunning();
159 * Main loop of the application
162 public void doMainLoop () {
164 this.getLogger().trace("CALLED!");
166 // @TODO The application should be running now
167 // Output introduction
170 // Set current menu to main
171 this.getClient().setCurrentMenu("main");
173 // --- Main loop starts here ---
174 while (this.getClient().isRunning()) {
175 // The application is still active, show menu selection
176 this.getClient().showCurrentMenu();
179 // Ask for user input and run proper method
180 this.getClient().doUserMenuChoice();
181 } catch (final UnhandledUserChoiceException ex) {
182 this.getLogger().catching(ex);
186 // Sleep a little to reduce system load
188 } catch (final InterruptedException ex) {
192 // --- Main loop ends here ---
195 this.getLogger().debug("Main loop exit - shutting down ...");
199 * Enables console client by setting propper flag
201 private void enableConsoleClient () {
202 this.getLogger().debug("Enabling console client (may become optional client) ...");
203 this.consoleClient = true;
204 this.guiClient = false;
208 * Enables GUI client by setting propper flag
210 private void enableGuiClient () {
211 this.getLogger().debug("Enabling GUI client (may become new default client) ...");
212 this.consoleClient = false;
213 this.guiClient = true;
217 * Checks whether the client shoule be console client should be launched by
218 * checking if -console is set.
220 * @return Whether console client should be taken
222 private boolean isConsole () {
223 return this.consoleClient;
227 * Checks whether the client shoule be GUI client should be launched by
228 * checking if -gui is set.
230 * @return Whether GUI client should be taken
232 private boolean isGui () {
233 return this.guiClient;
237 * Parses all given arguments
239 * @param args Arguments from program launch
241 private void parseArguments (final String[] args) {
243 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length));
245 for (final String arg : args) {
249 enableConsoleClient();
260 * Show introduction which depends on client
262 private void showIntro () {
263 // Let the client show it
264 this.getClient().showWelcome();
268 * Launches the application
270 * @param args Arguments handled to program
272 private void start (final String args[]) {
273 this.getLogger().info("Program is started.");
276 this.parseArguments(args);
278 // Launch application
279 ApplicationManager.getManager(this).start();
281 // Good bye, but this should not be reached ...
282 this.getLogger().warn("Unusual exit reached.");
287 * Main method (entry point)
289 * @param args the command line arguments
291 public static void main (String[] args) {
293 new AddressbookApplication().start(args);
297 * Shuts down the application.
300 public void doShutdown () {
302 this.getClient().doShutdown();
304 this.getLogger().info("End of program (last line)");
309 * Getter for this application
311 * @return Instance from this application
313 public static final Application getInstance () {