]> git.mxchange.org Git - jaddressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java
ea26bc1ec1bbe7117f389c6c3a2a0fcd9254202d
[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.io.IOException;
20 import java.text.MessageFormat;
21 import org.mxchange.addressbook.BaseAddressbookSystem;
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 BaseAddressbookSystem 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                 // Init bundle
152                 this.initBundle();
153         }
154
155         /**
156          * Bootstraps application
157          */
158         @Override
159         public void doBootstrap () {
160                 this.getLogger().debug("Initializing application ..."); //NOI18N
161
162                 // Init client variable
163                 Client client = null;
164
165                 // Is console or Swing choosen?
166                 if (this.isConsole()) {
167                         // Debug message
168                         this.getLogger().debug("Initializing console client ..."); //NOI18N
169
170                         // Init console client instance
171                         client = new ConsoleClient(this);
172                 } else if (this.isGui()) {
173                         // Debug message
174                         this.getLogger().debug("Initializing GUI (Swing) client ..."); //NOI18N
175
176                         // Init console instance
177                         client = new SwingClient(this);
178                 } else {
179                         // Not client choosen
180                         this.getLogger().error("No client choosen. Cannot launch."); //NOI18N
181                         System.exit(1);
182                 }
183
184                 // Init client
185                 client.init();
186
187                 // Set client instance
188                 this.setClient(client);
189
190                 // The application is running at this point
191                 this.getClient().enableIsRunning();
192
193                 // Trace message
194                 this.getLogger().trace("EXIT!"); //NOI18N
195         }
196
197         /**
198          * Main loop of the application
199          */
200         @Override
201         public void doMainLoop () {
202                 // Get client and cast it
203                 AddressbookClient client = (AddressbookClient) this.getClient();
204
205                 // Debug message
206                 this.getLogger().trace("CALLED!"); //NOI18N
207
208                 // @TODO The application should be running now
209                 // Output introduction
210                 this.showIntro();
211
212                 // Set current menu to main
213                 client.setCurrentMenu("main"); //NOI18N
214
215                 // --- Main loop starts here ---
216                 while (this.getClient().isRunning()) {
217                         // The application is still active, show menu selection
218                         client.showCurrentMenu();
219
220                         try {
221                                 // Ask for user input and run proper method
222                                 client.doUserMenuChoice();
223                         } catch (final UnhandledUserChoiceException ex) {
224                                 this.getLogger().catching(ex);
225                         }
226
227                         try {
228                                 // Sleep a little to reduce system load
229                                 Thread.sleep(100);
230                         } catch (final InterruptedException ex) {
231                                 // Ignore it
232                         }
233                 }
234                 // --- Main loop ends here ---
235
236                 // Debug message
237                 this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
238         }
239
240         /**
241          * Shuts down the application.
242          */
243         @Override
244         public void doShutdown () {
245                 // Trace message
246                 this.getLogger().trace("CALLED!"); //NOI18N
247                 
248                 // Shutdown client
249                 this.getClient().doShutdown();
250                 
251                 this.getLogger().info("End of program (last line)"); //NOI18N
252                 System.exit(0);
253         }
254
255         /**
256          * Enables console client by setting propper flag
257          */
258         private void enableConsoleClient () {
259                 this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
260                 this.consoleClient = true;
261                 this.guiClient = false;
262         }
263
264         /**
265          * Enables GUI client by setting propper flag
266          */
267         private void enableGuiClient () {
268                 this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
269                 this.consoleClient = false;
270                 this.guiClient = true;
271         }
272
273         /**
274          * Checks whether the client shoule be console client should be launched by
275          * checking if -console is set.
276          *
277          * @return Whether console client should be taken
278          */
279         private boolean isConsole () {
280                 return this.consoleClient;
281         }
282
283         /**
284          * Checks whether the client shoule be GUI client should be launched by
285          * checking if -gui is set.
286          *
287          * @return Whether GUI client should be taken
288          */
289         private boolean isGui () {
290                 return this.guiClient;
291         }
292
293         /**
294          * Parses all given arguments
295          *
296          * @param args Arguments from program launch
297          */
298         private void parseArguments (final String[] args) {
299                 // Trace message
300                 this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
301
302                 // Debug message
303                 this.getLogger().debug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
304
305                 for (final String arg : args) {
306                         // Switch on it
307                         switch (arg) {
308                                 case "-console": //NOI18N
309                                         enableConsoleClient();
310                                         break;
311
312                                 case "-gui": //NOI18N
313                                         enableGuiClient();
314                                         break;
315                         }
316                 }
317         }
318
319         /**
320          * Show introduction which depends on client
321          */
322         private void showIntro () {
323                 // Trace message
324                 this.getLogger().trace("CALLED!"); //NOI18N
325
326                 // Let the client show it
327                 this.getClient().showWelcome();
328
329                 // Trace message
330                 this.getLogger().trace("EXIT!"); //NOI18N
331         }
332
333         /**
334          * Launches the application
335          *
336          * @param args Arguments handled to program
337          */
338         private void start (final String args[]) {
339                 this.getLogger().info("Program is started."); //NOI18N
340
341                 // Parse arguments
342                 this.parseArguments(args);
343
344                 // Launch application
345                 ApplicationManager.getManager(this).start();
346
347                 // Good bye, but this should not be reached ...
348                 this.getLogger().warn("Unusual exit reached."); //NOI18N
349                 this.doShutdown();
350         }
351
352         /**
353          * Main method (entry point)
354          *
355          * @param args the command line arguments
356          */
357         public static void main (String[] args) {
358                 try {
359                         // Start application
360                         new AddressbookApplication().start(args);
361                 } catch (final IOException ex) {
362                         // Get instance
363                         BaseFrameworkSystem.getInstance().getLogger().catching(ex);
364                         System.exit(1);
365                 }
366         }
367
368         /**
369          * Getter for printable application name
370          *
371          * @return A printable name
372          */
373         public static String printableTitle () {
374                 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
375         }
376 }