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.jcontacts.exceptions.ContactNotFoundException;
27 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;;
28 import org.mxchange.jcontacts.contact.utils.ContactUtils;
33 * @author Roland Haeder<roland@mxchange.org>
35 @Stateless (name = "contact", mappedName = "ejb/stateless-pizza-contact", description = "A bean handling contact data")
36 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
41 private static final long serialVersionUID = 542_145_347_916L;
46 public PizzaContactSessionBean () {
50 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
52 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
54 // The parameter must be valid
55 if (null == contactId) {
57 throw new NullPointerException("contactId is null"); //NOI18N
58 } else if (contactId < 1) {
60 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
64 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
67 query.setParameter("contactId", contactId); //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("findContactById: Found contact={0}", contact)); //NOI18N
79 } catch (final NoResultException ex) {
81 throw new ContactNotFoundException(contactId, ex);
85 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
87 // Return found instance
92 @SuppressWarnings ("unchecked")
93 public List<Contact> getAllContacts () {
95 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
97 // Create query instance
98 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
101 List<Contact> contacts = query.getResultList();
104 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
111 @SuppressWarnings ("unchecked")
112 public List<String> getEmailAddressList () {
114 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
116 // Create query instance
117 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
120 List<String> emailAddresses = query.getResultList();
123 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
126 return emailAddresses;
130 public boolean isContactFound (final Contact contact) {
132 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
134 // Parameter should be valid
135 if (null == contact) {
137 throw new NullPointerException("contact is null"); //NOI18N
138 } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
140 // Id set, ask other method
141 return (this.findContactById(contact.getContactId()) instanceof Contact);
142 } catch (final ContactNotFoundException ex) {
143 // Not found, should not happen
144 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
148 // Default is not found
149 boolean isFound = false;
152 List<Contact> contacts = this.getAllContacts();
154 // Is the list empty?
155 if (contacts.isEmpty()) {
157 this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
162 Iterator<Contact> iterator = contacts.iterator();
165 while (iterator.hasNext()) {
167 Contact next = iterator.next();
170 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: next={0}", next)); //NOI18N
173 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
175 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: Found same contact, id={0}", next.getContactId())); //NOI18N
178 contact.setContactId(next.getContactId());
180 // Update also other ids
181 this.setAllContactPhoneEntries(contact, next);
190 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: isFound={0} - EXIT!", isFound)); //NOI18N
197 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
199 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
201 // The contact instance must be valid
202 if (null == contact) {
204 throw new NullPointerException("contact is null"); //NOI18N
205 } else if (contact.getContactId() == null) {
207 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
208 } else if (contact.getContactId() < 1) {
210 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
213 // Set updated timestamp
214 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
216 // Merge cellphone, land-line and fix
217 Contact detachedContact = this.mergeContactData(contact);
220 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
223 return detachedContact;
227 public Contact updateContactData (final Contact contact) {
229 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
231 // The contact instance must be valid
232 if (null == contact) {
234 throw new NullPointerException("contact is null"); //NOI18N
235 } else if (contact.getContactId() == null) {
237 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
238 } else if (contact.getContactId() < 1) {
240 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
243 // Is cell phone/land-line/fax number unlinked?
244 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
245 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
246 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
249 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
252 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
255 return detachedContact;