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