2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.manager.contact;
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;
34 * A manager for contacts, please note that this implementation loads the whole
37 * @author Roland Haeder
40 public class ContactManager extends BaseManager implements ManageableContact {
45 private final List<String> columnNames;
48 * A ContactWrapper instance
50 private final ContactWrapper contactDatabase;
53 * Constructor which accepts maxContacts for maximum (initial) contacts and
56 * @param client Client instance to use
58 public ContactManager (final Client client) {
60 this.getLogger().trace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
62 // Make sure all parameters are set correctly
65 throw new NullPointerException("client is null"); //NOI18N
68 // Set client instance
69 this.setClient(client);
71 // Init database connection
72 this.contactDatabase = new ContactDatabaseFrontend(this);
75 this.columnNames = new ArrayList<>(15);
78 this.fillColumnNamesFromBundle();
81 //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);
85 * Adds given Contact instance to list
87 * @param contact Contact instance to add
90 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
92 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
94 // Contact instance must not be null
95 if (contact == null) {
97 throw new NullPointerException("contact is null"); //NOI18N
101 this.getContactDatabase().addContact(contact);
104 this.getLogger().trace("EXIT!"); //NOI18N
108 * Let the user add a new other address
111 public void doAddOtherAddress () {
112 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
116 * Let the user change address data
118 * @param contact Instance to change data
121 public void doChangeAddressData (final Contact contact) {
123 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
125 // Contact must not be null
126 if (contact == null) {
128 throw new NullPointerException("contact is null"); //NOI18N
131 // First display it again
132 this.getClient().displayAddressBox(contact);
135 if (contact.isOwnContact()) {
136 // Deligate to client
137 this.getClient().doChangeOwnAddressData(contact);
139 // Other contact's address data to change
140 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
144 this.getLogger().trace("EXIT!"); //NOI18N
148 * Let the user change "name data"
150 * @param contact Instance to change data
153 public void doChangeNameData (final Contact contact) {
155 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
157 // Contact must not be null
158 if (contact == null) {
160 throw new NullPointerException("contact is null"); //NOI18N
163 // First display them again
164 this.getClient().displayNameBox(contact);
167 if (contact.isOwnContact()) {
169 this.getClient().doChangeOwnNameData(contact);
171 // Then re-ask them ...
172 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
176 this.getLogger().trace("EXIT!"); //NOI18N
180 * Let the user change other address
183 public void doChangeOtherAddress () {
184 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
188 * Let the user change other data
190 * @param contact Instance to change data
191 * @todo Didn't handle birthday
194 public void doChangeOtherData (final Contact contact) {
196 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
198 // Contact must not be null
199 if (contact == null) {
201 throw new NullPointerException("contact is null"); //NOI18N
204 // First display them again
205 this.getClient().displayOtherDataBox(contact);
208 if (contact.isOwnContact()) {
210 this.getClient().doChangeOwnOtherData(contact);
212 // Then re-ask them ...
213 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
217 this.getLogger().trace("EXIT!"); //NOI18N
221 * Allows the user to change his/her own data
224 public void doChangeOwnData () {
226 this.getLogger().trace("CALLED!"); //NOI18N
229 * First check if the user has registered own contact, before that
230 * nothing can be changed.
232 if (!this.isOwnContactAdded()) {
234 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
236 // Skip any below code
241 Contact contact = this.getOwnContact();
244 assert (contact instanceof Contact);
247 contact.show(this.getClient());
250 // Ask user what to change
251 this.getClient().userChooseChangeContactData(contact);
252 } catch (final UnhandledUserChoiceException ex) {
253 this.getLogger().catching(ex);
257 this.getLogger().trace("EXIT!"); //NOI18N
261 * Let the user delete other address
264 public void doDeleteOtherAddress () {
265 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
269 * Asks user for own data
272 public void doEnterOwnData () throws ContactAlreadyAddedException {
274 this.getLogger().trace("CALLED!"); //NOI18N
276 // Is own contact already added?
277 if (this.isOwnContactAdded()) {
278 // Don't continue here
279 throw new ContactAlreadyAddedException();
282 // Deligate this call to the client
283 Contact contact = this.getClient().doEnterOwnData();
286 if (contact instanceof Contact) {
287 // Add it to contact "book"
288 this.registerContact(contact);
292 this.getLogger().trace("EXIT!"); //NOI18N
296 public void doListContacts () {
297 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
301 public void doSearchContacts () {
302 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
306 * Shuts down this contact manager
309 public void doShutdown () {
311 this.getLogger().trace("CALLED!"); //NOI18N
313 // Shut down the database layer
314 this.getContactDatabase().doShutdown();
317 this.getLogger().trace("EXIT!"); //NOI18N
321 * Asks the user for his/her cellphone number
323 * @return User's cellphone number
326 public String enterOwnCellNumber () {
328 this.getLogger().trace("CALLED!"); //NOI18N
330 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
334 * Asks the user for his/her city's name
336 * @return City's name of the user
339 public String enterOwnCity () {
341 this.getLogger().trace("CALLED!"); //NOI18N
343 return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
347 * Asks the user for his/her city's name
349 * @return City's name of the user
352 public String enterOwnComment () {
354 this.getLogger().trace("CALLED!"); //NOI18N
356 return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
360 * Asks the user for his/her company name
362 * @return User's company name
365 public String enterOwnCompanyName () {
367 this.getLogger().trace("CALLED!"); //NOI18N
369 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
373 * Asks user for his/her own country code
375 * @return User's own country code
378 public String enterOwnCountryCode () {
380 this.getLogger().trace("CALLED!"); //NOI18N
382 return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
386 * Asks user for his/her own country code
388 * @return User's own country code
391 public String enterOwnEmailAddress () {
393 this.getLogger().trace("CALLED!"); //NOI18N
395 return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
399 * Asks the user for family name
401 * @return Family name of the user
404 public String enterOwnFamilyName () {
406 this.getLogger().trace("CALLED!"); //NOI18N
408 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
412 * Asks the user for family name
414 * @return Family name of the user
417 public String enterOwnFaxNumber () {
419 this.getLogger().trace("CALLED!"); //NOI18N
421 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
425 * Asks the user for gender, until a valid has been entered
427 * @return Gender of the user
430 public Gender enterOwnGender () {
432 this.getLogger().trace("CALLED!"); //NOI18N
434 return this.getClient().enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
438 * Asks the user for phone number
440 * @return Phone number of the user
443 public String enterOwnPhoneNumber () {
445 this.getLogger().trace("CALLED!"); //NOI18N
447 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
451 * Asks the user for own street (including number)
453 * @return Own street an number
456 public String enterOwnStreet () {
458 this.getLogger().trace("CALLED!"); //NOI18N
460 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
464 * Asks the user for surname
466 * @return Surname of the user
469 public String enterOwnSurname () {
471 this.getLogger().trace("CALLED!"); //NOI18N
473 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
477 * Asks the user for own ZIP code
482 public int enterOwnZipCode () {
484 this.getLogger().trace("CALLED!"); //NOI18N
486 return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
490 public final int getColumnCount () {
491 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
493 return this.columnNames.size();
497 * Getter for column name at given index.
499 * @param columnIndex Column index
500 * @return Human-readable column name
503 public String getColumnName (final int columnIndex) {
504 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
506 // Get column name at index
507 return this.columnNames.get(columnIndex);
511 * Checks whether own contact is already added by checking all entries for
514 * @return Whether own contact is already added
517 public boolean isOwnContactAdded () {
519 this.getLogger().trace("CALLED!"); //NOI18N
521 // Deligate this call to frontend
522 boolean isAdded = this.getContactDatabase().isOwnContactFound();
525 this.getLogger().trace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
532 * Adds given contact to address book and flushes all entries to database
534 * @param contact Contact being added
535 * @todo Add check for book size
538 public void registerContact (final Contact contact) {
540 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
543 if (contact == null) {
545 throw new NullPointerException("contact is null"); //NOI18N
549 /* NOISY-DEBUG: */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size())); //NOI18N
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
560 // Add contact to internal list
561 this.addContact(contact);
562 } catch (final ContactAlreadyAddedException ex) {
564 this.abortProgramWithException(ex);
565 } catch (final BadTokenException ex) {
567 this.abortProgramWithException(ex);
571 this.getLogger().trace("EXIT!"); //NOI18N
577 * @return size of contact "book"
580 public final int size () {
581 return this.getContactDatabase().getContactsCount();
585 * Fills the column names array with strings from bundle
587 private void fillColumnNamesFromBundle () {
588 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
591 this.getLogger().trace("CALLED!"); //NOI18N
593 // First get an iterator from key set to iterate over
594 Iterator<String> iterator = this.getBundle().keySet().iterator();
596 // Then iterate over all
597 while (iterator.hasNext()) {
599 String key = iterator.next();
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
607 this.columnNames.add(this.getBundle().getString(key));
612 this.getLogger().trace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
616 * A ContactWrapper instance
618 * @return the database
620 private ContactWrapper getContactDatabase () {
621 return this.contactDatabase;
625 * "Getter" for own contact instance or null if not found
627 * @return Contact instance or null
629 private Contact getOwnContact () {
631 this.getLogger().trace("CALLED!"); //NOI18N
633 // Deligate this call to database frontend
634 Contact contact = this.getContactDatabase().getOwnContact();
637 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
639 // Return instance or null