From: Roland Haeder Date: Mon, 24 Aug 2015 12:18:10 +0000 (+0200) Subject: Updated jcore + added more thrown exceptions + catched them X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8ef9a410dc1a2a9c14764fd9d0fcce49dd3e58eb;p=jaddressbook-share-lib.git Updated jcore + added more thrown exceptions + catched them Signed-off-by:Roland Häder --- diff --git a/lib/jcore.jar b/lib/jcore.jar index d426e0d..5a46f43 100644 Binary files a/lib/jcore.jar and b/lib/jcore.jar differ diff --git a/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/src/org/mxchange/addressbook/client/console/ConsoleClient.java index a9c81cb..06a6cc2 100644 --- a/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -17,6 +17,7 @@ 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; @@ -36,6 +37,7 @@ import org.mxchange.jcore.application.Application; 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; @@ -381,21 +383,20 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC 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); } diff --git a/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java b/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java index 4bdb6e9..73dc71a 100644 --- a/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java +++ b/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java @@ -25,6 +25,7 @@ import java.awt.event.MouseEvent; 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; @@ -55,6 +56,7 @@ import org.mxchange.jcore.client.gui.ClientFrame; 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; @@ -708,7 +710,7 @@ public class AddressbookFrame extends BaseAddressbookSystem implements ClientFra 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); } } diff --git a/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java b/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java index e51b942..25f8496 100644 --- a/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java +++ b/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java @@ -175,31 +175,43 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp 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 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 @@ -209,6 +221,11 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp return contact; } + @Override + public Storeable getStoreableAtRow (final int rowIndex) { + throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex); + } + /** * Checks if given Contact is found * @@ -280,26 +297,6 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp 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 map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { throw new UnsupportedOperationException("Not supported yet: map=" + map); diff --git a/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactFrontend.java b/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactFrontend.java index d1c46b7..b06940a 100644 --- a/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactFrontend.java +++ b/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactFrontend.java @@ -68,8 +68,15 @@ public interface AddressbookContactFrontend extends DatabaseFrontend { * 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 @@ -84,12 +91,4 @@ public interface AddressbookContactFrontend extends DatabaseFrontend { * @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); } diff --git a/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java b/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java index d928695..fd2a538 100644 --- a/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java +++ b/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java @@ -31,6 +31,8 @@ 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.Storeable; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; import org.mxchange.jcore.exceptions.UnhandledUserChoiceException; @@ -50,11 +52,6 @@ public class AddressbookContactManager extends BaseManager implements Manageable */ private final List columnNames; - /** - * A AddressbookContactFrontend instance - */ - private final AddressbookContactFrontend contactDatabase; - /** * Translated column name list */ @@ -82,7 +79,8 @@ public class AddressbookContactManager extends BaseManager implements Manageable 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); @@ -112,7 +110,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable } // Add it - this.getContactDatabase().addContact(contact); + ((AddressbookContactFrontend) this.getFrontend()).addContact(contact); // Trace message this.getLogger().trace("EXIT!"); //NOI18N @@ -244,7 +242,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable * 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 @@ -343,7 +341,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable this.getLogger().trace("CALLED!"); //NOI18N // Shut down the database layer - this.getContactDatabase().doShutdown(); + ((AddressbookContactFrontend) this.getFrontend()).doShutdown(); // Trace message this.getLogger().trace("EXIT!"); //NOI18N @@ -477,6 +475,22 @@ public class AddressbookContactManager extends BaseManager implements Manageable 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 * @@ -525,22 +539,6 @@ public class AddressbookContactManager extends BaseManager implements Manageable 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 * @@ -605,13 +603,13 @@ public class AddressbookContactManager extends BaseManager implements Manageable 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; @@ -626,7 +624,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable // 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); } @@ -654,7 +652,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable 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); @@ -688,7 +686,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable /* 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())) { @@ -713,7 +711,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable int size = -1; try { - size = this.getContactDatabase().getContactsCount(); + size = ((AddressbookContactFrontend) this.getFrontend()).getContactsCount(); } catch (final SQLException ex) { // Something happened this.abortProgramWithException(ex); @@ -768,26 +766,24 @@ public class AddressbookContactManager extends BaseManager implements Manageable 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 diff --git a/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java b/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java index be13daa..6a9bb1e 100644 --- a/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java +++ b/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java @@ -17,10 +17,13 @@ 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; /** @@ -175,10 +178,15 @@ public interface ManageableAddressbookContact extends 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