2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jcontacts.contact;
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;
32 * @author Roland Haeder<roland@mxchange.org>
34 @Stateless (name = "contact", description = "A bean handling contact data")
35 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
40 private static final long serialVersionUID = 542_145_347_916L;
45 public PizzaContactSessionBean () {
49 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
51 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
53 // The parameter must be valid
54 if (null == emailAddress) {
56 throw new NullPointerException("emailAddress is null"); //NOI18N
57 } else if (emailAddress.isEmpty()) {
59 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
63 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
66 query.setParameter("emailAddress", emailAddress); //NOI18N
68 // Init contact instance
71 // Try to find a result
73 // Find a single result
74 contact = (Contact) query.getSingleResult();
77 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
78 } catch (final NoResultException ex) {
80 throw new ContactNotFoundException(emailAddress, ex);
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
86 // Return found instance
91 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
93 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
95 // The parameter must be valid
96 if (null == contactId) {
98 throw new NullPointerException("contactId is null"); //NOI18N
99 } else if (contactId < 1) {
101 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
104 // Get query instance
105 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
108 query.setParameter("contactId", contactId); //NOI18N
110 // Init contact instance
113 // Try to find a result
115 // Find a single result
116 contact = (Contact) query.getSingleResult();
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
120 } catch (final NoResultException ex) {
122 throw new ContactNotFoundException(contactId, ex);
126 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
128 // Return found instance
133 @SuppressWarnings ("unchecked")
134 public List<Contact> getAllContacts () {
136 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
138 // Create query instance
139 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
142 List<Contact> contacts = query.getResultList();
145 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
152 @SuppressWarnings ("unchecked")
153 public List<String> getEmailAddressList () {
155 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
157 // Create query instance
158 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
161 List<String> emailAddresses = query.getResultList();
164 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
167 return emailAddresses;
171 public boolean isEmailAddressRegistered (final String emailAddress) {
173 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
175 // The email address should be valid
176 if (null == emailAddress) {
178 throw new NullPointerException("emailAddress is null"); //NOI18N
179 } else if (emailAddress.isEmpty()) {
181 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
184 // Default is not found
185 boolean isFound = false;
188 // Ask other method for contact instance
189 Contact contact = this.findContactByEmailAddress(emailAddress);
192 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
196 } catch (final ContactNotFoundException ex) {
197 // @TODO Was not found, log exception for spam check?
201 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
208 public Contact lookupContact (final Contact contact) {
210 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
212 // Parameter should be valid
213 if (null == contact) {
215 throw new NullPointerException("contact is null"); //NOI18N
216 } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
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
226 // Default is not found
227 Contact foundContact = null;
230 List<Contact> contacts = this.getAllContacts();
232 // Is the list empty?
233 if (contacts.isEmpty()) {
235 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
240 Iterator<Contact> iterator = contacts.iterator();
243 while (iterator.hasNext()) {
245 Contact next = iterator.next();
248 this.getLoggerBeanLocal().logDebug(MessageFormat.format("lookupContact: next={0}", next)); //NOI18N
251 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
253 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
262 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
269 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
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
273 // The contact instance must be valid
274 if (null == contact) {
276 throw new NullPointerException("contact is null"); //NOI18N
277 } else if (contact.getContactId() == null) {
279 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
280 } else if (contact.getContactId() < 1) {
282 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
285 // Set updated timestamp
286 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
288 // Merge cellphone, land-line and fix
289 Contact detachedContact = this.mergeContactData(contact);
292 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
295 return detachedContact;
299 public Contact updateContactData (final Contact contact) {
301 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
303 // The contact instance must be valid
304 if (null == contact) {
306 throw new NullPointerException("contact is null"); //NOI18N
307 } else if (contact.getContactId() == null) {
309 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
310 } else if (contact.getContactId() < 1) {
312 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
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);
321 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
324 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
327 return detachedContact;