From: Roland Haeder Date: Fri, 17 Jul 2015 08:48:30 +0000 (+0200) Subject: Rewrite start of storing contacts in database as the previous was just for demo ... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a9386126082a82d662e37cec79c3d449d89d25cf;p=jfinancials-swing.git Rewrite start of storing contacts in database as the previous was just for demo (needs Apache Log4J 2.x) Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java index ef465f7..3e9da4c 100644 --- a/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java +++ b/Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java @@ -543,7 +543,8 @@ public class BaseContact extends BaseFrameworkSystem { public String getCsvStringFromStoreableObject () { // Get all together String csvString = String.format( - "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"\n", + "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"\n", + this.isOwnContact(), this.getGender(), this.getSurname(), this.getFamilyName(), diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/BaseDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/BaseDatabaseBackend.java new file mode 100644 index 0000000..539f6d2 --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/BaseDatabaseBackend.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.backend; + +import org.mxchange.addressbook.BaseFrameworkSystem; + +/** + * Generall database backend + * + * @author Roland Haeder + */ +public class BaseDatabaseBackend extends BaseFrameworkSystem { + /** + * No instances from this class + */ + protected BaseDatabaseBackend () { + } +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java index a9d71f9..0d83aca 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/DatabaseBackend.java @@ -26,6 +26,12 @@ import org.mxchange.addressbook.database.storage.Storeable; * @author Roland Haeder */ public interface DatabaseBackend extends FrameworkInterface { + + /** + * Rewinds backend + */ + public void rewind (); + /** * Stores an object in the database. * diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/BaseDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/BaseDatabaseBackend.java deleted file mode 100644 index 59fe870..0000000 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/BaseDatabaseBackend.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2015 Roland Haeder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.database.backend.csv; - -import org.mxchange.addressbook.BaseFrameworkSystem; - -/** - * - * @author Roland Haeder - */ -class BaseDatabaseBackend extends BaseFrameworkSystem { - /** - * No instances from this class - */ - protected BaseDatabaseBackend () { - } -} diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvBackend.java new file mode 100644 index 0000000..4a4df5c --- /dev/null +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvBackend.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.database.backend.csv; + +import java.util.Iterator; +import org.mxchange.addressbook.contact.Contact; +import org.mxchange.addressbook.database.backend.DatabaseBackend; + +/** + * + * @author Roland Haeder + */ +public interface CsvBackend extends DatabaseBackend { + + /** + * Gets an iterator for contacts + * + * @return Iterator for contacts + */ + public Iterator contactIterator (); +} diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java index c2caeb3..a070abb 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/csv/CsvDatabaseBackend.java @@ -21,8 +21,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.text.MessageFormat; +import org.mxchange.addressbook.database.backend.BaseDatabaseBackend; import org.mxchange.addressbook.database.storage.Storeable; -import org.mxchange.addressbook.database.backend.DatabaseBackend; import org.mxchange.addressbook.database.storage.csv.StoreableCsv; /** @@ -30,7 +30,7 @@ import org.mxchange.addressbook.database.storage.csv.StoreableCsv; * * @author Roland Haeder */ -public class CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend { +public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBackend { /** * Output stream for this storage engine */ @@ -67,6 +67,17 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseB this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName)); } + @Override + public void rewind () { + try { + // Rewind underlaying database file + this.storageFile.seek(0); + } catch (final IOException ex) { + this.getLogger().catching(ex); + System.exit(1); + } + } + /** * Stores given object by "visiting" it * @@ -81,7 +92,7 @@ public class CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseB // Try to cast it, this will fail if the interface is not implemented StoreableCsv csv = (StoreableCsv) object; - // Now get the object that needs to be stored + // Now get a string from the object that needs to be stored String str = csv.getCsvStringFromStoreableObject(); // Debug message 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..2f55241 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,13 @@ package org.mxchange.addressbook.database.frontend.contact; import java.io.IOException; +import java.util.Iterator; +import java.util.List; 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 +44,58 @@ 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) { - 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()); - System.exit(1); + 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 = .contactIterator(); + + // Read all entries + while (iterator.hasNext()) { + // Get next entry + Contact contact = iterator.next(); } } } diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java index e5d5d8f..ce574ab 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactWrapper.java @@ -16,18 +16,27 @@ */ package org.mxchange.addressbook.database.frontend.contact; -import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.database.frontend.DatabaseWrapper; +import org.mxchange.addressbook.manager.contact.ManageableContact; /** * * @author Roland Häder */ public interface ContactWrapper extends DatabaseWrapper { + + /** + * Flushes all contact entries to database + * + * @param contactManager An instance of a MangeableContact class + */ + public void flushAllContacts (final ManageableContact contactManager); + /** - * Adds a Contact instance to database + * Reads all contacts from database backend and handles them over to the + * contact manager * - * @param contact Contact instance to add(store) to database + * @param contactManager */ - public void addContact (final Contact contact); + public void readAllContacts (final ManageableContact contactManager); } diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java index 73c095b..d4f7c32 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ContactManager.java @@ -17,6 +17,7 @@ package org.mxchange.addressbook.manager.contact; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; import org.mxchange.addressbook.UnhandledUserChoiceException; @@ -61,6 +62,9 @@ public class ContactManager extends BaseManager implements ManageableContact { // Init database connection this.contactDatabase = new ContactDatabaseFrontend(); + // Read all entries + this.contactDatabase.readAllContacts(this); + // Debug message //* NOISY-DEBUG: */ this.getLogger().debug("client=" + client); @@ -91,15 +95,8 @@ public class ContactManager extends BaseManager implements ManageableContact { // Add contact to internal list this.contacts.add(contact); - /* - * @TODO This call doesn't make sense, it would cause that only one - * entry exist in storage. A better solution is to iterate over all - * contacts and add them. This may also require to rewrite the database - * layer a little. For demonstrational purposes, this should be fine. - */ - - // Add object to database - this.getContactDatabase().addContact(contact); + // Flush whole list + this.flush(); } /** @@ -191,6 +188,9 @@ public class ContactManager extends BaseManager implements ManageableContact { // Other contact's address data to change throw new UnsupportedOperationException("Changing contact entries not finished."); } + + // Flush whole list + this.flush(); } /** @@ -225,6 +225,9 @@ public class ContactManager extends BaseManager implements ManageableContact { // Then re-ask them ... throw new UnsupportedOperationException("Changing contact entries not finished."); } + + // Flush whole list + this.flush(); } /** @@ -262,6 +265,9 @@ public class ContactManager extends BaseManager implements ManageableContact { // Then re-ask them ... throw new UnsupportedOperationException("Changing contact entries not finished."); } + + // Flush whole list + this.flush(); } /** @@ -291,6 +297,16 @@ public class ContactManager extends BaseManager implements ManageableContact { this.addContact(contact); } + /** + * Getter for whole contact list + * + * @return List of all contacts + */ + @Override + public List getList () { + return Collections.unmodifiableList(this.contacts); + } + /** * Getter for size * @@ -414,6 +430,23 @@ public class ContactManager extends BaseManager implements ManageableContact { return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: "); } + /** + * Flushes all entries by calling database backend + */ + private void flush () { + // Flusgh all + this.getContactDatabase().flushAllContacts(this); + } + + /** + * A ContactWrapper instance + * + * @return the database + */ + private ContactWrapper getContactDatabase () { + return this.contactDatabase; + } + /** * "Getter" for own contact instance or null if not found * @@ -481,7 +514,9 @@ public class ContactManager extends BaseManager implements ManageableContact { } /** - * Checks whether own contact is already added by checking all entries for isOwnContact flag + * Checks whether own contact is already added by checking all entries for + * isOwnContact flag + * * @return Whether own contact is already added */ private boolean isOwnContactAdded () { @@ -511,13 +546,4 @@ public class ContactManager extends BaseManager implements ManageableContact { // Return result return isAdded; } - - /** - * A ContactWrapper instance - * - * @return the database - */ - private ContactWrapper getContactDatabase () { - return this.contactDatabase; - } } diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java index 6df9615..14c3be6 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableContact.java @@ -16,6 +16,7 @@ */ package org.mxchange.addressbook.manager.contact; +import java.util.List; import org.mxchange.addressbook.client.Client; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.manager.Manageable; @@ -84,6 +85,12 @@ public interface ManageableContact extends Manageable { */ public void doEnterOwnData(); + /** + * Getter for whole list + * @return List of all contacts + */ + public List getList (); + /** * Getter for size *