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.addressbook.database.BaseAddressbookDatabaseBean;
26 import org.mxchange.jcontacts.contact.utils.ContactUtils;
27 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
28 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
33 * @author Roland Haeder<roland@mxchange.org>
35 @Stateless (name = "contact", description = "A bean handling contact data")
36 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
41 private static final long serialVersionUID = 542_145_347_916L;
46 public AddressbookContactSessionBean () {
50 public Contact addContact (Contact contact) throws ContactAlreadyAddedException {
51 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
55 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
57 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
59 // The parameter must be valid
60 if (null == contactId) {
62 throw new NullPointerException("contactId is null"); //NOI18N
63 } else if (contactId < 1) {
65 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
69 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
72 query.setParameter("contactId", contactId); //NOI18N
74 // Init contact instance
77 // Try to find a result
79 // Find a single result
80 contact = (Contact) query.getSingleResult();
83 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
84 } catch (final NoResultException ex) {
86 throw new ContactNotFoundException(contactId, ex);
90 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
92 // Return found instance
97 @SuppressWarnings ("unchecked")
98 public List<Contact> getAllContacts () {
100 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
102 // Create query instance
103 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
106 List<Contact> contacts = query.getResultList();
109 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
116 @SuppressWarnings ("unchecked")
117 public List<String> getEmailAddressList () {
119 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
121 // Create query instance
122 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
125 List<String> emailAddresses = query.getResultList();
128 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
131 return emailAddresses;
135 public boolean isContactFound (final Contact contact) {
137 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
139 // Parameter should be valid
140 if (null == contact) {
142 throw new NullPointerException("contact is null"); //NOI18N
143 } else if (contact.getContactId() > 0) {
145 // Id set, ask other method
146 return (this.findContactById(contact.getContactId()) instanceof Contact);
147 } catch (final ContactNotFoundException ex) {
148 // Not found, should not happen
149 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
153 // Default is not found
154 boolean isFound = false;
157 List<Contact> contacts = this.getAllContacts();
159 // Is the list empty?
160 if (contacts.isEmpty()) {
162 this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
167 Iterator<Contact> iterator = contacts.iterator();
170 while (iterator.hasNext()) {
172 Contact next = iterator.next();
175 if (ContactUtils.isSameContact(contact, next)) {
187 public Contact lookupContact (Contact contact) {
188 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
192 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
194 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
196 // The contact instance must be valid
197 if (null == contact) {
199 throw new NullPointerException("contact is null"); //NOI18N
200 } else if (contact.getContactId() == null) {
202 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
203 } else if (contact.getContactId() < 1) {
205 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
208 // Set updated timestamp
209 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
211 // Merge cellphone, land-line and fix
212 Contact detachedContact = this.mergeContactData(contact);
215 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
218 return detachedContact;
222 public Contact updateContactData (final Contact contact) {
224 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
226 // The contact instance must be valid
227 if (null == contact) {
229 throw new NullPointerException("contact is null"); //NOI18N
230 } else if (contact.getContactId() == null) {
232 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
233 } else if (contact.getContactId() < 1) {
235 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
238 // Is cell phone/land-line/fax number unlinked?
239 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
240 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
241 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
244 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
247 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
250 return detachedContact;