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.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.ContactAlreadyAddedException;
29 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
34 * @author Roland Haeder<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 addContact (final Contact contact) throws ContactAlreadyAddedException {
52 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
56 public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
58 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactByEmailAddress: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
60 // The parameter must be valid
61 if (null == emailAddress) {
63 throw new NullPointerException("emailAddress is null"); //NOI18N
64 } else if (emailAddress.isEmpty()) {
66 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
70 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
73 query.setParameter("emailAddress", emailAddress); //NOI18N
75 // Init contact instance
78 // Try to find a result
80 // Find a single result
81 contact = (Contact) query.getSingleResult();
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactByEmailAddress: Found contact={0}", contact)); //NOI18N
85 } catch (final NoResultException ex) {
87 throw new ContactNotFoundException(emailAddress, ex);
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactByEmailAddress: contact={0} - EXIT!", contact)); //NOI18N
93 // Return found instance
98 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
100 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
102 // The parameter must be valid
103 if (null == contactId) {
105 throw new NullPointerException("contactId is null"); //NOI18N
106 } else if (contactId < 1) {
108 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
111 // Get query instance
112 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
115 query.setParameter("contactId", contactId); //NOI18N
117 // Init contact instance
120 // Try to find a result
122 // Find a single result
123 contact = (Contact) query.getSingleResult();
126 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
127 } catch (final NoResultException ex) {
129 throw new ContactNotFoundException(contactId, ex);
133 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
135 // Return found instance
140 @SuppressWarnings ("unchecked")
141 public List<Contact> getAllContacts () {
143 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
145 // Create query instance
146 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
149 List<Contact> contacts = query.getResultList();
152 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
159 @SuppressWarnings ("unchecked")
160 public List<String> getEmailAddressList () {
162 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
164 // Create query instance
165 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
168 List<String> emailAddresses = query.getResultList();
171 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
174 return emailAddresses;
178 public boolean isEmailAddressRegistered (final String emailAddress) {
180 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
182 // The email address should be valid
183 if (null == emailAddress) {
185 throw new NullPointerException("emailAddress is null");
186 } else if (emailAddress.isEmpty()) {
188 throw new IllegalArgumentException("emailAddress is empty");
191 // Default is not found
192 boolean isFound = false;
195 // Ask other method for contact instance
196 Contact contact = this.findContactByEmailAddress(emailAddress);
199 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: Found contact={0} for emailAddress={1}", contact, emailAddress));
203 } catch (final ContactNotFoundException ex) {
204 // @TODO Was not found, log exception for spam check?
208 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: isFound={0} - EXIT!", isFound));
215 public Contact lookupContact (final Contact contact) {
217 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
219 // Parameter should be valid
220 if (null == contact) {
222 throw new NullPointerException("contact is null"); //NOI18N
223 } else if (contact.getContactId() > 0) {
225 // Id set, ask other method
226 return this.findContactById(contact.getContactId());
227 } catch (final ContactNotFoundException ex) {
228 // Not found, should not happen
229 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
233 // Default is not found
234 Contact foundContact = null;
237 List<Contact> contacts = this.getAllContacts();
239 // Is the list empty?
240 if (contacts.isEmpty()) {
242 this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
247 Iterator<Contact> iterator = contacts.iterator();
250 while (iterator.hasNext()) {
252 Contact next = iterator.next();
255 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
257 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: Found same contact, id={0}", next.getContactId())); //NOI18N
266 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: foundContact={0} - EXIT!", foundContact)); //NOI18N
268 // Return found contact
273 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
275 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
277 // The contact instance must be valid
278 if (null == contact) {
280 throw new NullPointerException("contact is null"); //NOI18N
281 } else if (contact.getContactId() == null) {
283 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
284 } else if (contact.getContactId() < 1) {
286 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
289 // Set updated timestamp
290 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
292 // Merge cellphone, land-line and fix
293 Contact detachedContact = this.mergeContactData(contact);
296 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
299 return detachedContact;
303 public Contact updateContactData (final Contact contact) {
305 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
307 // The contact instance must be valid
308 if (null == contact) {
310 throw new NullPointerException("contact is null"); //NOI18N
311 } else if (contact.getContactId() == null) {
313 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
314 } else if (contact.getContactId() < 1) {
316 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
319 // Is cell phone/land-line/fax number unlinked?
320 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
321 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
322 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
325 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
328 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
331 return detachedContact;