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