]> git.mxchange.org Git - jfinancials-swing.git/blob - src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java
3577e17b433a2888e201f95ae214c0dcf225ad9f
[jfinancials-swing.git] / src / org / mxchange / addressbook / database / frontend / contact / AddressbookContactDatabaseFrontend.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.database.frontend.contact;
18
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;
25 import java.util.Map;
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;
38
39 /**
40  * Stores and retrieves Contact instances
41  *
42  * @author Roland Haeder
43  */
44 public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend implements AddressbookContactFrontend {
45
46         /**
47          * Constructor which accepts a contact manager
48          *
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
52          */
53         public AddressbookContactDatabaseFrontend (final AddressbookContactManager manager) throws UnsupportedDatabaseBackendException, SQLException {
54                 // Call own constructor
55                 this();
56
57                 // Trace message
58                 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
59
60                 // Manager instance must not be null
61                 if (manager == null) {
62                         // Abort here
63                         throw new NullPointerException("manager is null"); //NOI18N
64                 }
65
66                 // Set contact manager
67                 this.setContactManager(manager);
68         }
69
70         /**
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
74          */
75         protected AddressbookContactDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
76                 // Trace message
77                 this.getLogger().trace("CALLED!"); //NOI18N
78
79                 // Set "table" name
80                 this.setTableName("contacts"); //NOI18N
81
82                 // Initalize backend
83                 this.initBackend();
84         }
85
86         @Override
87         public void addContact (final Contact contact) throws ContactAlreadyAddedException {
88                 // Trace message
89                 this.getLogger().trace("CALLED!"); //NOI18N
90
91                 // Make sure the contact is set
92                 if (contact == null) {
93                         // Abort here
94                         throw new NullPointerException("contact is null"); //NOI18N
95                 }
96
97                 try {
98                         // First check if the contact is there
99                         if (this.isContactFound(contact)) {
100                                 // Already there
101                                 throw new ContactAlreadyAddedException(contact);
102                         }
103
104                         // Clear dataset from previous usage
105                         this.clearDataSet();
106
107                         // Get field iterator
108                         Iterator<Map.Entry<Field, Object>> iterator = contact.iterator();
109
110                         // Iterate over all
111                         while (iterator.hasNext()) {
112                                 // Get next field
113                                 Map.Entry<Field, Object> field = iterator.next();
114
115                                 // Add it to data set
116                                 this.addToDataSet(field.getKey().getName(), field.getValue());
117                         }
118
119                         // Then add it
120                         Result<? extends Storeable> result = this.doInsertDataSet();
121
122                         // Debug message
123                         this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
124                 } catch (final IOException | BadTokenException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | CorruptedDatabaseFileException ex) {
125                         // Abort here
126                         this.abortProgramWithException(ex);
127                 }
128
129                 // Trace message
130                 this.getLogger().trace("CALLED!"); //NOI18N
131         }
132
133         @Override
134         public void doShutdown () throws SQLException, IOException {
135                 // Trace message
136                 this.getLogger().trace("CALLED!"); //NOI18N
137
138                 // Shutdown backend
139                 this.getBackend().doShutdown();
140
141                 // Trace message
142                 this.getLogger().trace("EXIT!"); //NOI18N
143         }
144
145         @Override
146         public Object emptyStringToNull (final String key, final Object value) {
147                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
148         }
149
150         /**
151          * Some "getter" for total contact count
152          *
153          * @return Total contact count
154          */
155         @Override
156         public int getContactsCount () throws SQLException, IOException {
157                 // And deligate to backend
158                 return this.getBackend().getTotalRows(); //NOI18N
159         }
160
161         @Override
162         public String getIdName () {
163                 // Return id column
164                 return AddressbookContactDatabaseConstants.COLUMN_ID;
165         }
166
167         @Override
168         public Contact getOwnContact () throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
169                 // Trace message
170                 this.getLogger().trace("CALLED!"); //NOI18N
171
172                 // Prepare search instance
173                 SearchableCriteria criteria = new SearchCriteria();
174
175                 // Add criteria and limit
176                 criteria.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
177                 criteria.setLimit(1);
178
179                 // Then search for it
180                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
181
182                 // Debug message
183                 this.getLogger().debug(MessageFormat.format("result={0}", result));
184
185                 // Init instance
186                 Contact contact = null;
187
188                 // Is there one row at least?
189                 if (result.hasNext()) {
190                         // Then get it
191                         Storeable storeable = result.next();
192
193                         // Debug message
194                         this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
195
196                         // Is it same instance?
197                         if (!(storeable instanceof Contact)) {
198                                 // Not same instance
199                                 throw new IllegalArgumentException(MessageFormat.format("storeable={0} is not implementing Contact", storeable));
200                         }
201
202                         // Cast it securely
203                         contact = (Contact) storeable;
204                 }
205
206                 // Trace message
207                 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
208
209                 // Return it
210                 return contact;
211         }
212
213         @Override
214         public Storeable getStoreableAtRow (final int rowIndex) {
215                 throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
216         }
217
218         @Override
219         public boolean isContactFound (final Contact contact) throws BadTokenException, IOException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
220                 // Trace message
221                 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
222
223                 // contact should not be null
224                 if (contact == null) {
225                         // Abort here
226                         throw new NullPointerException("contact is null"); //NOI18N
227                 }
228
229                 // Default is not found
230                 boolean isFound = false;
231
232                 // Init search instance (but empty)
233                 SearchableCriteria criteria = new SearchCriteria();
234
235                 // Look for all entries and compare here. Else all entries needs to be compared with many AND statements
236                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
237
238                 // Debug message
239                 this.getLogger().debug(MessageFormat.format("result({0})={1}", result.size(), result));
240                 // Start iteration
241                 Iterator<? extends Storeable> iterator = result.iterator();
242
243                 // Check all entries
244                 while (iterator.hasNext()) {
245                         // Get next element
246                         Contact c = (Contact) iterator.next();
247
248                         // Debug message
249                         this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
250
251                         // Is it added?
252                         if (c.equals(contact)) {
253                                 // Is found
254                                 isFound = true;
255                                 break;
256                         }
257                 }
258
259                 // Trace message
260                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
261
262                 // Return it
263                 return isFound;
264         }
265
266         @Override
267         public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
268                 // Get search criteria instance
269                 SearchableCriteria critera = new SearchCriteria();
270
271                 // Add condition
272                 critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_OWN_CONTACT, true);
273
274                 // Get result
275                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
276
277                 // Deligate this call to backend
278                 return result.hasNext();
279         }
280
281         @Override
282         public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
283                 throw new UnsupportedOperationException("Not supported yet: map=" + map);
284         }
285 }