]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
Please cherry-pick:
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / jcontacts / contact / AddressbookContactSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.contact;
18
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.addressbook.database.BaseAddressbookDatabaseBean;
28 import org.mxchange.jcontacts.contact.utils.ContactUtils;
29 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
30
31 /**
32  * A contact EJB
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @Stateless (name = "contact", description = "A bean handling contact data")
37 public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 542_145_347_916L;
43
44         /**
45          * Default constructor
46          */
47         public AddressbookContactSessionBean () {
48         }
49
50         @Override
51         public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
52                 // Log trace message
53                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
54
55                 // The parameter must be valid
56                 if (null == emailAddress) {
57                         // Throw NPE
58                         throw new NullPointerException("emailAddress is null"); //NOI18N
59                 } else if (emailAddress.isEmpty()) {
60                         // Not valid
61                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
62                 }
63
64                 // Get query instance
65                 Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
66
67                 // Set parameter
68                 query.setParameter("emailAddress", emailAddress); //NOI18N
69
70                 // Init contact instance
71                 Contact contact;
72
73                 // Try to find a result
74                 try {
75                         // Find a single result
76                         contact = (Contact) query.getSingleResult();
77
78                         // Log trace message
79                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
80                 } catch (final NoResultException ex) {
81                         // No result found
82                         throw new ContactNotFoundException(emailAddress, ex);
83                 }
84
85                 // Log trace message
86                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
87
88                 // Return found instance
89                 return contact;
90         }
91
92         @Override
93         public Contact findContactById (final Long contactId) throws ContactNotFoundException {
94                 // Log trace message
95                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
96
97                 // The parameter must be valid
98                 if (null == contactId) {
99                         // Throw NPE
100                         throw new NullPointerException("contactId is null"); //NOI18N
101                 } else if (contactId < 1) {
102                         // Not valid
103                         throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N
104                 }
105
106                 // Get query instance
107                 Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N
108
109                 // Set parameter
110                 query.setParameter("contactId", contactId); //NOI18N
111
112                 // Init contact instance
113                 Contact contact;
114
115                 // Try to find a result
116                 try {
117                         // Find a single result
118                         contact = (Contact) query.getSingleResult();
119
120                         // Log trace message
121                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
122                 } catch (final NoResultException ex) {
123                         // No result found
124                         throw new ContactNotFoundException(contactId, ex);
125                 }
126
127                 // Log trace message
128                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
129
130                 // Return found instance
131                 return contact;
132         }
133
134         @Override
135         @SuppressWarnings ("unchecked")
136         public List<Contact> getAllContacts () {
137                 // Log trace message
138                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
139
140                 // Create query instance
141                 Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
142
143                 // Get list
144                 List<Contact> contacts = query.getResultList();
145
146                 // Log trace message
147                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
148
149                 // Return it
150                 return contacts;
151         }
152
153         @Override
154         @SuppressWarnings ("unchecked")
155         public List<String> getEmailAddressList () {
156                 // Log trace message
157                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
158
159                 // Create query instance
160                 Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
161
162                 // Get list
163                 List<String> emailAddresses = query.getResultList();
164
165                 // Log trace message
166                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
167
168                 // Return it
169                 return emailAddresses;
170         }
171
172         @Override
173         public boolean isEmailAddressRegistered (final String emailAddress) {
174                 // Log trace message
175                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
176
177                 // The email address should be valid
178                 if (null == emailAddress) {
179                         // Is null
180                         throw new NullPointerException("emailAddress is null"); //NOI18N
181                 } else if (emailAddress.isEmpty()) {
182                         // Is empty
183                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
184                 }
185
186                 // Default is not found
187                 boolean isFound = false;
188
189                 try {
190                         // Ask other method for contact instance
191                         Contact contact = this.findContactByEmailAddress(emailAddress);
192
193                         // Log debug message
194                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
195
196                         // It is found ...
197                         isFound = true;
198                 } catch (final ContactNotFoundException ex) {
199                         // @TODO Was not found, log exception for spam check?
200                 }
201
202                 // Log trace message
203                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
204
205                 // Return status
206                 return isFound;
207         }
208
209         @Override
210         public Contact lookupContact (final Contact contact) {
211                 // Log trace message
212                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
213
214                 // Parameter should be valid
215                 if (null == contact) {
216                         // Throw NPE
217                         throw new NullPointerException("contact is null"); //NOI18N
218                 } else if (contact.getContactId() > 0) {
219                         try {
220                                 // Id set, ask other method
221                                 return this.findContactById(contact.getContactId());
222                         } catch (final ContactNotFoundException ex) {
223                                 // Not found, should not happen
224                                 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
225                         }
226                 }
227
228                 // Default is not found
229                 Contact foundContact = null;
230
231                 // Get whole list
232                 List<Contact> contacts = this.getAllContacts();
233
234                 // Is the list empty?
235                 if (contacts.isEmpty()) {
236                         // Then abort here
237                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
238                         return null;
239                 }
240
241                 // Get iterator
242                 Iterator<Contact> iterator = contacts.iterator();
243
244                 // Loop through all
245                 while (iterator.hasNext()) {
246                         // Get contact
247                         Contact next = iterator.next();
248
249                         // Is same contact?
250                         if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
251                                 // Debug message
252                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
253
254                                 // Found it
255                                 foundContact = next;
256                                 break;
257                         }
258                 }
259
260                 // Trace message
261                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
262
263                 // Return found contact
264                 return foundContact;
265         }
266
267         @Override
268         public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
269                 // Log trace message
270                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
271
272                 // The contact instance must be valid
273                 if (null == contact) {
274                         // Throw NPE again
275                         throw new NullPointerException("contact is null"); //NOI18N
276                 } else if (contact.getContactId() == null) {
277                         // Throw NPE again
278                         throw new NullPointerException("contact.contactId is null"); //NOI18N
279                 } else if (contact.getContactId() < 1) {
280                         // Not valid
281                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
282                 }
283
284                 // Set updated timestamp
285                 contact.setContactUpdated(new GregorianCalendar());
286
287                 // Detach all phone numbers
288                 this.detachAllContactPhoneEntries(contact);
289
290                 // Merge cellphone, land-line and fix
291                 Contact detachedContact = this.mergeContactData(contact);
292
293                 // Trace message
294                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
295
296                 // Return it
297                 return detachedContact;
298         }
299
300         @Override
301         public Contact updateContactData (final Contact contact) {
302                 // Log trace message
303                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
304
305                 // The contact instance must be valid
306                 if (null == contact) {
307                         // Throw NPE again
308                         throw new NullPointerException("contact is null"); //NOI18N
309                 } else if (contact.getContactId() == null) {
310                         // Throw NPE again
311                         throw new NullPointerException("contact.contactId is null"); //NOI18N
312                 } else if (contact.getContactId() < 1) {
313                         // Not valid
314                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
315                 }
316
317                 // Is cell phone/land-line/fax number unlinked?
318                 boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
319                 boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
320                 boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
321
322                 // Call other Method
323                 Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
324
325                 // Trace message
326                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
327
328                 // Return it
329                 return detachedContact;
330         }
331
332 }