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.GregorianCalendar;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.ejb.Stateless;
25 import javax.persistence.NoResultException;
26 import javax.persistence.Query;
27 import org.mxchange.jcontacts.contact.utils.ContactUtils;
28 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
29 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
30 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;;
31 import org.mxchange.jcontacts.contact.utils.ContactUtils;
36 * @author Roland Haeder<roland@mxchange.org>
38 @Stateless (name = "contact", description = "A bean handling contact data")
39 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
44 private static final long serialVersionUID = 542_145_347_916L;
49 public PizzaContactSessionBean () {
53 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
55 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: contact={0} - CALLED!", contact)); //NOI18N
57 // The contact instance must be valid
58 if (null == contact) {
60 throw new NullPointerException("contact is null"); //NOI18N
61 } else if (contact.getContactId() instanceof Long) {
63 throw new NullPointerException("contact.contactId is not null"); //NOI18N //NOI18N
64 } else if (this.lookupContact(contact) instanceof Contact) {
66 throw new ContactAlreadyAddedException(contact);
69 // Set created timestamp
70 contact.setContactCreated(new GregorianCalendar());
72 // Set all created timestamps in cellphone, land-line and fax number(s)
73 this.setAllContactPhoneEntriesCreated(contact);
76 this.getEntityManager().persist(contact);
79 this.getEntityManager().flush();
81 // Return updated instance
86 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
88 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
90 // The parameter must be valid
91 if (null == contactId) {
93 throw new NullPointerException("contactId is null"); //NOI18N
94 } else if (contactId < 1) {
96 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
100 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
103 query.setParameter("contactId", contactId); //NOI18N
105 // Init contact instance
108 // Try to find a result
110 // Find a single result
111 contact = (Contact) query.getSingleResult();
114 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
115 } catch (final NoResultException ex) {
117 throw new ContactNotFoundException(contactId, ex);
121 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
123 // Return found instance
128 @SuppressWarnings ("unchecked")
129 public List<Contact> getAllContacts () {
131 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
133 // Create query instance
134 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
137 List<Contact> contacts = query.getResultList();
140 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
147 @SuppressWarnings ("unchecked")
148 public List<String> getEmailAddressList () {
150 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
152 // Create query instance
153 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
156 List<String> emailAddresses = query.getResultList();
159 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
162 return emailAddresses;
166 public Contact lookupContact (final Contact contact) {
168 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
170 // Parameter should be valid
171 if (null == contact) {
173 throw new NullPointerException("contact is null"); //NOI18N
174 } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
176 // Id set, ask other method
177 return this.findContactById(contact.getContactId());
178 } catch (final ContactNotFoundException ex) {
179 // Not found, should not happen
180 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
184 // Default is not found
185 Contact foundContact = null;
188 List<Contact> contacts = this.getAllContacts();
190 // Is the list empty?
191 if (contacts.isEmpty()) {
193 this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
198 Iterator<Contact> iterator = contacts.iterator();
201 while (iterator.hasNext()) {
203 Contact next = iterator.next();
206 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: next={0}", next)); //NOI18N
209 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
211 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: Found same contact, id={0}", next.getContactId())); //NOI18N
220 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: foundContact={0} - EXIT!", foundContact)); //NOI18N
227 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
229 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //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 // Set updated timestamp
244 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
246 // Merge cellphone, land-line and fix
247 Contact detachedContact = this.mergeContactData(contact);
250 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
253 return detachedContact;
257 public Contact updateContactData (final Contact contact) {
259 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
261 // The contact instance must be valid
262 if (null == contact) {
264 throw new NullPointerException("contact is null"); //NOI18N
265 } else if (contact.getContactId() == null) {
267 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
268 } else if (contact.getContactId() < 1) {
270 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
273 // Is cell phone/land-line/fax number unlinked?
274 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
275 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
276 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
279 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
282 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
285 return detachedContact;