]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java
Fixes for new changes in jcore
[addressbook-swing.git] / Addressbook / 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.sql.SQLException;
21 import java.text.MessageFormat;
22 import java.util.Iterator;
23 import java.util.StringTokenizer;
24 import org.mxchange.addressbook.contact.book.BookContact;
25 import org.mxchange.addressbook.contact.user.UserContact;
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.contact.Gender;
31 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
32 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
33 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
34 import org.mxchange.jcore.database.result.Result;
35 import org.mxchange.jcore.database.storage.Storeable;
36 import org.mxchange.jcore.exceptions.BadTokenException;
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         /**
87          * Adds given contact instance to database
88          *
89          * @param contact Contact instance
90          * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
91          */
92         @Override
93         public void addContact (final Contact contact) throws ContactAlreadyAddedException {
94                 // Trace message
95                 this.getLogger().trace("CALLED!"); //NOI18N
96
97                 // Make sure the contact is set
98                 if (contact == null) {
99                         // Abort here
100                         throw new NullPointerException("contact is null"); //NOI18N
101                 }
102
103                 try {
104                         // First check if the contact is there
105                         if (this.isContactFound(contact)) {
106                                 // Already there
107                                 throw new ContactAlreadyAddedException(contact);
108                         }
109
110                         // Then add it
111                         this.getBackend().store((Storeable) contact);
112                 } catch (final IOException | BadTokenException ex) {
113                         // Abort here
114                         this.abortProgramWithException(ex);
115                 }
116
117                 // Trace message
118                 this.getLogger().trace("CALLED!"); //NOI18N
119         }
120
121         /**
122          * Shuts down the database layer
123          */
124         @Override
125         public void doShutdown () throws SQLException, IOException {
126                 // Trace message
127                 this.getLogger().trace("CALLED!"); //NOI18N
128
129                 // Shutdown backend
130                 this.getBackend().doShutdown();
131
132                 // Trace message
133                 this.getLogger().trace("EXIT!"); //NOI18N
134         }
135
136         @Override
137         public Object emptyStringToNull (final String key, final Object value) {
138                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
139         }
140
141         /**
142          * Some "getter" for total contact count
143          *
144          * @return Total contact count
145          */
146         @Override
147         public int getContactsCount () throws SQLException {
148                 // And deligate to backend
149                 return this.getBackend().getTotalCount(); //NOI18N
150         }
151
152         /**
153          * Some "getter" for own contact instance
154          *
155          * @return Own contact instance
156          */
157         @Override
158         public Contact getOwnContact () {
159                 // Trace message
160                 this.getLogger().trace("CALLED!"); //NOI18N
161
162                 // Get row index back from backend
163                 int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
164
165                 // Debug message
166                 this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
167
168                 // Init instance
169                 Contact contact = null;
170
171                 try {
172                         // Now simply read the row
173                         contact = (Contact) this.getBackend().readRow(rowIndex);
174                 } catch (final BadTokenException ex) {
175                         // Bad token found
176                         this.abortProgramWithException(ex);
177                 }
178
179                 // Trace message
180                 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
181
182                 // Return it
183                 return contact;
184         }
185
186         /**
187          * Checks if given Contact is found
188          * 
189          * @param contact Contact instance to check
190          * @return Whether the given Contact instance is found
191          */
192         @Override
193         public boolean isContactFound (final Contact contact) throws BadTokenException {
194                 // Trace message
195                 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
196
197                 // contact should not be null
198                 if (contact == null) {
199                         // Abort here
200                         throw new NullPointerException("contact is null"); //NOI18N
201                 }
202
203                 // Default is not found
204                 boolean isFound = false;
205
206                 // Start iteration
207                 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
208
209                 // Check all entries
210                 while (iterator.hasNext()) {
211                         // Get next element
212                         Contact c = (Contact) iterator.next();
213
214                         // Debug message
215                         this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
216
217                         // Is it added?
218                         if (c.equals(contact)) {
219                                 // Is found
220                                 isFound = true;
221                                 break;
222                         }
223                 }
224
225                 // Trace message
226                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
227
228                 // Return it
229                 return isFound;
230         }
231
232         /**
233          * Checks whether own contact is found in database
234          *
235          * @return Whether own contact is found
236          * @throws java.io.IOException If any IO error occurs
237          * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
238          */
239         @Override
240         public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException {
241                 // Get search criteria instance
242                 SearchableCritera critera = new SearchCriteria();
243
244                 // Add condition
245                 critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
246
247                 // Get result
248                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
249
250                 // Deligate this call to backend
251                 return result.hasNext();
252         }
253
254         /**
255          * Reads a single row and parses it to a contact instance
256          *
257          * @param rowIndex Row index (also how much to skip)
258          * @return Contact instance
259          */
260         @Override
261         public Contact readSingleContact (final int rowIndex) {
262                 try {
263                         // Deligate this to backend instance
264                         return (Contact) this.getBackend().readRow(rowIndex);
265                 } catch (final BadTokenException ex) {
266                         // Bad token found
267                         this.abortProgramWithException(ex);
268                 }
269
270                 // Bad state, should not be reached
271                 throw new IllegalStateException("This should not be reached");
272         }
273 }