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