2 * Copyright (C) 2016 Roland Haeder
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.Locale;
22 import java.util.Objects;
23 import java.util.Properties;
24 import javax.ejb.EJBException;
25 import javax.jms.JMSException;
26 import javax.jms.MessageProducer;
27 import javax.jms.ObjectMessage;
28 import javax.jms.Session;
29 import javax.mail.Address;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jcoreee.database.BaseDatabaseBean;
32 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
33 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
34 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
35 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
36 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
37 import org.mxchange.jphone.utils.PhoneUtils;
38 import org.mxchange.jusercore.model.user.User;
39 import org.mxchange.jusercore.model.user.UserUtils;
42 * A helper class for beans that access the database.
44 * @author Roland Haeder<roland@mxchange.org>
46 public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
51 private static final long serialVersionUID = 12_895_410_275_811_963L;
54 * Protected constructor
56 protected BaseAddressbookDatabaseBean () {
57 // Call super constructor
62 * Updates all contacts's phone entry's created timestamps
64 * @param contact Contact instance to update
66 protected void setAllContactPhoneEntriesCreated (final Contact contact) {
68 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesCreated: contact={0} - CALLED!", contact)); //NOI18N
70 // The contact instance must be valid
71 if (null == contact) {
73 throw new NullPointerException("contact is null"); //NOI18N
76 // Get all phone instances
77 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
78 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
79 DialableCellphoneNumber cellphoneNumber = contact.getContactCellphoneNumber();
82 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntriesCreated: landLineNumber={0},faxNumber={1},cellphoneNumber={2}", landLineNumber, faxNumber, cellphoneNumber)); //NOI18N
84 // Is a phone number instance set?
85 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
87 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //NOI18N
89 // Set updated timestamp
90 landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
93 // Is a fax number instance set?
94 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
96 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ..."); //NOI18N
98 // Set updated timestamp
99 faxNumber.setPhoneEntryCreated(new GregorianCalendar());
102 // Is a mobile number instance set?
103 if ((cellphoneNumber instanceof DialableCellphoneNumber) && (cellphoneNumber.getPhoneId() == null)) {
105 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ..."); //NOI18N
107 // Set updated timestamp
108 cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar());
112 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N
116 * Returnes a detached instance from given cellphone instance
118 * @param cellphoneNumber Cellphone instance
119 * @param fetchedNumber Found cellphone number in database
121 * @return Detached instance
123 protected DialableCellphoneNumber getDetached (final DialableCellphoneNumber cellphoneNumber, final DialableCellphoneNumber fetchedNumber) {
125 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: cellphoneNumber={0},fetchedNumber={1} - CALLED!", cellphoneNumber, fetchedNumber)); //NOI18N
128 if (null == cellphoneNumber) {
130 throw new NullPointerException("cellphoneNumber is null"); //NOI18N
131 } else if (fetchedNumber.getPhoneId() == null) {
133 throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
137 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
139 // Init query instance
140 DialableCellphoneNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
143 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
146 DialableCellphoneNumber detachedNumber = null;
148 // Is there a difference?
149 if (!PhoneUtils.isSameCellphoneNumber(cellphoneNumber, fetchedNumber)) {
151 detachedNumber = this.getEntityManager().merge(foundNumber);
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
160 return detachedNumber;
164 * Returnes a detached instance from given land-line instance
166 * @param landLineNumber Land-line instance
167 * @param fetchedNumber Found land-line number in database
169 * @return Detached instance
171 protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
173 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
176 if (null == landLineNumber) {
178 throw new NullPointerException("landLineNumber is null"); //NOI18N
179 } else if (fetchedNumber.getPhoneId() == null) {
181 throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
185 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
187 // Init query instance
188 DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
191 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
194 DialableLandLineNumber detachedNumber = null;
196 // Is there a difference?
197 if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
199 detachedNumber = this.getEntityManager().merge(foundNumber);
203 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
206 return detachedNumber;
210 * Returnes a detached instance from given fax instance
212 * @param faxNumber Fax instance
213 * @param fetchedNumber Found fax number in database
215 * @return Detached instance
217 protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
219 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
222 if (null == faxNumber) {
224 throw new NullPointerException("faxNumber is null"); //NOI18N
225 } else if (fetchedNumber.getPhoneId() == null) {
227 throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
231 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
233 // Init query instance
234 DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
237 this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
240 DialableFaxNumber detachedNumber = null;
242 // Is there a difference?
243 if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
245 detachedNumber = this.getEntityManager().merge(foundNumber);
249 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
252 return detachedNumber;
256 * Merges given contact's data
258 * @param contact Contact instance to merge
260 * @return Detached contact instance
262 protected Contact mergeContactData (final Contact contact) {
264 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", contact)); //NOI18N
266 // The contact instance must be valid
267 if (null == contact) {
269 throw new NullPointerException("contact is null"); //NOI18N
270 } else if (contact.getContactId() == null) {
272 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
273 } else if (contact.getContactId() < 1) {
275 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
278 // Set updated timestamp
279 contact.setContactUpdated(new GregorianCalendar());
281 // Get contact from it and find it
282 Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId());
285 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N
288 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
290 // Merge contact instance
291 Contact detachedContact = this.getEntityManager().merge(foundContact);
294 detachedContact.copyAll(contact);
297 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
299 // Return detached contact
300 return detachedContact;
304 * Merges given (detached) contact's cellphone, land-line and fax numbers
306 * @param detachedContact Detached contact instance
308 protected void mergeContactsCellphoneLandLineFaxNumbers (final Contact detachedContact) {
310 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactsCellphoneLandLineFaxNumbers: detachedContact={0} - CALLED!", detachedContact)); //NOI18N
312 // The contact instance must be valid
313 if (null == detachedContact) {
315 throw new NullPointerException("detachedContact is null"); //NOI18N
316 } else if (detachedContact.getContactId() == null) {
318 throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
319 } else if (detachedContact.getContactId() < 1) {
321 throw new IllegalStateException(MessageFormat.format("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N
325 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
326 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
327 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
329 // Is there a cellphone instance set?
330 if (cellphone instanceof DialableCellphoneNumber) {
332 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
335 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
338 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
340 // Then merge it, too
341 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
344 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
347 detachedCellphone.copyAll(detachedContact.getContactCellphoneNumber());
350 detachedContact.setContactCellphoneNumber(detachedCellphone);
353 // Is there a fax instance set?
354 if (fax instanceof DialableFaxNumber) {
356 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
359 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
362 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
364 // Then merge it, too
365 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
368 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
371 detachedFax.copyAll(detachedContact.getContactFaxNumber());
374 detachedContact.setContactFaxNumber(detachedFax);
377 // Is there a fax instance set?
378 if (landLine instanceof DialableLandLineNumber) {
380 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
383 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
386 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
388 // Then merge it, too
389 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
392 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
395 detachedLandLine.copyAll(detachedContact.getContactLandLineNumber());
398 detachedContact.setContactLandLineNumber(detachedLandLine);
402 this.getLoggerBeanLocal().logTrace("mergeContactsCellphoneLandLineFaxNumbers: EXIT!"); //NOI18N
406 * Sends an email with given subject line, template name to given recipient
409 * @param subjectLine Subject line
410 * @param templateName Template name
411 * @param emailAddress Recipient's email address
412 * @param user User instance
413 * @param session JMS Session
414 * @param messageProducer Message producer
416 protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final Session session, final MessageProducer messageProducer) {
418 this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={3},user={4},session={5},messageProducer={6} - CALLED!", subjectLine, templateName, emailAddress, user, session, messageProducer));
421 if (null == subjectLine) {
423 throw new NullPointerException("subjectLine is null");
424 } else if (subjectLine.isEmpty()) {
426 throw new IllegalArgumentException("subjectLine is empty");
427 } else if (null == templateName) {
429 throw new NullPointerException("templateName is null");
430 } else if (templateName.isEmpty()) {
432 throw new IllegalArgumentException("templateName is empty");
433 } else if (null == emailAddress) {
435 throw new NullPointerException("emailAddress is null");
438 // Prepare mail wrapper
439 WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
442 Properties variables = UserUtils.getAllUserFields(user);
445 // @TODO Get locale from user + language from message bundle
446 emailWrapper.setRecipient(emailAddress);
447 emailWrapper.setLocale(Locale.GERMAN);
448 emailWrapper.setSubjectLine(subjectLine);
449 emailWrapper.setTemplateName(templateName); //NOI18N
450 emailWrapper.setTemplateVariables(variables);
453 // Send out email change
454 ObjectMessage message = session.createObjectMessage();
455 message.setObject(emailWrapper);
458 this.sendMessage(message, messageProducer);
459 } catch (final JMSException ex) {
461 throw new EJBException(ex);
466 * Updates all contact's phone instances from other contact, both contacts
467 * should be the same.
469 * @param contact Contact to set instances
470 * @param other Other contact to get instances from
472 protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
474 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntries: contact={0},other={1} - CALLED!", contact, other)); //NOI18N
476 // Both must be the same and not null
477 if (null == contact) {
479 throw new NullPointerException("contact is null"); //NOI18N
480 } else if (null == other) {
482 throw new NullPointerException("other is null"); //NOI18N
483 } else if (!Objects.equals(contact, other)) {
484 // Not same instances
485 throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
489 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactCellphoneNumber={0}", other.getContactCellphoneNumber())); //NOI18N
491 // Is other cellphone not set?
492 if ((other.getContactCellphoneNumber() == null) || (PhoneUtils.isSameCellphoneNumber(contact.getContactCellphoneNumber(), other.getContactCellphoneNumber()))) {
494 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N
496 // Is the fax number set?
497 if (other.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
498 // Copy cellphone number
499 contact.setContactCellphoneNumber(this.getDetached(other.getContactCellphoneNumber(), contact.getContactCellphoneNumber()));
502 contact.setContactCellphoneNumber(null);
507 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
509 // Is other cellphone not set?
510 if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
512 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying land-line entry ..."); //NOI18N
514 // Is the land-line number set?
515 if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
516 // Copy land-line number
517 contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
520 contact.setContactLandLineNumber(null);
525 this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactFaxNumber={0}", other.getContactFaxNumber())); //NOI18N
527 // Is other cellphone not set?
528 if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
530 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying fax entry ..."); //NOI18N
532 // Is the fax number set?
533 if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
535 contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber()));
538 contact.setContactFaxNumber(null);
543 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntries: EXIT!"); //NOI18N
547 * Updates all contacts's phone entry's updated timestamps
549 * @param contact Contact instance to update
550 * @param isCellphoneUnlinked Whether a cellphone entry has been unlinked in
552 * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
554 * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
557 protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
559 this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesUpdated: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
561 // The contact instance must be valid
562 if (null == contact) {
564 throw new NullPointerException("contact is null"); //NOI18N
565 } else if (contact.getContactId() == null) {
567 throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
568 } else if (contact.getContactId() < 1) {
570 throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
573 // Get all phone instances
574 DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
575 DialableFaxNumber faxNumber = contact.getContactFaxNumber();
576 DialableCellphoneNumber cellphoneNumber = contact.getContactCellphoneNumber();
578 // Flags and instances must be constistent
579 if (isCellphoneUnlinked && cellphoneNumber instanceof DialableCellphoneNumber) {
581 throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber is set."); //NOI18N
582 } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
584 throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
585 } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
587 throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
590 // Is a phone number instance set?
591 if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
593 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ..."); //NOI18N
595 // Set updated timestamp
596 landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
599 // Is a fax number instance set?
600 if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
602 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ..."); //NOI18N
604 // Set updated timestamp
605 faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
608 // Is a mobile number instance set?
609 if ((cellphoneNumber instanceof DialableCellphoneNumber) && (cellphoneNumber.getPhoneId() instanceof Long) && (cellphoneNumber.getPhoneId() > 0)) {
611 this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N
613 // Set updated timestamp
614 cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar());
618 this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N