]> git.mxchange.org Git - jfinancials-swing.git/blob - Addressbook/src/org/mxchange/addressbook/database/frontend/contact/ContactDatabaseFrontend.java
Fixes for broken commit, as type-hints must be fixed or casted
[jfinancials-swing.git] / Addressbook / src / org / mxchange / addressbook / database / frontend / contact / ContactDatabaseFrontend.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 org.mxchange.addressbook.contact.Contact;
24 import org.mxchange.addressbook.database.contact.ContactDatabaseConstants;
25 import org.mxchange.addressbook.database.frontend.BaseDatabaseFrontend;
26 import org.mxchange.addressbook.database.storage.Storeable;
27 import org.mxchange.addressbook.exceptions.BadTokenException;
28 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
29 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseBackendException;
30 import org.mxchange.addressbook.manager.contact.ContactManager;
31
32 /**
33  * Stores and retrieves Contact instances
34  *
35  * @author Roland Haeder
36  */
37 public class ContactDatabaseFrontend extends BaseDatabaseFrontend implements ContactWrapper {
38
39         /**
40          * Constructor which accepts a contact manager
41          *
42          * @param manager Manager instance
43          */
44         public ContactDatabaseFrontend (final ContactManager manager) {
45                 // Call own constructor
46                 this();
47
48                 // Trace message
49                 this.getLogger().trace(MessageFormat.format("manager={0} - CALLED!", manager)); //NOI18N
50
51                 // Manager instance must not be null
52                 if (manager == null) {
53                         // Abort here
54                         throw new NullPointerException("manager is null"); //NOI18N
55                 }
56
57                 // Set contact manager
58                 this.setContactManager(manager);
59         }
60
61         /**
62          * Basic constrcutor
63          */
64         protected ContactDatabaseFrontend () {
65                 // Trace message
66                 this.getLogger().trace("CALLED!"); //NOI18N
67
68                 // Set "table" name
69                 this.setTableName("contacts"); //NOI18N
70
71                 try {
72                         // Initalize backend
73                         this.initBackend();
74                 } catch (final UnsupportedDatabaseBackendException ex) {
75                         // Abort program
76                         this.abortProgramWithException(ex);
77                 } catch (final SQLException ex) {
78                         // Abort here
79                         this.abortProgramWithException(ex);
80                 }
81         }
82
83         /**
84          * Adds given contact instance to database
85          *
86          * @param contact Contact instance
87          * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If the contact instance is already found
88          */
89         @Override
90         public void addContact (final Contact contact) throws ContactAlreadyAddedException {
91                 // Trace message
92                 this.getLogger().trace("CALLED!"); //NOI18N
93
94                 // Make sure the contact is set
95                 if (contact == null) {
96                         // Abort here
97                         throw new NullPointerException("contact is null"); //NOI18N
98                 }
99
100                 try {
101                         // First check if the contact is there
102                         if (this.isContactFound(contact)) {
103                                 // Already there
104                                 throw new ContactAlreadyAddedException(contact);
105                         }
106
107                         // Then add it
108                         this.getBackend().store((Storeable) contact);
109                 } catch (final IOException ex) {
110                         // Abort here
111                         this.abortProgramWithException(ex);
112                 } catch (final 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 () {
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         /**
137          * Some "getter" for total contact count
138          *
139          * @return Total contact count
140          */
141         @Override
142         public int getContactsCount () throws SQLException {
143                 // And deligate to backend
144                 return this.getBackend().getTotalCount(); //NOI18N
145         }
146
147         /**
148          * Some "getter" for own contact instance
149          *
150          * @return Own contact instance
151          */
152         @Override
153         public Contact getOwnContact () {
154                 // Trace message
155                 this.getLogger().trace("CALLED!"); //NOI18N
156
157                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
158         }
159
160         /**
161          * Checks if given Contact is found
162          * 
163          * @param contact Contact instance to check
164          * @return Whether the given Contact instance is found
165          */
166         @Override
167         public boolean isContactFound (final Contact contact) throws BadTokenException {
168                 // Trace message
169                 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
170
171                 // contact should not be null
172                 if (contact == null) {
173                         // Abort here
174                         throw new NullPointerException("contact is null"); //NOI18N
175                 }
176
177                 // Default is not found
178                 boolean isFound = false;
179
180                 // Start iteration
181                 Iterator<? extends Storeable> iterator = this.getBackend().iterator();
182
183                 // Check all entries
184                 while (iterator.hasNext()) {
185                         // Get next element
186                         Contact c = (Contact) iterator.next();
187
188                         // Debug message
189                         this.getLogger().debug(MessageFormat.format("c={0},contact={1}", c, contact)); //NOI18N
190
191                         // Is it added?
192                         if (c.equals(contact)) {
193                                 // Is found
194                                 isFound = true;
195                                 break;
196                         }
197                 }
198
199                 // Trace message
200                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
201
202                 // Return it
203                 return isFound;
204         }
205
206         /**
207          * Checks whether own contact is found in database
208          *
209          * @return Whether own contact is found
210          */
211         @Override
212         public boolean isOwnContactFound () throws SQLException {
213                 // Deligate this call to backend
214                 return this.getBackend().isRowFound(ContactDatabaseConstants.COLUMN_NAME_OWN_CONTACT, true);
215         }
216 }