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.frontend.BaseDatabaseFrontend;
25 import org.mxchange.addressbook.database.storage.Storeable;
26 import org.mxchange.addressbook.exceptions.BadTokenException;
27 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException;
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
74 } catch (final UnsupportedDatabaseBackendException ex) {
76 this.abortProgramWithException(ex);
81 * Shuts down the database layer
84 public void doShutdown () {
86 this.getLogger().trace("CALLED!"); //NOI18N
89 this.getBackend().doShutdown();
92 this.getLogger().trace("EXIT!"); //NOI18N
96 * Flushes all contact entries to database
99 public void flushAllContacts () {
101 this.getLogger().trace("CALLED!"); //NOI18N
104 List<Contact> contacts = this.getContactManager().getList();
107 Iterator<Contact> iterator = contacts.iterator();
110 this.getBackend().rewind();
113 while (iterator.hasNext()) {
115 Contact contact = iterator.next();
119 this.getBackend().store((Storeable) contact);
120 } catch (final IOException ex) {
121 // Should not happen?
122 this.abortProgramWithException(ex);
127 this.getLogger().trace("EXIT!"); //NOI18N
131 * Reads all contacts from database backend and handles them over to the
135 public void readAllContacts () {
137 this.getLogger().trace("CALLED!"); //NOI18N
139 // First rewind to beginning
140 this.getBackend().rewind();
142 // Get backend iterator
143 Iterator<Contact> iterator = null;
145 iterator = this.getBackend().contactIterator();
146 } catch (final BadTokenException ex) {
148 this.abortProgramWithException(ex);
152 while (iterator.hasNext()) {
154 Contact contact = iterator.next();
156 // Add contact instance to manager
157 this.getContactManager().addContact(contact);
161 this.getLogger().trace("EXIT!"); //NOI18N