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 org.mxchange.addressbook.contact.Contact;
24 import org.mxchange.addressbook.database.contact.ContactDatabaseConstants;
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.ContactAlreadyAddedException;
29 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException;
30 import org.mxchange.addressbook.manager.contact.ContactManager;
33 * Stores and retrieves Contact instances
35 * @author Roland Haeder
37 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {
40 * Constructor which accepts a contact manager
42 * @param manager Manager instance
44 public ContactDatabaseFrontend (final ContactManager manager) {
45 // Call own constructor
49 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
51 // Manager instance must not be null
52 if (manager == null) {
54 throw new NullPointerException("manager is null"); //NOI18N
57 // Set contact manager
58 this.setContactManager(manager);
64 protected ContactDatabaseFrontend () {
66 this.getLogger().trace("CALLED!"); //NOI18N
69 this.setTableName("contacts"); //NOI18N
74 } catch (final UnsupportedDatabaseBackendException ex) {
76 this.abortProgramWithException(ex);
77 } catch (final SQLException ex) {
79 this.abortProgramWithException(ex);
84 * Adds given contact instance to database
86 * @param contact Contact instance
87 * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
90 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
92 this.getLogger().trace("CALLED!"); //NOI18N
94 // Make sure the contact is set
95 if (contact == null) {
97 throw new NullPointerException("contact is null"); //NOI18N
101 // First check if the contact is there
102 if (this.isContactFound(contact)) {
104 throw new ContactAlreadyAddedException(contact);
108 this.getBackend().store((Storeable) contact);
109 } catch (final IOException ex) {
111 this.abortProgramWithException(ex);
112 } catch (final BadTokenException ex) {
114 this.abortProgramWithException(ex);
118 this.getLogger().trace("CALLED!"); //NOI18N
122 * Shuts down the database layer
125 public void doShutdown () {
127 this.getLogger().trace("CALLED!"); //NOI18N
130 this.getBackend().doShutdown();
133 this.getLogger().trace("EXIT!"); //NOI18N
137 * Some "getter" for total contact count
139 * @return Total contact count
142 public int getContactsCount () throws SQLException {
143 // And deligate to backend
144 return this.getBackend().getTotalCount(); //NOI18N
148 * Some "getter" for own contact instance
150 * @return Own contact instance
153 public Contact getOwnContact () {
155 this.getLogger().trace("CALLED!"); //NOI18N
157 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
161 * Checks if given Contact is found
163 * @param contact Contact instance to check
164 * @return Whether the given Contact instance is found
167 public boolean isContactFound (final Contact contact) throws BadTokenException {
169 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
171 // contact should not be null
172 if (contact == null) {
174 throw new NullPointerException("contact is null"); //NOI18N
177 // Default is not found
178 boolean isFound = false;
181 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
184 while (iterator.hasNext()) {
186 Contact c = (Contact) iterator.next();
189 this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
192 if (c.equals(contact)) {
200 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
207 * Checks whether own contact is found in database
209 * @return Whether own contact is found
212 public boolean isOwnContactFound () throws SQLException {
213 // Deligate this call to backend
214 return this.getBackend().isRowFound(ContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);