2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.model.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.exceptions.ContactNotFoundException;
32 * @author Roland Häder<roland@mxchange.org>
34 @Stateless (name = "contact", description = "A bean handling contact data")
35 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
40 private static final long serialVersionUID = 542_145_347_916L;
45 public AddressbookContactSessionBean () {
46 // Call super constructor
51 @SuppressWarnings ("unchecked")
52 public List<Contact> allContacts () {
54 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
56 // Create query instance
57 final Query query = this.getEntityManager().createNamedQuery("AllContacts"); //NOI18N
60 final List<Contact> contacts = query.getResultList();
63 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
70 @SuppressWarnings ("unchecked")
71 public List<String> allEmailAddresses () {
73 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
75 // Create query instance
76 final Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses"); //NOI18N
79 final List<String> emailAddresses = query.getResultList();
82 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
85 return emailAddresses;
89 public boolean isEmailAddressRegistered (final String emailAddress) {
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
93 // The email address should be valid
94 if (null == emailAddress) {
96 throw new NullPointerException("emailAddress is null"); //NOI18N
97 } else if (emailAddress.isEmpty()) {
99 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
102 // Default is not found
103 boolean isFound = false;
106 // Ask other method for contact instance
107 final Contact contact = this.findContactByEmailAddress(emailAddress);
110 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
114 } catch (final ContactNotFoundException ex) {
115 // @TODO Was not found, log exception for spam check?
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
126 public Contact lookupContact (final Contact contact) {
128 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
130 // Parameter should be valid
131 if (null == contact) {
133 throw new NullPointerException("contact is null"); //NOI18N
134 } else if (contact.getContactId() > 0) {
136 // Id set, ask other method
137 return this.findContactById(contact.getContactId());
138 } catch (final ContactNotFoundException ex) {
139 // Not found, should not happen
140 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
144 // Default is not found
145 Contact foundContact = null;
148 final List<Contact> contacts = this.allContacts();
150 // Is the list empty?
151 if (contacts.isEmpty()) {
153 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
158 final Iterator<Contact> iterator = contacts.iterator();
161 while (iterator.hasNext()) {
163 Contact next = iterator.next();
166 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
168 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
177 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
179 // Return found contact
184 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
186 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
188 // The contact instance must be valid
189 if (null == contact) {
191 throw new NullPointerException("contact is null"); //NOI18N
192 } else if (contact.getContactId() == null) {
194 throw new NullPointerException("contact.contactId is null"); //NOI18N
195 } else if (contact.getContactId() < 1) {
197 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
200 // Set updated timestamp
201 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
203 // Merge mobile, land-line and fix
204 Contact detachedContact = this.mergeContactData(contact);
207 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
210 return detachedContact;
214 public Contact updateContactData (final Contact contact) {
216 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
218 // The contact instance must be valid
219 if (null == contact) {
221 throw new NullPointerException("contact is null"); //NOI18N
222 } else if (contact.getContactId() == null) {
224 throw new NullPointerException("contact.contactId is null"); //NOI18N
225 } else if (contact.getContactId() < 1) {
227 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
230 // Is cell phone/land-line/fax number unlinked?
231 boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
232 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
233 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
236 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
239 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
242 return detachedContact;
246 * Returns a contact instance which has the given email address.
248 * @param emailAddress Email address
250 * @return Contact instance
252 * @throws ContactNotFoundException If the contact was not found
254 private Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
256 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
258 // The parameter must be valid
259 if (null == emailAddress) {
261 throw new NullPointerException("emailAddress is null"); //NOI18N
262 } else if (emailAddress.isEmpty()) {
264 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
267 // Get query instance
268 final Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
270 // Set parameter and limit
271 query.setParameter("emailAddress", emailAddress); //NOI18N
272 query.setMaxResults(1);
274 // Init contact instance
275 final Contact contact;
277 // Try to find a result
279 // Find a single result
280 contact = (Contact) query.getSingleResult();
283 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
284 } catch (final NoResultException ex) {
286 throw new ContactNotFoundException(emailAddress, ex);
290 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
292 // Return found instance
297 * Returns a contact instance which has the given id number.
299 * @param contactId Contact id
301 * @return Contact instance
303 * @throws ContactNotFoundException If the contact was not found
305 private Contact findContactById (final Long contactId) throws ContactNotFoundException {
307 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
309 // The parameter must be valid
310 if (null == contactId) {
312 throw new NullPointerException("contactId is null"); //NOI18N
313 } else if (contactId < 1) {
315 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
318 // Get query instance
319 final Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
322 query.setParameter("contactId", contactId); //NOI18N
323 query.setMaxResults(1);
325 // Init contact instance
326 final Contact contact;
328 // Try to find a result
330 // Find a single result
331 contact = (Contact) query.getSingleResult();
334 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
335 } catch (final NoResultException ex) {
337 throw new ContactNotFoundException(contactId, ex);
341 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
343 // Return found instance