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.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
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.criteria.searchable.SearchCriteria;
31 import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
32 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
33 import org.mxchange.jcore.database.result.Result;
34 import org.mxchange.jcore.database.storage.Storable;
35 import org.mxchange.jcore.exceptions.BadTokenException;
36 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
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 (null == manager) {
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 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
89 this.getLogger().trace("CALLED!"); //NOI18N
91 // Make sure the contact is set
92 if (null == contact) {
94 throw new NullPointerException("contact is null"); //NOI18N
98 // First check if the contact is there
99 if (this.isContactFound(contact)) {
101 throw new ContactAlreadyAddedException(contact);
104 // Clear dataset from previous usage
107 // Get field iterator
108 Iterator<Map.Entry<Field, Object>> iterator = contact.iterator();
111 while (iterator.hasNext()) {
113 Map.Entry<Field, Object> field = iterator.next();
115 // Add it to data set
116 this.addToDataSet(field.getKey().getName(), field.getValue());
120 // @todo Nothing is done yet!
121 Result<? extends Storable> result = this.doInsertDataSet();
124 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
125 } catch (final IOException | BadTokenException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | CorruptedDatabaseFileException ex) {
127 this.abortProgramWithException(ex);
131 this.getLogger().trace("CALLED!"); //NOI18N
135 public void doShutdown () throws SQLException, IOException {
137 this.getLogger().trace("CALLED!"); //NOI18N
140 this.getBackend().doShutdown();
143 this.getLogger().trace("EXIT!"); //NOI18N
147 public Object emptyStringToNull (final String key, final Object value) {
148 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
152 * Some "getter" for total contact count
154 * @return Total contact count
157 public int getContactsCount () throws SQLException, IOException {
158 // And deligate to backend
159 return this.getBackend().getTotalRows(); //NOI18N
163 public String getIdName () {
165 return AddressbookContactDatabaseConstants.COLUMN_ID;
169 public Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
171 this.getLogger().trace("CALLED!"); //NOI18N
173 // Prepare search instance
174 SearchableCriteria criteria = new SearchCriteria();
176 // Add criteria and limit
177 criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
178 criteria.setLimit(1);
180 // Then search for it
181 Result<? extends Storable> result = this.getBackend().doSelectByCriteria(criteria);
184 this.getLogger().debug(MessageFormat.format("result={0}", result));
187 Contact contact = null;
189 // Is there one row at least?
190 if (result.hasNext()) {
192 Storable storeable = result.next();
195 this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
197 // Is it same instance?
198 if (!(storeable instanceof Contact)) {
200 throw new IllegalArgumentException(MessageFormat.format("storeable={0} is not implementing Contact", storeable));
204 contact = (Contact) storeable;
208 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
215 public Storable getStorableAtRow (final int rowIndex) {
216 throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
220 public boolean isContactFound (final Contact contact) throws BadTokenException, IOException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
222 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
224 // contact should not be null
225 if (null == contact) {
227 throw new NullPointerException("contact is null"); //NOI18N
230 // Default is not found
231 boolean isFound = false;
233 // Init search instance (but empty)
234 SearchableCriteria criteria = new SearchCriteria();
236 // Look for all entries and compare here. Else all entries needs to be compared with many AND statements
237 Result<? extends Storable> result = this.getBackend().doSelectByCriteria(criteria);
240 this.getLogger().debug(MessageFormat.format("result({0})={1}", result.size(), result));
242 Iterator<? extends Storable> iterator = result.iterator();
245 while (iterator.hasNext()) {
247 Contact c = (Contact) iterator.next();
250 this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
253 if (c.equals(contact)) {
261 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
268 public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
269 // Get search criteria instance
270 SearchableCriteria criteria = new SearchCriteria();
273 criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
276 Result<? extends Storable> result = this.getBackend().doSelectByCriteria(criteria);
278 // Deligate this call to backend
279 return result.hasNext();
283 public Storable toStorable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
284 throw new UnsupportedOperationException("Not supported yet: map=" + map);