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