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 {
36 * Constructor which accepts a contact manager
39 public ContactDatabaseFrontend (final ContactManager manager) {
40 // Call own constructor
43 // Set contact manager
44 this.setContactManager(manager);
50 protected ContactDatabaseFrontend () {
54 this.setTableName("contacts");
61 * Shuts down the database layer
64 public void doShutdown () {
66 this.getBackend().doShutdown();
70 * Flushes all contact entries to database
73 public void flushAllContacts () {
75 List<Contact> contacts = this.getContactManager().getList();
78 Iterator<Contact> iterator = contacts.iterator();
81 this.getBackend().rewind();
84 while (iterator.hasNext()) {
86 Contact contact = iterator.next();
90 this.getBackend().store((Storeable) contact);
91 } catch (final IOException ex) {
93 this.getLogger().catching(ex);
100 * Reads all contacts from database backend and handles them over to the
104 public void readAllContacts () {
105 // Get iterator and case it
106 CsvBackend backend = (CsvBackend) this.getBackend();
108 // First rewind to beginning
109 this.getBackend().rewind();
111 // Get backend iterator
112 Iterator<Contact> iterator = null;
114 iterator = backend.contactIterator();
115 } catch (final BadTokenException ex) {
116 this.getLogger().catching(ex);
121 while (iterator.hasNext()) {
123 Contact contact = iterator.next();
125 // Add contact instance to manager
126 this.getContactManager().addContact(contact);