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.addressbook.database.BaseAddressbookDatabaseBean;
28 import org.mxchange.jcontacts.contact.utils.ContactUtils;
29 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
30 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
35 * @author Roland Haeder<roland@mxchange.org>
37 @Stateless (name = "contact", description = "A bean handling contact data")
38 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
43 private static final long serialVersionUID = 542_145_347_916L;
48 public AddressbookContactSessionBean () {
52 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
54 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
56 // Is the instance set?
57 if (null == contact) {
59 throw new NullPointerException("contact is null"); //NOI18N
60 } else if (contact.getContactId() != null) {
62 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N
65 // Set created timestamp
66 contact.setContactCreated(new GregorianCalendar());
68 // Set all created timestamps, if instance is there
69 this.setAllContactPhoneEntriesCreated(contact);
72 this.getEntityManager().persist(contact);
74 // Flush it to get contactId set
75 this.getEntityManager().flush();
78 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
85 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
87 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
89 // The parameter must be valid
90 if (null == emailAddress) {
92 throw new NullPointerException("emailAddress is null"); //NOI18N
93 } else if (emailAddress.isEmpty()) {
95 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
99 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
102 query.setParameter("emailAddress", emailAddress); //NOI18N
104 // Init contact instance
107 // Try to find a result
109 // Find a single result
110 contact = (Contact) query.getSingleResult();
113 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
114 } catch (final NoResultException ex) {
116 throw new ContactNotFoundException(emailAddress, ex);
120 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
122 // Return found instance
127 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
129 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
131 // The parameter must be valid
132 if (null == contactId) {
134 throw new NullPointerException("contactId is null"); //NOI18N
135 } else if (contactId < 1) {
137 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
140 // Get query instance
141 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
144 query.setParameter("contactId", contactId); //NOI18N
146 // Init contact instance
149 // Try to find a result
151 // Find a single result
152 contact = (Contact) query.getSingleResult();
155 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
156 } catch (final NoResultException ex) {
158 throw new ContactNotFoundException(contactId, ex);
162 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
164 // Return found instance
169 @SuppressWarnings ("unchecked")
170 public List<Contact> getAllContacts () {
172 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
174 // Create query instance
175 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
178 List<Contact> contacts = query.getResultList();
181 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
188 @SuppressWarnings ("unchecked")
189 public List<String> getEmailAddressList () {
191 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
193 // Create query instance
194 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
197 List<String> emailAddresses = query.getResultList();
200 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
203 return emailAddresses;
207 public boolean isEmailAddressRegistered (final String emailAddress) {
209 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
211 // The email address should be valid
212 if (null == emailAddress) {
214 throw new NullPointerException("emailAddress is null"); //NOI18N
215 } else if (emailAddress.isEmpty()) {
217 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
220 // Default is not found
221 boolean isFound = false;
224 // Ask other method for contact instance
225 Contact contact = this.findContactByEmailAddress(emailAddress);
228 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
232 } catch (final ContactNotFoundException ex) {
233 // @TODO Was not found, log exception for spam check?
237 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
244 public Contact lookupContact (final Contact contact) {
246 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
248 // Parameter should be valid
249 if (null == contact) {
251 throw new NullPointerException("contact is null"); //NOI18N
252 } else if (contact.getContactId() > 0) {
254 // Id set, ask other method
255 return this.findContactById(contact.getContactId());
256 } catch (final ContactNotFoundException ex) {
257 // Not found, should not happen
258 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
262 // Default is not found
263 Contact foundContact = null;
266 List<Contact> contacts = this.getAllContacts();
268 // Is the list empty?
269 if (contacts.isEmpty()) {
271 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
276 Iterator<Contact> iterator = contacts.iterator();
279 while (iterator.hasNext()) {
281 Contact next = iterator.next();
284 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
286 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
295 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
297 // Return found contact
302 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
304 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
306 // The contact instance must be valid
307 if (null == contact) {
309 throw new NullPointerException("contact is null"); //NOI18N
310 } else if (contact.getContactId() == null) {
312 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
313 } else if (contact.getContactId() < 1) {
315 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
318 // Set updated timestamp
319 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
321 // Merge cellphone, land-line and fix
322 Contact detachedContact = this.mergeContactData(contact);
325 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
328 return detachedContact;
332 public Contact updateContactData (final Contact contact) {
334 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
336 // The contact instance must be valid
337 if (null == contact) {
339 throw new NullPointerException("contact is null"); //NOI18N
340 } else if (contact.getContactId() == null) {
342 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
343 } else if (contact.getContactId() < 1) {
345 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
348 // Is cell phone/land-line/fax number unlinked?
349 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
350 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
351 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
354 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
357 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
360 return detachedContact;