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;
25 import java.util.StringTokenizer;
26 import org.mxchange.addressbook.contact.book.BookContact;
27 import org.mxchange.addressbook.contact.user.UserContact;
28 import org.mxchange.addressbook.database.contact.AddressbookContactDatabaseConstants;
29 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
30 import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
31 import org.mxchange.jcore.contact.Contact;
32 import org.mxchange.jcore.contact.Gender;
33 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
34 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
35 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
36 import org.mxchange.jcore.database.result.Result;
37 import org.mxchange.jcore.database.storage.Storeable;
38 import org.mxchange.jcore.exceptions.BadTokenException;
39 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
40 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
43 * Stores and retrieves Contact instances
45 * @author Roland Haeder
47 public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend implements AddressbookContactFrontend {
50 * Constructor which accepts a contact manager
52 * @param manager Manager instance
53 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
54 * @throws java.sql.SQLException If an SQL error occurs
56 public AddressbookContactDatabaseFrontend (final AddressbookContactManager manager) throws UnsupportedDatabaseBackendException, SQLException {
57 // Call own constructor
61 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
63 // Manager instance must not be null
64 if (manager == null) {
66 throw new NullPointerException("manager is null"); //NOI18N
69 // Set contact manager
70 this.setContactManager(manager);
74 * Default but protected constructor
75 * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
76 * @throws java.sql.SQLException Any SQL exception from e.g. MySQL connector
78 protected AddressbookContactDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
80 this.getLogger().trace("CALLED!"); //NOI18N
83 this.setTableName("contacts"); //NOI18N
90 * Adds given contact instance to database
92 * @param contact Contact instance
93 * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
96 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
98 this.getLogger().trace("CALLED!"); //NOI18N
100 // Make sure the contact is set
101 if (contact == null) {
103 throw new NullPointerException("contact is null"); //NOI18N
107 // First check if the contact is there
108 if (this.isContactFound(contact)) {
110 throw new ContactAlreadyAddedException(contact);
114 this.getBackend().store((Storeable) contact);
115 } catch (final IOException | BadTokenException ex) {
117 this.abortProgramWithException(ex);
121 this.getLogger().trace("CALLED!"); //NOI18N
125 * Shuts down the database layer
128 public void doShutdown () throws SQLException, IOException {
130 this.getLogger().trace("CALLED!"); //NOI18N
133 this.getBackend().doShutdown();
136 this.getLogger().trace("EXIT!"); //NOI18N
140 public Object emptyStringToNull (final String key, final Object value) {
141 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
145 * Some "getter" for total contact count
147 * @return Total contact count
150 public int getContactsCount () throws SQLException {
151 // And deligate to backend
152 return this.getBackend().getTotalCount(); //NOI18N
156 * Some "getter" for own contact instance
158 * @return Own contact instance
161 public Contact getOwnContact () {
163 this.getLogger().trace("CALLED!"); //NOI18N
165 // Get row index back from backend
166 int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
169 this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
172 Contact contact = null;
175 // Now simply read the row
176 contact = (Contact) this.getBackend().readRow(rowIndex);
177 } catch (final BadTokenException ex) {
179 this.abortProgramWithException(ex);
183 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
190 * Checks if given Contact is found
192 * @param contact Contact instance to check
193 * @return Whether the given Contact instance is found
196 public boolean isContactFound (final Contact contact) throws BadTokenException {
198 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
200 // contact should not be null
201 if (contact == null) {
203 throw new NullPointerException("contact is null"); //NOI18N
206 // Default is not found
207 boolean isFound = false;
210 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
213 while (iterator.hasNext()) {
215 Contact c = (Contact) iterator.next();
218 this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
221 if (c.equals(contact)) {
229 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
236 * Checks whether own contact is found in database
238 * @return Whether own contact is found
239 * @throws org.mxchange.jcore.exceptions.BadTokenException Continued throw
240 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
241 * @throws java.lang.NoSuchMethodException If a method cannot be found
242 * @throws java.lang.IllegalAccessException If a method is not accessible
243 * @throws java.lang.reflect.InvocationTargetException Any other problems?
246 public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
247 // Get search criteria instance
248 SearchableCritera critera = new SearchCriteria();
251 critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
254 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
256 // Deligate this call to backend
257 return result.hasNext();
261 * Reads a single row and parses it to a contact instance
263 * @param rowIndex Row index (also how much to skip)
264 * @return Contact instance
267 public Contact readSingleContact (final int rowIndex) {
269 // Deligate this to backend instance
270 return (Contact) this.getBackend().readRow(rowIndex);
271 } catch (final BadTokenException ex) {
273 this.abortProgramWithException(ex);
276 // Bad state, should not be reached
277 throw new IllegalStateException("This should not be reached");
281 public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
282 throw new UnsupportedOperationException("Not supported yet: map=" + map);
286 public String getIdName () {
288 return AddressbookContactDatabaseConstants.COLUMN_ID;