]> git.mxchange.org Git - jaddressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java
Global commit: Changed spaces to tabs, because all Java files have tabs for indenting
[jaddressbook-lib.git] / Addressbook / src / org / mxchange / addressbook / application / AddressbookApplication.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.application;
18
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;
26
27 /**
28  * ============================================
29  * AddressbookApplication management:
30  * ============================================
31  * 
32  * Inernet("public" service) and Intranet
33  * 
34  * Version 1.0+:
35  * - Single-user local application
36  * - Fields:
37  *   + Gender
38  *   + Surname
39  *   + Family name
40  *   + Company name
41  *   + Street + number
42  *   + ZIP code
43  *   + City
44  *   + Landline number
45  *   + Fax number
46  *   + Cell phone number
47  *   + Email address
48  *   + Birth day
49  *   + Comment (?)
50  * - Edit own data
51  * - Add new contact
52  * - Edit contacts
53  * - Delete contacts
54  * - Categorization of contacts
55  * 
56  * Version 1.1+:
57  * - Permanent storage in database
58  * 
59  * Version 2.0+:
60  * - Multi-user web application
61  * - Local user registration / login / resend confirmation link / password
62  *   recovery
63  * - User groups (aka. teams)
64  * - Administration area (user role)
65  *   + Create/edit/delete groups
66  *   + Edit/delete/lock/unlock user
67  *   + Assign user roles/rights
68  * - Allow other users / groups to view addressbook
69  *   + Full addressbook
70  *   + Only some categories
71  * - VCard export
72  *   + Allow users/guests (not recommended)
73  * - XML export of addressbook and compressable (ZIP)
74  * - Form to contact other user/group without need of mail program
75  *   + User can disabled this
76  * - Directory for ussers/groups (who allowed to be listed)
77  *   + Simple click to add user to own addressbook
78  *   + Search form?
79  * 
80  * Version 2.1+:
81  * - Multi-language support
82  * 
83  * Version 2.2+:("socialized")
84  * - "Social login" (OpenID consumer)
85  *   + Connect user account to social account
86  *   + Sync own data?
87  * - "Social profile"
88  *   + OpenID provider
89  *   + RSS/activity feed 
90  * 
91  * ============================================
92  * Time esitmation:
93  * ============================================
94  * 1.0 (console):
95  *   + 2 days
96  * 
97  * 1.1 (database):
98  *   + 2 day
99  *   + Initial tables: contacts, categories, contact_category
100  * 
101  * 2.0 (web):
102  *   + 3 days
103  *   + Additional tables: admins (?), admin_rights, groups,
104  *    users, user_contacts, user_user_rights, user_category_rights, 
105  * 
106  * 2.1 (language)
107  *   + 1 day
108  *   + Additional tables: languages (disable, enable language "pack" ?)
109  * 
110  * 2.2 (social):
111  *   + 3 days
112  *   + Additional tables: ???
113  * 
114  * @author Roland Haeder
115  * @version 0.0
116  * @since 0.0
117  */
118 public class AddressbookApplication extends BaseFrameworkSystem implements Application {
119
120         /**
121          * Application title
122          */
123         public static final String APP_TITLE = "Adressbuch";
124
125         /**
126          * Application version
127          */
128         public static final String APP_VERSION = "0.0";
129
130         /**
131          * Console client is enabled by default
132          */
133         private boolean consoleClient = true;
134
135         /**
136          * GUI client is disabled by default
137          */
138         private boolean guiClient = false;
139
140         /**
141          * Getter for printable application name
142          *
143          * @return A printable name
144          */
145         public static String printableTitle () {
146                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION);
147         }
148
149         /**
150          * Bootstraps application
151          */
152         @Override
153         public void doBootstrap () {
154                 this.getLogger().debug("Initializing application ...");
155
156                 // Init client variable
157                 Client client = null;
158
159                 // Is console or Swing choosen?
160                 if (this.isConsole()) {
161                         // Debug message
162                         this.getLogger().debug("Initializing console client ...");
163
164                         // Init console client instance
165                         client = new ConsoleClient(this);
166                 } else if (this.isGui()) {
167                         // Debug message
168                         this.getLogger().debug("Initializing GUI (Swing) client ...");
169
170                         // Init console instance
171                         client = new SwingClient(this);
172                 } else {
173                         // Not client choosen
174                         this.getLogger().error("No client choosen. Cannot launch.");
175                         System.exit(1);
176                 }
177
178                 // Init client
179                 client.init();
180
181                 // Set client instance
182                 this.setClient(client);
183
184                 // The application is running at this point
185                 this.getClient().enableIsRunning();
186         }
187
188         /**
189          * Main loop of the application
190          */
191         @Override
192         public void doMainLoop () {
193                 // Debug message
194                 this.getLogger().trace("CALLED!");
195
196         // @TODO The application should be running now
197                 // Output introduction
198                 this.showIntro();
199
200                 // Set current menu to main
201                 this.getClient().setCurrentMenu("main");
202
203                 // --- Main loop starts here ---
204                 while (this.getClient().isRunning()) {
205                         // The application is still active, show menu selection
206                         this.getClient().showCurrentMenu();
207
208                         try {
209                                 // Ask for user input and run proper method
210                                 this.getClient().doUserMenuChoice();
211                         } catch (final UnhandledUserChoiceException ex) {
212                                 this.getLogger().catching(ex);
213                         }
214
215                         try {
216                                 // Sleep a little to reduce system load
217                                 Thread.sleep(100);
218                         } catch (final InterruptedException ex) {
219                                 // Ignore it
220                         }
221                 }
222         // --- Main loop ends here ---
223
224                 // Debug message
225                 this.getLogger().debug("Main loop exit - shutting down ...");
226         }
227
228         /**
229          * Enables console client by setting propper flag
230          */
231         private void enableConsoleClient () {
232                 this.getLogger().debug("Enabling console client (may become optional client) ...");
233                 this.consoleClient = true;
234                 this.guiClient = false;
235         }
236
237         /**
238          * Enables GUI client by setting propper flag
239          */
240         private void enableGuiClient () {
241                 this.getLogger().debug("Enabling GUI client (may become new default client) ...");
242                 this.consoleClient = false;
243                 this.guiClient = true;
244         }
245
246         /**
247          * Checks whether the client shoule be console client should be launched by
248          * checking if -console is set.
249          *
250          * @return Whether console client should be taken
251          */
252         private boolean isConsole () {
253                 return this.consoleClient;
254         }
255
256         /**
257          * Checks whether the client shoule be GUI client should be launched by
258          * checking if -gui is set.
259          *
260          * @return Whether GUI client should be taken
261          */
262         private boolean isGui () {
263                 return this.guiClient;
264         }
265
266         /**
267          * Parses all given arguments
268          *
269          * @param args Arguments from program launch
270          */
271         private void parseArguments (final String[] args) {
272                 // Debug message
273                 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length));
274
275                 for (final String arg : args) {
276                         // Switch on it
277                         switch (arg) {
278                                 case "-console":
279                                         enableConsoleClient();
280                                         break;
281
282                                 case "-gui":
283                                         enableGuiClient();
284                                         break;
285                         }
286                 }
287         }
288
289         /**
290          * Show introduction which depends on client
291          */
292         private void showIntro () {
293                 // Let the client show it
294                 this.getClient().showWelcome();
295         }
296
297         /**
298          * Launches the application
299          *
300          * @param args Arguments handled to program
301          */
302         private void start (final String args[]) {
303                 this.getLogger().info("Program is started.");
304
305                 // Parse arguments
306                 this.parseArguments(args);
307
308                 // Launch application
309                 ApplicationManager.getManager(this).start();
310
311                 // Good bye, but this should not be reached ...
312                 this.getLogger().warn("Unusual exit reached.");
313                 this.doShutdown();
314         }
315
316         /**
317          * Main method (entry point)
318          *
319          * @param args the command line arguments
320          */
321         public static void main (String[] args) {
322                 // Start application
323                 new AddressbookApplication().start(args);
324         }
325
326         /**
327          * Shuts down the application.
328          */
329         @Override
330         public void doShutdown () {
331                 // Shutdown client
332                 this.getClient().doShutdown();
333
334                 this.getLogger().info("End of program (last line)");
335                 System.exit(0);
336         }
337 }