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.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;
34 * @author Roland Haeder<roland@mxchange.org>
36 @Stateless (name = "contact", description = "A bean handling contact data")
37 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
42 private static final long serialVersionUID = 542_145_347_916L;
47 public PizzaContactSessionBean () {
51 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
53 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
55 // Is the instance set?
56 if (null == contact) {
58 throw new NullPointerException("contact is null"); //NOI18N
59 } else if (contact.getContactId() != null) {
61 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
64 // Set created timestamp
65 contact.setContactCreated(new GregorianCalendar());
67 // Set all created timestamps, if instance is there
68 this.setAllContactPhoneEntriesCreated(contact);
71 this.getEntityManager().persist(contact);
73 // Flush it to get contactId set
74 this.getEntityManager().flush();
77 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
84 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
86 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
88 // The parameter must be valid
89 if (null == emailAddress) {
91 throw new NullPointerException("emailAddress is null"); //NOI18N
92 } else if (emailAddress.isEmpty()) {
94 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
98 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
101 query.setParameter("emailAddress", emailAddress); //NOI18N
103 // Init contact instance
106 // Try to find a result
108 // Find a single result
109 contact = (Contact) query.getSingleResult();
112 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
113 } catch (final NoResultException ex) {
115 throw new ContactNotFoundException(emailAddress, ex);
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
121 // Return found instance
126 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
128 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
130 // The parameter must be valid
131 if (null == contactId) {
133 throw new NullPointerException("contactId is null"); //NOI18N
134 } else if (contactId < 1) {
136 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
139 // Get query instance
140 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
143 query.setParameter("contactId", contactId); //NOI18N
145 // Init contact instance
148 // Try to find a result
150 // Find a single result
151 contact = (Contact) query.getSingleResult();
154 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
155 } catch (final NoResultException ex) {
157 throw new ContactNotFoundException(contactId, ex);
161 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
163 // Return found instance
168 @SuppressWarnings ("unchecked")
169 public List<Contact> getAllContacts () {
171 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
173 // Create query instance
174 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
177 List<Contact> contacts = query.getResultList();
180 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
187 @SuppressWarnings ("unchecked")
188 public List<String> getEmailAddressList () {
190 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
192 // Create query instance
193 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
196 List<String> emailAddresses = query.getResultList();
199 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
202 return emailAddresses;
206 public boolean isEmailAddressRegistered (final String emailAddress) {
208 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
210 // The email address should be valid
211 if (null == emailAddress) {
213 throw new NullPointerException("emailAddress is null"); //NOI18N
214 } else if (emailAddress.isEmpty()) {
216 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
219 // Default is not found
220 boolean isFound = false;
223 // Ask other method for contact instance
224 Contact contact = this.findContactByEmailAddress(emailAddress);
227 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
231 } catch (final ContactNotFoundException ex) {
232 // @TODO Was not found, log exception for spam check?
236 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
243 public Contact lookupContact (final Contact contact) {
245 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
247 // Parameter should be valid
248 if (null == contact) {
250 throw new NullPointerException("contact is null"); //NOI18N
251 } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
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
261 // Default is not found
262 Contact foundContact = null;
265 List<Contact> contacts = this.getAllContacts();
267 // Is the list empty?
268 if (contacts.isEmpty()) {
270 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
275 Iterator<Contact> iterator = contacts.iterator();
278 while (iterator.hasNext()) {
280 Contact next = iterator.next();
283 this.getLoggerBeanLocal().logDebug(MessageFormat.format("lookupContact: next={0}", next)); //NOI18N
286 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
288 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
297 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
304 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
306 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
308 // The contact instance must be valid
309 if (null == contact) {
311 throw new NullPointerException("contact is null"); //NOI18N
312 } else if (contact.getContactId() == null) {
314 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
315 } else if (contact.getContactId() < 1) {
317 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
320 // Set updated timestamp
321 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
323 // Merge cellphone, land-line and fix
324 Contact detachedContact = this.mergeContactData(contact);
327 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
330 return detachedContact;
334 public Contact updateContactData (final Contact contact) {
336 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
338 // The contact instance must be valid
339 if (null == contact) {
341 throw new NullPointerException("contact is null"); //NOI18N
342 } else if (contact.getContactId() == null) {
344 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
345 } else if (contact.getContactId() < 1) {
347 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
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);
356 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
359 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
362 return detachedContact;