]> git.mxchange.org Git - addressbook-swing.git/blob - src/org/mxchange/addressbook/application/AddressbookApplication.java
Merge branch 'master' of /media/quix0r/INTENSO/Java Project/addressbook
[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.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
186                         try {
187                                 this.doShutdown();
188                         } catch (final SQLException | IOException ex) {
189                                 // Abort here
190                                 this.abortProgramWithException(ex);
191                         }
192
193                         // Bye ...
194                         System.exit(1);
195                 }
196
197                 // Init client
198                 client.init();
199
200                 // Set client instance
201                 this.setClient(client);
202
203                 // The application is running at this point
204                 this.getClient().enableIsRunning();
205
206                 // Trace message
207                 this.getLogger().trace("EXIT!"); //NOI18N
208         }
209
210         /**
211          * Main loop of the application
212          */
213         @Override
214         public void doMainLoop () {
215                 // Get client and cast it
216                 AddressbookClient client = (AddressbookClient) this.getClient();
217
218                 // Debug message
219                 this.getLogger().trace("CALLED!"); //NOI18N
220
221                 // TODO The application should be running now
222                 // Output introduction
223                 this.showIntro();
224
225                 // Set current menu to main
226                 client.setCurrentMenu("main"); //NOI18N
227
228                 // --- Main loop starts here ---
229                 while (this.getClient().isRunning()) {
230                         // The application is still active, show menu selection
231                         client.showCurrentMenu();
232
233                         try {
234                                 // Ask for user input and run proper method
235                                 client.doUserMenuChoice();
236                         } catch (final UnhandledUserChoiceException ex) {
237                                 this.getLogger().catching(ex);
238                         }
239
240                         try {
241                                 // Sleep a little to reduce system load
242                                 Thread.sleep(100);
243                         } catch (final InterruptedException ex) {
244                                 // Ignore it
245                         }
246                 }
247                 // --- Main loop ends here ---
248
249                 // Debug message
250                 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
251         }
252
253         /**
254          * Shuts down the application.
255          */
256         @Override
257         public void doShutdown () throws SQLException, IOException {
258                 // Trace message
259                 this.getLogger().trace("CALLED!"); //NOI18N
260
261                 // Shutdown client
262                 this.getClient().doShutdown();
263
264                 this.getLogger().info("End of program (last line)"); //NOI18N
265                 System.exit(0);
266         }
267
268         /**
269          * Enables console client by setting propper flag
270          */
271         private void enableConsoleClient () {
272                 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
273                 this.consoleClient = true;
274                 this.guiClient = false;
275         }
276
277         /**
278          * Enables GUI client by setting propper flag
279          */
280         private void enableGuiClient () {
281                 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
282                 this.consoleClient = false;
283                 this.guiClient = true;
284         }
285
286         /**
287          * Checks whether the client shoule be console client should be launched by
288          * checking if -console is set.
289          *
290          * @return Whether console client should be taken
291          */
292         private boolean isConsole () {
293                 return this.consoleClient;
294         }
295
296         /**
297          * Checks whether the client shoule be GUI client should be launched by
298          * checking if -gui is set.
299          *
300          * @return Whether GUI client should be taken
301          */
302         private boolean isGui () {
303                 return this.guiClient;
304         }
305
306         /**
307          * Parses all given arguments
308          *
309          * @param args Arguments from program launch
310          */
311         private void parseArguments (final String[] args) {
312                 // Trace message
313                 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
314
315                 // Debug message
316                 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
317
318                 for (final String arg : args) {
319                         // Switch on it
320                         switch (arg) {
321                                 case "-console": //NOI18N
322                                         enableConsoleClient();
323                                         break;
324
325                                 case "-gui": //NOI18N
326                                         enableGuiClient();
327                                         break;
328                         }
329                 }
330         }
331
332         /**
333          * Show introduction which depends on client
334          */
335         private void showIntro () {
336                 // Trace message
337                 this.getLogger().trace("CALLED!"); //NOI18N
338
339                 // Let the client show it
340                 this.getClient().showWelcome();
341
342                 // Trace message
343                 this.getLogger().trace("EXIT!"); //NOI18N
344         }
345
346         /**
347          * Launches the application
348          *
349          * @param args Arguments handled to program
350          */
351         private void start (final String args[]) {
352                 this.getLogger().info("Program is started."); //NOI18N
353                 try {
354                         // Init properties file
355                         this.initProperties();
356                 } catch (final IOException ex) {
357                         // Something bad happened
358                         this.abortProgramWithException(ex);
359                 }
360
361                 // Parse arguments
362                 this.parseArguments(args);
363
364                 // Launch application
365                 ApplicationManager.getSingeltonManager(this).start();
366
367                 // Good bye, but this should not be reached ...
368                 this.getLogger().warn("Unusual exit reached."); //NOI18N
369                 try {
370                         this.doShutdown();
371                 } catch (final SQLException | IOException ex) {
372                         this.abortProgramWithException(ex);
373                 }
374         }
375
376         /**
377          * Main method (entry point)
378          *
379          * @param args the command line arguments
380          */
381         public static void main (String[] args) {
382                 try {
383                         // Start application
384                         new AddressbookApplication().start(args);
385                 } catch (final IOException ex) {
386                         // Get instance
387                         BaseFrameworkSystem.getInstance().getLogger().catching(ex);
388                         System.exit(1);
389                 }
390         }
391
392         /**
393          * Getter for printable application name
394          *
395          * @return A printable name
396          */
397         public static String printableTitle () {
398                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
399         }
400 }