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