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.jcontacts.contact.utils.ContactUtils;
27 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
28 import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean;
33 * @author Roland Häder<roland@mxchange.org>
35 @Stateless (name = "contact", description = "A bean handling contact data")
36 public class FinancialsContactSessionBean extends BaseFinancialsDatabaseBean implements ContactSessionBeanRemote {
41 private static final long serialVersionUID = 542_145_347_916L;
46 public FinancialsContactSessionBean () {
50 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
52 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
54 // The parameter must be valid
55 if (null == emailAddress) {
57 throw new NullPointerException("emailAddress is null"); //NOI18N
58 } else if (emailAddress.isEmpty()) {
60 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
64 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
67 query.setParameter("emailAddress", emailAddress); //NOI18N
69 // Init contact instance
72 // Try to find a result
74 // Find a single result
75 contact = (Contact) query.getSingleResult();
78 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
79 } catch (final NoResultException ex) {
81 throw new ContactNotFoundException(emailAddress, ex);
85 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
87 // Return found instance
92 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
94 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
96 // The parameter must be valid
97 if (null == contactId) {
99 throw new NullPointerException("contactId is null"); //NOI18N
100 } else if (contactId < 1) {
102 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
105 // Get query instance
106 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
109 query.setParameter("contactId", contactId); //NOI18N
111 // Init contact instance
114 // Try to find a result
116 // Find a single result
117 contact = (Contact) query.getSingleResult();
120 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
121 } catch (final NoResultException ex) {
123 throw new ContactNotFoundException(contactId, ex);
127 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
129 // Return found instance
134 @SuppressWarnings ("unchecked")
135 public List<Contact> getAllContacts () {
137 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
139 // Create query instance
140 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
143 List<Contact> contacts = query.getResultList();
146 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
153 @SuppressWarnings ("unchecked")
154 public List<String> getEmailAddressList () {
156 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
158 // Create query instance
159 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
162 List<String> emailAddresses = query.getResultList();
165 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
168 return emailAddresses;
172 public boolean isEmailAddressRegistered (final String emailAddress) {
174 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
176 // The email address should be valid
177 if (null == emailAddress) {
179 throw new NullPointerException("emailAddress is null"); //NOI18N
180 } else if (emailAddress.isEmpty()) {
182 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
185 // Default is not found
186 boolean isFound = false;
189 // Ask other method for contact instance
190 Contact contact = this.findContactByEmailAddress(emailAddress);
193 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
197 } catch (final ContactNotFoundException ex) {
198 // @TODO Was not found, log exception for spam check?
202 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
209 public Contact lookupContact (final Contact contact) {
211 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
213 // Parameter should be valid
214 if (null == contact) {
216 throw new NullPointerException("contact is null"); //NOI18N
217 } else if (contact.getContactId() > 0) {
219 // Id set, ask other method
220 return this.findContactById(contact.getContactId());
221 } catch (final ContactNotFoundException ex) {
222 // Not found, should not happen
223 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
227 // Default is not found
228 Contact foundContact = null;
231 List<Contact> contacts = this.getAllContacts();
233 // Is the list empty?
234 if (contacts.isEmpty()) {
236 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
241 Iterator<Contact> iterator = contacts.iterator();
244 while (iterator.hasNext()) {
246 Contact next = iterator.next();
249 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
251 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
260 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
262 // Return found contact
267 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
269 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
271 // The contact instance must be valid
272 if (null == contact) {
274 throw new NullPointerException("contact is null"); //NOI18N
275 } else if (contact.getContactId() == null) {
277 throw new NullPointerException("contact.contactId is null"); //NOI18N
278 } else if (contact.getContactId() < 1) {
280 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
283 // Set updated timestamp
284 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
286 // Merge mobile, land-line and fix
287 Contact detachedContact = this.mergeContactData(contact);
290 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
293 return detachedContact;
297 public Contact updateContactData (final Contact contact) {
299 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
301 // The contact instance must be valid
302 if (null == contact) {
304 throw new NullPointerException("contact is null"); //NOI18N
305 } else if (contact.getContactId() == null) {
307 throw new NullPointerException("contact.contactId is null"); //NOI18N
308 } else if (contact.getContactId() < 1) {
310 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
313 // Is cell phone/land-line/fax number unlinked?
314 boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
315 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
316 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
319 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
322 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
325 return detachedContact;