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.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.mxchange.addressbook.client.Client;
25 import org.mxchange.addressbook.contact.Contact;
26 import org.mxchange.addressbook.contact.Gender;
27 import org.mxchange.addressbook.database.frontend.contact.ContactDatabaseFrontend;
28 import org.mxchange.addressbook.database.frontend.contact.ContactWrapper;
29 import org.mxchange.addressbook.exceptions.UnhandledUserChoiceException;
30 import org.mxchange.addressbook.manager.BaseManager;
33 * A manager for contacts, please note that this implementation loads the whole
36 * @author Roland Haeder
39 public class ContactManager extends BaseManager implements ManageableContact {
44 private final List<String> columnNames;
47 * A ContactWrapper instance
49 private final ContactWrapper contactDatabase;
52 * A list of all contacts
54 private final List<Contact> contacts;
57 * Constructor which accepts maxContacts for maximum (initial) contacts and
60 * @param maxContacts Maximum allowed contacts
61 * @param client Client instance to use
63 public ContactManager (final int maxContacts, final Client client) {
64 // Always call super constructor first
67 // Set client instance
68 this.setClient(client);
71 this.contacts = new ArrayList<>(maxContacts);
73 // Init database connection
74 this.contactDatabase = new ContactDatabaseFrontend(this);
77 this.columnNames = new ArrayList<>(15);
80 this.fillColumnNamesFromBundle();
83 this.contactDatabase.readAllContacts();
86 //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);
90 * Adds given Contact instance to list
92 * @param contact Contact instance to add
95 public void addContact (final Contact contact) {
96 assert (this.contacts instanceof List) : "this.contacts is not initialized";
98 this.contacts.add(contact);
102 * Let the user add a new other address
105 public void doAddOtherAddress () {
106 throw new UnsupportedOperationException("Not supported yet.");
110 * Let the user change address data
112 * @param contact Instance to change data
113 * @param client Client instance to call back
116 public void doChangeAddressData (final Contact contact, final Client client) {
117 // First display it again
118 client.displayAddressBox(contact);
121 if (contact.isOwnContact()) {
122 // Deligate to client
123 this.getClient().doChangeOwnAddressData(contact);
125 // Other contact's address data to change
126 throw new UnsupportedOperationException("Changing contact entries not finished.");
134 * Let the user change "name data"
136 * @param contact Instance to change data
137 * @param client Client instance to call back
140 public void doChangeNameData (final Contact contact, final Client client) {
141 // First display them again
142 client.displayNameBox(contact);
145 if (contact.isOwnContact()) {
147 this.getClient().doChangeOwnNameData(contact);
149 // Then re-ask them ...
150 throw new UnsupportedOperationException("Changing contact entries not finished.");
158 * Let the user change other address
161 public void doChangeOtherAddress () {
162 throw new UnsupportedOperationException("Not supported yet.");
166 * Let the user change other data
168 * @param contact Instance to change data
169 * @param client Client instance to call back
170 * @todo Didn't handle birthday
173 public void doChangeOtherData (final Contact contact, final Client client) {
174 // First display them again
175 this.getClient().displayOtherDataBox(contact);
178 if (contact.isOwnContact()) {
180 this.getClient().doChangeOwnOtherData(contact);
182 // Then re-ask them ...
183 throw new UnsupportedOperationException("Changing contact entries not finished.");
191 * Allows the user to change his/her own data
194 public void doChangeOwnData () {
196 * First check if the user has registered own contact, before that
197 * nothing can be changed.
199 if (!this.isOwnContactAdded()) {
201 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben.");
203 // Skip any below code
208 Contact contact = this.getOwnContact();
211 assert (contact instanceof Contact);
214 contact.show(this.getClient());
217 // Ask user what to change
218 this.getClient().userChooseChangeContactData(contact);
219 } catch (final UnhandledUserChoiceException ex) {
220 this.getLogger().catching(ex);
225 * Let the user delete other address
228 public void doDeleteOtherAddress () {
229 throw new UnsupportedOperationException("Not supported yet.");
233 * Asks user for own data
236 public void doEnterOwnData () {
237 // Deligate this call to the client
238 Contact contact = this.getClient().doEnterOwnData();
240 // Add it to contact "book"
241 this.registerContact(contact);
245 public void doListContacts () {
246 throw new UnsupportedOperationException("Not supported yet.");
250 public void doSearchContacts () {
251 throw new UnsupportedOperationException("Not supported yet.");
255 * Shuts down this contact manager
258 public void doShutdown () {
259 // Shut down the database layer
260 this.getContactDatabase().doShutdown();
264 * Asks the user for his/her cellphone number
266 * @return User's cellphone number
269 public String enterOwnCellNumber () {
270 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
274 * Asks the user for his/her city's name
276 * @return City's name of the user
279 public String enterOwnCity () {
280 return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
284 * Asks the user for his/her city's name
286 * @return City's name of the user
289 public String enterOwnComment () {
290 return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
294 * Asks the user for his/her company name
296 * @return User's company name
299 public String enterOwnCompanyName () {
300 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
304 * Asks user for his/her own country code
306 * @return User's own country code
309 public String enterOwnCountryCode () {
310 return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
314 * Asks user for his/her own country code
316 * @return User's own country code
319 public String enterOwnEmailAddress () {
320 return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
324 * Asks the user for family name
326 * @return Family name of the user
329 public String enterOwnFamilyName () {
330 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
334 * Asks the user for family name
336 * @return Family name of the user
339 public String enterOwnFaxNumber () {
340 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
344 * Asks the user for gender, until a valid has been entered
346 * @return Gender of the user
349 public Gender enterOwnGender () {
350 return this.getClient().enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
354 * Asks the user for phone number
356 * @return Phone number of the user
359 public String enterOwnPhoneNumber () {
360 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
364 * Asks the user for own street (including number)
366 * @return Own street an number
369 public String enterOwnStreet () {
370 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
374 * Asks the user for surname
376 * @return Surname of the user
379 public String enterOwnSurname () {
380 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
384 * Asks the user for own ZIP code
389 public int enterOwnZipCode () {
390 return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
394 public final int getColumnCount () {
395 assert (this.columnNames instanceof List) : "this.columnNames is not initialized";
397 return this.columnNames.size();
401 * Getter for column name at given index.
403 * @param columnIndex Column index
404 * @return Human-readable column name
407 public String getColumnName (final int columnIndex) {
408 assert (this.columnNames instanceof List) : "this.columnNames is not initialized";
410 // Get column name at index
411 return this.columnNames.get(columnIndex);
415 * Getter for whole contact list
417 * @return List of all contacts
420 public List<Contact> getList () {
421 assert (this.contacts instanceof List) : "this.contacts is not initialized";
422 return Collections.unmodifiableList(this.contacts);
426 * Checks whether own contact is already added by checking all entries for
429 * @return Whether own contact is already added
432 public boolean isOwnContactAdded () {
434 this.getLogger().trace("CALLED!");
436 assert (this.contacts instanceof List) : "this.contacts is not initialized";
438 // Default is not added
439 boolean isAdded = false;
441 // Now get it back from address book, first get an iterator
442 Iterator<Contact> iterator = this.contacts.iterator();
445 while (iterator.hasNext()) {
447 Contact contact = iterator.next();
450 if (contact instanceof Contact) {
452 isAdded = contact.isOwnContact();
454 // Is this own contact?
463 this.getLogger().trace(MessageFormat.format("isAdded={0} : EXIT!", isAdded));
470 * Adds given contact to address book and flushes all entries to database
472 * @param contact Contact being added
473 * @todo Add check for book size
476 public void registerContact (final Contact contact) {
477 // Check if contact is found
478 if (this.isContactAlreadyAdded(contact)) {
479 // Contact already added
480 // @todo Do something here
481 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
482 // Own contact already added
483 // @todo Do something
489 */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size()));
491 // Add contact to internal list
492 this.addContact(contact);
501 * @return size of contact "book"
504 public final int size () {
505 assert (this.contacts instanceof List) : "this.contacts is not initialized";
506 return this.contacts.size();
510 * Fills the column names array with strings from bundle
512 private void fillColumnNamesFromBundle () {
513 assert (this.columnNames instanceof List) : "this.columnNames is not initialized";
516 this.getLogger().trace("CALLED!");
518 // First get an iterator from key set to iterate over
519 Iterator<String> iterator = this.getBundle().keySet().iterator();
521 // Then iterate over all
522 while (iterator.hasNext()) {
524 String key = iterator.next();
526 // Does the key start with ContactManager.columnName ?
527 if (key.startsWith("ContactManager.columnName")) {
528 // This is the wanted entry.
529 this.getLogger().debug(MessageFormat.format("key={0}", key));
532 this.columnNames.add(this.getBundle().getString(key));
537 this.getLogger().trace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount()));
541 * Flushes all entries by calling database backend
543 private void flush () {
545 this.getContactDatabase().flushAllContacts();
549 * A ContactWrapper instance
551 * @return the database
553 private ContactWrapper getContactDatabase () {
554 return this.contactDatabase;
558 * "Getter" for own contact instance or null if not found
560 * @return Contact instance or null
562 private Contact getOwnContact () {
563 assert (this.contacts instanceof List) : "this.contacts is not initialized";
565 // Now get it back from address book, first get an iterator
566 Iterator<Contact> iterator = this.contacts.iterator();
569 Contact contact = null;
571 // Search all contact
572 while (iterator.hasNext()) {
574 Contact next = iterator.next();
576 // Is this own contact?
577 if (next.isOwnContact()) {
585 // Return instance or null
590 * Checks whether given contact was found in "address book"
592 * @param checkContact Contact to be checked
593 * @return TRUE if found, FALSE if not found
595 private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException {
596 assert (this.contacts instanceof List) : "this.contacts is not initialized";
598 // Default is not found
599 boolean isFound = false;
602 //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" + this.contacts.length + "' entries...");
603 // Now get it back from address book, first get an iterator
604 Iterator<Contact> iterator = this.contacts.iterator();
607 while (iterator.hasNext()) {
609 Contact contact = iterator.next();
612 //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact);
614 if ((contact instanceof Contact) && ((contact.equals(checkContact)))) {
615 // Found matching entry