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.lang.reflect.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.Iterator;
24 import java.util.StringTokenizer;
25 import org.mxchange.addressbook.contact.book.BookContact;
26 import org.mxchange.addressbook.contact.user.UserContact;
27 import org.mxchange.addressbook.database.contact.AddressbookContactDatabaseConstants;
28 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
29 import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
30 import org.mxchange.jcore.contact.Contact;
31 import org.mxchange.jcore.contact.Gender;
32 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
33 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
34 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
35 import org.mxchange.jcore.database.result.Result;
36 import org.mxchange.jcore.database.storage.Storeable;
37 import org.mxchange.jcore.exceptions.BadTokenException;
38 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
39 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
42 * Stores and retrieves Contact instances
44 * @author Roland Haeder
46 public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend implements AddressbookContactFrontend {
49 * Constructor which accepts a contact manager
51 * @param manager Manager instance
52 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
53 * @throws java.sql.SQLException If an SQL error occurs
55 public AddressbookContactDatabaseFrontend (final AddressbookContactManager manager) throws UnsupportedDatabaseBackendException, SQLException {
56 // Call own constructor
60 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
62 // Manager instance must not be null
63 if (manager == null) {
65 throw new NullPointerException("manager is null"); //NOI18N
68 // Set contact manager
69 this.setContactManager(manager);
73 * Default but protected constructor
74 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
75 * @throws java.sql.SQLException Any SQL exception from e.g. MySQL connector
77 protected AddressbookContactDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
79 this.getLogger().trace("CALLED!"); //NOI18N
82 this.setTableName("contacts"); //NOI18N
89 * Adds given contact instance to database
91 * @param contact Contact instance
92 * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
95 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
97 this.getLogger().trace("CALLED!"); //NOI18N
99 // Make sure the contact is set
100 if (contact == null) {
102 throw new NullPointerException("contact is null"); //NOI18N
106 // First check if the contact is there
107 if (this.isContactFound(contact)) {
109 throw new ContactAlreadyAddedException(contact);
113 this.getBackend().store((Storeable) contact);
114 } catch (final IOException | BadTokenException ex) {
116 this.abortProgramWithException(ex);
120 this.getLogger().trace("CALLED!"); //NOI18N
124 * Shuts down the database layer
127 public void doShutdown () throws SQLException, IOException {
129 this.getLogger().trace("CALLED!"); //NOI18N
132 this.getBackend().doShutdown();
135 this.getLogger().trace("EXIT!"); //NOI18N
139 public Object emptyStringToNull (final String key, final Object value) {
140 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
144 * Some "getter" for total contact count
146 * @return Total contact count
149 public int getContactsCount () throws SQLException {
150 // And deligate to backend
151 return this.getBackend().getTotalCount(); //NOI18N
155 * Some "getter" for own contact instance
157 * @return Own contact instance
160 public Contact getOwnContact () {
162 this.getLogger().trace("CALLED!"); //NOI18N
164 // Get row index back from backend
165 int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
168 this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
171 Contact contact = null;
174 // Now simply read the row
175 contact = (Contact) this.getBackend().readRow(rowIndex);
176 } catch (final BadTokenException ex) {
178 this.abortProgramWithException(ex);
182 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
189 * Checks if given Contact is found
191 * @param contact Contact instance to check
192 * @return Whether the given Contact instance is found
195 public boolean isContactFound (final Contact contact) throws BadTokenException {
197 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
199 // contact should not be null
200 if (contact == null) {
202 throw new NullPointerException("contact is null"); //NOI18N
205 // Default is not found
206 boolean isFound = false;
209 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
212 while (iterator.hasNext()) {
214 Contact c = (Contact) iterator.next();
217 this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
220 if (c.equals(contact)) {
228 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
235 * Checks whether own contact is found in database
237 * @return Whether own contact is found
238 * @throws org.mxchange.jcore.exceptions.BadTokenException Continued throw
239 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
240 * @throws java.lang.NoSuchMethodException If a method cannot be found
241 * @throws java.lang.IllegalAccessException If a method is not accessible
242 * @throws java.lang.reflect.InvocationTargetException Any other problems?
245 public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
246 // Get search criteria instance
247 SearchableCritera critera = new SearchCriteria();
250 critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
253 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
255 // Deligate this call to backend
256 return result.hasNext();
260 * Reads a single row and parses it to a contact instance
262 * @param rowIndex Row index (also how much to skip)
263 * @return Contact instance
266 public Contact readSingleContact (final int rowIndex) {
268 // Deligate this to backend instance
269 return (Contact) this.getBackend().readRow(rowIndex);
270 } catch (final BadTokenException ex) {
272 this.abortProgramWithException(ex);
275 // Bad state, should not be reached
276 throw new IllegalStateException("This should not be reached");