]> git.mxchange.org Git - addressbook-core.git/blob - src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
Please cherry-pick:
[addressbook-core.git] / src / org / mxchange / addressbook / database / BaseAddressbookDatabaseBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.database;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.Objects;
22 import java.util.Properties;
23 import javax.ejb.EJBException;
24 import javax.jms.JMSException;
25 import javax.jms.ObjectMessage;
26 import javax.mail.Address;
27 import org.mxchange.jcontacts.contact.Contact;
28 import org.mxchange.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 BaseAddressbookDatabaseBean 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 BaseAddressbookDatabaseBean () {
55                 // Call super constructor
56                 super("jms/addressbook-queue-factory", "jms/addressbook-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 number
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 number
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 number
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          */
441         protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
442                 // Trace message
443                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
444
445                 // All should be set
446                 if (null == subjectLine) {
447                         // Throw NPE
448                         throw new NullPointerException("subjectLine is null"); //NOI18N
449                 } else if (subjectLine.isEmpty()) {
450                         // No subject line
451                         throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
452                 } else if (null == templateName) {
453                         // Throw NPE
454                         throw new NullPointerException("templateName is null"); //NOI18N
455                 } else if (templateName.isEmpty()) {
456                         // No template name
457                         throw new IllegalArgumentException("templateName is empty"); //NOI18N
458                 } else if (null == emailAddress) {
459                         // Throw NPE
460                         throw new NullPointerException("emailAddress is null"); //NOI18N
461                 }else if (null == user) {
462                         // Throw NPE
463                         throw new NullPointerException("user is null"); //NOI18N
464                 } else if (user.getUserId() == null) {
465                         // Throw NPE again
466                         throw new NullPointerException("user.userId is null"); //NOI18N
467                 } else if (user.getUserId() < 1) {
468                         // Not valid number
469                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
470                 } else if (user.getUserName() == null) {
471                         // Throw NPE again
472                         throw new NullPointerException("user.userName is null"); //NOI18N
473                 } else if (user.getUserName().isEmpty()) {
474                         // Empty string
475                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
476                 } else if (user.getUserAccountStatus() == null) {
477                         // Throw NPE
478                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
479                 } else if (user.getUserContact() == null) {
480                         // Throw it again
481                         throw new NullPointerException("user.userContact is null"); //NOI18N
482                 } else if (user.getUserContact().getContactId() == null) {
483                         // .. and again
484                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
485                 } else if (user.getUserContact().getContactId() < 1) {
486                         // Invalid id
487                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
488                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
489                         // Throw NPE again
490                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
491                 }
492
493                 // Prepare mail wrapper
494                 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
495
496                 // Set all values
497                 Properties variables = UserUtils.getAllUserFields(user);
498
499                 // Set base URL
500                 variables.put("baseUrl", baseUrl); //NOI18N
501
502                 // Set all
503                 // @TODO Language from message bundle
504                 emailWrapper.setRecipient(emailAddress);
505                 emailWrapper.setLocale(user.getUserLocale());
506                 emailWrapper.setSubjectLine(subjectLine);
507                 emailWrapper.setTemplateName(templateName);
508                 emailWrapper.setTemplateVariables(variables);
509
510                 try {
511                         // Send out email change
512                         ObjectMessage message = this.getSession().createObjectMessage();
513                         message.setObject(emailWrapper);
514
515                         // Send message
516                         this.sendMessage(message);
517                 } catch (final JMSException ex) {
518                         // Throw again
519                         throw new EJBException(ex);
520                 }
521
522                 // Trace message
523                 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
524         }
525
526         /**
527          * Updates all contact's phone instances from other contact, both contacts
528          * should be the same.
529          * <p>
530          * @param contact Contact to set instances
531          * @param other Other contact to get instances from
532          */
533         protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
534                 // Trace message
535                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
536
537                 // Both must be the same and not null
538                 if (null == contact) {
539                         // Throw NPE
540                         throw new NullPointerException("contact is null"); //NOI18N
541                 } else if (null == other) {
542                         // Throw NPE
543                         throw new NullPointerException("other is null"); //NOI18N
544                 } else if (!Objects.equals(contact, other)) {
545                         // Not same instances
546                         throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
547                 }
548
549                 // Debug message
550                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
551
552                 // Is other cellphone not set?
553                 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
554                         // Debug message
555                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying cellphone entry ...", this.getClass().getSimpleName())); //NOI18N
556
557                         // Is the fax number set?
558                         if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
559                                 // Copy cellphone number
560                                 contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
561                         } else {
562                                 // Null it
563                                 contact.setContactMobileNumber(null);
564                         }
565                 }
566
567                 // Debug message
568                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
569
570                 // Is other cellphone not set?
571                 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
572                         // Debug message
573                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
574
575                         // Is the land-line number set?
576                         if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
577                                 // Copy land-line number
578                                 contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
579                         } else {
580                                 // Null it
581                                 contact.setContactLandLineNumber(null);
582                         }
583                 }
584
585                 // Debug message
586                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
587
588                 // Is other cellphone not set?
589                 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
590                         // Debug message
591                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
592
593                         // Is the fax number set?
594                         if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
595                                 // Copy fax number
596                                 contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
597                         } else {
598                                 // Null it
599                                 contact.setContactFaxNumber(null);
600                         }
601                 }
602
603                 // Trace message
604                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
605         }
606
607         /**
608          * Updates all contact's phone entry's updated timestamps
609          * <p>
610          * @param contact Contact instance to update
611          * @param isMobileUnlinked Whether a cellphone entry has been unlinked in
612          * contact instance
613          * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
614          * contact instance
615          * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
616          * instance
617          */
618         protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
619                 // Trace message
620                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
621
622                 // The contact instance must be valid
623                 if (null == contact) {
624                         // Throw NPE again
625                         throw new NullPointerException("contact is null"); //NOI18N
626                 } else if (contact.getContactId() == null) {
627                         // Throw NPE again
628                         throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
629                 } else if (contact.getContactId() < 1) {
630                         // Not valid
631                         throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
632                 }
633
634                 // Get all phone instances
635                 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
636                 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
637                 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
638
639                 // Flags and instances must be constistent
640                 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
641                         // Bad state
642                         throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
643                 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
644                         // Bad state
645                         throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
646                 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
647                         // Bad state
648                         throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
649                 }
650
651                 // Is a phone number instance set?
652                 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
653                         // Debug message
654                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
655
656                         // Set updated timestamp
657                         landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
658                 }
659
660                 // Is a fax number instance set?
661                 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
662                         // Debug message
663                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
664
665                         // Set updated timestamp
666                         faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
667                 }
668
669                 // Is a mobile number instance set?
670                 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
671                         // Debug message
672                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
673
674                         // Set updated timestamp
675                         mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
676                 }
677
678                 // Trace message
679                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N
680         }
681
682 }