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.io.IOException;
20 import java.lang.reflect.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import java.util.List;
27 import org.mxchange.addressbook.client.AddressbookClient;
28 import org.mxchange.addressbook.database.frontend.contact.AddressbookContactDatabaseFrontend;
29 import org.mxchange.addressbook.database.frontend.contact.AddressbookContactFrontend;
30 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
31 import org.mxchange.jcore.client.Client;
32 import org.mxchange.jcore.contact.Contact;
33 import org.mxchange.jcore.contact.Gender;
34 import org.mxchange.jcore.exceptions.BadTokenException;
35 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
36 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
37 import org.mxchange.jcore.manager.BaseManager;
40 * A manager for contacts.
42 * @author Roland Haeder
45 public class AddressbookContactManager extends BaseManager implements ManageableAddressbookContact {
50 private final List<String> columnNames;
53 * A AddressbookContactFrontend instance
55 private final AddressbookContactFrontend contactDatabase;
58 * Translated column name list
60 private final List<String> translatedColumnNames;
63 * Constructor which accepts maxContacts for maximum (initial) contacts and
66 * @param client Client instance to use
67 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured database backend is not supported
68 * @throws java.sql.SQLException If an SQL error occurs
70 public AddressbookContactManager (final Client client) throws UnsupportedDatabaseBackendException, SQLException {
72 this.getLogger().trace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
74 // Make sure all parameters are set correctly
77 throw new NullPointerException("client is null"); //NOI18N
80 // Set client instance
81 this.setClient(client);
83 // Init database connection
84 this.contactDatabase = new AddressbookContactDatabaseFrontend(this);
87 this.columnNames = new ArrayList<>(15);
88 this.translatedColumnNames = new ArrayList<>(15);
91 this.fillColumnNamesFromBundle();
94 //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);
98 * Adds given Contact instance to list
100 * @param contact Contact instance to add
103 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
105 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
107 // Contact instance must not be null
108 if (contact == null) {
110 throw new NullPointerException("contact is null"); //NOI18N
114 this.getContactDatabase().addContact(contact);
117 this.getLogger().trace("EXIT!"); //NOI18N
121 * Let the user add a new other address
124 public void doAddOtherAddress () {
125 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
129 * Let the user change address data
131 * @param contact Instance to change data
134 public void doChangeAddressData (final Contact contact) {
136 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
138 // Contact must not be null
139 if (contact == null) {
141 throw new NullPointerException("contact is null"); //NOI18N
144 // Get and cast client instance
145 AddressbookClient client = (AddressbookClient) this.getClient();
147 // First display it again
148 client.displayAddressBox(contact);
151 if (contact.isOwnContact()) {
152 // Deligate to client
153 client.doChangeOwnAddressData(contact);
155 // Other contact's address data to change
156 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
160 this.getLogger().trace("EXIT!"); //NOI18N
164 * Let the user change "name data"
166 * @param contact Instance to change data
169 public void doChangeNameData (final Contact contact) {
171 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
173 // Contact must not be null
174 if (contact == null) {
176 throw new NullPointerException("contact is null"); //NOI18N
179 // Get and cast client instance
180 AddressbookClient client = (AddressbookClient) this.getClient();
182 // First display them again
183 client.displayNameBox(contact);
186 if (contact.isOwnContact()) {
188 client.doChangeOwnNameData(contact);
190 // Then re-ask them ...
191 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
195 this.getLogger().trace("EXIT!"); //NOI18N
199 * Let the user change other address
202 public void doChangeOtherAddress () {
203 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
207 * Let the user change other data
209 * @param contact Instance to change data
210 * @todo Didn't handle birthday
213 public void doChangeOtherData (final Contact contact) {
215 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
217 // Contact must not be null
218 if (contact == null) {
220 throw new NullPointerException("contact is null"); //NOI18N
223 // Get and cast client instance
224 AddressbookClient client = (AddressbookClient) this.getClient();
226 // First display them again
227 client.displayOtherDataBox(contact);
230 if (contact.isOwnContact()) {
232 client.doChangeOwnOtherData(contact);
234 // Then re-ask them ...
235 throw new UnsupportedOperationException("Changing contact entries not finished."); //NOI18N
239 this.getLogger().trace("EXIT!"); //NOI18N
243 * Allows the user to change his/her own data
246 public void doChangeOwnData () throws IOException , BadTokenException{
248 this.getLogger().trace("CALLED!"); //NOI18N
251 * First check if the user has registered own contact, before that
252 * nothing can be changed.
254 if (!this.isOwnContactAdded()) {
256 this.getClient().outputMessage("Sie haben noch nicht Ihre Daten eingegeben."); //NOI18N
258 // Skip any below code
263 Contact contact = this.getOwnContact();
266 assert (contact instanceof Contact);
269 contact.show(this.getClient());
271 // Get and cast client instance
272 AddressbookClient client = (AddressbookClient) this.getClient();
275 // Ask user what to change
276 client.userChooseChangeContactData(contact);
277 } catch (final UnhandledUserChoiceException ex) {
278 this.getLogger().catching(ex);
282 this.getLogger().trace("EXIT!"); //NOI18N
286 * Let the user delete other address
289 public void doDeleteOtherAddress () {
290 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
294 * Asks user for own data
297 public void doEnterOwnData () throws ContactAlreadyAddedException, IOException , BadTokenException {
299 this.getLogger().trace("CALLED!"); //NOI18N
301 // Is own contact already added?
302 if (this.isOwnContactAdded()) {
303 // Don't continue here
304 throw new ContactAlreadyAddedException();
307 // Get and cast client instance
308 AddressbookClient client = (AddressbookClient) this.getClient();
310 // Deligate this call to the client
311 Contact contact = client.doEnterOwnData();
314 if (contact instanceof Contact) {
315 // Add it to contact "book"
316 this.registerContact(contact);
320 this.getLogger().trace("EXIT!"); //NOI18N
324 public void doListContacts () {
325 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
329 public void doSearchContacts () {
330 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
334 * Shuts down this contact manager
336 * @throws java.sql.SQLException If an SQL error occurs
337 * @throws java.io.IOException If an IO error occurs
340 public void doShutdown () throws SQLException, IOException {
342 this.getLogger().trace("CALLED!"); //NOI18N
344 // Shut down the database layer
345 this.getContactDatabase().doShutdown();
348 this.getLogger().trace("EXIT!"); //NOI18N
352 * Asks the user for his/her cellphone number
354 * @return User's cellphone number
357 public String enterOwnCellNumber () {
359 this.getLogger().trace("CALLED!"); //NOI18N
361 // Get and cast client instance
362 AddressbookClient client = (AddressbookClient) this.getClient();
364 return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
368 * Asks the user for his/her city's name
370 * @return City's name of the user
373 public String enterOwnCity () {
375 this.getLogger().trace("CALLED!"); //NOI18N
377 // Get and cast client instance
378 AddressbookClient client = (AddressbookClient) this.getClient();
380 return client.enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
384 * Asks the user for his/her city's name
386 * @return City's name of the user
389 public String enterOwnComment () {
391 this.getLogger().trace("CALLED!"); //NOI18N
393 // Get and cast client instance
394 AddressbookClient client = (AddressbookClient) this.getClient();
396 return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
400 * Asks the user for his/her company name
402 * @return User's company name
405 public String enterOwnCompanyName () {
407 this.getLogger().trace("CALLED!"); //NOI18N
409 // Get and cast client instance
410 AddressbookClient client = (AddressbookClient) this.getClient();
412 return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
416 * Asks user for his/her own country code
418 * @return User's own country code
421 public String enterOwnCountryCode () {
423 this.getLogger().trace("CALLED!"); //NOI18N
425 // Get and cast client instance
426 AddressbookClient client = (AddressbookClient) this.getClient();
428 return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
432 * Asks user for his/her own country code
434 * @return User's own country code
437 public String enterOwnEmailAddress () {
439 this.getLogger().trace("CALLED!"); //NOI18N
441 // Get and cast client instance
442 AddressbookClient client = (AddressbookClient) this.getClient();
444 return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
448 * Asks the user for family name
450 * @return Family name of the user
453 public String enterOwnFamilyName () {
455 this.getLogger().trace("CALLED!"); //NOI18N
457 // Get and cast client instance
458 AddressbookClient client = (AddressbookClient) this.getClient();
460 return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
464 * Asks the user for family name
466 * @return Family name of the user
469 public String enterOwnFaxNumber () {
471 this.getLogger().trace("CALLED!"); //NOI18N
473 // Get and cast client instance
474 AddressbookClient client = (AddressbookClient) this.getClient();
476 return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
480 * Asks the user for gender, until a valid has been entered
482 * @return Gender of the user
485 public Gender enterOwnGender () {
487 this.getLogger().trace("CALLED!"); //NOI18N
489 // Get and cast client instance
490 AddressbookClient client = (AddressbookClient) this.getClient();
492 return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
496 * Asks the user for phone number
498 * @return Phone number of the user
501 public String enterOwnPhoneNumber () {
503 this.getLogger().trace("CALLED!"); //NOI18N
505 // Get and cast client instance
506 AddressbookClient client = (AddressbookClient) this.getClient();
508 return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
512 * Asks the user for own street (including number)
514 * @return Own street an number
517 public String enterOwnStreet () {
519 this.getLogger().trace("CALLED!"); //NOI18N
521 // Get and cast client instance
522 AddressbookClient client = (AddressbookClient) this.getClient();
524 return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
528 * Asks the user for surname
530 * @return Surname of the user
533 public String enterOwnSurname () {
535 this.getLogger().trace("CALLED!"); //NOI18N
537 // Get and cast client instance
538 AddressbookClient client = (AddressbookClient) this.getClient();
540 return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
544 * Asks the user for own ZIP code
549 public int enterOwnZipCode () {
551 this.getLogger().trace("CALLED!"); //NOI18N
553 // Get and cast client instance
554 AddressbookClient client = (AddressbookClient) this.getClient();
556 return client.enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");
560 public final int getColumnCount () {
561 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
563 return this.columnNames.size();
567 * Getter for column name at given index.
569 * @param columnIndex Column index
570 * @return Database column name
573 public String getColumnName (final int columnIndex) {
574 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
576 // Get column name at index
577 return this.columnNames.get(columnIndex);
581 * Getter for translated column name at given index.
583 * @param columnIndex Column index
584 * @return Human-readable column name
587 public String getTranslatedColumnName (final int columnIndex) {
588 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
590 // Get column name at index
591 return this.translatedColumnNames.get(columnIndex);
595 * Somewhat "getter" for value from given row and column index
597 * @param rowIndex Row index
598 * @param columnIndex Column index
599 * @return Value from given row/column
602 public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
604 this.getLogger().trace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex));
606 // Then get specific row from database which is a Contact instance
607 Contact contact = this.getContactDatabase().readSingleContact(rowIndex);
610 this.getLogger().debug(MessageFormat.format("contact={0}", contact));
612 // It may return null
613 if (contact == null) {
615 this.getLogger().warn("contact is null - returning null ...");
619 // Convert column index -> name
620 String columnName = this.getColumnName(columnIndex);
623 this.getLogger().debug(MessageFormat.format("columnName={0}", columnName));
625 // Now get that column
628 value = contact.getValueFromColumn(columnName);
629 } catch (final IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
630 this.abortProgramWithException(ex);
634 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value));
641 * Checks whether own contact is already added by checking all entries for
644 * @return Whether own contact is already added
647 public boolean isOwnContactAdded () throws IOException, BadTokenException {
649 this.getLogger().trace("CALLED!"); //NOI18N
652 boolean isAdded = false;
655 // Deligate this call to frontend
656 isAdded = this.getContactDatabase().isOwnContactFound();
657 } catch (final SQLException | IOException | BadTokenException ex) {
658 // Something bad happened
659 this.abortProgramWithException(ex);
663 this.getLogger().trace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
670 * Adds given contact to address book and flushes all entries to database
672 * @param contact Contact being added
673 * @todo Add check for book size
676 public void registerContact (final Contact contact) {
678 this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
681 if (contact == null) {
683 throw new NullPointerException("contact is null"); //NOI18N
687 /* NOISY-DEBUG: */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getSurname(), contact.getFamilyName(), this.size())); //NOI18N
689 // Check if contact is found
690 if (this.getContactDatabase().isContactFound(contact)) {
691 // Contact already added
692 // @todo Do something here
693 } else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
694 // Own contact already added
695 // @todo Do something
698 // Add contact to internal list
699 this.addContact(contact);
700 } catch (final ContactAlreadyAddedException | BadTokenException | IOException ex) {
702 this.abortProgramWithException(ex);
706 this.getLogger().trace("EXIT!"); //NOI18N
712 * @return size of contact "book"
715 public final int size () {
720 size = this.getContactDatabase().getContactsCount();
721 } catch (final SQLException ex) {
722 // Something happened
723 this.abortProgramWithException(ex);
731 * Fills the column names array with strings from bundle
733 private void fillColumnNamesFromBundle () {
734 assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
735 assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
738 this.getLogger().trace("CALLED!"); //NOI18N
740 // First get an iterator from key set to iterate over
741 Iterator<String> iterator = this.getBundle().keySet().iterator();
743 // Then iterate over all
744 while (iterator.hasNext()) {
746 String key = iterator.next();
748 // Does the key start with AddressbookContactManager.columnName ?
749 if (key.startsWith("ContactManager.columnName")) { //NOI18N
750 // This is the wanted entry.
751 this.getLogger().debug(MessageFormat.format("key={0}", key)); //NOI18N
753 // Convert string to array based on delimiter '.'
754 String[] tokens = this.getArrayFromString(key, ".", 4);
756 // Token array must contain 4 elements (AddressbookContactManager.columnName.foo.text)
757 assert(tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens));
759 // Get pre-last element
760 String columnName = tokens[tokens.length - 2];
763 this.getLogger().debug(MessageFormat.format("columnName={0} - adding ...", columnName));
766 this.columnNames.add(columnName);
767 this.translatedColumnNames.add(this.getBundle().getString(key));
772 this.getLogger().trace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
776 * A AddressbookContactFrontend instance
778 * @return the database
780 private AddressbookContactFrontend getContactDatabase () {
781 return this.contactDatabase;
785 * "Getter" for own contact instance or null if not found
787 * @return Contact instance or null
789 private Contact getOwnContact () {
791 this.getLogger().trace("CALLED!"); //NOI18N
793 // Deligate this call to database frontend
794 Contact contact = this.getContactDatabase().getOwnContact();
797 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
799 // Return instance or null