X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=Addressbook%2Fsrc%2Forg%2Fmxchange%2Faddressbook%2Fdatabase%2Ffrontend%2Fcontact%2FContactDatabaseFrontend.java;h=87126f0b9111b107900446b9ff8328055b3ccf9d;hb=b36c8456e5101449d0be2c5c6b90a84f0b0ef5ea;hp=a67be31b5fdec0431c81ec6eec3b43cbce23dca1;hpb=b868f84e773bc3ab97540e8f1979d8ed559955bb;p=jaddressbook-share-lib.git diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java index a67be31..87126f0 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java @@ -17,9 +17,14 @@ package org.mxchange.addressbook.database.frontend.contact; import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import org.mxchange.addressbook.BadTokenException; import org.mxchange.addressbook.contact.Contact; -import org.mxchange.addressbook.database.storage.Storeable; +import org.mxchange.addressbook.database.backend.csv.CsvBackend; import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend; +import org.mxchange.addressbook.database.storage.Storeable; +import org.mxchange.addressbook.manager.contact.ManageableContact; /** * Stores and retrieves Contact instances @@ -40,14 +45,67 @@ public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements Con this.initBackend(); } + /** + * Flushes all contact entries to database + * @param contactManager An instance of a MangeableContact class + */ + @Override + public void flushAllContacts (final ManageableContact contactManager) { + // Get full list + List contacts = contactManager.getList(); + + // Get iterator + Iterator iterator = contacts.iterator(); + + // Rewind backend + this.getBackend().rewind(); + + // Get all entries + while (iterator.hasNext()) { + // Get next entry + Contact contact = iterator.next(); + + try { + // Store this entry + this.getBackend().store((Storeable) contact); + } catch (final IOException ex) { + // Should not happen? + this.getLogger().catching(ex); + System.exit(1); + } + } + } + + /** + * Reads all contacts from database backend and handles them over to the + * contact manager + * + * @param contactManager Contact manager to handle loaded contacts + */ @Override - public void addContact (final Contact contact) { + public void readAllContacts (final ManageableContact contactManager) { + // Get iterator and case it + CsvBackend backend = (CsvBackend) this.getBackend(); + + // First rewind to beginning + this.getBackend().rewind(); + + // Get backend iterator + Iterator iterator = null; try { - // Try to cast the object and handle it over to the backend - this.getBackend().store((Storeable) contact); - } catch (final IOException ex) { - this.getLogger().error("Cannot write contact to storage: " + ex.getMessage()); + iterator = backend.contactIterator(); + } catch (final BadTokenException ex) { + this.getLogger().catching(ex); System.exit(1); } + + // Read all entries + while (iterator.hasNext()) { + // Get next entry + Contact contact = iterator.next(); + + // Add contact instance to manager + contactManager.addContact(contact); + } } }