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.StringTokenizer;
24 import org.mxchange.addressbook.contact.book.BookContact;
25 import org.mxchange.addressbook.contact.user.UserContact;
26 import org.mxchange.addressbook.database.contact.AddressbookContactDatabaseConstants;
27 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
28 import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
29 import org.mxchange.jcore.contact.Contact;
30 import org.mxchange.jcore.contact.Gender;
31 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
32 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
33 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
34 import org.mxchange.jcore.database.result.Result;
35 import org.mxchange.jcore.database.storage.Storeable;
36 import org.mxchange.jcore.exceptions.BadTokenException;
37 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
40 * Stores and retrieves Contact instances
42 * @author Roland Haeder
44 public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend implements AddressbookContactFrontend {
47 * Constructor which accepts a contact manager
49 * @param manager Manager instance
50 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
51 * @throws java.sql.SQLException If an SQL error occurs
53 public AddressbookContactDatabaseFrontend (final AddressbookContactManager manager) throws UnsupportedDatabaseBackendException, SQLException {
54 // Call own constructor
58 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
60 // Manager instance must not be null
61 if (manager == null) {
63 throw new NullPointerException("manager is null"); //NOI18N
66 // Set contact manager
67 this.setContactManager(manager);
71 * Default but protected constructor
72 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
73 * @throws java.sql.SQLException Any SQL exception from e.g. MySQL connector
75 protected AddressbookContactDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
77 this.getLogger().trace("CALLED!"); //NOI18N
80 this.setTableName("contacts"); //NOI18N
87 * Adds given contact instance to database
89 * @param contact Contact instance
90 * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
93 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
95 this.getLogger().trace("CALLED!"); //NOI18N
97 // Make sure the contact is set
98 if (contact == null) {
100 throw new NullPointerException("contact is null"); //NOI18N
104 // First check if the contact is there
105 if (this.isContactFound(contact)) {
107 throw new ContactAlreadyAddedException(contact);
111 this.getBackend().store((Storeable) contact);
112 } catch (final IOException | BadTokenException ex) {
114 this.abortProgramWithException(ex);
118 this.getLogger().trace("CALLED!"); //NOI18N
122 * Shuts down the database layer
125 public void doShutdown () throws SQLException, IOException {
127 this.getLogger().trace("CALLED!"); //NOI18N
130 this.getBackend().doShutdown();
133 this.getLogger().trace("EXIT!"); //NOI18N
137 public Object emptyStringToNull (final String key, final Object value) {
138 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
142 * Some "getter" for total contact count
144 * @return Total contact count
147 public int getContactsCount () throws SQLException {
148 // And deligate to backend
149 return this.getBackend().getTotalCount(); //NOI18N
153 * Some "getter" for own contact instance
155 * @return Own contact instance
158 public Contact getOwnContact () {
160 this.getLogger().trace("CALLED!"); //NOI18N
162 // Get row index back from backend
163 int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
166 this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
169 Contact contact = null;
172 // Now simply read the row
173 contact = (Contact) this.getBackend().readRow(rowIndex);
174 } catch (final BadTokenException ex) {
176 this.abortProgramWithException(ex);
180 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
187 * Checks if given Contact is found
189 * @param contact Contact instance to check
190 * @return Whether the given Contact instance is found
193 public boolean isContactFound (final Contact contact) throws BadTokenException {
195 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
197 // contact should not be null
198 if (contact == null) {
200 throw new NullPointerException("contact is null"); //NOI18N
203 // Default is not found
204 boolean isFound = false;
207 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
210 while (iterator.hasNext()) {
212 Contact c = (Contact) iterator.next();
215 this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
218 if (c.equals(contact)) {
226 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
233 * Checks whether own contact is found in database
235 * @return Whether own contact is found
236 * @throws java.io.IOException If any IO error occurs
237 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
240 public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException {
241 // Get search criteria instance
242 SearchableCritera critera = new SearchCriteria();
245 critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
248 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
250 // Deligate this call to backend
251 return result.hasNext();
255 * Reads a single row and parses it to a contact instance
257 * @param rowIndex Row index (also how much to skip)
258 * @return Contact instance
261 public Contact readSingleContact (final int rowIndex) {
263 // Deligate this to backend instance
264 return (Contact) this.getBackend().readRow(rowIndex);
265 } catch (final BadTokenException ex) {
267 this.abortProgramWithException(ex);
270 // Bad state, should not be reached
271 throw new IllegalStateException("This should not be reached");