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