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