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.Storeable;
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 (manager == null) {
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 * Adds given contact instance to database
89 * @param contact Contact instance
90 * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
93 public void addContact (final Contact contact) throws ContactAlreadyAddedException {
95 this.getLogger().trace("CALLED!"); //NOI18N
97 // Make sure the contact is set
98 if (contact == null) {
100 throw new NullPointerException("contact is null"); //NOI18N
104 // First check if the contact is there
105 if (this.isContactFound(contact)) {
107 throw new ContactAlreadyAddedException(contact);
110 // Clear dataset from previous usage
113 // Get field iterator
114 Iterator<Map.Entry<Field, Object>> iterator = contact.iterator();
117 while (iterator.hasNext()) {
119 Map.Entry<Field, Object> field = iterator.next();
121 // Add it to data set
122 this.addToDataSet(field.getKey().getName(), field.getValue());
126 Result<? extends Storeable> result = this.doInsertDataSet();
129 this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
130 } catch (final IOException | BadTokenException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
132 this.abortProgramWithException(ex);
136 this.getLogger().trace("CALLED!"); //NOI18N
140 * Shuts down the database layer
141 * @throws java.sql.SQLException If an SQL error occurs
142 * @throws java.io.IOException If an IO error occurs
145 public void doShutdown () throws SQLException, IOException {
147 this.getLogger().trace("CALLED!"); //NOI18N
150 this.getBackend().doShutdown();
153 this.getLogger().trace("EXIT!"); //NOI18N
157 public Object emptyStringToNull (final String key, final Object value) {
158 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
162 * Some "getter" for total contact count
164 * @return Total contact count
167 public int getContactsCount () throws SQLException, IOException {
168 // And deligate to backend
169 return this.getBackend().getTotalRows(); //NOI18N
173 public String getIdName () {
175 return AddressbookContactDatabaseConstants.COLUMN_ID;
179 public Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
181 this.getLogger().trace("CALLED!"); //NOI18N
183 // Prepare search instance
184 SearchableCriteria criteria = new SearchCriteria();
186 // Add criteria and limit
187 criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
188 criteria.setLimit(1);
190 // Then search for it
191 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
194 this.getLogger().debug(MessageFormat.format("result={0}", result));
197 Contact contact = null;
199 // Is there one row at least?
200 if (result.hasNext()) {
202 Storeable storeable = result.next();
205 this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
207 // Is it same instance?
208 if (!(storeable instanceof Contact)) {
210 throw new IllegalArgumentException(MessageFormat.format("storeable={0} is not implementing Contact", storeable));
214 contact = (Contact) storeable;
218 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
225 public Storeable getStoreableAtRow (final int rowIndex) {
226 throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
230 * Checks if given Contact is found
232 * @param contact Contact instance to check
233 * @return Whether the given Contact instance is found
236 public boolean isContactFound (final Contact contact) throws BadTokenException {
238 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
240 // contact should not be null
241 if (contact == null) {
243 throw new NullPointerException("contact is null"); //NOI18N
246 // Default is not found
247 boolean isFound = false;
250 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
253 while (iterator.hasNext()) {
255 Contact c = (Contact) iterator.next();
258 this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
261 if (c.equals(contact)) {
269 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
276 * Checks whether own contact is found in database
278 * @return Whether own contact is found
279 * @throws org.mxchange.jcore.exceptions.BadTokenException Continued throw
280 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
281 * @throws java.lang.NoSuchMethodException If a method cannot be found
282 * @throws java.lang.IllegalAccessException If a method is not accessible
283 * @throws java.lang.reflect.InvocationTargetException Any other problems?
286 public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
287 // Get search criteria instance
288 SearchableCriteria critera = new SearchCriteria();
291 critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
294 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
296 // Deligate this call to backend
297 return result.hasNext();
301 public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
302 throw new UnsupportedOperationException("Not supported yet: map=" + map);