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