public String getCsvStringFromStoreableObject () {\r
// Get all together\r
String csvString = String.format(\r
- "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"\n",\r
+ "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"\n",\r
+ this.isOwnContact(),\r
this.getGender(),\r
this.getSurname(),\r
this.getFamilyName(),\r
--- /dev/null
+/*\r
+ * Copyright (C) 2015 Roland Haeder\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package org.mxchange.addressbook.database.backend;\r
+\r
+import org.mxchange.addressbook.BaseFrameworkSystem;\r
+\r
+/**\r
+ * Generall database backend\r
+ * \r
+ * @author Roland Haeder\r
+ */\r
+public class BaseDatabaseBackend extends BaseFrameworkSystem {\r
+ /**\r
+ * No instances from this class\r
+ */\r
+ protected BaseDatabaseBackend () {\r
+ }\r
+}\r
* @author Roland Haeder\r
*/\r
public interface DatabaseBackend extends FrameworkInterface {\r
+\r
+ /**\r
+ * Rewinds backend\r
+ */\r
+ public void rewind ();\r
+\r
/**\r
* Stores an object in the database.\r
* \r
+++ /dev/null
-/*\r
- * Copyright (C) 2015 Roland Haeder\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-package org.mxchange.addressbook.database.backend.csv;\r
-\r
-import org.mxchange.addressbook.BaseFrameworkSystem;\r
-\r
-/**\r
- *\r
- * @author Roland Haeder\r
- */\r
-class BaseDatabaseBackend extends BaseFrameworkSystem {\r
- /**\r
- * No instances from this class\r
- */\r
- protected BaseDatabaseBackend () {\r
- }\r
-}\r
--- /dev/null
+/*\r
+ * Copyright (C) 2015 Roland Haeder\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package org.mxchange.addressbook.database.backend.csv;\r
+\r
+import java.util.Iterator;\r
+import org.mxchange.addressbook.contact.Contact;\r
+import org.mxchange.addressbook.database.backend.DatabaseBackend;\r
+\r
+/**\r
+ *\r
+ * @author Roland Haeder\r
+ */\r
+public interface CsvBackend extends DatabaseBackend {\r
+\r
+ /**\r
+ * Gets an iterator for contacts\r
+ * \r
+ * @return Iterator for contacts\r
+ */\r
+ public Iterator<Contact> contactIterator ();\r
+}\r
import java.io.IOException;\r
import java.io.RandomAccessFile;\r
import java.text.MessageFormat;\r
+import org.mxchange.addressbook.database.backend.BaseDatabaseBackend;\r
import org.mxchange.addressbook.database.storage.Storeable;\r
-import org.mxchange.addressbook.database.backend.DatabaseBackend;\r
import org.mxchange.addressbook.database.storage.csv.StoreableCsv;\r
\r
/**\r
* \r
* @author Roland Haeder\r
*/\r
-public class CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {\r
+public class CsvDatabaseBackend extends BaseDatabaseBackend implements CsvBackend {\r
/**\r
* Output stream for this storage engine\r
*/\r
this.getLogger().debug(MessageFormat.format("Database for {0} has been initialized.", tableName));\r
}\r
\r
+ @Override\r
+ public void rewind () {\r
+ try {\r
+ // Rewind underlaying database file\r
+ this.storageFile.seek(0);\r
+ } catch (final IOException ex) {\r
+ this.getLogger().catching(ex);\r
+ System.exit(1);\r
+ }\r
+ }\r
+\r
/**\r
* Stores given object by "visiting" it\r
*\r
// Try to cast it, this will fail if the interface is not implemented\r
StoreableCsv csv = (StoreableCsv) object;\r
\r
- // Now get the object that needs to be stored\r
+ // Now get a string from the object that needs to be stored\r
String str = csv.getCsvStringFromStoreableObject();\r
\r
// Debug message\r
package org.mxchange.addressbook.database.frontend.contact;\r
\r
import java.io.IOException;\r
+import java.util.Iterator;\r
+import java.util.List;\r
import org.mxchange.addressbook.contact.Contact;\r
-import org.mxchange.addressbook.database.storage.Storeable;\r
+import org.mxchange.addressbook.database.backend.csv.CsvBackend;\r
import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend;\r
+import org.mxchange.addressbook.database.storage.Storeable;\r
+import org.mxchange.addressbook.manager.contact.ManageableContact;\r
\r
/**\r
* Stores and retrieves Contact instances\r
this.initBackend();\r
}\r
\r
+ /**\r
+ * Flushes all contact entries to database\r
+ * @param contactManager An instance of a MangeableContact class\r
+ */\r
+ @Override\r
+ public void flushAllContacts (final ManageableContact contactManager) {\r
+ // Get full list\r
+ List<Contact> contacts = contactManager.getList();\r
+\r
+ // Get iterator\r
+ Iterator<Contact> iterator = contacts.iterator();\r
+\r
+ // Rewind backend\r
+ this.getBackend().rewind();\r
+\r
+ // Get all entries\r
+ while (iterator.hasNext()) {\r
+ // Get next entry\r
+ Contact contact = iterator.next();\r
+\r
+ try {\r
+ // Store this entry\r
+ this.getBackend().store((Storeable) contact);\r
+ } catch (final IOException ex) {\r
+ // Should not happen?\r
+ this.getLogger().catching(ex);\r
+ System.exit(1);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Reads all contacts from database backend and handles them over to the\r
+ * contact manager\r
+ * \r
+ * @param contactManager Contact manager to handle loaded contacts\r
+ */\r
@Override\r
- public void addContact (final Contact contact) {\r
- try {\r
- // Try to cast the object and handle it over to the backend\r
- this.getBackend().store((Storeable) contact);\r
- } catch (final IOException ex) {\r
- this.getLogger().error("Cannot write contact to storage: " + ex.getMessage());\r
- System.exit(1);\r
+ public void readAllContacts (final ManageableContact contactManager) {\r
+ // Get iterator and case it\r
+ CsvBackend backend = (CsvBackend) this.getBackend();\r
+ \r
+ // First rewind to beginning\r
+ this.getBackend().rewind();\r
+\r
+ // Get backend iterator\r
+ Iterator<Contact> iterator = .contactIterator();\r
+\r
+ // Read all entries\r
+ while (iterator.hasNext()) {\r
+ // Get next entry\r
+ Contact contact = iterator.next();\r
}\r
}\r
}\r
*/\r
package org.mxchange.addressbook.database.frontend.contact;\r
\r
-import org.mxchange.addressbook.contact.Contact;\r
import org.mxchange.addressbook.database.frontend.DatabaseWrapper;\r
+import org.mxchange.addressbook.manager.contact.ManageableContact;\r
\r
/**\r
*\r
* @author Roland Häder\r
*/\r
public interface ContactWrapper extends DatabaseWrapper {\r
+\r
+ /**\r
+ * Flushes all contact entries to database\r
+ * \r
+ * @param contactManager An instance of a MangeableContact class\r
+ */\r
+ public void flushAllContacts (final ManageableContact contactManager);\r
+\r
/**\r
- * Adds a Contact instance to database\r
+ * Reads all contacts from database backend and handles them over to the\r
+ * contact manager\r
* \r
- * @param contact Contact instance to add(store) to database\r
+ * @param contactManager \r
*/\r
- public void addContact (final Contact contact);\r
+ public void readAllContacts (final ManageableContact contactManager);\r
}\r
package org.mxchange.addressbook.manager.contact;\r
\r
import java.util.ArrayList;\r
+import java.util.Collections;\r
import java.util.Iterator;\r
import java.util.List;\r
import org.mxchange.addressbook.UnhandledUserChoiceException;\r
// Init database connection\r
this.contactDatabase = new ContactDatabaseFrontend();\r
\r
+ // Read all entries\r
+ this.contactDatabase.readAllContacts(this);\r
+\r
// Debug message\r
//* NOISY-DEBUG: */ this.getLogger().debug("client=" + client);\r
\r
// Add contact to internal list\r
this.contacts.add(contact);\r
\r
- /*\r
- * @TODO This call doesn't make sense, it would cause that only one\r
- * entry exist in storage. A better solution is to iterate over all\r
- * contacts and add them. This may also require to rewrite the database\r
- * layer a little. For demonstrational purposes, this should be fine.\r
- */\r
-\r
- // Add object to database\r
- this.getContactDatabase().addContact(contact);\r
+ // Flush whole list\r
+ this.flush();\r
}\r
\r
/**\r
// Other contact's address data to change\r
throw new UnsupportedOperationException("Changing contact entries not finished.");\r
}\r
+\r
+ // Flush whole list\r
+ this.flush();\r
}\r
\r
/**\r
// Then re-ask them ...\r
throw new UnsupportedOperationException("Changing contact entries not finished.");\r
}\r
+\r
+ // Flush whole list\r
+ this.flush();\r
}\r
\r
/**\r
// Then re-ask them ...\r
throw new UnsupportedOperationException("Changing contact entries not finished.");\r
}\r
+\r
+ // Flush whole list\r
+ this.flush();\r
}\r
\r
/**\r
this.addContact(contact);\r
}\r
\r
+ /**\r
+ * Getter for whole contact list\r
+ * \r
+ * @return List of all contacts\r
+ */\r
+ @Override\r
+ public List<Contact> getList () {\r
+ return Collections.unmodifiableList(this.contacts);\r
+ }\r
+\r
/**\r
* Getter for size\r
*\r
return this.getClient().enterInt(0, 99_999, "Bitte geben Sie Ihre Postleitzahl ein: ");\r
}\r
\r
+ /**\r
+ * Flushes all entries by calling database backend\r
+ */\r
+ private void flush () {\r
+ // Flusgh all\r
+ this.getContactDatabase().flushAllContacts(this);\r
+ }\r
+\r
+ /**\r
+ * A ContactWrapper instance\r
+ *\r
+ * @return the database\r
+ */\r
+ private ContactWrapper getContactDatabase () {\r
+ return this.contactDatabase;\r
+ }\r
+\r
/**\r
* "Getter" for own contact instance or null if not found\r
*\r
}\r
\r
/**\r
- * Checks whether own contact is already added by checking all entries for isOwnContact flag\r
+ * Checks whether own contact is already added by checking all entries for\r
+ * isOwnContact flag\r
+ * \r
* @return Whether own contact is already added\r
*/\r
private boolean isOwnContactAdded () {\r
// Return result\r
return isAdded;\r
}\r
-\r
- /**\r
- * A ContactWrapper instance\r
- *\r
- * @return the database\r
- */\r
- private ContactWrapper getContactDatabase () {\r
- return this.contactDatabase;\r
- }\r
}\r
*/\r
package org.mxchange.addressbook.manager.contact;\r
\r
+import java.util.List;\r
import org.mxchange.addressbook.client.Client;\r
import org.mxchange.addressbook.contact.Contact;\r
import org.mxchange.addressbook.manager.Manageable;\r
*/\r
public void doEnterOwnData();\r
\r
+ /**\r
+ * Getter for whole list\r
+ * @return List of all contacts\r
+ */\r
+ public List<Contact> getList ();\r
+\r
/**\r
* Getter for size\r
*\r