2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.database.frontend.contact;
19 import java.io.IOException;
20 import java.util.Iterator;
21 import java.util.List;
22 import org.mxchange.addressbook.exceptions.BadTokenException;
23 import org.mxchange.addressbook.contact.Contact;
24 import org.mxchange.addressbook.database.backend.csv.CsvBackend;
25 import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend;
26 import org.mxchange.addressbook.database.storage.Storeable;
27 import org.mxchange.addressbook.manager.contact.ContactManager;
30 * Stores and retrieves Contact instances
32 * @author Roland Haeder
34 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {
37 * Constructor which accepts a contact manager
41 public ContactDatabaseFrontend (final ContactManager manager) {
42 // Call own constructor
45 // Set contact manager
46 this.setContactManager(manager);
52 protected ContactDatabaseFrontend () {
56 this.setTableName("contacts");
63 * Shuts down the database layer
66 public void doShutdown () {
68 this.getBackend().doShutdown();
72 * Flushes all contact entries to database
75 public void flushAllContacts () {
77 List<Contact> contacts = this.getContactManager().getList();
80 Iterator<Contact> iterator = contacts.iterator();
83 this.getBackend().rewind();
86 while (iterator.hasNext()) {
88 Contact contact = iterator.next();
92 this.getBackend().store((Storeable) contact);
93 } catch (final IOException ex) {
95 this.getLogger().catching(ex);
102 * Reads all contacts from database backend and handles them over to the
106 public void readAllContacts () {
107 // Get iterator and case it
108 CsvBackend backend = (CsvBackend) this.getBackend();
110 // First rewind to beginning
111 this.getBackend().rewind();
113 // Get backend iterator
114 Iterator<Contact> iterator = null;
116 iterator = backend.contactIterator();
117 } catch (final BadTokenException ex) {
118 this.getLogger().catching(ex);
123 while (iterator.hasNext()) {
125 Contact contact = iterator.next();
127 // Add contact instance to manager
128 this.getContactManager().addContact(contact);