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.sql.SQLException;
21 import java.text.MessageFormat;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.mxchange.addressbook.contact.Contact;
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.exceptions.UnsupportedDatabaseBackendException;
29 import org.mxchange.addressbook.manager.contact.ContactManager;
32 * Stores and retrieves Contact instances
34 * @author Roland Haeder
36 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {
39 * Constructor which accepts a contact manager
43 public ContactDatabaseFrontend (final ContactManager manager) {
44 // Call own constructor
48 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
50 // Manager instance must not be null
51 if (manager == null) {
53 throw new NullPointerException("manager is null"); //NOI18N
56 // Set contact manager
57 this.setContactManager(manager);
63 protected ContactDatabaseFrontend () {
67 this.getLogger().trace("CALLED!"); //NOI18N
70 this.setTableName("contacts"); //NOI18N
75 } catch (final UnsupportedDatabaseBackendException ex) {
77 this.abortProgramWithException(ex);
78 } catch (final SQLException ex) {
80 this.abortProgramWithException(ex);
85 * Shuts down the database layer
88 public void doShutdown () {
90 this.getLogger().trace("CALLED!"); //NOI18N
93 this.getBackend().doShutdown();
96 this.getLogger().trace("EXIT!"); //NOI18N
100 * Flushes all contact entries to database
103 public void flushAllContacts () {
105 this.getLogger().trace("CALLED!"); //NOI18N
108 List<Contact> contacts = this.getContactManager().getList();
111 Iterator<Contact> iterator = contacts.iterator();
114 this.getBackend().rewind();
117 while (iterator.hasNext()) {
119 Contact contact = iterator.next();
123 this.getBackend().store((Storeable) contact);
124 } catch (final IOException ex) {
125 // Should not happen?
126 this.abortProgramWithException(ex);
131 this.getLogger().trace("EXIT!"); //NOI18N
135 * Reads all contacts from database backend and handles them over to the
139 public void readAllContacts () {
141 this.getLogger().trace("CALLED!"); //NOI18N
143 // First rewind to beginning
144 this.getBackend().rewind();
146 // Get backend iterator
147 Iterator<Contact> iterator = null;
149 iterator = this.getBackend().contactIterator();
150 } catch (final BadTokenException ex) {
152 this.abortProgramWithException(ex);
156 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
158 Contact contact = iterator.next();
160 // Add contact instance to manager
161 this.getContactManager().addContact(contact);
165 this.getLogger().trace("EXIT!"); //NOI18N