2 * Copyright (C) 2015 Roland Haeder
\r
4 * This program is free software: you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation, either version 3 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
17 package org.mxchange.addressbook.manager.contact;
\r
19 import java.util.ArrayList;
\r
20 import java.util.Iterator;
\r
21 import java.util.List;
\r
22 import org.mxchange.addressbook.UnhandledUserChoiceException;
\r
23 import org.mxchange.addressbook.client.Client;
\r
24 import org.mxchange.addressbook.contact.Contact;
\r
25 import org.mxchange.addressbook.contact.user.UserContact;
\r
26 import org.mxchange.addressbook.database.frontend.contact.ContactDatabaseFrontend;
\r
27 import org.mxchange.addressbook.database.frontend.contact.ContactWrapper;
\r
28 import org.mxchange.addressbook.manager.BaseManager;
\r
31 * A manager for contacts, please note that this implementation loads the whole
\r
34 * @author Roland Haeder
\r
38 public class ContactManager extends BaseManager implements ManageableContact {
\r
41 * A ContactWrapper instance
\r
43 private final ContactWrapper contactDatabase;
\r
46 * A list of all contacts
\r
48 private final List<Contact> contacts;
\r
51 * @param maxContacts Maximum allowed contacts
\r
52 * @param client Client instance to use
\r
54 public ContactManager (final int maxContacts, final Client client) {
\r
55 // Always call super constructor first
\r
59 this.contacts = new ArrayList<>(maxContacts);
\r
61 // Init database connection
\r
62 this.contactDatabase = new ContactDatabaseFrontend();
\r
65 //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);
\r
68 this.setClient(client);
\r
72 * Adds given contact to address book
\r
74 * @param contact Contact being added
\r
75 * @todo Add check for book size
\r
78 public void addContact (final Contact contact) {
\r
79 // Check if contact is found
\r
80 if (this.isContactAlreadyAdded(contact)) {
\r
81 // Contact already added
\r
82 // @todo Do something here
\r
83 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
\r
84 // Own contact already added
\r
85 // @todo Do something
\r
89 /* NOISY-DEBUG: */ this.getLogger().debug("Adding '" + contact.getSurname() + "' '" + contact.getFamilyName() + "' at pos '" + this.size () + "' ...");
\r
91 // Add contact to internal list
\r
92 this.contacts.add(contact);
\r
95 * @TODO This call doesn't make sense, it would cause that only one
\r
96 * entry exist in storage. A better solution is to iterate over all
\r
97 * contacts and add them. This may also require to rewrite the database
\r
98 * layer a little. For demonstrational purposes, this should be fine.
\r
101 // Add object to database
\r
102 this.getContactDatabase().addContact(contact);
\r
106 * Let the user add a new other address
\r
109 public void addOtherAddress () {
\r
110 throw new UnsupportedOperationException("Not supported yet.");
\r
114 * Let the user change other address
\r
117 public void changeOtherAddress () {
\r
118 throw new UnsupportedOperationException("Not supported yet.");
\r
122 * Allows the user to change his/her own data
\r
125 public void changeOwnData () {
\r
127 * First check if the user has registered own contact, before that
\r
128 * nothing can be changed.
\r
130 if (!this.isOwnContactAdded()) {
\r
132 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben.");
\r
134 // Skip any below code
\r
139 Contact contact = this.getOwnContact();
\r
141 // It must be found
\r
142 assert(contact instanceof Contact);
\r
145 contact.show(this.getClient());
\r
148 // Ask user what to change
\r
149 this.getClient().userChooseChangeContactData(contact);
\r
150 } catch (final UnhandledUserChoiceException ex) {
\r
151 this.getLogger().catching(ex);
\r
156 * Let the user delete other address
\r
159 public void deleteOtherAddress () {
\r
160 throw new UnsupportedOperationException("Not supported yet.");
\r
164 * Let the user change address data
\r
166 * @param contact Instance to change data
\r
167 * @param client Client instance to call back
\r
170 public void doChangeAddressData (final Contact contact, final Client client) {
\r
171 // First display it again
\r
172 client.displayAddressBox(contact);
\r
175 if (contact.isOwnContact()) {
\r
176 // Own address data
\r
177 String street = this.enterOwnStreet();
\r
180 int zipCode = this.enterOwnZipCode();
\r
183 String city = this.enterOwnCity();
\r
185 // Get country code
\r
186 String countryCode = this.enterOwnCountryCode();
\r
188 // Update address data
\r
189 contact.updateAddressData(street, zipCode, city, countryCode);
\r
191 // Other contact's address data to change
\r
192 throw new UnsupportedOperationException("Changing contact entries not finished.");
\r
197 * Let the user change "name data"
\r
199 * @param contact Instance to change data
\r
200 * @param client Client instance to call back
\r
203 public void doChangeNameData (final Contact contact, final Client client) {
\r
204 // First display them again
\r
205 client.displayNameBox(contact);
\r
207 // Is this own data?
\r
208 if (contact.isOwnContact()) {
\r
211 char gender = this.enterOwnGender();
\r
214 String surname = this.enterOwnSurname();
\r
217 String familyName = this.enterOwnFamilyName();
\r
220 String companyName = this.enterOwnCompanyName();
\r
222 // Update contact instance
\r
223 contact.updateNameData(gender, surname, familyName, companyName);
\r
225 // Then re-ask them ...
\r
226 throw new UnsupportedOperationException("Changing contact entries not finished.");
\r
231 * Let the user change other data
\r
233 * @param contact Instance to change data
\r
234 * @param client Client instance to call back
\r
237 public void doChangeOtherData (final Contact contact, final Client client) {
\r
238 // First display them again
\r
239 client.displayOtherDataBox(contact);
\r
241 // Is this own data?
\r
242 if (contact.isOwnContact()) {
\r
245 String phoneNumber = this.enterOwnPhoneNumber();
\r
248 String cellNumber = this.enterOwnCellNumber();
\r
251 String faxNumber = this.enterOwnFaxNumber();
\r
254 String email = this.enterOwnEmailAddress();
\r
257 String comment = this.enterOwnComment();
\r
259 // Update contact instance
\r
260 contact.updateOtherData(phoneNumber, cellNumber, faxNumber, email, comment);
\r
262 // Then re-ask them ...
\r
263 throw new UnsupportedOperationException("Changing contact entries not finished.");
\r
268 * Asks user for own data
\r
271 public void doEnterOwnData () {
\r
272 // First ask for gender
\r
273 char gender = this.enterOwnGender();
\r
276 String surname = this.enterOwnSurname();
\r
278 // And 3rd for family name
\r
279 String familyName = this.enterOwnFamilyName();
\r
281 // Company name ...
\r
282 String companyName = this.enterOwnCompanyName();
\r
284 // Construct UserContact instance
\r
285 Contact contact = new UserContact(gender, surname, familyName, companyName);
\r
287 // Mark contact as own
\r
288 contact.enableFlagOwnContact();
\r
290 // Add it to contact "book"
\r
291 this.addContact(contact);
\r
297 * @return size of contact "book"
\r
300 public int size () {
\r
301 return this.contacts.size();
\r
305 * Asks the user for his/her cellphone number
\r
307 * @return User's cellphone number
\r
309 private String enterOwnCellNumber () {
\r
310 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
\r
314 * Asks the user for his/her city's name
\r
316 * @return City's name of the user
\r
318 private String enterOwnCity () {
\r
319 return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
\r
323 * Asks the user for his/her city's name
\r
325 * @return City's name of the user
\r
327 private String enterOwnComment () {
\r
328 return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
\r
332 * Asks the user for his/her company name
\r
334 * @return User's company name
\r
336 private String enterOwnCompanyName () {
\r
337 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
\r
341 * Asks user for his/her own country code
\r
343 * @return User's own country code
\r
345 private String enterOwnCountryCode () {
\r
346 return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
\r
350 * Asks user for his/her own country code
\r
352 * @return User's own country code
\r
354 private String enterOwnEmailAddress () {
\r
355 return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
\r
359 * Asks the user for family name
\r
361 * @return Family name of the user
\r
363 private String enterOwnFamilyName () {
\r
364 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
\r
368 * Asks the user for family name
\r
370 * @return Family name of the user
\r
372 private String enterOwnFaxNumber () {
\r
373 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
\r
377 * Asks the user for gender, until a valid has been entered
\r
379 * @return Gender of the user
\r
381 private char enterOwnGender () {
\r
382 return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
\r
386 * Asks the user for phone number
\r
388 * @return Phone number of the user
\r
390 private String enterOwnPhoneNumber () {
\r
391 return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
\r
395 * Asks the user for own street (including number)
\r
397 private String enterOwnStreet () {
\r
398 return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
\r
402 * Asks the user for surname
\r
403 * @return Surname of the user
\r
405 private String enterOwnSurname () {
\r
406 return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
\r
410 * Asks the user for own ZIP code
\r
413 private int enterOwnZipCode () {
\r
414 return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
\r
418 * "Getter" for own contact instance or null if not found
\r
420 * @return Contact instance or null
\r
422 private Contact getOwnContact () {
\r
423 // Now get it back from address book, first get an iterator
\r
424 Iterator<Contact> iterator = this.contacts.iterator();
\r
427 Contact contact = null;
\r
429 // Search all contact
\r
430 while (iterator.hasNext()) {
\r
431 // Get next instance
\r
432 Contact next = iterator.next();
\r
434 // Is this own contact?
\r
435 if (next.isOwnContact()) {
\r
443 // Return instance or null
\r
448 * Checks whether given contact was found in "address book"
\r
450 * @param checkContact Contact to be checked
\r
451 * @return TRUE if found, FALSE if not found
\r
453 private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException {
\r
454 // Default is not found
\r
455 boolean isFound = false;
\r
458 //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" + this.contacts.length + "' entries...");
\r
460 // Now get it back from address book, first get an iterator
\r
461 Iterator<Contact> iterator = this.contacts.iterator();
\r
464 while (iterator.hasNext()) {
\r
466 Contact contact = iterator.next();
\r
469 //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact);
\r
472 if ((contact instanceof Contact) && ((contact.equals(checkContact)))) {
\r
473 // Found matching entry
\r
484 * Checks whether own contact is already added by checking all entries for isOwnContact flag
\r
485 * @return Whether own contact is already added
\r
487 private boolean isOwnContactAdded () {
\r
488 // Default is not added
\r
489 boolean isAdded = false;
\r
491 // Now get it back from address book, first get an iterator
\r
492 Iterator<Contact> iterator = this.contacts.iterator();
\r
495 while (iterator.hasNext()) {
\r
497 Contact contact = iterator.next();
\r
500 if (contact instanceof Contact) {
\r
502 isAdded = contact.isOwnContact();
\r
504 // Is this own contact?
\r
516 * A ContactWrapper instance
\r
518 * @return the database
\r
520 private ContactWrapper getContactDatabase () {
\r
521 return this.contactDatabase;
\r