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