From: Roland Häder Date: Tue, 17 May 2016 15:15:50 +0000 (+0200) Subject: added method sendEmail() which is a wrapper for sending mails X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=53304f982e8d02b92baf60361bcf6c8e5dcda025;p=addressbook-core.git added method sendEmail() which is a wrapper for sending mails --- diff --git a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java b/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java index 9be914e..b970892 100644 --- a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java +++ b/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java @@ -18,13 +18,25 @@ package org.mxchange.addressbook.database; import java.text.MessageFormat; import java.util.GregorianCalendar; +import java.util.Locale; import java.util.Objects; +import java.util.Properties; +import javax.ejb.EJBException; +import javax.jms.JMSException; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; +import javax.mail.Address; import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper; +import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; import org.mxchange.jphone.utils.PhoneUtils; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserUtils; /** * A helper class for beans that access the database. @@ -390,6 +402,66 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { this.getLoggerBeanLocal().logTrace("mergeContactsCellphoneLandLineFaxNumbers: EXIT!"); //NOI18N } + /** + * Sends an email with given subject line, template name to given recipient + * and user data + *

+ * @param subjectLine Subject line + * @param templateName Template name + * @param emailAddress Recipient's email address + * @param user User instance + * @param session JMS Session + * @param messageProducer Message producer + */ + protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final Session session, final MessageProducer messageProducer) { + // Trace message + 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)); + + // All should be set + if (null == subjectLine) { + // Throw NPE + throw new NullPointerException("subjectLine is null"); + } else if (subjectLine.isEmpty()) { + // No subject line + throw new IllegalArgumentException("subjectLine is empty"); + } else if (null == templateName) { + // Throw NPE + throw new NullPointerException("templateName is null"); + } else if (templateName.isEmpty()) { + // No template name + throw new IllegalArgumentException("templateName is empty"); + } else if (null == emailAddress) { + // Throw NPE + throw new NullPointerException("emailAddress is null"); + } + + // Prepare mail wrapper + WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper(); + + // Set all values + Properties variables = UserUtils.getAllUserFields(user); + + // Set all + // @TODO Get locale from user + language from message bundle + emailWrapper.setRecipient(emailAddress); + emailWrapper.setLocale(Locale.GERMAN); + emailWrapper.setSubjectLine(subjectLine); + emailWrapper.setTemplateName(templateName); //NOI18N + emailWrapper.setTemplateVariables(variables); + + try { + // Send out email change + ObjectMessage message = session.createObjectMessage(); + message.setObject(emailWrapper); + + // Send message + this.sendMessage(message, messageProducer); + } catch (final JMSException ex) { + // Throw again + throw new EJBException(ex); + } + } + /** * Updates all contact's phone instances from other contact, both contacts * should be the same.