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