-/*\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.frontend.contact;\r
-\r
-import java.io.IOException;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import org.mxchange.addressbook.BadTokenException;\r
-import org.mxchange.addressbook.contact.Contact;\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
- *\r
- * @author Roland Haeder\r
- */\r
-public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {\r
- /**\r
- * Basic constrcutor\r
- */\r
- public ContactDatabaseFrontend () {\r
- super();\r
-\r
- // Set "table" name\r
- this.setTableName("contacts");\r
-\r
- // Initalize backend\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 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 = null;\r
- try {\r
- iterator = backend.contactIterator();\r
- } catch (final BadTokenException ex) {\r
- this.getLogger().catching(ex);\r
- System.exit(1);\r
- }\r
-\r
- // Read all entries\r
- while (iterator.hasNext()) {\r
- // Get next entry\r
- Contact contact = iterator.next();\r
-\r
- // Add contact instance to manager\r
- contactManager.addContact(contact);\r
- }\r
- }\r
-}\r
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.database.frontend.contact;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import org.mxchange.addressbook.exceptions.BadTokenException;
+import org.mxchange.addressbook.contact.Contact;
+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.ContactManager;
+
+/**
+ * Stores and retrieves Contact instances
+ *
+ * @author Roland Haeder
+ */
+public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {
+
+ /**
+ * Constructor which accepts a contact manager
+ *
+ * @param manager
+ */
+ public ContactDatabaseFrontend (final ContactManager manager) {
+ // Call own constructor
+ this();
+
+ // Set contact manager
+ this.setContactManager(manager);
+ }
+
+ /**
+ * Basic constrcutor
+ */
+ protected ContactDatabaseFrontend () {
+ super();
+
+ // Set "table" name
+ this.setTableName("contacts");
+
+ // Initalize backend
+ this.initBackend();
+ }
+
+ /**
+ * Shuts down the database layer
+ */
+ @Override
+ public void doShutdown () {
+ // Shutdown backend
+ this.getBackend().doShutdown();
+ }
+
+ /**
+ * Flushes all contact entries to database
+ */
+ @Override
+ public void flushAllContacts () {
+ // Get full list
+ List<Contact> contacts = this.getContactManager().getList();
+
+ // Get iterator
+ Iterator<Contact> 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
+ */
+ @Override
+ public void readAllContacts () {
+ // Get iterator and case it
+ CsvBackend backend = (CsvBackend) this.getBackend();
+
+ // First rewind to beginning
+ this.getBackend().rewind();
+
+ // Get backend iterator
+ Iterator<Contact> iterator = null;
+ try {
+ 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
+ this.getContactManager().addContact(contact);
+ }
+ }
+}