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;
34 * @author Roland Haeder<roland@mxchange.org>
36 @Stateless (name = "contact", description = "A bean handling contact data")
37 public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
42 private static final long serialVersionUID = 542_145_347_916L;
47 public PizzaContactSessionBean () {
51 public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
53 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: contact={0} - CALLED!", contact)); //NOI18N
55 // The contact instance must be valid
56 if (null == contact) {
58 throw new NullPointerException("contact is null"); //NOI18N
59 } else if (contact.getContactId() instanceof Long) {
61 throw new NullPointerException("contact.contactId is not null"); //NOI18N //NOI18N
62 } else if (this.lookupContact(contact) instanceof Contact) {
64 throw new ContactAlreadyAddedException(contact);
67 // Set created timestamp
68 contact.setContactCreated(new GregorianCalendar());
70 // Set all created timestamps in cellphone, land-line and fax number(s)
71 this.setAllContactPhoneEntriesCreated(contact);
74 this.getEntityManager().persist(contact);
77 this.getEntityManager().flush();
79 // Return updated instance
84 public Contact findContactById (final Long contactId) throws ContactNotFoundException {
86 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
88 // The parameter must be valid
89 if (null == contactId) {
91 throw new NullPointerException("contactId is null"); //NOI18N
92 } else if (contactId < 1) {
94 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
98 Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
101 query.setParameter("contactId", contactId); //NOI18N
103 // Init contact instance
106 // Try to find a result
108 // Find a single result
109 contact = (Contact) query.getSingleResult();
112 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
113 } catch (final NoResultException ex) {
115 throw new ContactNotFoundException(contactId, ex);
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
121 // Return found instance
126 @SuppressWarnings ("unchecked")
127 public List<Contact> getAllContacts () {
129 this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
131 // Create query instance
132 Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
135 List<Contact> contacts = query.getResultList();
138 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
145 @SuppressWarnings ("unchecked")
146 public List<String> getEmailAddressList () {
148 this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
150 // Create query instance
151 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
154 List<String> emailAddresses = query.getResultList();
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
160 return emailAddresses;
164 public Contact lookupContact (final Contact contact) {
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("lookupContact: contact={0} - CALLED!", contact)); //NOI18N
168 // Parameter should be valid
169 if (null == contact) {
171 throw new NullPointerException("contact is null"); //NOI18N
172 } else if ((contact.getContactId() instanceof Long) && (contact.getContactId() > 0)) {
174 // Id set, ask other method
175 return this.findContactById(contact.getContactId());
176 } catch (final ContactNotFoundException ex) {
177 // Not found, should not happen
178 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
182 // Default is not found
183 Contact foundContact = null;
186 List<Contact> contacts = this.getAllContacts();
188 // Is the list empty?
189 if (contacts.isEmpty()) {
191 this.getLoggerBeanLocal().logTrace("lookupContact: No contacts registered, returning 'false' ..."); //NOI18N
196 Iterator<Contact> iterator = contacts.iterator();
199 while (iterator.hasNext()) {
201 Contact next = iterator.next();
204 this.getLoggerBeanLocal().logDebug(MessageFormat.format("lookupContact: next={0}", next)); //NOI18N
207 if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
209 this.getLoggerBeanLocal().logDebug(MessageFormat.format("lookupContact: Found same contact, id={0}", next.getContactId())); //NOI18N
218 this.getLoggerBeanLocal().logTrace(MessageFormat.format("lookupContact: foundContact={0} - EXIT!", foundContact)); //NOI18N
225 public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
227 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
229 // The contact instance must be valid
230 if (null == contact) {
232 throw new NullPointerException("contact is null"); //NOI18N
233 } else if (contact.getContactId() == null) {
235 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
236 } else if (contact.getContactId() < 1) {
238 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
241 // Set updated timestamp
242 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
244 // Merge cellphone, land-line and fix
245 Contact detachedContact = this.mergeContactData(contact);
248 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
251 return detachedContact;
255 public Contact updateContactData (final Contact contact) {
257 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N
259 // The contact instance must be valid
260 if (null == contact) {
262 throw new NullPointerException("contact is null"); //NOI18N
263 } else if (contact.getContactId() == null) {
265 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
266 } else if (contact.getContactId() < 1) {
268 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
271 // Is cell phone/land-line/fax number unlinked?
272 boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == null);
273 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
274 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
277 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
280 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
283 return detachedContact;