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 other address
107 public void doChangeOtherAddress () {
108 throw new UnsupportedOperationException("Not supported yet.");
112 * Allows the user to change his/her own data
115 public void doChangeOwnData () {
117 * First check if the user has registered own contact, before that
118 * nothing can be changed.
120 if (!this.isOwnContactAdded()) {
122 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben.");
124 // Skip any below code
129 Contact contact = this.getOwnContact();
132 assert(contact instanceof Contact);
135 contact.show(this.getClient());
138 // Ask user what to change
139 this.getClient().userChooseChangeContactData(contact);
140 } catch (final UnhandledUserChoiceException ex) {
141 this.getLogger().catching(ex);
146 * Let the user delete other address
149 public void doDeleteOtherAddress () {
150 throw new UnsupportedOperationException("Not supported yet.");
154 * Let the user change address data
156 * @param contact Instance to change data
157 * @param client Client instance to call back
160 public void doChangeAddressData (final Contact contact, final Client client) {
161 // First display it again
162 client.displayAddressBox(contact);
165 if (contact.isOwnContact()) {
166 // Deligate to client
167 this.getClient().doChangeOwnAddressData(contact);
169 // Other contact's address data to change
170 throw new UnsupportedOperationException("Changing contact entries not finished.");
178 * Let the user change "name data"
180 * @param contact Instance to change data
181 * @param client Client instance to call back
184 public void doChangeNameData (final Contact contact, final Client client) {
185 // First display them again
186 client.displayNameBox(contact);
189 if (contact.isOwnContact()) {
191 this.getClient().doChangeOwnNameData(contact);
193 // Then re-ask them ...
194 throw new UnsupportedOperationException("Changing contact entries not finished.");
202 * Let the user change other data
204 * @param contact Instance to change data
205 * @param client Client instance to call back
206 * @todo Didn't handle birthday
209 public void doChangeOtherData (final Contact contact, final Client client) {
210 // First display them again
211 this.getClient().displayOtherDataBox(contact);
214 if (contact.isOwnContact()) {
216 this.getClient().doChangeOwnOtherData(contact);
218 // Then re-ask them ...
219 throw new UnsupportedOperationException("Changing contact entries not finished.");
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 * Shuts down this contact manager
242 public void doShutdown () {
243 // Shut down the database layer
244 this.getContactDatabase().doShutdown();
248 * Asks the user for his/her cellphone number
250 * @return User's cellphone number
253 public String enterOwnCellNumber () {
254 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
258 * Asks the user for his/her city's name
260 * @return City's name of the user
263 public String enterOwnCity () {
264 return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
268 * Asks the user for his/her city's name
270 * @return City's name of the user
273 public String enterOwnComment () {
274 return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
278 * Asks the user for his/her company name
280 * @return User's company name
283 public String enterOwnCompanyName () {
284 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
288 * Asks user for his/her own country code
290 * @return User's own country code
293 public String enterOwnCountryCode () {
294 return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
298 * Asks user for his/her own country code
300 * @return User's own country code
303 public String enterOwnEmailAddress () {
304 return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
308 * Asks the user for family name
310 * @return Family name of the user
313 public String enterOwnFamilyName () {
314 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
318 * Asks the user for family name
320 * @return Family name of the user
323 public String enterOwnFaxNumber () {
324 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
328 * Asks the user for gender, until a valid has been entered
330 * @return Gender of the user
333 public char enterOwnGender () {
334 return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
338 * Asks the user for phone number
340 * @return Phone number of the user
343 public String enterOwnPhoneNumber () {
344 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
348 * Asks the user for own street (including number)
349 * @return Own street an number
352 public String enterOwnStreet () {
353 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
357 * Asks the user for surname
358 * @return Surname of the user
361 public String enterOwnSurname () {
362 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
366 * Asks the user for own ZIP code
371 public int enterOwnZipCode () {
372 return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
376 public final int getColumnCount () {
377 return this.columnNames.size();
381 * Getter for whole contact list
383 * @return List of all contacts
386 public List<Contact> getList () {
387 return Collections.unmodifiableList(this.contacts);
391 * Checks whether own contact is already added by checking all entries for
394 * @return Whether own contact is already added
397 public boolean isOwnContactAdded () {
398 // Default is not added
399 boolean isAdded = false;
401 // Now get it back from address book, first get an iterator
402 Iterator<Contact> iterator = this.contacts.iterator();
405 while (iterator.hasNext()) {
407 Contact contact = iterator.next();
410 if (contact instanceof Contact) {
412 isAdded = contact.isOwnContact();
414 // Is this own contact?
426 public void doListContacts () {
427 throw new UnsupportedOperationException("Not supported yet.");
431 * Adds given contact to address book and flushes all entries to database
433 * @param contact Contact being added
434 * @todo Add check for book size
437 public void registerContact (final Contact contact) {
438 // Check if contact is found
439 if (this.isContactAlreadyAdded(contact)) {
440 // Contact already added
441 // @todo Do something here
442 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
443 // Own contact already added
444 // @todo Do something
448 /* NOISY-DEBUG: */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size()));
450 // Add contact to internal list
451 this.addContact(contact);
458 public void doSearchContacts () {
459 throw new UnsupportedOperationException("Not supported yet.");
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("ContactM,anager.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 * Flushes all entries by calling database backend
504 private void flush () {
506 this.getContactDatabase().flushAllContacts();
510 * A ContactWrapper instance
512 * @return the database
514 private ContactWrapper getContactDatabase () {
515 return this.contactDatabase;
519 * "Getter" for own contact instance or null if not found
521 * @return Contact instance or null
523 private Contact getOwnContact () {
524 // Now get it back from address book, first get an iterator
525 Iterator<Contact> iterator = this.contacts.iterator();
528 Contact contact = null;
530 // Search all contact
531 while (iterator.hasNext()) {
533 Contact next = iterator.next();
535 // Is this own contact?
536 if (next.isOwnContact()) {
544 // Return instance or null
549 * Checks whether given contact was found in "address book"
551 * @param checkContact Contact to be checked
552 * @return TRUE if found, FALSE if not found
554 private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException {
555 // Default is not found
556 boolean isFound = false;
559 //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" + this.contacts.length + "' entries...");
561 // Now get it back from address book, first get an iterator
562 Iterator<Contact> iterator = this.contacts.iterator();
565 while (iterator.hasNext()) {
567 Contact contact = iterator.next();
570 //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact);
573 if ((contact instanceof Contact) && ((contact.equals(checkContact)))) {
574 // Found matching entry