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.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.addressbook.database.BaseAddressbookDatabaseBean;
28 import org.mxchange.jcontacts.contact.utils.ContactUtils;
29 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
34 * @author Roland Häder<roland@mxchange.org>
36 @Stateless (name = "contact", description = "A bean handling contact data")
37 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
42 private static final long serialVersionUID = 542_145_347_916L;
47 public AddressbookContactSessionBean () {
51 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
53 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
55 // The parameter must be valid
56 if (null == emailAddress) {
58 throw new NullPointerException("emailAddress is null"); //NOI18N
59 } else if (emailAddress.isEmpty()) {
61 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
65 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
68 query.setParameter("emailAddress", emailAddress); //NOI18N
70 // Init contact instance
73 // Try to find a result
75 // Find a single result
76 contact = (Contact) query.getSingleResult();
79 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
80 } catch (final NoResultException ex) {
82 throw new ContactNotFoundException(emailAddress, ex);
86 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
88 // Return found instance
93 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
95 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
97 // The parameter must be valid
98 if (null == contactId) {
100 throw new NullPointerException("contactId is null"); //NOI18N
101 } else if (contactId < 1) {
103 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
106 // Get query instance
107 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
110 query.setParameter("contactId", contactId); //NOI18N
112 // Init contact instance
115 // Try to find a result
117 // Find a single result
118 contact = (Contact) query.getSingleResult();
121 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
122 } catch (final NoResultException ex) {
124 throw new ContactNotFoundException(contactId, ex);
128 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
130 // Return found instance
135 @SuppressWarnings ("unchecked")
136 public List<Contact> getAllContacts () {
138 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
140 // Create query instance
141 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
144 List<Contact> contacts = query.getResultList();
147 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
154 @SuppressWarnings ("unchecked")
155 public List<String> getEmailAddressList () {
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
159 // Create query instance
160 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
163 List<String> emailAddresses = query.getResultList();
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
169 return emailAddresses;
173 public boolean isEmailAddressRegistered (final String emailAddress) {
175 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
177 // The email address should be valid
178 if (null == emailAddress) {
180 throw new NullPointerException("emailAddress is null"); //NOI18N
181 } else if (emailAddress.isEmpty()) {
183 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
186 // Default is not found
187 boolean isFound = false;
190 // Ask other method for contact instance
191 Contact contact = this.findContactByEmailAddress(emailAddress);
194 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
198 } catch (final ContactNotFoundException ex) {
199 // @TODO Was not found, log exception for spam check?
203 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
210 public Contact lookupContact (final Contact contact) {
212 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
214 // Parameter should be valid
215 if (null == contact) {
217 throw new NullPointerException("contact is null"); //NOI18N
218 } else if (contact.getContactId() > 0) {
220 // Id set, ask other method
221 return this.findContactById(contact.getContactId());
222 } catch (final ContactNotFoundException ex) {
223 // Not found, should not happen
224 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
228 // Default is not found
229 Contact foundContact = null;
232 List<Contact> contacts = this.getAllContacts();
234 // Is the list empty?
235 if (contacts.isEmpty()) {
237 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
242 Iterator<Contact> iterator = contacts.iterator();
245 while (iterator.hasNext()) {
247 Contact next = iterator.next();
250 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
252 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
261 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
263 // Return found contact
268 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
270 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
272 // The contact instance must be valid
273 if (null == contact) {
275 throw new NullPointerException("contact is null"); //NOI18N
276 } else if (contact.getContactId() == null) {
278 throw new NullPointerException("contact.contactId is null"); //NOI18N
279 } else if (contact.getContactId() < 1) {
281 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
284 // Set updated timestamp
285 contact.setContactUpdated(new GregorianCalendar());
287 // Detach all phone numbers
288 this.detachAllContactPhoneEntries(contact);
290 // Merge cellphone, land-line and fix
291 Contact detachedContact = this.mergeContactData(contact);
294 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
297 return detachedContact;
301 public Contact updateContactData (final Contact contact) {
303 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
305 // The contact instance must be valid
306 if (null == contact) {
308 throw new NullPointerException("contact is null"); //NOI18N
309 } else if (contact.getContactId() == null) {
311 throw new NullPointerException("contact.contactId is null"); //NOI18N
312 } else if (contact.getContactId() < 1) {
314 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
317 // Is cell phone/land-line/fax number unlinked?
318 boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
319 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
320 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
323 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
326 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
329 return detachedContact;