2 * Copyright (C) 2016 - 2024 Free Software Foundation
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.model.contact;
19 import java.text.MessageFormat;
20 import java.util.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
25 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
30 * @author Roland Häder<roland@mxchange.org>
32 @Stateless (name = "contact", description = "A bean handling contact data")
33 public class JobsContactSessionBean extends BaseJobsEnterpriseBean implements ContactSessionBeanRemote {
38 private static final long serialVersionUID = 542_145_348_001L;
43 public JobsContactSessionBean () {
44 // Call super constructor
49 @SuppressWarnings ("unchecked")
50 public List<Contact> fetchAllContacts () {
52 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
54 // Create query instance
55 final Query query = this.getEntityManager().createNamedQuery("AllContacts"); //NOI18N
58 final List<Contact> contacts = query.getResultList();
61 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
68 public Contact updateContactData (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
70 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
72 // The contact instance must be valid
73 if (null == contact) {
75 throw new NullPointerException("contact is null"); //NOI18N
76 } else if (contact.getContactId() == null) {
78 throw new NullPointerException("contact.contactId is null"); //NOI18N
79 } else if (contact.getContactId() < 1) {
81 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
84 // Set updated timestamp
85 this.setAllContactPhoneEntriesUpdated(contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked);
87 // Merge mobile, land-line and fix
88 final Contact detachedContact = this.mergeContactData(contact);
91 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
94 return detachedContact;
98 public Contact updateContactData (final Contact contact) {
100 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
102 // The contact instance must be valid
103 if (null == contact) {
105 throw new NullPointerException("contact is null"); //NOI18N
106 } else if (contact.getContactId() == null) {
108 throw new NullPointerException("contact.contactId is null"); //NOI18N
109 } else if (contact.getContactId() < 1) {
111 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
114 // Is cell phone/land-line/fax number unlinked?
115 final boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
116 final boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
117 final boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
120 final Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
123 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
126 return detachedContact;
130 * Returns a contact instance which has the given id number.
132 * @param contactId Contact id
134 * @return Contact instance
136 * @throws ContactNotFoundException If the contact was not found
138 private Contact findContactById (final Long contactId) throws ContactNotFoundException {
140 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
142 // The parameter must be valid
143 if (null == contactId) {
145 throw new NullPointerException("contactId is null"); //NOI18N
146 } else if (contactId < 1) {
148 throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
151 // Get query instance
152 final Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
155 query.setParameter("contactId", contactId); //NOI18N
156 query.setMaxResults(1);
158 // Init contact instance
159 final Contact contact;
161 // Try to find a result
163 // Find a single result
164 contact = (Contact) query.getSingleResult();
167 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
168 } catch (final NoResultException ex) {
170 throw new ContactNotFoundException(contactId, ex);
174 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
176 // Return found instance