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