2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.addressbook.database;
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.User;
36 import org.mxchange.jusercore.model.user.UserUtils;
39 * A helper class for beans that access the database.
41 * @author Roland Häder<roland@mxchange.org>
43 public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
48 private static final long serialVersionUID = 12_895_410_275_811_963L;
51 * Protected constructor
53 protected BaseAddressbookDatabaseBean () {
54 // Call super constructor
55 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
59 * Updates all contacts's phone entry's created timestamps
61 * @param contact Contact instance to update
63 protected void setAllContactPhoneEntriesCreated (final Contact contact) {
65 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
67 // The contact instance must be valid
68 if (null == contact) {
70 throw new NullPointerException("contact is null"); //NOI18N
73 // Get all phone instances
74 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
75 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
76 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
79 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: landLineNumber={1},faxNumber={2},mobileNumber={3}", this.getClass().getSimpleName(), landLineNumber, faxNumber, mobileNumber)); //NOI18N
81 // Is a phone number instance set?
82 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
84 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
86 // Set updated timestamp
87 landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
90 // Is a fax number instance set?
91 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
93 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
95 // Set updated timestamp
96 faxNumber.setPhoneEntryCreated(new GregorianCalendar());
99 // Is a mobile number instance set?
100 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
102 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
104 // Set updated timestamp
105 mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
109 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: EXIT!", this.getClass().getSimpleName())); //NOI18N
113 * Returns a managed instance from given mobile number
115 * @param mobileNumber Mobile number
116 * @param fetchedNumber Found mobile number in database
118 * @return Managed instance
120 protected DialableMobileNumber getManaged (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
122 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: mobileNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), mobileNumber, fetchedNumber)); //NOI18N
125 if (null == mobileNumber) {
127 throw new NullPointerException("mobileNumber is null"); //NOI18N
128 } else if (null == fetchedNumber) {
130 throw new NullPointerException("fetchedNumber is null"); //NOI18N
131 } else if (fetchedNumber.getPhoneId() == null) {
133 throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
137 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
140 DialableMobileNumber managedNumber = null;
142 // Is there a difference?
143 if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
145 managedNumber = this.getEntityManager().merge(fetchedNumber);
149 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
152 return managedNumber;
156 * Returns a managed instance from given land-line number
158 * @param landLineNumber Land-line number
159 * @param fetchedNumber Found land-line number in database
161 * @return Managed instance
163 protected DialableLandLineNumber getManaged (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
165 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: landLineNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), landLineNumber, fetchedNumber)); //NOI18N
168 if (null == landLineNumber) {
170 throw new NullPointerException("landLineNumber is null"); //NOI18N
171 } else if (null == fetchedNumber) {
173 throw new NullPointerException("fetchedNumber is null"); //NOI18N
174 } else if (fetchedNumber.getPhoneId() == null) {
176 throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
180 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
183 DialableLandLineNumber managedNumber = null;
185 // Is there a difference?
186 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
188 managedNumber = this.getEntityManager().merge(fetchedNumber);
192 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
195 return managedNumber;
199 * Returns a managed instance from given fax number
201 * @param faxNumber Fax number
202 * @param fetchedNumber Found fax number in database
204 * @return Managed instance
206 protected DialableFaxNumber getManaged (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
208 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: faxNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), faxNumber, fetchedNumber)); //NOI18N
211 if (null == faxNumber) {
213 throw new NullPointerException("faxNumber is null"); //NOI18N
214 } else if (null == fetchedNumber) {
216 throw new NullPointerException("fetchedNumber is null"); //NOI18N
217 } else if (fetchedNumber.getPhoneId() == null) {
219 throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
223 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
226 DialableFaxNumber managedNumber = null;
228 // Is there a difference?
229 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
231 managedNumber = this.getEntityManager().merge(fetchedNumber);
235 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
238 return managedNumber;
242 * Merges given (detached) contact's data
244 * @param detachedContact Contact instance to merge
246 * @return Detached contact instance
248 protected Contact mergeContactData (final Contact detachedContact) {
250 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
252 // The contact instance must be valid
253 if (null == detachedContact) {
255 throw new NullPointerException("detachedContact is null"); //NOI18N
256 } else if (detachedContact.getContactId() == null) {
258 throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
259 } else if (detachedContact.getContactId() < 1) {
261 throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
264 // Get contact from it and find it
265 Contact foundContact = this.getEntityManager().getReference(detachedContact.getClass(), detachedContact.getContactId());
268 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N
271 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateUserPersonalData: foundContact.contactId={1}", this.getClass().getSimpleName(), foundContact.getContactId())); //NOI18N
273 // Merge contact instance
274 Contact managedContact = this.getEntityManager().merge(foundContact);
276 // Set updated timestamp
277 managedContact.setContactUpdated(new GregorianCalendar());
280 managedContact.copyAll(detachedContact);
283 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
285 // Return detached contact
286 return managedContact;
290 * Merges given (detached) contact's cellphone, land-line and fax numbers
292 * @param detachedContact Detached contact instance
294 protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
296 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactsMobileLandLineFaxNumbers: detachedContact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
298 // The contact instance must be valid
299 if (null == detachedContact) {
301 throw new NullPointerException("detachedContact is null"); //NOI18N
302 } else if (detachedContact.getContactId() == null) {
304 throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
305 } else if (detachedContact.getContactId() < 1) {
307 throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
311 DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
312 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
313 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
315 // Is there a cellphone instance set?
316 if (cellphone instanceof DialableMobileNumber) {
318 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
321 DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
324 assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
327 foundMobile.copyAll(detachedContact.getContactMobileNumber());
330 detachedContact.setContactMobileNumber(foundMobile);
333 // Is there a fax instance set?
334 if (fax instanceof DialableFaxNumber) {
336 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
339 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
342 foundFax.copyAll(detachedContact.getContactFaxNumber());
345 detachedContact.setContactFaxNumber(foundFax);
348 // Is there a fax instance set?
349 if (landLine instanceof DialableLandLineNumber) {
351 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
354 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
357 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
360 foundLandLine.copyAll(detachedContact.getContactLandLineNumber());
363 detachedContact.setContactLandLineNumber(foundLandLine);
367 this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
371 * Sends an email with given subject line, template name to given recipient
374 * @param subjectLine Subject line
375 * @param templateName Template name
376 * @param emailAddress Recipient's email address
377 * @param user User instance
378 * @param baseUrl Base URL
380 protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
382 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
385 if (null == subjectLine) {
387 throw new NullPointerException("subjectLine is null"); //NOI18N
388 } else if (subjectLine.isEmpty()) {
390 throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
391 } else if (null == templateName) {
393 throw new NullPointerException("templateName is null"); //NOI18N
394 } else if (templateName.isEmpty()) {
396 throw new IllegalArgumentException("templateName is empty"); //NOI18N
397 } else if (null == emailAddress) {
399 throw new NullPointerException("emailAddress is null"); //NOI18N
402 // Prepare mail wrapper
403 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
406 Properties variables = UserUtils.getAllUserFields(user);
409 variables.put("baseUrl", baseUrl); //NOI18N
412 // @TODO Language from message bundle
413 emailWrapper.setRecipient(emailAddress);
414 emailWrapper.setLocale(user.getUserLocale());
415 emailWrapper.setSubjectLine(subjectLine);
416 emailWrapper.setTemplateName(templateName);
417 emailWrapper.setTemplateVariables(variables);
420 // Send out email change
421 ObjectMessage message = this.getSession().createObjectMessage();
422 message.setObject(emailWrapper);
425 this.sendMessage(message);
426 } catch (final JMSException ex) {
428 throw new EJBException(ex);
432 this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
436 * Updates all contact's phone instances from other contact, both contacts
437 * should be the same.
439 * @param contact Contact to set instances
440 * @param other Other contact to get instances from
442 protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
444 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
446 // Both must be the same and not null
447 if (null == contact) {
449 throw new NullPointerException("contact is null"); //NOI18N
450 } else if (null == other) {
452 throw new NullPointerException("other is null"); //NOI18N
453 } else if (!Objects.equals(contact, other)) {
454 // Not same instances
455 throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
459 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
461 // Is other cellphone not set?
462 if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
464 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying cellphone entry ...", this.getClass().getSimpleName())); //NOI18N
466 // Is the fax number set?
467 if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
468 // Copy cellphone number
469 contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
472 contact.setContactMobileNumber(null);
477 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
479 // Is other cellphone not set?
480 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
482 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
484 // Is the land-line number set?
485 if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
486 // Copy land-line number
487 contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
490 contact.setContactLandLineNumber(null);
495 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
497 // Is other cellphone not set?
498 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
500 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
502 // Is the fax number set?
503 if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
505 contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
508 contact.setContactFaxNumber(null);
513 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
517 * Updates all contact's phone entry's updated timestamps
519 * @param contact Contact instance to update
520 * @param isMobileUnlinked Whether a cellphone entry has been unlinked in
522 * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
524 * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
527 protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
529 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
531 // The contact instance must be valid
532 if (null == contact) {
534 throw new NullPointerException("contact is null"); //NOI18N
535 } else if (contact.getContactId() == null) {
537 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
538 } else if (contact.getContactId() < 1) {
540 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
543 // Get all phone instances
544 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
545 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
546 DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
548 // Flags and instances must be constistent
549 if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
551 throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
552 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
554 throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
555 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
557 throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
560 // Is a phone number instance set?
561 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
563 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
565 // Set updated timestamp
566 landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
569 // Is a fax number instance set?
570 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
572 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
574 // Set updated timestamp
575 faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
578 // Is a mobile number instance set?
579 if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
581 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
583 // Set updated timestamp
584 mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
588 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N