]> git.mxchange.org Git - addressbook-core.git/blob - src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
Please cherry-pick:
[addressbook-core.git] / src / org / mxchange / addressbook / database / BaseAddressbookDatabaseBean.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 General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.database;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.Objects;
22 import java.util.Properties;
23 import javax.ejb.EJBException;
24 import javax.jms.JMSException;
25 import javax.jms.ObjectMessage;
26 import javax.mail.Address;
27 import org.mxchange.jcontacts.contact.Contact;
28 import org.mxchange.jcontacts.contact.ContactUtils;
29 import org.mxchange.jcoreee.database.BaseDatabaseBean;
30 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
31 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
32 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
33 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
34 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
35 import org.mxchange.jphone.utils.PhoneUtils;
36 import org.mxchange.jusercore.model.user.LoginUser;
37 import org.mxchange.jusercore.model.user.User;
38 import org.mxchange.jusercore.model.user.UserUtils;
39
40 /**
41  * A helper class for beans that access the database.
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 12_895_410_275_811_963L;
51
52         /**
53          * Protected constructor
54          */
55         protected BaseAddressbookDatabaseBean () {
56                 // Call super constructor
57                 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
58         }
59
60         /**
61          * Updates all contact's phone entry's created timestamps
62          * <p>
63          * @param contact Contact instance to update
64          */
65         protected void setAllContactPhoneEntriesCreated (final Contact contact) {
66                 // Trace message
67                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
68
69                 // The contact instance must be valid
70                 if (null == contact) {
71                         // Throw NPE again
72                         throw new NullPointerException("contact is null"); //NOI18N
73                 }
74
75                 // Get all phone instances
76                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
77                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
78                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
79
80                 // Debug message
81                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: landLineNumber={1},faxNumber={2},mobileNumber={3}", this.getClass().getSimpleName(), landLineNumber, faxNumber, mobileNumber)); //NOI18N
82
83                 // Is a phone number instance set?
84                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
85                         // Debug message
86                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
87
88                         // Set updated timestamp
89                         landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
90                 }
91
92                 // Is a fax number instance set?
93                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
94                         // Debug message
95                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
96
97                         // Set updated timestamp
98                         faxNumber.setPhoneEntryCreated(new GregorianCalendar());
99                 }
100
101                 // Is a mobile number instance set?
102                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
103                         // Debug message
104                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
105
106                         // Set updated timestamp
107                         mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
108                 }
109
110                 // Trace message
111                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: EXIT!", this.getClass().getSimpleName())); //NOI18N
112         }
113
114         /**
115          * Returns a managed instance from given mobile number
116          * <p>
117          * @param mobileNumber  Mobile instance
118          * @param fetchedNumber Found mobile number in database
119          * <p>
120          * @return Managed instance
121          */
122         protected DialableMobileNumber getManaged (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
123                 // Trace message
124                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: mobileNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), mobileNumber, fetchedNumber)); //NOI18N
125
126                 // Should be valid
127                 if (null == mobileNumber) {
128                         // Throw NPE
129                         throw new NullPointerException("mobileNumber is null"); //NOI18N
130                 } else if (null == fetchedNumber) {
131                         // Throw NPE again
132                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
133                 } else if (fetchedNumber.getPhoneId() == null) {
134                         // ..and again
135                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
136                 }
137
138                 // Debug message
139                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
140
141                 // Default is null
142                 DialableMobileNumber managedNumber = null;
143
144                 // Is there a difference?
145                 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
146                         // Merge this entry
147                         managedNumber = this.getEntityManager().merge(fetchedNumber);
148                 }
149
150                 // Trace message
151                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
152
153                 // Return it
154                 return managedNumber;
155         }
156
157         /**
158          * Returns a managed instance from given land-line number
159          * <p>
160          * @param landLineNumber Land-line instance
161          * @param fetchedNumber  Found land-line number in database
162          * <p>
163          * @return Managed instance
164          */
165         protected DialableLandLineNumber getManaged (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
166                 // Trace message
167                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: landLineNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), landLineNumber, fetchedNumber)); //NOI18N
168
169                 // Should be valid
170                 if (null == landLineNumber) {
171                         // Throw NPE
172                         throw new NullPointerException("landLineNumber is null"); //NOI18N
173                 } else if (null == fetchedNumber) {
174                         // Throw NPE again
175                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
176                 } else if (fetchedNumber.getPhoneId() == null) {
177                         // ..and again
178                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
179                 }
180
181                 // Debug message
182                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
183
184                 // Default is null
185                 DialableLandLineNumber managedNumber = null;
186
187                 // Is there a difference?
188                 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
189                         // Merge this entry
190                         managedNumber = this.getEntityManager().merge(fetchedNumber);
191                 }
192
193                 // Trace message
194                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
195
196                 // Return it
197                 return managedNumber;
198         }
199
200         /**
201          * Returns a managed instance from given fax number
202          * <p>
203          * @param faxNumber     Fax instance
204          * @param fetchedNumber Found fax number in database
205          * <p>
206          * @return Managed instance
207          */
208         protected DialableFaxNumber getManaged (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
209                 // Trace message
210                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: faxNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), faxNumber, fetchedNumber)); //NOI18N
211
212                 // Should be valid
213                 if (null == faxNumber) {
214                         // Throw NPE
215                         throw new NullPointerException("faxNumber is null"); //NOI18N
216                 } else if (null == fetchedNumber) {
217                         // Throw NPE again
218                         throw new NullPointerException("fetchedNumber is null"); //NOI18N
219                 } else if (fetchedNumber.getPhoneId() == null) {
220                         // ..and again
221                         throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
222                 }
223
224                 // Debug message
225                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
226
227                 // Default is null
228                 DialableFaxNumber managedNumber = null;
229
230                 // Is there a difference?
231                 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
232                         // Merge this entry
233                         managedNumber = this.getEntityManager().merge(fetchedNumber);
234                 }
235
236                 // Trace message
237                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
238
239                 // Return it
240                 return managedNumber;
241         }
242
243         /**
244          * Get back a managed instance from given user
245          * <p>
246          * @param user Unmanaged/detached user instance
247          * <p>
248          * @return Managed user instance
249          */
250         protected User getManagedUser (final User user) {
251                 // Trace message
252                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
253
254                 // user should not be null
255                 if (null == user) {
256                         // Abort here
257                         throw new NullPointerException("user is null"); //NOI18N
258                 } else if (user.getUserId() == null) {
259                         // Id is set
260                         throw new NullPointerException("user.userId is null"); //NOI18N
261                 } else if (user.getUserId() < 1) {
262                         // Id is set
263                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
264                 } else if (user.getUserContact() == null) {
265                         // Throw NPE again
266                         throw new NullPointerException("user.userContact is null"); //NOI18N
267                 } else if (user.getUserContact().getContactId() == null) {
268                         // Throw NPE again
269                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
270                 } else if (user.getUserContact().getContactId() < 1) {
271                         // Not valid id number
272                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N#
273                 }
274
275                 // Try to find it (should be there)
276                 User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
277
278                 // Should be there
279                 assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
280
281                 // Trace message
282                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
283
284                 // Return it
285                 return managedUser;
286         }
287
288         /**
289          * Merges given (detached) contact's data
290          * <p>
291          * @param contact Contact instance to merge
292          * <p>
293          * @return Detached contact instance
294          */
295         protected Contact mergeContactData (final Contact contact) {
296                 // Trace message
297                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
298
299                 // The contact instance must be valid
300                 if (null == contact) {
301                         // Throw NPE again
302                         throw new NullPointerException("detachedContact is null"); //NOI18N
303                 } else if (contact.getContactId() == null) {
304                         // Throw NPE again
305                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
306                 } else if (contact.getContactId() < 1) {
307                         // Not valid
308                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
309                 }
310
311                 // Get contact from it and find it
312                 Contact managedContact = this.getEntityManager().find(contact.getClass(), contact.getContactId());
313
314                 // Should be found
315                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N
316
317                 // Debug message
318                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateUserPersonalData: managedContact.contactId={1}", this.getClass().getSimpleName(), managedContact.getContactId())); //NOI18N
319
320                 // Is a fax number set?
321                 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
322                         // Make fax numbers managed
323                         managedContact.setContactFaxNumber(this.getManaged(contact.getContactFaxNumber(), contact.getContactFaxNumber()));
324                 }
325
326                 // Is a land-line number set?
327                 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
328                         // Make land-line numbers managed
329                         managedContact.setContactLandLineNumber(this.getManaged(contact.getContactLandLineNumber(), contact.getContactLandLineNumber()));
330                 }
331
332                 // Is a mobile number set?
333                 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
334                         // Make mobile numbers managed
335                         managedContact.setContactMobileNumber(this.getManaged(contact.getContactMobileNumber(), contact.getContactMobileNumber()));
336                 }
337
338                 // Set updated timestamp
339                 managedContact.setContactUpdated(new GregorianCalendar());
340
341                 // Copy all
342                 ContactUtils.copyAll(contact, managedContact);
343
344                 // Trace message
345                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
346
347                 // Return detached contact
348                 return managedContact;
349         }
350
351         /**
352          * Merges given (detached) contact's cellphone, land-line and fax numbers
353          * <p>
354          * @param detachedContact Detached contact instance
355          */
356         protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
357                 // Trace message
358                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactsMobileLandLineFaxNumbers: detachedContact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
359
360                 // The contact instance must be valid
361                 if (null == detachedContact) {
362                         // Throw NPE again
363                         throw new NullPointerException("detachedContact is null"); //NOI18N
364                 } else if (detachedContact.getContactId() == null) {
365                         // Throw NPE again
366                         throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
367                 } else if (detachedContact.getContactId() < 1) {
368                         // Not valid
369                         throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
370                 }
371
372                 // Get all instances
373                 DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
374                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
375                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
376
377                 // Is there a  cellphone instance set?
378                 if (cellphone instanceof DialableMobileNumber) {
379                         // Debug message
380                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
381
382                         // Then find it, too
383                         DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
384
385                         // Should be there
386                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
387
388                         // Copy all
389                         foundMobile.copyAll(detachedContact.getContactMobileNumber());
390
391                         // Set it back
392                         detachedContact.setContactMobileNumber(foundMobile);
393                 }
394
395                 // Is there a  fax instance set?
396                 if (fax instanceof DialableFaxNumber) {
397                         // Debug message
398                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
399
400                         // Then find it, too
401                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
402
403                         // Copy all
404                         foundFax.copyAll(detachedContact.getContactFaxNumber());
405
406                         // Set it back
407                         detachedContact.setContactFaxNumber(foundFax);
408                 }
409
410                 // Is there a  fax instance set?
411                 if (landLine instanceof DialableLandLineNumber) {
412                         // Debug message
413                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
414
415                         // Then find it, too
416                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
417
418                         // Should be there
419                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
420
421                         // Copy all
422                         foundLandLine.copyAll(detachedContact.getContactLandLineNumber());
423
424                         // Set it back
425                         detachedContact.setContactLandLineNumber(foundLandLine);
426                 }
427
428                 // Trace message
429                 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
430         }
431
432         /**
433          * Sends an email with given subject line, template name to given recipient
434          * and user data
435          * <p>
436          * @param subjectLine    Subject line
437          * @param templateName   Template name
438          * @param emailAddress   Recipient's email address
439          * @param user           User instance
440          * @param baseUrl        Base URL
441          * @param randomPassword A randomly-generated password or NULL if user had
442          *                       to enter it.
443          */
444         protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl, final String randomPassword) {
445                 // Trace message
446                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
447
448                 // All should be set
449                 if (null == subjectLine) {
450                         // Throw NPE
451                         throw new NullPointerException("subjectLine is null"); //NOI18N
452                 } else if (subjectLine.isEmpty()) {
453                         // No subject line
454                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
455                 } else if (null == templateName) {
456                         // Throw NPE
457                         throw new NullPointerException("templateName is null"); //NOI18N
458                 } else if (templateName.isEmpty()) {
459                         // No template name
460                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
461                 } else if (null == emailAddress) {
462                         // Throw NPE
463                         throw new NullPointerException("emailAddress is null"); //NOI18N
464                 } else if (null == user) {
465                         // Throw NPE
466                         throw new NullPointerException("user is null"); //NOI18N
467                 } else if (user.getUserId() == null) {
468                         // Throw NPE again
469                         throw new NullPointerException("user.userId is null"); //NOI18N
470                 } else if (user.getUserId() < 1) {
471                         // Not valid number
472                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
473                 } else if (user.getUserName() == null) {
474                         // Throw NPE again
475                         throw new NullPointerException("user.userName is null"); //NOI18N
476                 } else if (user.getUserName().isEmpty()) {
477                         // Empty string
478                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
479                 } else if (user.getUserAccountStatus() == null) {
480                         // Throw NPE
481                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
482                 } else if (user.getUserContact() == null) {
483                         // Throw it again
484                         throw new NullPointerException("user.userContact is null"); //NOI18N
485                 } else if (user.getUserContact().getContactId() == null) {
486                         // .. and again
487                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
488                 } else if (user.getUserContact().getContactId() < 1) {
489                         // Invalid id
490                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
491                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
492                         // Throw NPE again
493                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
494                 }
495
496                 // Prepare mail wrapper
497                 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
498
499                 // Set all values
500                 Properties variables = UserUtils.getAllUserFields(user);
501
502                 // Set base URL and random password
503                 variables.put("baseUrl", baseUrl); //NOI18N
504                 variables.put("randomPassword", randomPassword); //NOI18N
505
506                 // Set all
507                 // @TODO Language from message bundle
508                 emailWrapper.setRecipient(emailAddress);
509                 emailWrapper.setLocale(user.getUserLocale());
510                 emailWrapper.setSubjectLine(subjectLine);
511                 emailWrapper.setTemplateName(templateName);
512                 emailWrapper.setTemplateVariables(variables);
513
514                 try {
515                         // Send out email change
516                         ObjectMessage message = this.getSession().createObjectMessage();
517                         message.setObject(emailWrapper);
518
519                         // Send message
520                         this.sendMessage(message);
521                 } catch (final JMSException ex) {
522                         // Throw again
523                         throw new EJBException(ex);
524                 }
525
526                 // Trace message
527                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
528         }
529
530         /**
531          * Updates all contact's phone instances from other contact, both contacts
532          * should be the same.
533          * <p>
534          * @param contact Contact to set instances
535          * @param other   Other contact to get instances from
536          */
537         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
538                 // Trace message
539                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
540
541                 // Both must be the same and not null
542                 if (null == contact) {
543                         // Throw NPE
544                         throw new NullPointerException("contact is null"); //NOI18N
545                 } else if (null == other) {
546                         // Throw NPE
547                         throw new NullPointerException("other is null"); //NOI18N
548                 } else if (!Objects.equals(contact, other)) {
549                         // Not same instances
550                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
551                 }
552
553                 // Debug message
554                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
555
556                 // Is other cellphone not set?
557                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
558                         // Debug message
559                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying cellphone entry ...", this.getClass().getSimpleName())); //NOI18N
560
561                         // Is the fax number set?
562                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
563                                 // Copy cellphone number
564                                 contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
565                         } else {
566                                 // Null it
567                                 contact.setContactMobileNumber(null);
568                         }
569                 }
570
571                 // Debug message
572                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
573
574                 // Is other cellphone not set?
575                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
576                         // Debug message
577                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
578
579                         // Is the land-line number set?
580                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
581                                 // Copy land-line number
582                                 contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
583                         } else {
584                                 // Null it
585                                 contact.setContactLandLineNumber(null);
586                         }
587                 }
588
589                 // Debug message
590                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
591
592                 // Is other cellphone not set?
593                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
594                         // Debug message
595                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
596
597                         // Is the fax number set?
598                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
599                                 // Copy fax number
600                                 contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
601                         } else {
602                                 // Null it
603                                 contact.setContactFaxNumber(null);
604                         }
605                 }
606
607                 // Trace message
608                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
609         }
610
611         /**
612          * Updates all contact's phone entry's updated timestamps
613          * <p>
614          * @param contact            Contact instance to update
615          * @param isMobileUnlinked   Whether a mobile entry has been unlinked in
616          *                           contact instance
617          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
618          *                           contact instance
619          * @param isFaxUnlinked      Whether a fax entry has been unlinked in
620          *                           contact instance
621          */
622         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
623                 // Trace message
624                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
625
626                 // The contact instance must be valid
627                 if (null == contact) {
628                         // Throw NPE again
629                         throw new NullPointerException("contact is null"); //NOI18N
630                 } else if (contact.getContactId() == null) {
631                         // Throw NPE again
632                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
633                 } else if (contact.getContactId() < 1) {
634                         // Not valid
635                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
636                 }
637
638                 // Get all phone instances
639                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
640                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
641                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
642
643                 // Flags and instances must be constistent
644                 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
645                         // Bad state
646                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
647                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
648                         // Bad state
649                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
650                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
651                         // Bad state
652                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
653                 }
654
655                 // Is a phone number instance set?
656                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
657                         // Debug message
658                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
659
660                         // Set updated timestamp
661                         landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
662                 }
663
664                 // Is a fax number instance set?
665                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
666                         // Debug message
667                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
668
669                         // Set updated timestamp
670                         faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
671                 }
672
673                 // Is a mobile number instance set?
674                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
675                         // Debug message
676                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
677
678                         // Set updated timestamp
679                         mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
680                 }
681
682                 // Trace message
683                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N
684         }
685
686 }