]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java
A lot refacturings ...
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / manager / contact / ContactManager.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.manager.contact;
18
19 import java.text.MessageFormat;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import org.mxchange.addressbook.client.Client;
24 import org.mxchange.addressbook.contact.Contact;
25 import org.mxchange.addressbook.contact.Gender;
26 import org.mxchange.addressbook.database.frontend.contact.ContactDatabaseFrontend;
27 import org.mxchange.addressbook.database.frontend.contact.ContactWrapper;
28 import org.mxchange.addressbook.exceptions.BadTokenException;
29 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
30 import org.mxchange.addressbook.exceptions.UnhandledUserChoiceException;
31 import org.mxchange.addressbook.manager.BaseManager;
32
33 /**
34  * A manager for contacts, please note that this implementation loads the whole
35  * list into RAM.
36  *
37  * @author Roland Haeder
38  * @version 0.0
39  */
40 public class ContactManager extends BaseManager implements ManageableContact {
41
42         /**
43          * Column name list
44          */
45         private final List<String> columnNames;
46
47         /**
48          * A ContactWrapper instance
49          */
50         private final ContactWrapper contactDatabase;
51
52         /**
53          * Constructor which accepts maxContacts for maximum (initial) contacts and
54          * a client instance.
55          *
56          * @param client Client instance to use
57          */
58         public ContactManager (final Client client) {
59                 // Trace message
60                 this.getLogger().trace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
61
62                 // Make sure all parameters are set correctly
63                 if (client == null) {
64                         // Abort here
65                         throw new NullPointerException("client is null"); //NOI18N
66                 }
67
68                 // Set client instance
69                 this.setClient(client);
70
71                 // Init database connection
72                 this.contactDatabase = new ContactDatabaseFrontend(this);
73
74                 // Initialize list
75                 this.columnNames = new ArrayList<>(15);
76
77                 // And fill it
78                 this.fillColumnNamesFromBundle();
79
80                 // Debug message
81                 //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);
82         }
83
84         /**
85          * Adds given Contact instance to list
86          *
87          * @param contact Contact instance to add
88          */
89         @Override
90         public void addContact (final Contact contact)  throws ContactAlreadyAddedException {
91                 // Trace message
92                 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
93
94                 // Contact instance must not be null
95                 if (contact == null) {
96                         // Abort here
97                         throw new NullPointerException("contact is null"); //NOI18N
98                 }
99
100                 // Add it
101                 this.getContactDatabase().addContact(contact);
102
103                 // Trace message
104                 this.getLogger().trace("EXIT!"); //NOI18N
105         }
106
107         /**
108          * Let the user add a new other address
109          */
110         @Override
111         public void doAddOtherAddress () {
112                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
113         }
114
115         /**
116          * Let the user change address data
117          *
118          * @param contact Instance to change data
119          */
120         @Override
121         public void doChangeAddressData (final Contact contact) {
122                 // Trace message
123                 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
124
125                 // Contact must not be null
126                 if (contact == null) {
127                         // Abort here
128                         throw new NullPointerException("contact is null"); //NOI18N
129                 }
130
131                 // First display it again
132                 this.getClient().displayAddressBox(contact);
133
134                 // Is it own data?
135                 if (contact.isOwnContact()) {
136                         // Deligate to client
137                         this.getClient().doChangeOwnAddressData(contact);
138                 } else {
139                         // Other contact's address data to change
140                         throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
141                 }
142
143                 // Trace message
144                 this.getLogger().trace("EXIT!"); //NOI18N
145         }
146
147         /**
148          * Let the user change "name data"
149          *
150          * @param contact Instance to change data
151          */
152         @Override
153         public void doChangeNameData (final Contact contact) {
154                 // Trace message
155                 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
156
157                 // Contact must not be null
158                 if (contact == null) {
159                         // Abort here
160                         throw new NullPointerException("contact is null"); //NOI18N
161                 }
162
163                 // First display them again
164                 this.getClient().displayNameBox(contact);
165
166                 // Is this own data?
167                 if (contact.isOwnContact()) {
168                         // Re-ask own data
169                         this.getClient().doChangeOwnNameData(contact);
170                 } else {
171                         // Then re-ask them ...
172                         throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
173                 }
174
175                 // Trace message
176                 this.getLogger().trace("EXIT!"); //NOI18N
177         }
178
179         /**
180          * Let the user change other address
181          */
182         @Override
183         public void doChangeOtherAddress () {
184                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
185         }
186
187         /**
188          * Let the user change other data
189          *
190          * @param contact Instance to change data
191          * @todo Didn't handle birthday
192          */
193         @Override
194         public void doChangeOtherData (final Contact contact) {
195                 // Trace message
196                 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
197
198                 // Contact must not be null
199                 if (contact == null) {
200                         // Abort here
201                         throw new NullPointerException("contact is null"); //NOI18N
202                 }
203
204                 // First display them again
205                 this.getClient().displayOtherDataBox(contact);
206
207                 // Is this own data?
208                 if (contact.isOwnContact()) {
209                         // Re-ask own data
210                         this.getClient().doChangeOwnOtherData(contact);
211                 } else {
212                         // Then re-ask them ...
213                         throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
214                 }
215
216                 // Trace message
217                 this.getLogger().trace("EXIT!"); //NOI18N
218         }
219
220         /**
221          * Allows the user to change his/her own data
222          */
223         @Override
224         public void doChangeOwnData () {
225                 // Trace message
226                 this.getLogger().trace("CALLED!"); //NOI18N
227
228                 /*
229                  * First check if the user has registered own contact, before that
230                  * nothing can be changed.
231                  */
232                 if (!this.isOwnContactAdded()) {
233                         // Not added
234                         this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
235
236                         // Skip any below code
237                         return;
238                 }
239
240                 // Instance
241                 Contact contact = this.getOwnContact();
242
243                 // It must be found
244                 assert (contact instanceof Contact);
245
246                 // Display contact
247                 contact.show(this.getClient());
248
249                 try {
250                         // Ask user what to change
251                         this.getClient().userChooseChangeContactData(contact);
252                 } catch (final UnhandledUserChoiceException ex) {
253                         this.getLogger().catching(ex);
254                 }
255
256                 // Trace message
257                 this.getLogger().trace("EXIT!"); //NOI18N
258         }
259
260         /**
261          * Let the user delete other address
262          */
263         @Override
264         public void doDeleteOtherAddress () {
265                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
266         }
267
268         /**
269          * Asks user for own data
270          */
271         @Override
272         public void doEnterOwnData () throws ContactAlreadyAddedException {
273                 // Trace message
274                 this.getLogger().trace("CALLED!"); //NOI18N
275
276                 // Is own contact already added?
277                 if (this.isOwnContactAdded()) {
278                         // Don't continue here
279                         throw new ContactAlreadyAddedException();
280                 }
281
282                 // Deligate this call to the client
283                 Contact contact = this.getClient().doEnterOwnData();
284
285                 // Is it set?
286                 if (contact instanceof Contact) {
287                         // Add it to contact "book"
288                         this.registerContact(contact);
289                 }
290
291                 // Trace message
292                 this.getLogger().trace("EXIT!"); //NOI18N
293         }
294
295         @Override
296         public void doListContacts () {
297                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
298         }
299
300         @Override
301         public void doSearchContacts () {
302                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
303         }
304
305         /**
306          * Shuts down this contact manager
307          */
308         @Override
309         public void doShutdown () {
310                 // Trace message
311                 this.getLogger().trace("CALLED!"); //NOI18N
312
313                 // Shut down the database layer
314                 this.getContactDatabase().doShutdown();
315
316                 // Trace message
317                 this.getLogger().trace("EXIT!"); //NOI18N
318         }
319
320         /**
321          * Asks the user for his/her cellphone number
322          *
323          * @return User's cellphone number
324          */
325         @Override
326         public String enterOwnCellNumber () {
327                 // Trace message
328                 this.getLogger().trace("CALLED!"); //NOI18N
329
330                 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
331         }
332
333         /**
334          * Asks the user for his/her city's name
335          *
336          * @return City's name of the user
337          */
338         @Override
339         public String enterOwnCity () {
340                 // Trace message
341                 this.getLogger().trace("CALLED!"); //NOI18N
342
343                 return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
344         }
345
346         /**
347          * Asks the user for his/her city's name
348          *
349          * @return City's name of the user
350          */
351         @Override
352         public String enterOwnComment () {
353                 // Trace message
354                 this.getLogger().trace("CALLED!"); //NOI18N
355
356                 return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
357         }
358
359         /**
360          * Asks the user for his/her company name
361          *
362          * @return User's company name
363          */
364         @Override
365         public String enterOwnCompanyName () {
366                 // Trace message
367                 this.getLogger().trace("CALLED!"); //NOI18N
368
369                 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
370         }
371
372         /**
373          * Asks user for his/her own country code
374          *
375          * @return User's own country code
376          */
377         @Override
378         public String enterOwnCountryCode () {
379                 // Trace message
380                 this.getLogger().trace("CALLED!"); //NOI18N
381
382                 return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
383         }
384
385         /**
386          * Asks user for his/her own country code
387          *
388          * @return User's own country code
389          */
390         @Override
391         public String enterOwnEmailAddress () {
392                 // Trace message
393                 this.getLogger().trace("CALLED!"); //NOI18N
394
395                 return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
396         }
397
398         /**
399          * Asks the user for family name
400          *
401          * @return Family name of the user
402          */
403         @Override
404         public String enterOwnFamilyName () {
405                 // Trace message
406                 this.getLogger().trace("CALLED!"); //NOI18N
407
408                 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
409         }
410
411         /**
412          * Asks the user for family name
413          *
414          * @return Family name of the user
415          */
416         @Override
417         public String enterOwnFaxNumber () {
418                 // Trace message
419                 this.getLogger().trace("CALLED!"); //NOI18N
420
421                 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
422         }
423
424         /**
425          * Asks the user for gender, until a valid has been entered
426          *
427          * @return Gender of the user
428          */
429         @Override
430         public Gender enterOwnGender () {
431                 // Trace message
432                 this.getLogger().trace("CALLED!"); //NOI18N
433
434                 return this.getClient().enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
435         }
436
437         /**
438          * Asks the user for phone number
439          *
440          * @return Phone number of the user
441          */
442         @Override
443         public String enterOwnPhoneNumber () {
444                 // Trace message
445                 this.getLogger().trace("CALLED!"); //NOI18N
446
447                 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
448         }
449
450         /**
451          * Asks the user for own street (including number)
452          *
453          * @return Own street an number
454          */
455         @Override
456         public String enterOwnStreet () {
457                 // Trace message
458                 this.getLogger().trace("CALLED!"); //NOI18N
459
460                 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
461         }
462
463         /**
464          * Asks the user for surname
465          *
466          * @return Surname of the user
467          */
468         @Override
469         public String enterOwnSurname () {
470                 // Trace message
471                 this.getLogger().trace("CALLED!"); //NOI18N
472
473                 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
474         }
475
476         /**
477          * Asks the user for own ZIP code
478          *
479          * @return ZIP code
480          */
481         @Override
482         public int enterOwnZipCode () {
483                 // Trace message
484                 this.getLogger().trace("CALLED!"); //NOI18N
485
486                 return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
487         }
488
489         @Override
490         public final int getColumnCount () {
491                 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
492
493                 return this.columnNames.size();
494         }
495
496         /**
497          * Getter for column name at given index.
498          *
499          * @param columnIndex Column index
500          * @return Human-readable column name
501          */
502         @Override
503         public String getColumnName (final int columnIndex) {
504                 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
505
506                 // Get column name at index
507                 return this.columnNames.get(columnIndex);
508         }
509
510         /**
511          * Checks whether own contact is already added by checking all entries for
512          * isOwnContact flag
513          *
514          * @return Whether own contact is already added
515          */
516         @Override
517         public boolean isOwnContactAdded () {
518                 // Trace message
519                 this.getLogger().trace("CALLED!"); //NOI18N
520
521                 // Deligate this call to frontend
522                 boolean isAdded = this.getContactDatabase().isOwnContactFound();
523
524                 // Trace message
525                 this.getLogger().trace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
526
527                 // Return result
528                 return isAdded;
529         }
530
531         /**
532          * Adds given contact to address book and flushes all entries to database
533          *
534          * @param contact Contact being added
535          * @todo Add check for book size
536          */
537         @Override
538         public void registerContact (final Contact contact) {
539                 // Trace message
540                 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
541
542                 // Sanity check
543                 if (contact == null) {
544                         // Abort here
545                         throw new NullPointerException("contact is null"); //NOI18N
546                 }
547
548                 // Debug message
549                 /* NOISY-DEBUG: */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size())); //NOI18N
550                 try {
551                         // Check if contact is found
552                         if (this.getContactDatabase().isContactFound(contact)) {
553                                 // Contact already added
554                                 // @todo Do something here
555                         } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
556                                 // Own contact already added
557                                 // @todo Do something
558                         }
559
560                         // Add contact to internal list
561                         this.addContact(contact);
562                 } catch (final ContactAlreadyAddedException ex) {
563                         // Abort here
564                         this.abortProgramWithException(ex);
565                 } catch (final BadTokenException ex) {
566                         // Abort here
567                         this.abortProgramWithException(ex);
568                 }
569
570                 // Trace message
571                 this.getLogger().trace("EXIT!"); //NOI18N
572         }
573
574         /**
575          * Getter for size
576          *
577          * @return size of contact "book"
578          */
579         @Override
580         public final int size () {
581                 return this.getContactDatabase().getContactsCount();
582         }
583
584         /**
585          * Fills the column names array with strings from bundle
586          */
587         private void fillColumnNamesFromBundle () {
588                 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
589
590                 // Debug message
591                 this.getLogger().trace("CALLED!"); //NOI18N
592
593                 // First get an iterator from key set to iterate over
594                 Iterator<String> iterator = this.getBundle().keySet().iterator();
595
596                 // Then iterate over all
597                 while (iterator.hasNext()) {
598                         // Get next element
599                         String key = iterator.next();
600
601                         // Does the key start with ContactManager.columnName ?
602                         if (key.startsWith("ContactManager.columnName")) { //NOI18N
603                                 // This is the wanted entry.
604                                 this.getLogger().debug(MessageFormat.format("key={0}", key)); //NOI18N
605
606                                 // So add it
607                                 this.columnNames.add(this.getBundle().getString(key));
608                         }
609                 }
610
611                 // Debug message
612                 this.getLogger().trace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
613         }
614
615         /**
616          * A ContactWrapper instance
617          *
618          * @return the database
619          */
620         private ContactWrapper getContactDatabase () {
621                 return this.contactDatabase;
622         }
623
624         /**
625          * "Getter" for own contact instance or null if not found
626          *
627          * @return Contact instance or null
628          */
629         private Contact getOwnContact () {
630                 // Trace message
631                 this.getLogger().trace("CALLED!"); //NOI18N
632
633                 // Deligate this call to database frontend
634                 Contact contact = this.getContactDatabase().getOwnContact();
635
636                 // Trace message
637                 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
638
639                 // Return instance or null
640                 return contact;
641         }
642 }