]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontacts/model/contact/JobsContactSessionBean.java
Please cherry-pick:
[jjobs-ejb.git] / src / java / org / mxchange / jcontacts / model / contact / JobsContactSessionBean.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.jjobs.database.BaseJobsDatabaseBean;
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 JobsContactSessionBean extends BaseJobsDatabaseBean 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 JobsContactSessionBean () {
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 Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
90                 // Log trace message
91                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
92
93                 // The parameter must be valid
94                 if (null == emailAddress) {
95                         // Throw NPE
96                         throw new NullPointerException("emailAddress is null"); //NOI18N
97                 } else if (emailAddress.isEmpty()) {
98                         // Not valid
99                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
100                 }
101
102                 // Get query instance
103                 final Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
104
105                 // Set parameter and limit
106                 query.setParameter("emailAddress", emailAddress); //NOI18N
107                 query.setMaxResults(1);
108
109                 // Init contact instance
110                 final Contact contact;
111
112                 // Try to find a result
113                 try {
114                         // Find a single result
115                         contact = (Contact) query.getSingleResult();
116
117                         // Log trace message
118                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
119                 } catch (final NoResultException ex) {
120                         // No result found
121                         throw new ContactNotFoundException(emailAddress, ex);
122                 }
123
124                 // Log trace message
125                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
126
127                 // Return found instance
128                 return contact;
129         }
130
131         @Override
132         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
133                 // Log trace message
134                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
135
136                 // The parameter must be valid
137                 if (null == contactId) {
138                         // Throw NPE
139                         throw new NullPointerException("contactId is null"); //NOI18N
140                 } else if (contactId < 1) {
141                         // Not valid
142                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
143                 }
144
145                 // Get query instance
146                 final Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
147
148                 // Set parameter
149                 query.setParameter("contactId", contactId); //NOI18N
150                 query.setMaxResults(1);
151
152                 // Init contact instance
153                 final Contact contact;
154
155                 // Try to find a result
156                 try {
157                         // Find a single result
158                         contact = (Contact) query.getSingleResult();
159
160                         // Log trace message
161                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
162                 } catch (final NoResultException ex) {
163                         // No result found
164                         throw new ContactNotFoundException(contactId, ex);
165                 }
166
167                 // Log trace message
168                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
169
170                 // Return found instance
171                 return contact;
172         }
173
174         @Override
175         public boolean isEmailAddressRegistered (final String emailAddress) {
176                 // Log trace message
177                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
178
179                 // The email address should be valid
180                 if (null == emailAddress) {
181                         // Is null
182                         throw new NullPointerException("emailAddress is null"); //NOI18N
183                 } else if (emailAddress.isEmpty()) {
184                         // Is empty
185                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
186                 }
187
188                 // Default is not found
189                 boolean isFound = false;
190
191                 try {
192                         // Ask other method for contact instance
193                         final Contact contact = this.findContactByEmailAddress(emailAddress);
194
195                         // Log debug message
196                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
197
198                         // It is found ...
199                         isFound = true;
200                 } catch (final ContactNotFoundException ex) {
201                         // @TODO Was not found, log exception for spam check?
202                 }
203
204                 // Log trace message
205                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
206
207                 // Return status
208                 return isFound;
209         }
210
211         @Override
212         public Contact lookupContact (final Contact contact) {
213                 // Log trace message
214                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
215
216                 // Parameter should be valid
217                 if (null == contact) {
218                         // Throw NPE
219                         throw new NullPointerException("contact is null"); //NOI18N
220                 } else if (contact.getContactId() > 0) {
221                         try {
222                                 // Id set, ask other method
223                                 return this.findContactById(contact.getContactId());
224                         } catch (final ContactNotFoundException ex) {
225                                 // Not found, should not happen
226                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
227                         }
228                 }
229
230                 // Default is not found
231                 Contact foundContact = null;
232
233                 // Get whole list
234                 final List<Contact> contacts = this.allContacts();
235
236                 // Is the list empty?
237                 if (contacts.isEmpty()) {
238                         // Then abort here
239                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
240                         return null;
241                 }
242
243                 // Get iterator
244                 final Iterator<Contact> iterator = contacts.iterator();
245
246                 // Loop through all
247                 while (iterator.hasNext()) {
248                         // Get contact
249                         Contact next = iterator.next();
250
251                         // Is same contact?
252                         if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
253                                 // Debug message
254                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
255
256                                 // Found it
257                                 foundContact = next;
258                                 break;
259                         }
260                 }
261
262                 // Trace message
263                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
264
265                 // Return found contact
266                 return foundContact;
267         }
268
269         @Override
270         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
271                 // Log trace message
272                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
273
274                 // The contact instance must be valid
275                 if (null == contact) {
276                         // Throw NPE again
277                         throw new NullPointerException("contact is null"); //NOI18N
278                 } else if (contact.getContactId() == null) {
279                         // Throw NPE again
280                         throw new NullPointerException("contact.contactId is null"); //NOI18N
281                 } else if (contact.getContactId() < 1) {
282                         // Not valid
283                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
284                 }
285
286                 // Set updated timestamp
287                 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
288
289                 // Merge mobile, land-line and fix
290                 Contact detachedContact = this.mergeContactData(contact);
291
292                 // Trace message
293                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
294
295                 // Return it
296                 return detachedContact;
297         }
298
299         @Override
300         public Contact updateContactData (final Contact contact) {
301                 // Log trace message
302                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
303
304                 // The contact instance must be valid
305                 if (null == contact) {
306                         // Throw NPE again
307                         throw new NullPointerException("contact is null"); //NOI18N
308                 } else if (contact.getContactId() == null) {
309                         // Throw NPE again
310                         throw new NullPointerException("contact.contactId is null"); //NOI18N
311                 } else if (contact.getContactId() < 1) {
312                         // Not valid
313                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
314                 }
315
316                 // Is cell phone/land-line/fax number unlinked?
317                 boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
318                 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
319                 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
320
321                 // Call other Method
322                 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
323
324                 // Trace message
325                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
326
327                 // Return it
328                 return detachedContact;
329         }
330
331 }