-/*\r
- * Copyright (C) 2015 Roland Haeder\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-package org.mxchange.addressbook.manager.contact;\r
-\r
-import java.text.MessageFormat;\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import org.mxchange.addressbook.UnhandledUserChoiceException;\r
-import org.mxchange.addressbook.client.Client;\r
-import org.mxchange.addressbook.contact.Contact;\r
-import org.mxchange.addressbook.contact.user.UserContact;\r
-import org.mxchange.addressbook.database.frontend.contact.ContactDatabaseFrontend;\r
-import org.mxchange.addressbook.database.frontend.contact.ContactWrapper;\r
-import org.mxchange.addressbook.manager.BaseManager;\r
-\r
-/**\r
- * A manager for contacts, please note that this implementation loads the whole\r
- * list into RAM.\r
- *\r
- * @author Roland Haeder\r
- * @version 0.0\r
- * @since 0.0\r
- */\r
-public class ContactManager extends BaseManager implements ManageableContact {\r
-\r
- /**\r
- * A ContactWrapper instance\r
- */\r
- private final ContactWrapper contactDatabase;\r
-\r
- /**\r
- * A list of all contacts\r
- */\r
- private final List<Contact> contacts;\r
-\r
- /**\r
- * @param maxContacts Maximum allowed contacts\r
- * @param client Client instance to use\r
- */\r
- public ContactManager (final int maxContacts, final Client client) {\r
- // Always call super constructor first\r
- super();\r
-\r
- // Init contacts\r
- this.contacts = new ArrayList<>(maxContacts);\r
-\r
- // Init database connection\r
- this.contactDatabase = new ContactDatabaseFrontend(this);\r
-\r
- // Read all entries\r
- this.contactDatabase.readAllContacts();\r
-\r
- // Debug message\r
- //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);\r
-\r
- // Init client\r
- this.setClient(client);\r
- }\r
-\r
- /**\r
- * Adds given Contact instance to list\r
- *\r
- * @param contact Contact instance to add\r
- */\r
- @Override\r
- public void addContact (final Contact contact) {\r
- this.contacts.add(contact);\r
- }\r
-\r
- /**\r
- * Let the user add a new other address\r
- */\r
- @Override\r
- public void addOtherAddress () {\r
- throw new UnsupportedOperationException("Not supported yet.");\r
- }\r
-\r
- /**\r
- * Let the user change other address\r
- */\r
- @Override\r
- public void changeOtherAddress () {\r
- throw new UnsupportedOperationException("Not supported yet.");\r
- }\r
-\r
- /**\r
- * Allows the user to change his/her own data\r
- */\r
- @Override\r
- public void changeOwnData () {\r
- /*\r
- * First check if the user has registered own contact, before that\r
- * nothing can be changed.\r
- */\r
- if (!this.isOwnContactAdded()) {\r
- // Not added\r
- this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben.");\r
- \r
- // Skip any below code\r
- return;\r
- }\r
- \r
- // Instance\r
- Contact contact = this.getOwnContact();\r
- \r
- // It must be found\r
- assert(contact instanceof Contact);\r
- \r
- // Display contact\r
- contact.show(this.getClient());\r
- \r
- try {\r
- // Ask user what to change\r
- this.getClient().userChooseChangeContactData(contact);\r
- } catch (final UnhandledUserChoiceException ex) {\r
- this.getLogger().catching(ex);\r
- }\r
- }\r
-\r
- /**\r
- * Let the user delete other address\r
- */\r
- @Override\r
- public void deleteOtherAddress () {\r
- throw new UnsupportedOperationException("Not supported yet.");\r
- }\r
-\r
- /**\r
- * Let the user change address data\r
- * \r
- * @param contact Instance to change data\r
- * @param client Client instance to call back\r
- */\r
- @Override\r
- public void doChangeAddressData (final Contact contact, final Client client) {\r
- // First display it again\r
- client.displayAddressBox(contact);\r
-\r
- // Is it own data?\r
- if (contact.isOwnContact()) {\r
- // Own address data\r
- String street = this.enterOwnStreet();\r
-\r
- // Get zip code\r
- int zipCode = this.enterOwnZipCode();\r
-\r
- // Get city name\r
- String city = this.enterOwnCity();\r
-\r
- // Get country code\r
- String countryCode = this.enterOwnCountryCode();\r
-\r
- // Update address data\r
- contact.updateAddressData(street, zipCode, city, countryCode);\r
- } else {\r
- // Other contact's address data to change\r
- throw new UnsupportedOperationException("Changing contact entries not finished.");\r
- }\r
-\r
- // Flush whole list\r
- this.flush();\r
- }\r
-\r
- /**\r
- * Let the user change "name data"\r
- * \r
- * @param contact Instance to change data\r
- * @param client Client instance to call back\r
- */\r
- @Override\r
- public void doChangeNameData (final Contact contact, final Client client) {\r
- // First display them again\r
- client.displayNameBox(contact);\r
-\r
- // Is this own data?\r
- if (contact.isOwnContact()) {\r
- // Re-ask own data\r
- // Gender:\r
- char gender = this.enterOwnGender();\r
-\r
- // Surname\r
- String surname = this.enterOwnSurname();\r
-\r
- // Family name\r
- String familyName = this.enterOwnFamilyName();\r
-\r
- // And company\r
- String companyName = this.enterOwnCompanyName();\r
-\r
- // Update contact instance\r
- contact.updateNameData(gender, surname, familyName, companyName);\r
- } else {\r
- // Then re-ask them ...\r
- throw new UnsupportedOperationException("Changing contact entries not finished.");\r
- }\r
-\r
- // Flush whole list\r
- this.flush();\r
- }\r
-\r
- /**\r
- * Let the user change other data\r
- *\r
- * @param contact Instance to change data\r
- * @param client Client instance to call back\r
- * @todo Didn't handle birthday\r
- */\r
- @Override\r
- public void doChangeOtherData (final Contact contact, final Client client) {\r
- // First display them again\r
- client.displayOtherDataBox(contact);\r
-\r
- // Is this own data?\r
- if (contact.isOwnContact()) {\r
- // Re-ask own data\r
- // Phone number\r
- String phoneNumber = this.enterOwnPhoneNumber();\r
-\r
- // Phone number\r
- String cellNumber = this.enterOwnCellNumber();\r
-\r
- // Fax number\r
- String faxNumber = this.enterOwnFaxNumber();\r
-\r
- // Email address\r
- String email = this.enterOwnEmailAddress();\r
-\r
- // Comment\r
- String comment = this.enterOwnComment();\r
-\r
- // Update contact instance\r
- contact.updateOtherData(phoneNumber, cellNumber, faxNumber, email, null, comment);\r
- } else {\r
- // Then re-ask them ...\r
- throw new UnsupportedOperationException("Changing contact entries not finished.");\r
- }\r
-\r
- // Flush whole list\r
- this.flush();\r
- }\r
-\r
- /**\r
- * Asks user for own data\r
- */\r
- @Override\r
- public void doEnterOwnData () {\r
- // First ask for gender\r
- char gender = this.enterOwnGender();\r
-\r
- // 2nd for surname\r
- String surname = this.enterOwnSurname();\r
- \r
- // And 3rd for family name\r
- String familyName = this.enterOwnFamilyName();\r
-\r
- // Company name ...\r
- String companyName = this.enterOwnCompanyName();\r
-\r
- // Construct UserContact instance\r
- Contact contact = new UserContact(gender, surname, familyName, companyName);\r
-\r
- // Add it to contact "book"\r
- this.registerContact(contact);\r
- }\r
-\r
- /**\r
- * Shuts down this contact manager\r
- */\r
- @Override\r
- public void doShutdown () {\r
- // Shut down the database layer\r
- this.contactDatabase.doShutdown();\r
- }\r
-\r
- /**\r
- * Getter for whole contact list\r
- * \r
- * @return List of all contacts\r
- */\r
- @Override\r
- public List<Contact> getList () {\r
- return Collections.unmodifiableList(this.contacts);\r
- }\r
-\r
- /**\r
- * Checks whether own contact is already added by checking all entries for\r
- * isOwnContact flag\r
- *\r
- * @return Whether own contact is already added\r
- */\r
- @Override\r
- public boolean isOwnContactAdded () {\r
- // Default is not added\r
- boolean isAdded = false;\r
- \r
- // Now get it back from address book, first get an iterator\r
- Iterator<Contact> iterator = this.contacts.iterator();\r
- \r
- // Check entries\r
- while (iterator.hasNext()) {\r
- // Get next entry\r
- Contact contact = iterator.next();\r
- \r
- // Is it valid?\r
- if (contact instanceof Contact) {\r
- // Get flag\r
- isAdded = contact.isOwnContact();\r
- \r
- // Is this own contact?\r
- if (isAdded) {\r
- // Then abort loop\r
- break;\r
- }\r
- }\r
- }\r
- // Return result\r
- return isAdded;\r
- }\r
-\r
- @Override\r
- public void listContacts () {\r
- throw new UnsupportedOperationException("Not supported yet.");\r
- }\r
-\r
- /**\r
- * Adds given contact to address book and flushes all entries to database\r
- *\r
- * @param contact Contact being added\r
- * @todo Add check for book size\r
- */\r
- @Override\r
- public void registerContact (final Contact contact) {\r
- // Check if contact is found\r
- if (this.isContactAlreadyAdded(contact)) {\r
- // Contact already added\r
- // @todo Do something here\r
- } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {\r
- // Own contact already added\r
- // @todo Do something\r
- }\r
- \r
- // Debug message\r
- /* NOISY-DEBUG: */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size()));\r
- \r
- // Add contact to internal list\r
- this.addContact(contact);\r
- \r
- // Flush whole list\r
- this.flush();\r
- }\r
-\r
- @Override\r
- public void searchContacts () {\r
- throw new UnsupportedOperationException("Not supported yet.");\r
- }\r
-\r
- /**\r
- * Getter for size\r
- *\r
- * @return size of contact "book"\r
- */\r
- @Override\r
- public int size () {\r
- return this.contacts.size();\r
- }\r
-\r
- /**\r
- * Asks the user for his/her cellphone number\r
- * \r
- * @return User's cellphone number\r
- */\r
- private String enterOwnCellNumber () {\r
- return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);\r
- }\r
-\r
- /**\r
- * Asks the user for his/her city's name\r
- *\r
- * @return City's name of the user\r
- */\r
- private String enterOwnCity () {\r
- return this.getClient().enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);\r
- }\r
-\r
- /**\r
- * Asks the user for his/her city's name\r
- *\r
- * @return City's name of the user\r
- */\r
- private String enterOwnComment () {\r
- return this.getClient().enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);\r
- }\r
-\r
- /**\r
- * Asks the user for his/her company name\r
- * \r
- * @return User's company name\r
- */\r
- private String enterOwnCompanyName () {\r
- return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);\r
- }\r
-\r
- /**\r
- * Asks user for his/her own country code\r
- * \r
- * @return User's own country code\r
- */\r
- private String enterOwnCountryCode () {\r
- return this.getClient().enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();\r
- }\r
-\r
- /**\r
- * Asks user for his/her own country code\r
- * \r
- * @return User's own country code\r
- */\r
- private String enterOwnEmailAddress () {\r
- return this.getClient().enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);\r
- }\r
-\r
- /**\r
- * Asks the user for family name\r
- * \r
- * @return Family name of the user\r
- */\r
- private String enterOwnFamilyName () {\r
- return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);\r
- }\r
-\r
- /**\r
- * Asks the user for family name\r
- * \r
- * @return Family name of the user\r
- */\r
- private String enterOwnFaxNumber () {\r
- return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);\r
- }\r
-\r
- /**\r
- * Asks the user for gender, until a valid has been entered\r
- * \r
- * @return Gender of the user\r
- */\r
- private char enterOwnGender () {\r
- return this.getClient().enterChar(new char[] {'M', 'F', 'C'}, "Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");\r
- }\r
-\r
- /**\r
- * Asks the user for phone number\r
- * \r
- * @return Phone number of the user\r
- */\r
- private String enterOwnPhoneNumber () {\r
- return this.getClient().enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);\r
- }\r
-\r
- /**\r
- * Asks the user for own street (including number)\r
- */\r
- private String enterOwnStreet () {\r
- return this.getClient().enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);\r
- }\r
-\r
- /**\r
- * Asks the user for surname\r
- * @return Surname of the user\r
- */\r
- private String enterOwnSurname () {\r
- return this.getClient().enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);\r
- }\r
-\r
- /**\r
- * Asks the user for own ZIP code\r
- * @return ZIP code\r
- */\r
- private int enterOwnZipCode () {\r
- return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");\r
- }\r
-\r
- /**\r
- * Flushes all entries by calling database backend\r
- */\r
- private void flush () {\r
- // Flusgh all\r
- this.getContactDatabase().flushAllContacts();\r
- }\r
-\r
- /**\r
- * A ContactWrapper instance\r
- *\r
- * @return the database\r
- */\r
- private ContactWrapper getContactDatabase () {\r
- return this.contactDatabase;\r
- }\r
-\r
- /**\r
- * "Getter" for own contact instance or null if not found\r
- *\r
- * @return Contact instance or null\r
- */\r
- private Contact getOwnContact () {\r
- // Now get it back from address book, first get an iterator\r
- Iterator<Contact> iterator = this.contacts.iterator();\r
-\r
- // Init instance\r
- Contact contact = null;\r
-\r
- // Search all contact\r
- while (iterator.hasNext()) {\r
- // Get next instance\r
- Contact next = iterator.next();\r
-\r
- // Is this own contact?\r
- if (next.isOwnContact()) {\r
- // Found it\r
- contact = next;\r
- break;\r
- \r
- }\r
- }\r
-\r
- // Return instance or null\r
- return contact;\r
- }\r
-\r
- /**\r
- * Checks whether given contact was found in "address book"\r
- *\r
- * @param checkContact Contact to be checked\r
- * @return TRUE if found, FALSE if not found\r
- */\r
- private boolean isContactAlreadyAdded (final Contact checkContact) throws NullPointerException {\r
- // Default is not found\r
- boolean isFound = false;\r
-\r
- // Debug message\r
- //* NOISY-DEBUG: */ this.getLogger().debug("Checking '" + this.contacts.length + "' entries...");\r
-\r
- // Now get it back from address book, first get an iterator\r
- Iterator<Contact> iterator = this.contacts.iterator();\r
-\r
- // Check entries\r
- while (iterator.hasNext()) {\r
- // Get next entry\r
- Contact contact = iterator.next();\r
-\r
- // Debug message\r
- //* NOISY-DEBUG: */ this.getLogger().debug("contact=" + contact + ",checkContent=" + checkContact);\r
-\r
- // Is it valid?\r
- if ((contact instanceof Contact) && ((contact.equals(checkContact)))) {\r
- // Found matching entry\r
- isFound = true;\r
- break;\r
- }\r
- }\r
-\r
- // Return result\r
- return isFound;\r
- }\r
-}\r