]> git.mxchange.org Git - addressbook-swing.git/blob
48f8bbee5babe22fa4506afebd53ebbd56777186
[addressbook-swing.git] /
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.InvocationTargetException;
21 import java.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.Iterator;
24 import java.util.StringTokenizer;
25 import org.mxchange.addressbook.contact.book.BookContact;
26 import org.mxchange.addressbook.contact.user.UserContact;
27 import org.mxchange.addressbook.database.contact.AddressbookContactDatabaseConstants;
28 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
29 import org.mxchange.addressbook.manager.contact.AddressbookContactManager;
30 import org.mxchange.jcore.contact.Contact;
31 import org.mxchange.jcore.contact.Gender;
32 import org.mxchange.jcore.criteria.searchable.SearchCriteria;
33 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
34 import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
35 import org.mxchange.jcore.database.result.Result;
36 import org.mxchange.jcore.database.storage.Storeable;
37 import org.mxchange.jcore.exceptions.BadTokenException;
38 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
39 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
40
41 /**
42  * Stores and retrieves Contact instances
43  *
44  * @author Roland Haeder
45  */
46 public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend implements AddressbookContactFrontend {
47
48         /**
49          * Constructor which accepts a contact manager
50          *
51          * @param manager Manager instance
52          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
53          * @throws java.sql.SQLException If an SQL error occurs
54          */
55         public AddressbookContactDatabaseFrontend (final AddressbookContactManager manager) throws UnsupportedDatabaseBackendException, SQLException {
56                 // Call own constructor
57                 this();
58
59                 // Trace message
60                 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
61
62                 // Manager instance must not be null
63                 if (manager == null) {
64                         // Abort here
65                         throw new NullPointerException("manager is null"); //NOI18N
66                 }
67
68                 // Set contact manager
69                 this.setContactManager(manager);
70         }
71
72         /**
73          * Default but protected constructor
74          * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported
75          * @throws java.sql.SQLException Any SQL exception from e.g. MySQL connector
76          */
77         protected AddressbookContactDatabaseFrontend () throws UnsupportedDatabaseBackendException, SQLException {
78                 // Trace message
79                 this.getLogger().trace("CALLED!"); //NOI18N
80
81                 // Set "table" name
82                 this.setTableName("contacts"); //NOI18N
83
84                 // Initalize backend
85                 this.initBackend();
86         }
87
88         /**
89          * Adds given contact instance to database
90          *
91          * @param contact Contact instance
92          * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
93          */
94         @Override
95         public void addContact (final Contact contact) throws ContactAlreadyAddedException {
96                 // Trace message
97                 this.getLogger().trace("CALLED!"); //NOI18N
98
99                 // Make sure the contact is set
100                 if (contact == null) {
101                         // Abort here
102                         throw new NullPointerException("contact is null"); //NOI18N
103                 }
104
105                 try {
106                         // First check if the contact is there
107                         if (this.isContactFound(contact)) {
108                                 // Already there
109                                 throw new ContactAlreadyAddedException(contact);
110                         }
111
112                         // Then add it
113                         this.getBackend().store((Storeable) contact);
114                 } catch (final IOException | BadTokenException ex) {
115                         // Abort here
116                         this.abortProgramWithException(ex);
117                 }
118
119                 // Trace message
120                 this.getLogger().trace("CALLED!"); //NOI18N
121         }
122
123         /**
124          * Shuts down the database layer
125          */
126         @Override
127         public void doShutdown () throws SQLException, IOException {
128                 // Trace message
129                 this.getLogger().trace("CALLED!"); //NOI18N
130
131                 // Shutdown backend
132                 this.getBackend().doShutdown();
133
134                 // Trace message
135                 this.getLogger().trace("EXIT!"); //NOI18N
136         }
137
138         @Override
139         public Object emptyStringToNull (final String key, final Object value) {
140                 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: key={0},value={1}", key, value));
141         }
142
143         /**
144          * Some "getter" for total contact count
145          *
146          * @return Total contact count
147          */
148         @Override
149         public int getContactsCount () throws SQLException {
150                 // And deligate to backend
151                 return this.getBackend().getTotalCount(); //NOI18N
152         }
153
154         /**
155          * Some "getter" for own contact instance
156          *
157          * @return Own contact instance
158          */
159         @Override
160         public Contact getOwnContact () {
161                 // Trace message
162                 this.getLogger().trace("CALLED!"); //NOI18N
163
164                 // Get row index back from backend
165                 int rowIndex = this.getBackend().getRowIndexFromColumn(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
166
167                 // Debug message
168                 this.getLogger().debug(MessageFormat.format("rowIndex={0}", rowIndex));
169
170                 // Init instance
171                 Contact contact = null;
172
173                 try {
174                         // Now simply read the row
175                         contact = (Contact) this.getBackend().readRow(rowIndex);
176                 } catch (final BadTokenException ex) {
177                         // Bad token found
178                         this.abortProgramWithException(ex);
179                 }
180
181                 // Trace message
182                 this.getLogger().trace(MessageFormat.format("contact={0} - EXIT!", contact));
183
184                 // Return it
185                 return contact;
186         }
187
188         /**
189          * Checks if given Contact is found
190          * 
191          * @param contact Contact instance to check
192          * @return Whether the given Contact instance is found
193          */
194         @Override
195         public boolean isContactFound (final Contact contact) throws BadTokenException {
196                 // Trace message
197                 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
198
199                 // contact should not be null
200                 if (contact == null) {
201                         // Abort here
202                         throw new NullPointerException("contact is null"); //NOI18N
203                 }
204
205                 // Default is not found
206                 boolean isFound = false;
207
208                 // Start iteration
209                 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
210
211                 // Check all entries
212                 while (iterator.hasNext()) {
213                         // Get next element
214                         Contact c = (Contact) iterator.next();
215
216                         // Debug message
217                         this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
218
219                         // Is it added?
220                         if (c.equals(contact)) {
221                                 // Is found
222                                 isFound = true;
223                                 break;
224                         }
225                 }
226
227                 // Trace message
228                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
229
230                 // Return it
231                 return isFound;
232         }
233
234         /**
235          * Checks whether own contact is found in database
236          *
237          * @return Whether own contact is found
238          * @throws org.mxchange.jcore.exceptions.BadTokenException Continued throw
239          * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
240          * @throws java.lang.NoSuchMethodException If a method cannot be found
241          * @throws java.lang.IllegalAccessException If a method is not accessible
242          * @throws java.lang.reflect.InvocationTargetException Any other problems?
243          */
244         @Override
245         public boolean isOwnContactFound () throws SQLException, IOException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
246                 // Get search criteria instance
247                 SearchableCritera critera = new SearchCriteria();
248
249                 // Add condition
250                 critera.addCriteria(AddressbookContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
251
252                 // Get result
253                 Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
254
255                 // Deligate this call to backend
256                 return result.hasNext();
257         }
258
259         /**
260          * Reads a single row and parses it to a contact instance
261          *
262          * @param rowIndex Row index (also how much to skip)
263          * @return Contact instance
264          */
265         @Override
266         public Contact readSingleContact (final int rowIndex) {
267                 try {
268                         // Deligate this to backend instance
269                         return (Contact) this.getBackend().readRow(rowIndex);
270                 } catch (final BadTokenException ex) {
271                         // Bad token found
272                         this.abortProgramWithException(ex);
273                 }
274
275                 // Bad state, should not be reached
276                 throw new IllegalStateException("This should not be reached");
277         }
278 }