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 findContactById (final Long contactId) throws ContactNotFoundException {
58 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
60 // The parameter must be valid
61 if (null == contactId) {
63 throw new NullPointerException("contactId is null"); //NOI18N
64 } else if (contactId < 1) {
66 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
70 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
73 query.setParameter("contactId", contactId); //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("findContactById: Found contact={0}", contact)); //NOI18N
85 } catch (final NoResultException ex) {
87 throw new ContactNotFoundException(contactId, ex);
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
93 // Return found instance
98 @SuppressWarnings ("unchecked")
99 public List<Contact> getAllContacts () {
101 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
103 // Create query instance
104 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
107 List<Contact> contacts = query.getResultList();
110 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
117 @SuppressWarnings ("unchecked")
118 public List<String> getEmailAddressList () {
120 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
122 // Create query instance
123 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
126 List<String> emailAddresses = query.getResultList();
129 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
132 return emailAddresses;
136 public Contact lookupContact (final Contact contact) {
138 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
140 // Parameter should be valid
141 if (null == contact) {
143 throw new NullPointerException("contact is null"); //NOI18N
144 } else if (contact.getContactId() > 0) {
146 // Id set, ask other method
147 return this.findContactById(contact.getContactId());
148 } catch (final ContactNotFoundException ex) {
149 // Not found, should not happen
150 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
154 // Default is not found
155 Contact foundContact = null;
158 List<Contact> contacts = this.getAllContacts();
160 // Is the list empty?
161 if (contacts.isEmpty()) {
163 this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
168 Iterator<Contact> iterator = contacts.iterator();
171 while (iterator.hasNext()) {
173 Contact next = iterator.next();
176 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
178 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: Found same contact, id={0}", next.getContactId())); //NOI18N
187 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: foundContact={0} - EXIT!", foundContact)); //NOI18N
189 // Return found contact
194 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
196 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
198 // The contact instance must be valid
199 if (null == contact) {
201 throw new NullPointerException("contact is null"); //NOI18N
202 } else if (contact.getContactId() == null) {
204 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
205 } else if (contact.getContactId() < 1) {
207 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
210 // Set updated timestamp
211 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
213 // Merge cellphone, land-line and fix
214 Contact detachedContact = this.mergeContactData(contact);
217 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
220 return detachedContact;
224 public Contact updateContactData (final Contact contact) {
226 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
228 // The contact instance must be valid
229 if (null == contact) {
231 throw new NullPointerException("contact is null"); //NOI18N
232 } else if (contact.getContactId() == null) {
234 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
235 } else if (contact.getContactId() < 1) {
237 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
240 // Is cell phone/land-line/fax number unlinked?
241 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
242 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
243 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
246 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
249 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
252 return detachedContact;