From 8ef9a410dc1a2a9c14764fd9d0fcce49dd3e58eb Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 24 Aug 2015 14:18:10 +0200 Subject: [PATCH] =?utf8?q?Updated=20jcore=20+=20added=20more=20thrown=20ex?= =?utf8?q?ceptions=20+=20catched=20them=20Signed-off-by:Roland=20H=C3=A4de?= =?utf8?q?r=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- lib/jcore.jar | Bin 112955 -> 113036 bytes .../client/console/ConsoleClient.java | 19 ++-- .../client/gui/AddressbookFrame.java | 4 +- .../AddressbookContactDatabaseFrontend.java | 67 +++++++------- .../contact/AddressbookContactFrontend.java | 17 ++-- .../contact/AddressbookContactManager.java | 82 +++++++++--------- .../contact/ManageableAddressbookContact.java | 14 ++- 7 files changed, 103 insertions(+), 100 deletions(-) diff --git a/lib/jcore.jar b/lib/jcore.jar index d426e0dec31b4479bf275fbb9cc6ca3ba25ffd7c..5a46f43c19df0400e7fd341521152ed3a8d56f59 100644 GIT binary patch delta 2036 zcmY*ZYfzMB6n>xGWxs`8E=vd^#4Z#K8j5M6h$iZUr0xK&mW3pST-02&Fd9XYhD`I4 z@Xh!-RDr%!mQ!`GcPBnI!%1Iq(8ceIfoPIQ&^L?1GGw(jIZ@5tY8e$qC320FI~b^bVhG}jDO`%&3vHGK{rp@40&aZFB$i1DCASOn>8%?-eb->1zY~ z?$!hiE83MX=69TVU-M1pzOu0Y&1VGx9G7~j=UK)(!3Ztg)A5vxUrKK!XsU+ULC1=gA-)W5`+dlqlhOJ5vK@;{M~_E8NC#1;gnTN zQ4?h^DJplDm%UZA=8ZRt=9e!ktALo6^pdQ#Ax_?RAwn8lNJG4w)#*G$X43llg4@6@^ICjJ{&r z0`JbDQiN&S)Uh6+MrO`##0`x(x(Qi)?frMQ;tReKeqSY4X;~pv@S0ij)nK8<41a)H zEo;{Y8o(mHGmCPSXBM;c|z2ldEcoaMz-#`Rv}NpJRopN1@L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x^@n!yk?kW4JQJ0h<1EH5}#kG5u{!X`wejFuS%KPUmbWP%o$`aczOuzvtt#bs;& 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 -- 2.39.5