]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontacts/model/contact/JobsContactSessionBean.java
Please cherry-pick:
[jjobs-ejb.git] / src / java / org / mxchange / jcontacts / model / contact / JobsContactSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcontacts.model.contact;
18
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;
26
27 /**
28  * A contact EJB
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @Stateless (name = "contact", description = "A bean handling contact data")
33 public class JobsContactSessionBean extends BaseJobsEnterpriseBean implements ContactSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 542_145_347_916L;
39
40         /**
41          * Default constructor
42          */
43         public JobsContactSessionBean () {
44                 // Call super constructor
45                 super();
46         }
47
48         @Override
49         @SuppressWarnings ("unchecked")
50         public List<Contact> fetchAllContacts () {
51                 // Log trace message
52                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
53
54                 // Create query instance
55                 final Query query = this.getEntityManager().createNamedQuery("AllContacts"); //NOI18N
56
57                 // Get list
58                 final List<Contact> contacts = query.getResultList();
59
60                 // Log trace message
61                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
62
63                 // Return it
64                 return contacts;
65         }
66
67         @Override
68         public Contact updateContactData (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
69                 // Log trace message
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
71
72                 // The contact instance must be valid
73                 if (null == contact) {
74                         // Throw NPE again
75                         throw new NullPointerException("contact is null"); //NOI18N
76                 } else if (contact.getContactId() == null) {
77                         // Throw NPE again
78                         throw new NullPointerException("contact.contactId is null"); //NOI18N
79                 } else if (contact.getContactId() < 1) {
80                         // Not valid
81                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
82                 }
83
84                 // Set updated timestamp
85                 this.setAllContactPhoneEntriesUpdated(contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked);
86
87                 // Merge mobile, land-line and fix
88                 final Contact detachedContact = this.mergeContactData(contact);
89
90                 // Trace message
91                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
92
93                 // Return it
94                 return detachedContact;
95         }
96
97         @Override
98         public Contact updateContactData (final Contact contact) {
99                 // Log trace message
100                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
101
102                 // The contact instance must be valid
103                 if (null == contact) {
104                         // Throw NPE again
105                         throw new NullPointerException("contact is null"); //NOI18N
106                 } else if (contact.getContactId() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("contact.contactId is null"); //NOI18N
109                 } else if (contact.getContactId() < 1) {
110                         // Not valid
111                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
112                 }
113
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);
118
119                 // Call other Method
120                 final Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
121
122                 // Trace message
123                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
124
125                 // Return it
126                 return detachedContact;
127         }
128
129         /**
130          * Returns a contact instance which has the given id number.
131          * <p>
132          * @param contactId Contact id
133          * <p>
134          * @return Contact instance
135          * <p>
136          * @throws ContactNotFoundException If the contact was not found
137          */
138         private Contact findContactById (final Long contactId) throws ContactNotFoundException {
139                 // Log trace message
140                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
141
142                 // The parameter must be valid
143                 if (null == contactId) {
144                         // Throw NPE
145                         throw new NullPointerException("contactId is null"); //NOI18N
146                 } else if (contactId < 1) {
147                         // Not valid
148                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
149                 }
150
151                 // Get query instance
152                 final Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
153
154                 // Set parameter
155                 query.setParameter("contactId", contactId); //NOI18N
156                 query.setMaxResults(1);
157
158                 // Init contact instance
159                 final Contact contact;
160
161                 // Try to find a result
162                 try {
163                         // Find a single result
164                         contact = (Contact) query.getSingleResult();
165
166                         // Log trace message
167                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
168                 } catch (final NoResultException ex) {
169                         // No result found
170                         throw new ContactNotFoundException(contactId, ex);
171                 }
172
173                 // Log trace message
174                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
175
176                 // Return found instance
177                 return contact;
178         }
179
180 }