+++ /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.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.sql.SQLException;
-import java.text.MessageFormat;
-import java.util.Iterator;
-import java.util.Map;
-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.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.Storable;
-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 (null == manager) {
- // 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();
- }
-
- @Override
- public void addContact (final Contact contact) throws ContactAlreadyAddedException {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
- // Make sure the contact is set
- if (null == contact) {
- // 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);
- }
-
- // Clear dataset from previous usage
- this.clearDataSet();
-
- // Get field iterator
- Iterator<Map.Entry<Field, Object>> iterator = contact.iterator();
-
- // Iterate over all
- while (iterator.hasNext()) {
- // Get next field
- Map.Entry<Field, Object> field = iterator.next();
-
- // Add it to data set
- this.addToDataSet(field.getKey().getName(), field.getValue());
- }
-
- // Then add it
- // TODO Nothing is done yet!
- Result<? extends Storable> result = this.doInsertDataSet();
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
- } catch (final IOException | BadTokenException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | CorruptedDatabaseFileException ex) {
- // Abort here
- this.abortProgramWithException(ex);
- }
-
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
- }
-
- @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, IOException {
- // And deligate to backend
- return this.getBackend().getTotalRows(); //NOI18N
- }
-
- @Override
- public String getIdName () {
- // Return id column
- return AddressbookContactDatabaseConstants.COLUMN_ID;
- }
-
- @Override
- public Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
- // Prepare search instance
- SearchableCriteria criteria = new SearchCriteria();
-
- // Add criteria and limit
- criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
- criteria.setLimit(1);
-
- // Then search for it
- Result<? extends Storable> result = this.getBackend().doSelectByCriteria(criteria);
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("result={0}", result));
-
- // Init instance
- Contact contact = null;
-
- // Is there one row at least?
- if (result.hasNext()) {
- // Then get it
- Storable storable = result.next();
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("storable={0}", storable));
-
- // Is it same instance?
- if (!(storable instanceof Contact)) {
- // Not same instance
- throw new IllegalArgumentException(MessageFormat.format("storable={0} is not implementing Contact", storable));
- }
-
- // Cast it securely
- contact = (Contact) storable;
- }
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
-
- // Return it
- return contact;
- }
-
- @Override
- public Storable getStorableAtRow (final int rowIndex) {
- throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
- }
-
- @Override
- public boolean isContactFound (final Contact contact) throws BadTokenException, IOException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Trace message
- this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
-
- // contact should not be null
- if (null == contact) {
- // Abort here
- throw new NullPointerException("contact is null"); //NOI18N
- }
-
- // Default is not found
- boolean isFound = false;
-
- // Init search instance (but empty)
- SearchableCriteria criteria = new SearchCriteria();
-
- // Look for all entries and compare here. Else all entries needs to be compared with many AND statements
- Result<? extends Storable> result = this.getBackend().doSelectByCriteria(criteria);
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("result({0})={1}", result.size(), result));
- // Start iteration
- Iterator<? extends Storable> iterator = result.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;
- }
-
- @Override
- public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Get search criteria instance
- SearchableCriteria criteria = new SearchCriteria();
-
- // Add condition
- criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
-
- // Get result
- Result<? extends Storable> result = this.getBackend().doSelectByCriteria(criteria);
-
- // Deligate this call to backend
- return result.hasNext();
- }
-
- @Override
- public Storable toStorable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- throw new UnsupportedOperationException("Not supported yet: map=" + map);
- }
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Roland Häder
- *
- * 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 org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
-import org.mxchange.jcore.contact.Contact;
-import org.mxchange.jcore.database.frontend.DatabaseFrontend;
-import org.mxchange.jcore.exceptions.BadTokenException;
-import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
-
-/**
- * An interface for addressbook contact database frontends
- *
- * @author Roland Häder
- */
-public interface AddressbookContactFrontend extends DatabaseFrontend {
-
- /**
- * Adds given contact instance to database
- *
- * @param contact Contact instance to add
- * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact is already added
- */
- public void addContact (final Contact contact) throws ContactAlreadyAddedException;
-
- /**
- * Some "getter" for total contacts count
- *
- * @return Total contacts count
- * @throws java.sql.SQLException If an SQL error occurs
- * @throws java.io.IOException If an IO error occurs
- */
- public int getContactsCount () throws SQLException, IOException;
-
- /**
- * Checks if given Contact is found
- *
- * @param contact Contact instance to check
- * @return Whether the given Contact instance is found
- * @throws java.sql.SQLException If an SQL error occurs
- * @throws java.io.IOException If an IO error occurs
- * @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?
- */
- public boolean isContactFound (final Contact contact) throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
-
- /**
- * Some "getter" for own contact instance
- *
- * @return Own contact instance
- * @throws java.sql.SQLException If an SQL error occurs
- * @throws java.io.IOException If an IO error occurs
- * @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?
- */
- public Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
-
- /**
- * Checks whether own contact is found
- *
- * @return Whether own contact is found
- * @throws java.sql.SQLException If any SQL error occurs
- * @throws java.io.IOException If an IO error occurs
- * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
- * @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?
- */
- public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
-}