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