]> git.mxchange.org Git - jfinancials-swing.git/blobdiff - src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java
Better compare this way:
[jfinancials-swing.git] / src / org / mxchange / addressbook / database / frontend / contact / AddressbookContactDatabaseFrontend.java
index e51b94209566dae5ae8a970e23fade28283e57c1..02089cbe250b75015cf39b32d196ecc9c533ae34 100644 (file)
@@ -58,7 +58,7 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp
                this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
 
                // Manager instance must not be null
-               if (manager == null) {
+               if (null == manager) {
                        // Abort here
                        throw new NullPointerException("manager is null"); //NOI18N
                }
@@ -83,19 +83,13 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp
                this.initBackend();
        }
 
-       /**
-        * Adds given contact instance to database
-        *
-        * @param contact Contact instance
-        * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
-        */
        @Override
        public void addContact (final Contact contact) throws ContactAlreadyAddedException {
                // Trace message
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Make sure the contact is set
-               if (contact == null) {
+               if (null == contact) {
                        // Abort here
                        throw new NullPointerException("contact is null"); //NOI18N
                }
@@ -123,11 +117,12 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp
                        }
 
                        // Then add it
+                       // @todo Nothing is done yet!
                        Result<? extends Storeable> result = this.doInsertDataSet();
 
                        // Debug message
                        this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
-               } catch (final IOException | BadTokenException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+               } catch (final IOException | BadTokenException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | CorruptedDatabaseFileException ex) {
                        // Abort here
                        this.abortProgramWithException(ex);
                }
@@ -136,11 +131,6 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp
                this.getLogger().trace("CALLED!"); //NOI18N
        }
 
-       /**
-        * Shuts down the database layer
-        * @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
@@ -175,31 +165,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<? 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
@@ -209,19 +211,18 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp
                return contact;
        }
 
-       /**
-        * Checks if given Contact is found
-        * 
-        * @param contact Contact instance to check
-        * @return Whether the given Contact instance is found
-        */
        @Override
-       public boolean isContactFound (final Contact contact) throws BadTokenException {
+       public Storeable getStoreableAtRow (final int rowIndex) {
+               throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
+       }
+
+       @Override
+       public boolean isContactFound (final Contact contact) throws BadTokenException, IOException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
                // Trace message
                this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
 
                // contact should not be null
-               if (contact == null) {
+               if (null == contact) {
                        // Abort here
                        throw new NullPointerException("contact is null"); //NOI18N
                }
@@ -229,8 +230,16 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp
                // Default is not found
                boolean isFound = false;
 
+               // Init search instance (but empty)
+               SearchableCriteria criteria = new SearchCriteria();
+
+               // Look for all entries and compare here. Else all entries needs to be compared with many AND statements
+               Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("result({0})={1}", result.size(), result));
                // Start iteration
-               Iterator<? extends Storeable> iterator = this.getBackend().iterator();
+               Iterator<? extends Storeable> iterator = result.iterator();
 
                // Check all entries
                while (iterator.hasNext()) {
@@ -255,51 +264,21 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp
                return isFound;
        }
 
-       /**
-        * Checks whether own contact is found in database
-        *
-        * @return Whether own contact is found
-        * @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?
-        */
        @Override
        public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
                // Get search criteria instance
-               SearchableCriteria critera = new SearchCriteria();
+               SearchableCriteria criteria = new SearchCriteria();
 
                // Add condition
-               critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
+               criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
 
                // Get result
-               Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
+               Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
 
                // Deligate this call to backend
                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);