]> git.mxchange.org Git - jjobs-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>
Wed, 25 May 2016 19:00:20 +0000 (21:00 +0200)
src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java

index 01b117fabb9f718051ef9b3f101a43649960bb8e..b45b6cb4870a61ba8f2f334d241925a4d7f82c68 100644 (file)
@@ -18,13 +18,25 @@ package org.mxchange.jjobs.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 BaseJobsDatabaseBean 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.