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 javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
26 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;;
27 import org.mxchange.jcontacts.contact.utils.ContactUtils;
32 * @author Roland Haeder<roland@mxchange.org>
34 @Stateless (name = "contact", mappedName = "ejb/stateless-pizza-contact", description = "A bean handling contact data")
35 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
40 private static final long serialVersionUID = 542_145_347_916L;
45 public PizzaContactSessionBean () {
49 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
51 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
53 // The parameter must be valid
54 if (null == contactId) {
56 throw new NullPointerException("contactId is null"); //NOI18N
57 } else if (contactId < 1) {
59 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
63 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
66 query.setParameter("contactId", contactId); //NOI18N
68 // Init contact instance
71 // Try to find a result
73 // Find a single result
74 contact = (Contact) query.getSingleResult();
77 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
78 } catch (final NoResultException ex) {
80 throw new ContactNotFoundException(contactId, ex);
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
86 // Return found instance
91 @SuppressWarnings ("unchecked")
92 public List<Contact> getAllContacts () {
94 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
96 // Create query instance
97 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
100 List<Contact> contacts = query.getResultList();
103 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
110 @SuppressWarnings ("unchecked")
111 public List<String> getEmailAddressList () {
113 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
115 // Create query instance
116 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
119 List<String> emailAddresses = query.getResultList();
122 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
125 return emailAddresses;
129 public boolean isContactFound (final Contact contact) {
131 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
133 // Parameter should be valid
134 if (null == contact) {
136 throw new NullPointerException("contact is null"); //NOI18N
137 } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
139 // Id set, ask other method
140 return (this.findContactById(contact.getContactId()) instanceof Contact);
141 } catch (final ContactNotFoundException ex) {
142 // Not found, should not happen
143 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
147 // Default is not found
148 boolean isFound = false;
151 List<Contact> contacts = this.getAllContacts();
153 // Is the list empty?
154 if (contacts.isEmpty()) {
156 this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
161 Iterator<Contact> iterator = contacts.iterator();
164 while (iterator.hasNext()) {
166 Contact next = iterator.next();
169 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: next={0}", next)); //NOI18N
172 if (ContactUtils.isSameContact(contact, next)) {
174 this.getLoggerBeanLocal().logDebug("isContactFound: Found same contact."); //NOI18N
187 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
189 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
191 // The contact instance must be valid
192 if (null == contact) {
194 throw new NullPointerException("contact is null"); //NOI18N
195 } else if (contact.getContactId() == null) {
197 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
198 } else if (contact.getContactId() < 1) {
200 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
203 // Set updated timestamp
204 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
206 // Merge cellphone, land-line and fix
207 Contact detachedContact = this.mergeContactData(contact);
210 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
213 return detachedContact;
217 public Contact updateContactData (final Contact contact) {
219 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
221 // The contact instance must be valid
222 if (null == contact) {
224 throw new NullPointerException("contact is null"); //NOI18N
225 } else if (contact.getContactId() == null) {
227 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
228 } else if (contact.getContactId() < 1) {
230 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
233 // Is cell phone/land-line/fax number unlinked?
234 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
235 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
236 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
239 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
242 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
245 return detachedContact;