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.text.MessageFormat;
21 import java.util.Iterator;
22 import java.util.List;
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.exceptions.BadTokenException;
28 import org.mxchange.addressbook.manager.contact.ContactManager;
31 * Stores and retrieves Contact instances
33 * @author Roland Haeder
35 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {
38 * Constructor which accepts a contact manager
42 public ContactDatabaseFrontend (final ContactManager manager) {
43 // Call own constructor
47 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
49 // Manager instance must not be null
50 if (manager == null) {
52 throw new NullPointerException("manager is null"); //NOI18N
55 // Set contact manager
56 this.setContactManager(manager);
62 protected ContactDatabaseFrontend () {
66 this.getLogger().trace("CALLED!"); //NOI18N
69 this.setTableName("contacts"); //NOI18N
76 * Shuts down the database layer
79 public void doShutdown () {
81 this.getLogger().trace("CALLED!"); //NOI18N
84 this.getBackend().doShutdown();
87 this.getLogger().trace("EXIT!"); //NOI18N
91 * Flushes all contact entries to database
94 public void flushAllContacts () {
96 this.getLogger().trace("CALLED!"); //NOI18N
99 List<Contact> contacts = this.getContactManager().getList();
102 Iterator<Contact> iterator = contacts.iterator();
105 this.getBackend().rewind();
108 while (iterator.hasNext()) {
110 Contact contact = iterator.next();
114 this.getBackend().store((Storeable) contact);
115 } catch (final IOException ex) {
116 // Should not happen?
117 this.getLogger().catching(ex);
123 this.getLogger().trace("EXIT!"); //NOI18N
127 * Reads all contacts from database backend and handles them over to the
131 public void readAllContacts () {
133 this.getLogger().trace("CALLED!"); //NOI18N
135 // Get iterator and case it
136 CsvBackend backend = (CsvBackend) this.getBackend();
138 // First rewind to beginning
139 this.getBackend().rewind();
141 // Get backend iterator
142 Iterator<Contact> iterator = null;
144 iterator = backend.contactIterator();
145 } catch (final BadTokenException ex) {
146 this.getLogger().catching(ex);
151 while (iterator.hasNext()) {
153 Contact contact = iterator.next();
155 // Add contact instance to manager
156 this.getContactManager().addContact(contact);
160 this.getLogger().trace("EXIT!"); //NOI18N