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