]> git.mxchange.org Git - pizzaservice-core.git/commitdiff
added method sendEmail() which is a wrapper for sending mails
authorRoland Häder <roland@mxchange.org>
Tue, 17 May 2016 15:15:50 +0000 (17:15 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 20 May 2016 19:00:56 +0000 (21:00 +0200)
src/org/mxchange/pizzaaplication/database/BasePizzaDatabaseBean.java

index 0534ede404a4c247eecd90e7315c2a78ecc28f9d..df190334ab373e05d37c41c569a8f496f638a0f8 100644 (file)
@@ -18,13 +18,25 @@ package org.mxchange.pizzaaplication.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 BasePizzaDatabaseBean extends BaseDatabaseBean {
                this.getLoggerBeanLocal().logTrace("mergeContactsCellphoneLandLineFaxNumbers: EXIT!"); //NOI18N
        }
 
+       /**
+        * Sends an email with given subject line, template name to given recipient
+        * and user data
+        * <p>
+        * @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.