]> 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 - 2020 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 java.util.Objects;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
26 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
27
28 /**
29  * A contact EJB
30  * <p>
31  * @author Roland Häder<roland@mxchange.org>
32  */
33 @Stateless (name = "contact", description = "A bean handling contact data")
34 public class JobsContactSessionBean extends BaseJobsEnterpriseBean implements ContactSessionBeanRemote {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 542_145_347_916L;
40
41         /**
42          * Default constructor
43          */
44         public JobsContactSessionBean () {
45                 // Call super constructor
46                 super();
47         }
48
49         @Override
50         @SuppressWarnings ("unchecked")
51         public List<Contact> allContacts () {
52                 // Log trace message
53                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
54
55                 // Create query instance
56                 final Query query = this.getEntityManager().createNamedQuery("AllContacts"); //NOI18N
57
58                 // Get list
59                 final List<Contact> contacts = query.getResultList();
60
61                 // Log trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
63
64                 // Return it
65                 return contacts;
66         }
67
68         @Override
69         public Contact lookupContact (final Contact contact) {
70                 // Log trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
72
73                 // Parameter should be valid
74                 if (null == contact) {
75                         // Throw NPE
76                         throw new NullPointerException("contact is null"); //NOI18N
77                 } else if (contact.getContactId() > 0) {
78                         try {
79                                 // Id set, ask other method
80                                 return this.findContactById(contact.getContactId());
81                         } catch (final ContactNotFoundException ex) {
82                                 // Not found, should not happen
83                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
84                         }
85                 }
86
87                 // Default is not found
88                 Contact foundContact = null;
89
90                 // Get whole list
91                 final List<Contact> contacts = this.allContacts();
92
93                 // Is the list empty?
94                 if (contacts.isEmpty()) {
95                         // Then abort here
96                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
97                         return null;
98                 }
99
100                 // Loop through all
101                 for (final Contact currentContact : contacts) {
102                         // Is same contact?
103                         if ((Objects.equals(contact, currentContact)) || (Contacts.isSameContact(contact, currentContact))) {
104                                 // Debug message
105                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), currentContact.getContactId())); //NOI18N
106
107                                 // Found it
108                                 foundContact = currentContact;
109                                 break;
110                         }
111                 }
112
113                 // Trace message
114                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
115
116                 // Return found contact
117                 return foundContact;
118         }
119
120         @Override
121         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
122                 // Log trace message
123                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
124
125                 // The contact instance must be valid
126                 if (null == contact) {
127                         // Throw NPE again
128                         throw new NullPointerException("contact is null"); //NOI18N
129                 } else if (contact.getContactId() == null) {
130                         // Throw NPE again
131                         throw new NullPointerException("contact.contactId is null"); //NOI18N
132                 } else if (contact.getContactId() < 1) {
133                         // Not valid
134                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
135                 }
136
137                 // Set updated timestamp
138                 this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
139
140                 // Merge mobile, land-line and fix
141                 final Contact detachedContact = this.mergeContactData(contact);
142
143                 // Trace message
144                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
145
146                 // Return it
147                 return detachedContact;
148         }
149
150         @Override
151         public Contact updateContactData (final Contact contact) {
152                 // Log trace message
153                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
154
155                 // The contact instance must be valid
156                 if (null == contact) {
157                         // Throw NPE again
158                         throw new NullPointerException("contact is null"); //NOI18N
159                 } else if (contact.getContactId() == null) {
160                         // Throw NPE again
161                         throw new NullPointerException("contact.contactId is null"); //NOI18N
162                 } else if (contact.getContactId() < 1) {
163                         // Not valid
164                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
165                 }
166
167                 // Is cell phone/land-line/fax number unlinked?
168                 final boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
169                 final boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
170                 final boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
171
172                 // Call other Method
173                 final Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
174
175                 // Trace message
176                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
177
178                 // Return it
179                 return detachedContact;
180         }
181
182         /**
183          * Returns a contact instance which has the given id number.
184          * <p>
185          * @param contactId Contact id
186          * <p>
187          * @return Contact instance
188          * <p>
189          * @throws ContactNotFoundException If the contact was not found
190          */
191         private Contact findContactById (final Long contactId) throws ContactNotFoundException {
192                 // Log trace message
193                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
194
195                 // The parameter must be valid
196                 if (null == contactId) {
197                         // Throw NPE
198                         throw new NullPointerException("contactId is null"); //NOI18N
199                 } else if (contactId < 1) {
200                         // Not valid
201                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
202                 }
203
204                 // Get query instance
205                 final Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
206
207                 // Set parameter
208                 query.setParameter("contactId", contactId); //NOI18N
209                 query.setMaxResults(1);
210
211                 // Init contact instance
212                 final Contact contact;
213
214                 // Try to find a result
215                 try {
216                         // Find a single result
217                         contact = (Contact) query.getSingleResult();
218
219                         // Log trace message
220                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
221                 } catch (final NoResultException ex) {
222                         // No result found
223                         throw new ContactNotFoundException(contactId, ex);
224                 }
225
226                 // Log trace message
227                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
228
229                 // Return found instance
230                 return contact;
231         }
232
233 }