--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.database.frontend.contact;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.SQLException;
+import java.text.MessageFormat;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
+import org.mxchange.addressbook.contact.book.BookContact;
+import org.mxchange.addressbook.contact.user.UserContact;
+import org.mxchange.addressbook.database.contact.AddressbookContactDatabaseConstants;
+import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
+import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
+import org.mxchange.jcore.contact.Contact;
+import org.mxchange.jcore.contact.Gender;
+import org.mxchange.jcore.criteria.searchable.SearchCriteria;
+import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
+import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
+import org.mxchange.jcore.database.result.Result;
+import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
+import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
+
+/**
+ * Stores and retrieves Contact instances
+ *
+ * @author Roland Haeder
+ */
+public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend implements AddressbookContactFrontend {
+
+ /**
+ * Constructor which accepts a contact manager
+ *
+ * @param manager Manager instance
+ * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
+ * @throws java.sql.SQLException If an SQL error occurs
+ */
+ public AddressbookContactDatabaseFrontend (final AddressbookContactManager manager) throws UnsupportedDatabaseBackendException, SQLException {
+ // Call own constructor
+ this();
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
+
+ // Manager instance must not be null
+ if (manager == null) {
+ // Abort here
+ throw new NullPointerException("manager is null"); //NOI18N
+ }
+
+ // Set contact manager
+ this.setContactManager(manager);
+ }
+
+ /**
+ * Default but protected constructor
+ * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
+ * @throws java.sql.SQLException Any SQL exception from e.g. MySQL connector
+ */
+ protected AddressbookContactDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Set "table" name
+ this.setTableName("contacts"); //NOI18N
+
+ // Initalize backend
+ this.initBackend();
+ }
+
+ /**
+ * Adds given contact instance to database
+ *
+ * @param contact Contact instance
+ * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
+ */
+ @Override
+ public void addContact (final Contact contact) throws ContactAlreadyAddedException {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Make sure the contact is set
+ if (contact == null) {
+ // Abort here
+ throw new NullPointerException("contact is null"); //NOI18N
+ }
+
+ try {
+ // First check if the contact is there
+ if (this.isContactFound(contact)) {
+ // Already there
+ throw new ContactAlreadyAddedException(contact);
+ }
+
+ // Then add it
+ this.getBackend().store((Storeable) contact);
+ } catch (final IOException | BadTokenException ex) {
+ // Abort here
+ this.abortProgramWithException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+ }
+
+ /**
+ * Shuts down the database layer
+ */
+ @Override
+ public void doShutdown () throws SQLException, IOException {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Shutdown backend
+ this.getBackend().doShutdown();
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ @Override
+ public Object emptyStringToNull (final String key, final Object value) {
+ throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
+ }
+
+ /**
+ * Some "getter" for total contact count
+ *
+ * @return Total contact count
+ */
+ @Override
+ public int getContactsCount () throws SQLException {
+ // And deligate to backend
+ return this.getBackend().getTotalCount(); //NOI18N
+ }
+
+ /**
+ * Some "getter" for own contact instance
+ *
+ * @return Own contact instance
+ */
+ @Override
+ public Contact getOwnContact () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Get row index back from backend
+ int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
+
+ // Init instance
+ Contact contact = null;
+
+ try {
+ // Now simply read the row
+ contact = (Contact) this.getBackend().readRow(rowIndex);
+ } catch (final BadTokenException ex) {
+ // Bad token found
+ this.abortProgramWithException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
+
+ // Return it
+ return contact;
+ }
+
+ /**
+ * Checks if given Contact is found
+ *
+ * @param contact Contact instance to check
+ * @return Whether the given Contact instance is found
+ */
+ @Override
+ public boolean isContactFound (final Contact contact) throws BadTokenException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
+
+ // contact should not be null
+ if (contact == null) {
+ // Abort here
+ throw new NullPointerException("contact is null"); //NOI18N
+ }
+
+ // Default is not found
+ boolean isFound = false;
+
+ // Start iteration
+ Iterator<? extends Storeable> iterator = this.getBackend().iterator();
+
+ // Check all entries
+ while (iterator.hasNext()) {
+ // Get next element
+ Contact c = (Contact) iterator.next();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
+
+ // Is it added?
+ if (c.equals(contact)) {
+ // Is found
+ isFound = true;
+ break;
+ }
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
+
+ // Return it
+ return isFound;
+ }
+
+ /**
+ * Checks whether own contact is found in database
+ *
+ * @return Whether own contact is found
+ * @throws org.mxchange.jcore.exceptions.BadTokenException Continued throw
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
+ * @throws java.lang.NoSuchMethodException If a method cannot be found
+ * @throws java.lang.IllegalAccessException If a method is not accessible
+ * @throws java.lang.reflect.InvocationTargetException Any other problems?
+ */
+ @Override
+ public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ // Get search criteria instance
+ SearchableCriteria critera = new SearchCriteria();
+
+ // Add condition
+ critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
+
+ // Get result
+ Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
+
+ // Deligate this call to backend
+ return result.hasNext();
+ }
+
+ /**
+ * Reads a single row and parses it to a contact instance
+ *
+ * @param rowIndex Row index (also how much to skip)
+ * @return Contact instance
+ */
+ @Override
+ public Contact readSingleContact (final int rowIndex) {
+ try {
+ // Deligate this to backend instance
+ return (Contact) this.getBackend().readRow(rowIndex);
+ } catch (final BadTokenException ex) {
+ // Bad token found
+ this.abortProgramWithException(ex);
+ }
+
+ // Bad state, should not be reached
+ throw new IllegalStateException("This should not be reached");
+ }
+
+ @Override
+ public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ throw new UnsupportedOperationException("Not supported yet: map=" + map);
+ }
+
+ @Override
+ public String getIdName () {
+ // Return id column
+ return AddressbookContactDatabaseConstants.COLUMN_ID;
+ }
+}