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.database.frontend.contact.ContactDatabaseFrontend;
27 import org.mxchange.addressbook.database.frontend.contact.ContactWrapper;
28 import org.mxchange.addressbook.exceptions.UnhandledUserChoiceException;
29 import org.mxchange.addressbook.manager.BaseManager;
32 * A manager for contacts, please note that this implementation loads the whole
35 * @author Roland Haeder
38 public class ContactManager extends BaseManager implements ManageableContact {
42 private final List<String> columnNames;
45 * A ContactWrapper instance
47 private final ContactWrapper contactDatabase;
50 * A list of all contacts
52 private final List<Contact> contacts;
56 * @param maxContacts Maximum allowed contacts
57 * @param client Client instance to use
59 public ContactManager (final int maxContacts, final Client client) {
60 // Always call super constructor first
63 // Set client instance
64 this.setClient(client);
67 this.contacts = new ArrayList<>(maxContacts);
69 // Init database connection
70 this.contactDatabase = new ContactDatabaseFrontend(this);
73 this.columnNames = new ArrayList<>(15);
76 this.fillColumnNamesFromBundle();
79 this.contactDatabase.readAllContacts();
82 //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);
86 * Adds given Contact instance to list
88 * @param contact Contact instance to add
91 public void addContact (final Contact contact) {
92 this.contacts.add(contact);
96 * Let the user add a new other address
99 public void doAddOtherAddress () {
100 throw new UnsupportedOperationException("Not supported yet.");
104 * Let the user change address data
106 * @param contact Instance to change data
107 * @param client Client instance to call back
110 public void doChangeAddressData (final Contact contact, final Client client) {
111 // First display it again
112 client.displayAddressBox(contact);
115 if (contact.isOwnContact()) {
116 // Deligate to client
117 this.getClient().doChangeOwnAddressData(contact);
119 // Other contact's address data to change
120 throw new UnsupportedOperationException("Changing contact entries not finished.");
128 * Let the user change "name data"
130 * @param contact Instance to change data
131 * @param client Client instance to call back
134 public void doChangeNameData (final Contact contact, final Client client) {
135 // First display them again
136 client.displayNameBox(contact);
139 if (contact.isOwnContact()) {
141 this.getClient().doChangeOwnNameData(contact);
143 // Then re-ask them ...
144 throw new UnsupportedOperationException("Changing contact entries not finished.");
152 * Let the user change other address
155 public void doChangeOtherAddress () {
156 throw new UnsupportedOperationException("Not supported yet.");
160 * Let the user change other data
162 * @param contact Instance to change data
163 * @param client Client instance to call back
164 * @todo Didn't handle birthday
167 public void doChangeOtherData (final Contact contact, final Client client) {
168 // First display them again
169 this.getClient().displayOtherDataBox(contact);
172 if (contact.isOwnContact()) {
174 this.getClient().doChangeOwnOtherData(contact);
176 // Then re-ask them ...
177 throw new UnsupportedOperationException("Changing contact entries not finished.");
185 * Allows the user to change his/her own data
188 public void doChangeOwnData () {
190 * First check if the user has registered own contact, before that
191 * nothing can be changed.
193 if (!this.isOwnContactAdded()) {
195 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben.");
197 // Skip any below code
202 Contact contact = this.getOwnContact();
205 assert(contact instanceof Contact);
208 contact.show(this.getClient());
211 // Ask user what to change
212 this.getClient().userChooseChangeContactData(contact);
213 } catch (final UnhandledUserChoiceException ex) {
214 this.getLogger().catching(ex);
219 * Let the user delete other address
222 public void doDeleteOtherAddress () {
223 throw new UnsupportedOperationException("Not supported yet.");
227 * Asks user for own data
230 public void doEnterOwnData () {
231 // Deligate this call to the client
232 Contact contact = this.getClient().doEnterOwnData();
234 // Add it to contact "book"
235 this.registerContact(contact);
239 public void doListContacts () {
240 throw new UnsupportedOperationException("Not supported yet.");
244 public void doSearchContacts () {
245 throw new UnsupportedOperationException("Not supported yet.");
249 * Shuts down this contact manager
252 public void doShutdown () {
253 // Shut down the database layer
254 this.getContactDatabase().doShutdown();
258 * Asks the user for his/her cellphone number
260 * @return User's cellphone number
263 public String enterOwnCellNumber () {
264 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
268 * Asks the user for his/her city's name
270 * @return City's name of the user
273 public String enterOwnCity () {
274 return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
278 * Asks the user for his/her city's name
280 * @return City's name of the user
283 public String enterOwnComment () {
284 return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
288 * Asks the user for his/her company name
290 * @return User's company name
293 public String enterOwnCompanyName () {
294 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
298 * Asks user for his/her own country code
300 * @return User's own country code
303 public String enterOwnCountryCode () {
304 return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
308 * Asks user for his/her own country code
310 * @return User's own country code
313 public String enterOwnEmailAddress () {
314 return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
318 * Asks the user for family name
320 * @return Family name of the user
323 public String enterOwnFamilyName () {
324 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
328 * Asks the user for family name
330 * @return Family name of the user
333 public String enterOwnFaxNumber () {
334 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
338 * Asks the user for gender, until a valid has been entered
340 * @return Gender of the user
343 public char enterOwnGender () {
344 return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
348 * Asks the user for phone number
350 * @return Phone number of the user
353 public String enterOwnPhoneNumber () {
354 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
358 * Asks the user for own street (including number)
359 * @return Own street an number
362 public String enterOwnStreet () {
363 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
367 * Asks the user for surname
368 * @return Surname of the user
371 public String enterOwnSurname () {
372 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
376 * Asks the user for own ZIP code
381 public int enterOwnZipCode () {
382 return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
386 public final int getColumnCount () {
387 return this.columnNames.size();
391 * Getter for whole contact list
393 * @return List of all contacts
396 public List<Contact> getList () {
397 return Collections.unmodifiableList(this.contacts);
401 * Checks whether own contact is already added by checking all entries for
404 * @return Whether own contact is already added
407 public boolean isOwnContactAdded () {
408 // Default is not added
409 boolean isAdded = false;
411 // Now get it back from address book, first get an iterator
412 Iterator<Contact> iterator = this.contacts.iterator();
415 while (iterator.hasNext()) {
417 Contact contact = iterator.next();
420 if (contact instanceof Contact) {
422 isAdded = contact.isOwnContact();
424 // Is this own contact?
436 * Adds given contact to address book and flushes all entries to database
438 * @param contact Contact being added
439 * @todo Add check for book size
442 public void registerContact (final Contact contact) {
443 // Check if contact is found
444 if (this.isContactAlreadyAdded(contact)) {
445 // Contact already added
446 // @todo Do something here
447 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
448 // Own contact already added
449 // @todo Do something
453 /* NOISY-DEBUG: */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size()));
455 // Add contact to internal list
456 this.addContact(contact);
465 * @return size of contact "book"
468 public final int size () {
469 return this.contacts.size();
473 * Fills the column names array with strings from bundle
475 private void fillColumnNamesFromBundle () {
477 this.getLogger().trace("CALLED!");
479 // First get an iterator from key set to iterate over
480 Iterator<String> iterator = this.getBundle().keySet().iterator();
482 // Then iterate over all
483 while (iterator.hasNext()) {
485 String key = iterator.next();
487 // Does the key start with ContactManager.columnName ?
488 if (key.startsWith("ContactManager.columnName")) {
489 // This is the wanted entry.
490 this.getLogger().debug(MessageFormat.format("key={0}", key));
493 this.columnNames.add(this.getBundle().getString(key));
498 this.getLogger().trace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount()));
502 * Getter for column name at given index.
504 * @param columnIndex Column index
505 * @return Human-readable column name
508 public String getColumnName (final int columnIndex) {
509 // Get column name at index
510 return this.columnNames.get(columnIndex);
514 * Flushes all entries by calling database backend
516 private void flush () {
518 this.getContactDatabase().flushAllContacts();
522 * A ContactWrapper instance
524 * @return the database
526 private ContactWrapper getContactDatabase () {
527 return this.contactDatabase;
531 * "Getter" for own contact instance or null if not found
533 * @return Contact instance or null
535 private Contact getOwnContact () {
536 // Now get it back from address book, first get an iterator
537 Iterator<Contact> iterator = this.contacts.iterator();
540 Contact contact = null;
542 // Search all contact
543 while (iterator.hasNext()) {
545 Contact next = iterator.next();
547 // Is this own contact?
548 if (next.isOwnContact()) {
556 // Return instance or null
561 * Checks whether given contact was found in "address book"
563 * @param checkContact Contact to be checked
564 * @return TRUE if found, FALSE if not found
566 private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException {
567 // Default is not found
568 boolean isFound = false;
571 //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" + this.contacts.length + "' entries...");
573 // Now get it back from address book, first get an iterator
574 Iterator<Contact> iterator = this.contacts.iterator();
577 while (iterator.hasNext()) {
579 Contact contact = iterator.next();
582 //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact);
585 if ((contact instanceof Contact) && ((contact.equals(checkContact)))) {
586 // Found matching entry