import java.util.Iterator;
import java.util.List;
import org.mxchange.addressbook.client.AddressbookClient;
-import org.mxchange.addressbook.database.frontend.contact.AddressbookContactDatabaseFrontend;
-import org.mxchange.addressbook.database.frontend.contact.AddressbookContactFrontend;
import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
import org.mxchange.jcore.client.Client;
-import org.mxchange.jcore.contact.Contact;
-import org.mxchange.jcore.contact.Gender;
-import org.mxchange.jcore.database.frontend.DatabaseFrontend;
-import org.mxchange.jcore.database.storage.Storable;
-import org.mxchange.jcore.exceptions.BadTokenException;
-import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
-import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
import org.mxchange.jcore.manager.BaseManager;
+import org.mxchange.jcore.model.contact.Contact;
+import org.mxchange.jcore.model.contact.gender.Gender;
+import org.mxchange.jcoreeelogger.beans.local.logger.Log;
+import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
/**
* A manager for contacts.
- *
+ * <p>
* @author Roland Haeder
* @version 0.0
*/
-public class AddressbookContactManager extends BaseManager implements ManageableAddressbookContact {
+public class AddressbookContactManager extends BaseManager implements ManageableContactAddressbook {
/**
* Column name list
*/
private final List<String> columnNames;
+ /**
+ * Logger instance
+ */
+ @Log
+ private LoggerBeanLocal logger;
+
/**
* Translated column name list
*/
/**
* Constructor which accepts maxContacts for maximum (initial) contacts and
* a client instance.
- *
+ * <p>
* @param client Client instance to use
- * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured database backend is not supported
* @throws java.sql.SQLException If an SQL error occurs
*/
- public AddressbookContactManager (final Client client) throws UnsupportedDatabaseBackendException, SQLException {
+ public AddressbookContactManager (final Client client) throws SQLException {
// Trace message
- this.getLogger().trace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("client={1} - CALLED!", client)); //NOI18N
// Make sure all parameters are set correctly
if (null == client) {
this.fillColumnNamesFromBundle();
// Debug message
- //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);
+ //* NOISY-DEBUG: */ this.getLogger().logDebug("client=" + client);
}
- /**
- * Adds given Contact instance to list
- *
- * @param contact Contact instance to add
- */
@Override
- public void addContact (final Contact contact) throws ContactAlreadyAddedException {
+ public void addContact (final Contact contact) throws ContactAlreadyAddedException {
// Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
// Contact instance must not be null
if (null == contact) {
((AddressbookContactFrontend) this.getFrontend()).addContact(contact);
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
- /**
- * Let the user add a new other address
- */
@Override
public void doAddOtherAddress () {
throw new UnsupportedOperationException("Not supported yet."); //NOI18N
}
- /**
- * Let the user change address data
- *
- * @param contact Instance to change data
- */
@Override
public void doChangeAddressData (final Contact contact) {
// Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
// Contact must not be null
if (null == contact) {
// Abort here
throw new NullPointerException("contact is null"); //NOI18N
}
+ if (!(this.getClient() instanceof AddressbookClient)) {
+ // Cannot cast
+ throw new IllegalArgumentException(MessageFormat.format("this.getClient()={0} does not implement AddressbookClient", this.getClient())); //NOI18N
+ }
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
}
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
- /**
- * Let the user change "name data"
- *
- * @param contact Instance to change data
- */
@Override
public void doChangeNameData (final Contact contact) {
// Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
// Contact must not be null
if (null == contact) {
}
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
- /**
- * Let the user change other address
- */
@Override
public void doChangeOtherAddress () {
throw new UnsupportedOperationException("Not supported yet."); //NOI18N
}
- /**
- * Let the user change other data
- *
- * @param contact Instance to change data
- * @todo Didn't handle birthday
- */
@Override
public void doChangeOtherData (final Contact contact) {
// Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
// Contact must not be null
if (null == contact) {
}
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
- /**
- * Allows the user to change his/her own data
- */
@Override
- public void doChangeOwnData () throws IOException , BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
+ public void doChangeOwnData () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
/*
* First check if the user has registered own contact, before that
Contact contact = this.getOwnContact();
// It must be found
- assert (contact instanceof Contact);
+ assert (contact instanceof Contact) : ": contact is not implementing Contact: " + contact;
// Display contact
- contact.show(this.getClient());
+ this.getClient().show(contact);
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
// Ask user what to change
client.userChooseChangeContactData(contact);
} catch (final UnhandledUserChoiceException ex) {
- this.getLogger().catching(ex);
+ this.getLogger().logException(ex);
}
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
- /**
- * Let the user delete other address
- */
@Override
public void doDeleteOtherAddress () {
throw new UnsupportedOperationException("Not supported yet."); //NOI18N
}
- /**
- * Asks user for own data
- */
@Override
- public void doEnterOwnData () throws ContactAlreadyAddedException, IOException , BadTokenException {
+ public void doEnterOwnData () throws ContactAlreadyAddedException, IOException {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Is own contact already added?
if (this.isOwnContactAdded()) {
}
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
@Override
throw new UnsupportedOperationException("Not supported yet."); //NOI18N
}
- /**
- * Shuts down this contact manager
- *
- * @throws java.sql.SQLException If an SQL error occurs
- * @throws java.io.IOException If an IO error occurs
- */
@Override
public void doShutdown () throws SQLException, IOException {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Shut down the database layer
((AddressbookContactFrontend) this.getFrontend()).doShutdown();
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
- /**
- * Asks the user for his/her cellphone number
- *
- * @return User's cellphone number
- */
@Override
public String enterOwnCellNumber () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(5, 30, "Bitte geben Sie Ihre Handynummer an: ", true);
}
- /**
- * Asks the user for his/her city's name
- *
- * @return City's name of the user
- */
@Override
public String enterOwnCity () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(3, 50, "Bitte geben Sie Ihre Wohnort ein: ", false);
}
- /**
- * Asks the user for his/her city's name
- *
- * @return City's name of the user
- */
@Override
public String enterOwnComment () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(0, 100, "Kommentar zu Ihrem Eintrag: ", true);
}
- /**
- * Asks the user for his/her company name
- *
- * @return User's company name
- */
@Override
public String enterOwnCompanyName () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(5, 50, "Bitte geben Sie Ihre Firmenbezeichnung ein: ", true);
}
- /**
- * Asks user for his/her own country code
- *
- * @return User's own country code
- */
@Override
public String enterOwnCountryCode () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(2, 2, "Bitte geben Sie den zweistelligen Ländercode von Ihrem Land ein: ", false).toUpperCase();
}
- /**
- * Asks user for his/her own country code
- *
- * @return User's own country code
- */
@Override
public String enterOwnEmailAddress () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(10, 50, "Bitte geben Sie Ihre Email-Adresse ein: ", true);
}
- /**
- * Asks the user for family name
- *
- * @return Family name of the user
- */
@Override
public String enterOwnFamilyName () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(2, 50, "Bitte geben Sie Ihren Nachnamen ein: ", false);
}
- /**
- * Asks the user for family name
- *
- * @return Family name of the user
- */
@Override
public String enterOwnFaxNumber () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(5, 30, "Bitte geben Sie Ihre Faxnummer an: ", true);
}
- /**
- * Asks the user for surname
- *
- * @return Surname of the user
- */
@Override
public String enterOwnFirstName () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
+ this.getLogger().logTrace("CALLED!"); //NOI18N
+
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
-
+
return client.enterString(2, 50, "Bitte geben Sie Ihren Vornamen ein: ", false);
}
- /**
- * Asks the user for gender, until a valid has been entered
- *
- * @return Gender of the user
- */
@Override
public Gender enterOwnGender () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterGender("Bitte geben Sie die Anrede ein: (M=Herr, F=Frau, C=Firma): ");
}
- /**
- * Asks the user for phone number
- *
- * @return Phone number of the user
- */
@Override
public String enterOwnPhoneNumber () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(5, 30, "Bitte geben Sie Ihre Telefonnummer an: ", true);
}
- /**
- * Asks the user for own street (including number)
- *
- * @return Own street an number
- */
@Override
public String enterOwnStreet () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
}
- /**
- * Asks the user for own ZIP code
- *
- * @return ZIP code
- */
@Override
public int enterOwnZipCode () {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Get and cast client instance
AddressbookClient client = (AddressbookClient) this.getClient();
return this.columnNames.size();
}
- /**
- * Getter for column name at given index.
- *
- * @param columnIndex Column index
- * @return Database column name
- */
@Override
public String getColumnName (final int columnIndex) {
assert (this.columnNames instanceof List) : "this.columnNames is not initialized"; //NOI18N
return this.columnNames.get(columnIndex);
}
- /**
- * Getter for translated column name at given index.
- *
- * @param columnIndex Column index
- * @return Human-readable column name
- */
@Override
public String getTranslatedColumnName (final int columnIndex) {
assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
return this.translatedColumnNames.get(columnIndex);
}
- /**
- * Somewhat "getter" for value from given row and column index
- *
- * @param rowIndex Row index
- * @param columnIndex Column index
- * @return Value from given row/column
- */
@Override
public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace message
- this.getLogger().trace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex));
+ this.getLogger().logTrace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); //NOI18N
// Then get specific row from database which is a Contact instance
- Storable storeable = this.getFrontend().getStorableAtRow(rowIndex);
+ Storable storable = this.getFrontend().getStorableAtRow(rowIndex);
// Debug message
- this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
+ this.getLogger().logDebug(MessageFormat.format("storable={0}", storable)); //NOI18N
// It may return null
- if (null == storeable) {
+ if (null == storable) {
// Nothing found
- this.getLogger().warn("contact is null - returning null ...");
+ this.getLogger().logWarning("contact is null - returning null ..."); //NOI18N
return null;
}
String columnName = this.getColumnName(columnIndex);
// Debug message
- this.getLogger().debug(MessageFormat.format("columnName={0}", columnName));
+ this.getLogger().logDebug(MessageFormat.format("columnName={0}", columnName)); //NOI18N
// Now get that column
Object value = null;
try {
- value = storeable.getValueFromColumn(columnName);
+ value = storable.getValueFromColumn(columnName);
} catch (final IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
this.abortProgramWithException(ex);
}
// Trace message
- this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value));
+ this.getLogger().logTrace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
// Return it
return value;
}
- /**
- * Checks whether own contact is already added by checking all entries for
- * isOwnContact flag
- *
- * @return Whether own contact is already added
- */
@Override
- public boolean isOwnContactAdded () throws IOException, BadTokenException {
+ public boolean isOwnContactAdded () throws IOException {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Init variable
boolean isAdded = false;
try {
// Deligate this call to frontend
isAdded = ((AddressbookContactFrontend) this.getFrontend()).isOwnContactFound();
- } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ } catch (final SQLException | IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
// Something bad happened
this.abortProgramWithException(ex);
}
// Trace message
- this.getLogger().trace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("isAdded={0} : EXIT!", isAdded)); //NOI18N
// Return result
return isAdded;
}
- /**
- * Adds given contact to address book and flushes all entries to database
- *
- * @param contact Contact being added
- * @todo Add check for book size
- */
@Override
public void registerContact (final Contact contact) {
// Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("contact={0} CALLED!", contact)); //NOI18N
// Sanity check
if (null == contact) {
throw new NullPointerException("contact is null"); //NOI18N
}
try {
- // Debug message
- /* NOISY-DEBUG: */ this.getLogger().debug(MessageFormat.format("Adding '{0}' '{1}' at pos '{2}' ...", contact.getFirstName(), contact.getFamilyName(), this.size())); //NOI18N
-
- // Check if contact is found
+ // Check if contact is found
if (((AddressbookContactFrontend) this.getFrontend()).isContactFound(contact)) {
// Contact already added
- // @todo Do something here
+ // TODO Do something here
} else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
// Own contact already added
- // @todo Do something
+ // TODO Do something
}
// Add contact to internal list
this.addContact(contact);
- } catch (final ContactAlreadyAddedException | BadTokenException | SQLException | IOException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ } catch (final ContactAlreadyAddedException | SQLException | IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
// Abort here
this.abortProgramWithException(ex);
}
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
- @Override
- public final int size () throws IOException {
- // Init size
- int size = -1;
-
- try {
- size = ((AddressbookContactFrontend) this.getFrontend()).getContactsCount();
- } catch (final SQLException ex) {
- // Something happened
- this.abortProgramWithException(ex);
- }
+ /**
+ * Logs given exception and exits program
+ *
+ * @param throwable Throwable
+ */
+ private void abortProgramWithException (Throwable throwable) {
+ // Log exception
+ this.logException(throwable);
- // Return amount
- return size;
+ // Abort here
+ System.exit(1);
}
/**
assert (this.translatedColumnNames instanceof List) : "this.translatedColumnNames is not initialized"; //NOI18N
// Debug message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// First get an iterator from key set to iterate over
Iterator<String> iterator = this.getBundle().keySet().iterator();
// Then iterate over all
while (iterator.hasNext()) {
// Get next element
- String key = iterator.next();
+ String key = iterator.next().toLowerCase();
// Does the key start with AddressbookContactManager.columnName ?
if (key.startsWith("ContactManager.columnName")) { //NOI18N
// This is the wanted entry.
- this.getLogger().debug(MessageFormat.format("key={0}", key)); //NOI18N
+ this.getLogger().logDebug(MessageFormat.format("key={0}", key)); //NOI18N
// Convert string to array based on delimiter '.'
- String[] tokens = this.getArrayFromString(key, ".", 4);
+ String[] tokens = this.getArrayFromString(key, "."); //NOI18N
// Token array must contain 4 elements (AddressbookContactManager.columnName.foo.text)
- assert(tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens));
+ assert (tokens.length == 4) : MessageFormat.format("Array tokens contains not 4 elements: {0}", Arrays.toString(tokens)); //NOI18N
// Get pre-last element
String columnName = tokens[tokens.length - 2];
// Debug message
- this.getLogger().debug(MessageFormat.format("columnName={0} - adding ...", columnName));
+ this.getLogger().logDebug(MessageFormat.format("columnName={0} - adding ...", columnName)); //NOI18N
// So add it
this.columnNames.add(columnName);
}
// Debug message
- this.getLogger().trace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
}
/**
- * "Getter" for own contact instance or null if not found
+ * Getter for logger instance
*
+ * @return Logger instance
+ */
+ private LoggerBeanLocal getLogger () {
+ return this.logger;
+ }
+
+ /**
+ * "Getter" for own contact instance or null if not found
+ * <p>
* @return Contact instance or null
* @throws java.sql.SQLException If an SQL error occurs
* @throws java.io.IOException If an IO error occurs
- * @throws org.mxchange.jcore.exceptions.BadTokenException Continued throw
- * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
* @throws java.lang.NoSuchMethodException If a method cannot be found
* @throws java.lang.IllegalAccessException If a method is not accessible
* @throws java.lang.reflect.InvocationTargetException Any other problems?
*/
- private Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ private Contact getOwnContact () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().logTrace("CALLED!"); //NOI18N
// Deligate this call to database frontend
Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
// Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
+ this.getLogger().logTrace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
// Return instance or null
return contact;
}
+
+ /**
+ * Logs given exception
+ * <p>
+ * @param exception Throwable
+ */
+ protected void logException (final Throwable exception) {
+ this.getLogger().logException(exception);
+ }
}