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