]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
410ee2e7f8d2e4cbd9e9ad28e43ab2e9642f7cc5
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / jcontacts / contact / AddressbookContactSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcontacts.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.ejb.Stateless;
24 import javax.persistence.NoResultException;
25 import javax.persistence.Query;
26 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
27 import org.mxchange.jcontacts.contact.utils.ContactUtils;
28 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
29
30 /**
31  * A contact EJB
32  * <p>
33  * @author Roland Häder<roland@mxchange.org>
34  */
35 @Stateless (name = "contact", description = "A bean handling contact data")
36 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 542_145_347_916L;
42
43         /**
44          * Default constructor
45          */
46         public AddressbookContactSessionBean () {
47         }
48
49         @Override
50         public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
51                 // Log trace message
52                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
53
54                 // The parameter must be valid
55                 if (null == emailAddress) {
56                         // Throw NPE
57                         throw new NullPointerException("emailAddress is null"); //NOI18N
58                 } else if (emailAddress.isEmpty()) {
59                         // Not valid
60                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
61                 }
62
63                 // Get query instance
64                 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
65
66                 // Set parameter
67                 query.setParameter("emailAddress", emailAddress); //NOI18N
68
69                 // Init contact instance
70                 Contact contact;
71
72                 // Try to find a result
73                 try {
74                         // Find a single result
75                         contact = (Contact) query.getSingleResult();
76
77                         // Log trace message
78                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
79                 } catch (final NoResultException ex) {
80                         // No result found
81                         throw new ContactNotFoundException(emailAddress, ex);
82                 }
83
84                 // Log trace message
85                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
86
87                 // Return found instance
88                 return contact;
89         }
90
91         @Override
92         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
93                 // Log trace message
94                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
95
96                 // The parameter must be valid
97                 if (null == contactId) {
98                         // Throw NPE
99                         throw new NullPointerException("contactId is null"); //NOI18N
100                 } else if (contactId < 1) {
101                         // Not valid
102                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
103                 }
104
105                 // Get query instance
106                 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
107
108                 // Set parameter
109                 query.setParameter("contactId", contactId); //NOI18N
110
111                 // Init contact instance
112                 Contact contact;
113
114                 // Try to find a result
115                 try {
116                         // Find a single result
117                         contact = (Contact) query.getSingleResult();
118
119                         // Log trace message
120                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
121                 } catch (final NoResultException ex) {
122                         // No result found
123                         throw new ContactNotFoundException(contactId, ex);
124                 }
125
126                 // Log trace message
127                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
128
129                 // Return found instance
130                 return contact;
131         }
132
133         @Override
134         @SuppressWarnings ("unchecked")
135         public List<Contact> getAllContacts () {
136                 // Log trace message
137                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
138
139                 // Create query instance
140                 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
141
142                 // Get list
143                 List<Contact> contacts = query.getResultList();
144
145                 // Log trace message
146                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
147
148                 // Return it
149                 return contacts;
150         }
151
152         @Override
153         @SuppressWarnings ("unchecked")
154         public List<String> getEmailAddressList () {
155                 // Log trace message
156                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
157
158                 // Create query instance
159                 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
160
161                 // Get list
162                 List<String> emailAddresses = query.getResultList();
163
164                 // Log trace message
165                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
166
167                 // Return it
168                 return emailAddresses;
169         }
170
171         @Override
172         public boolean isEmailAddressRegistered (final String emailAddress) {
173                 // Log trace message
174                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
175
176                 // The email address should be valid
177                 if (null == emailAddress) {
178                         // Is null
179                         throw new NullPointerException("emailAddress is null"); //NOI18N
180                 } else if (emailAddress.isEmpty()) {
181                         // Is empty
182                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
183                 }
184
185                 // Default is not found
186                 boolean isFound = false;
187
188                 try {
189                         // Ask other method for contact instance
190                         Contact contact = this.findContactByEmailAddress(emailAddress);
191
192                         // Log debug message
193                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
194
195                         // It is found ...
196                         isFound = true;
197                 } catch (final ContactNotFoundException ex) {
198                         // @TODO Was not found, log exception for spam check?
199                 }
200
201                 // Log trace message
202                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
203
204                 // Return status
205                 return isFound;
206         }
207
208         @Override
209         public Contact lookupContact (final Contact contact) {
210                 // Log trace message
211                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
212
213                 // Parameter should be valid
214                 if (null == contact) {
215                         // Throw NPE
216                         throw new NullPointerException("contact is null"); //NOI18N
217                 } else if (contact.getContactId() > 0) {
218                         try {
219                                 // Id set, ask other method
220                                 return this.findContactById(contact.getContactId());
221                         } catch (final ContactNotFoundException ex) {
222                                 // Not found, should not happen
223                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
224                         }
225                 }
226
227                 // Default is not found
228                 Contact foundContact = null;
229
230                 // Get whole list
231                 List<Contact> contacts = this.getAllContacts();
232
233                 // Is the list empty?
234                 if (contacts.isEmpty()) {
235                         // Then abort here
236                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
237                         return null;
238                 }
239
240                 // Get iterator
241                 Iterator<Contact> iterator = contacts.iterator();
242
243                 // Loop through all
244                 while (iterator.hasNext()) {
245                         // Get contact
246                         Contact next = iterator.next();
247
248                         // Is same contact?
249                         if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
250                                 // Debug message
251                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
252
253                                 // Found it
254                                 foundContact = next;
255                                 break;
256                         }
257                 }
258
259                 // Trace message
260                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
261
262                 // Return found contact
263                 return foundContact;
264         }
265
266         @Override
267         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
268                 // Log trace message
269                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
270
271                 // The contact instance must be valid
272                 if (null == contact) {
273                         // Throw NPE again
274                         throw new NullPointerException("contact is null"); //NOI18N
275                 } else if (contact.getContactId() == null) {
276                         // Throw NPE again
277                         throw new NullPointerException("contact.contactId is null"); //NOI18N
278                 } else if (contact.getContactId() < 1) {
279                         // Not valid
280                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
281                 }
282
283                 // Set updated timestamp
284                 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
285
286                 // Merge cellphone, land-line and fix
287                 Contact detachedContact = this.mergeContactData(contact);
288
289                 // Trace message
290                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
291
292                 // Return it
293                 return detachedContact;
294         }
295
296         @Override
297         public Contact updateContactData (final Contact contact) {
298                 // Log trace message
299                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
300
301                 // The contact instance must be valid
302                 if (null == contact) {
303                         // Throw NPE again
304                         throw new NullPointerException("contact is null"); //NOI18N
305                 } else if (contact.getContactId() == null) {
306                         // Throw NPE again
307                         throw new NullPointerException("contact.contactId is null"); //NOI18N
308                 } else if (contact.getContactId() < 1) {
309                         // Not valid
310                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
311                 }
312
313                 // Is cell phone/land-line/fax number unlinked?
314                 boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
315                 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
316                 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
317
318                 // Call other Method
319                 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
320
321                 // Trace message
322                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
323
324                 // Return it
325                 return detachedContact;
326         }
327
328 }