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("addContact: contact={0} - CALLED!", contact)); //NOI18N
55 // The contact instance must be valid
56 if (null == contact) {
58 throw new NullPointerException("contact is null"); //NOI18N
59 } else if (contact.getContactId() instanceof Long) {
61 throw new NullPointerException("contact.contactId is not null"); //NOI18N //NOI18N
62 } else if (this.lookupContact(contact) instanceof Contact) {
64 throw new ContactAlreadyAddedException(contact);
67 // Set created timestamp
68 contact.setContactCreated(new GregorianCalendar());
70 // Set all created timestamps in cellphone, land-line and fax number(s)
71 this.setAllContactPhoneEntriesCreated(contact);
74 this.getEntityManager().persist(contact);
77 this.getEntityManager().flush();
79 // Return updated instance
84 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
86 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactByEmailAddress: emailAddress={0} - CALLED!", 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("findContactByEmailAddress: Found contact={0}", contact)); //NOI18N
113 } catch (final NoResultException ex) {
115 throw new ContactNotFoundException(emailAddress, ex);
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactByEmailAddress: contact={0} - EXIT!", contact)); //NOI18N
121 // Return found instance
126 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
128 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", 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("findContactById: Found contact={0}", contact)); //NOI18N
155 } catch (final NoResultException ex) {
157 throw new ContactNotFoundException(contactId, ex);
161 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
163 // Return found instance
168 @SuppressWarnings ("unchecked")
169 public List<Contact> getAllContacts () {
171 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
173 // Create query instance
174 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
177 List<Contact> contacts = query.getResultList();
180 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
187 @SuppressWarnings ("unchecked")
188 public List<String> getEmailAddressList () {
190 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
192 // Create query instance
193 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
196 List<String> emailAddresses = query.getResultList();
199 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
202 return emailAddresses;
206 public boolean isEmailAddressRegistered (final String emailAddress) {
208 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
210 // The email address should be valid
211 if (null == emailAddress) {
213 throw new NullPointerException("emailAddress is null");
214 } else if (emailAddress.isEmpty()) {
216 throw new IllegalArgumentException("emailAddress is empty");
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("isEmailAddressRegistered: Found contact={0} for emailAddress={1}", contact, emailAddress));
231 } catch (final ContactNotFoundException ex) {
232 // @TODO Was not found, log exception for spam check?
236 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: isFound={0} - EXIT!", isFound));
243 public Contact lookupContact (final Contact contact) {
245 this.getLoggerBeanLocal().logTrace(MessageFormat.format("lookupContact: contact={0} - CALLED!", 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("lookupContact: No contacts registered, returning 'false' ..."); //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("lookupContact: Found same contact, id={0}", next.getContactId())); //NOI18N
297 this.getLoggerBeanLocal().logTrace(MessageFormat.format("lookupContact: foundContact={0} - EXIT!", foundContact)); //NOI18N
304 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
306 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", 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("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
330 return detachedContact;
334 public Contact updateContactData (final Contact contact) {
336 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", 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("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
362 return detachedContact;