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