2 * Copyright (C) 2016, 2017 Roland Häder
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.addressbook.database.BaseAddressbookDatabaseBean;
27 import org.mxchange.jcontacts.contact.utils.ContactUtils;
28 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
33 * @author Roland Häder<roland@mxchange.org>
35 @Stateless (name = "contact", description = "A bean handling contact data")
36 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
41 private static final long serialVersionUID = 542_145_347_916L;
46 public AddressbookContactSessionBean () {
47 // Call super constructor
52 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
54 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
56 // The parameter must be valid
57 if (null == emailAddress) {
59 throw new NullPointerException("emailAddress is null"); //NOI18N
60 } else if (emailAddress.isEmpty()) {
62 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
66 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
69 query.setParameter("emailAddress", emailAddress); //NOI18N
71 // Init contact instance
74 // Try to find a result
76 // Find a single result
77 contact = (Contact) query.getSingleResult();
80 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
81 } catch (final NoResultException ex) {
83 throw new ContactNotFoundException(emailAddress, ex);
87 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
89 // Return found instance
94 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
96 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
98 // The parameter must be valid
99 if (null == contactId) {
101 throw new NullPointerException("contactId is null"); //NOI18N
102 } else if (contactId < 1) {
104 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
107 // Get query instance
108 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
111 query.setParameter("contactId", contactId); //NOI18N
113 // Init contact instance
116 // Try to find a result
118 // Find a single result
119 contact = (Contact) query.getSingleResult();
122 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
123 } catch (final NoResultException ex) {
125 throw new ContactNotFoundException(contactId, ex);
129 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
131 // Return found instance
136 @SuppressWarnings ("unchecked")
137 public List<Contact> getAllContacts () {
139 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
141 // Create query instance
142 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
145 List<Contact> contacts = query.getResultList();
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
155 @SuppressWarnings ("unchecked")
156 public List<String> getEmailAddressList () {
158 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
160 // Create query instance
161 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
164 List<String> emailAddresses = query.getResultList();
167 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
170 return emailAddresses;
174 public boolean isEmailAddressRegistered (final String emailAddress) {
176 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
178 // The email address should be valid
179 if (null == emailAddress) {
181 throw new NullPointerException("emailAddress is null"); //NOI18N
182 } else if (emailAddress.isEmpty()) {
184 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
187 // Default is not found
188 boolean isFound = false;
191 // Ask other method for contact instance
192 Contact contact = this.findContactByEmailAddress(emailAddress);
195 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
199 } catch (final ContactNotFoundException ex) {
200 // @TODO Was not found, log exception for spam check?
204 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
211 public Contact lookupContact (final Contact contact) {
213 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
215 // Parameter should be valid
216 if (null == contact) {
218 throw new NullPointerException("contact is null"); //NOI18N
219 } else if (contact.getContactId() > 0) {
221 // Id set, ask other method
222 return this.findContactById(contact.getContactId());
223 } catch (final ContactNotFoundException ex) {
224 // Not found, should not happen
225 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
229 // Default is not found
230 Contact foundContact = null;
233 List<Contact> contacts = this.getAllContacts();
235 // Is the list empty?
236 if (contacts.isEmpty()) {
238 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
243 Iterator<Contact> iterator = contacts.iterator();
246 while (iterator.hasNext()) {
248 Contact next = iterator.next();
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
264 // Return found contact
269 public Contact updateContactData (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
271 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isMobileUnlinked, 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
280 } else if (contact.getContactId() < 1) {
282 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
285 // Merge cellphone, land-line and fix
286 Contact managedContact = this.mergeContactData(contact);
289 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
292 return managedContact;
296 public Contact updateContactData (final Contact contact) {
298 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
300 // The contact instance must be valid
301 if (null == contact) {
303 throw new NullPointerException("contact is null"); //NOI18N
304 } else if (contact.getContactId() == null) {
306 throw new NullPointerException("contact.contactId is null"); //NOI18N
307 } else if (contact.getContactId() < 1) {
309 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
312 // Is cell phone/land-line/fax number unlinked?
313 boolean isMobileUnlinked = (contact.getContactMobileNumber() == null);
314 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
315 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
318 Contact managedContact = this.updateContactData(contact, isMobileUnlinked, isLandLineUnlinked, isFaxUnlinked);
321 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
324 return managedContact;