package org.mxchange.addressbook.client.console;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Arrays;
import org.mxchange.jcore.contact.Contact;
import org.mxchange.jcore.contact.Gender;
import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
manager.doDeleteOtherAddress();
break;
- case '0': {
- try {
- // Program exit
- this.getApplication().doShutdown();
- } catch (final SQLException | IOException ex) {
- this.abortProgramWithException(ex);
- }
- }
+ case '0':
+ try {
+ // Program exit
+ this.getApplication().doShutdown();
+ } catch (final SQLException | IOException ex) {
+ this.abortProgramWithException(ex);
+ }
break;
default:
// @TODO throw own exception
throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); //NOI18N
}
- } catch (final IOException | BadTokenException ex) {
+ } catch (final IOException | BadTokenException | CorruptedDatabaseFileException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
// Something bad happened
this.abortProgramWithException(ex);
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import javax.swing.BorderFactory;
import org.mxchange.jcore.contact.Contact;
import org.mxchange.jcore.contact.Gender;
import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.jcore.exceptions.FrameAlreadyInitializedException;
import org.mxchange.jcore.model.swing.contact.ContactTableModel;
ManageableAddressbookContact manager = (ManageableAddressbookContact) self.getClient().getManager();
try {
manager.doChangeOwnData();
- } catch (final IOException | BadTokenException ex) {
+ } catch (final IOException | BadTokenException | CorruptedDatabaseFileException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
self.logException(ex);
}
}
return AddressbookContactDatabaseConstants.COLUMN_ID;
}
- /**
- * Some "getter" for own contact instance
- *
- * @return Own contact instance
- */
@Override
- public Contact getOwnContact () {
+ public Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace message
this.getLogger().trace("CALLED!"); //NOI18N
- // Get row index back from backend
- int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
+ // Prepare search instance
+ SearchableCriteria criteria = new SearchCriteria();
+
+ // Add criteria and limit
+ criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
+ criteria.setLimit(1);
+
+ // Then search for it
+ Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
// Debug message
- this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
+ this.getLogger().debug(MessageFormat.format("result={0}", result));
// Init instance
Contact contact = null;
- try {
- // Now simply read the row
- contact = (Contact) this.getBackend().readRow(rowIndex);
- } catch (final BadTokenException ex) {
- // Bad token found
- this.abortProgramWithException(ex);
+ // Is there one row at least?
+ if (result.hasNext()) {
+ // Then get it
+ Storeable storeable = result.next();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
+
+ // Is it same instance?
+ if (!(storeable instanceof Contact)) {
+ // Not same instance
+ throw new IllegalArgumentException(MessageFormat.format("storeable={0} is not implementing Contact", storeable));
+ }
+
+ // Cast it securely
+ contact = (Contact) storeable;
}
// Trace message
return contact;
}
+ @Override
+ public Storeable getStoreableAtRow (final int rowIndex) {
+ throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
+ }
+
/**
* Checks if given Contact is found
*
return result.hasNext();
}
- /**
- * Reads a single row and parses it to a contact instance
- *
- * @param rowIndex Row index (also how much to skip)
- * @return Contact instance
- */
- @Override
- public Contact readSingleContact (final int rowIndex) {
- try {
- // Deligate this to backend instance
- return (Contact) this.getBackend().readRow(rowIndex);
- } catch (final BadTokenException ex) {
- // Bad token found
- this.abortProgramWithException(ex);
- }
-
- // Bad state, should not be reached
- throw new IllegalStateException("This should not be reached");
- }
-
@Override
public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
throw new UnsupportedOperationException("Not supported yet: map=" + map);
* Some "getter" for own contact instance
*
* @return Own contact instance
+ * @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?
*/
- public Contact getOwnContact ();
+ public Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
/**
* Checks whether own contact is found
* @throws java.lang.reflect.InvocationTargetException Any other problems?
*/
public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
-
- /**
- * Reads a single row and parses it to a contact instance
- *
- * @param rowIndex Row index (also how much to skip)
- * @return Contact instance
- */
- public Contact readSingleContact (final int rowIndex);
}
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.Storeable;
import org.mxchange.jcore.exceptions.BadTokenException;
import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
*/
private final List<String> columnNames;
- /**
- * A AddressbookContactFrontend instance
- */
- private final AddressbookContactFrontend contactDatabase;
-
/**
* Translated column name list
*/
this.setClient(client);
// Init database connection
- this.contactDatabase = new AddressbookContactDatabaseFrontend(this);
+ DatabaseFrontend frontend = new AddressbookContactDatabaseFrontend(this);
+ this.setFrontend(frontend);
// Initialize list
this.columnNames = new ArrayList<>(15);
}
// Add it
- this.getContactDatabase().addContact(contact);
+ ((AddressbookContactFrontend) this.getFrontend()).addContact(contact);
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
* Allows the user to change his/her own data
*/
@Override
- public void doChangeOwnData () throws IOException , BadTokenException{
+ public void doChangeOwnData () throws IOException , BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
// Trace message
this.getLogger().trace("CALLED!"); //NOI18N
this.getLogger().trace("CALLED!"); //NOI18N
// Shut down the database layer
- this.getContactDatabase().doShutdown();
+ ((AddressbookContactFrontend) this.getFrontend()).doShutdown();
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
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
+
+ // 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 client.enterString(5, 50, "Bitte geben Sie Ihre Strasse und Hausnummer ein: ", false);
}
- /**
- * Asks the user for surname
- *
- * @return Surname of the user
- */
- @Override
- public String enterOwnFirstName () {
- // Trace message
- this.getLogger().trace("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 own ZIP code
*
this.getLogger().trace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex));
// Then get specific row from database which is a Contact instance
- Contact contact = this.getContactDatabase().readSingleContact(rowIndex);
+ Storeable storeable = this.getFrontend().getStoreableAtRow(rowIndex);
// Debug message
- this.getLogger().debug(MessageFormat.format("contact={0}", contact));
+ this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
// It may return null
- if (contact == null) {
+ if (storeable == null) {
// Nothing found
this.getLogger().warn("contact is null - returning null ...");
return null;
// Now get that column
Object value = null;
try {
- value = contact.getValueFromColumn(columnName);
+ value = storeable.getValueFromColumn(columnName);
} catch (final IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
this.abortProgramWithException(ex);
}
try {
// Deligate this call to frontend
- isAdded = this.getContactDatabase().isOwnContactFound();
+ isAdded = ((AddressbookContactFrontend) this.getFrontend()).isOwnContactFound();
} catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
// Something bad happened
this.abortProgramWithException(ex);
/* 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
- if (this.getContactDatabase().isContactFound(contact)) {
+ if (((AddressbookContactFrontend) this.getFrontend()).isContactFound(contact)) {
// Contact already added
// @todo Do something here
} else if ((contact.isOwnContact()) && (this.isOwnContactAdded())) {
int size = -1;
try {
- size = this.getContactDatabase().getContactsCount();
+ size = ((AddressbookContactFrontend) this.getFrontend()).getContactsCount();
} catch (final SQLException ex) {
// Something happened
this.abortProgramWithException(ex);
this.getLogger().trace(MessageFormat.format("getColumnCount()={0}: EXIT!", this.getColumnCount())); //NOI18N
}
- /**
- * A AddressbookContactFrontend instance
- *
- * @return the database
- */
- private AddressbookContactFrontend getContactDatabase () {
- return this.contactDatabase;
- }
-
/**
* "Getter" for own contact instance or null if not found
*
* @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 () {
+ private Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace message
this.getLogger().trace("CALLED!"); //NOI18N
// Deligate this call to database frontend
- Contact contact = this.getContactDatabase().getOwnContact();
+ Contact contact = ((AddressbookContactFrontend) this.getFrontend()).getOwnContact();
// Trace message
this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact)); //NOI18N
package org.mxchange.addressbook.manager.contact;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.SQLException;
import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
import org.mxchange.jcore.contact.Contact;
import org.mxchange.jcore.contact.Gender;
import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.jcore.manager.database.ManageableDatabase;
/**
/**
* Let the user change own data
- * @throws java.io.IOException If an IO error was found
- * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @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?
*/
- public void doChangeOwnData () throws IOException , BadTokenException;
+ public void doChangeOwnData () throws IOException , BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
/**
* Let the user delete other address