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