9 Replies Latest reply on Apr 21, 2010 1:58 PM by Alejandro Guizar

    Mail template for custom MailProducer

    Kevin Moodley Newbie

      How do I get the mail template from the jpdl set on my custom MailProducer?

      This is what I have so far:

       

      public class CustomMailProducer extends MailProducerImpl {

      ..

          public Collection<Message> produce(Execution execution) {

           MailTemplate template = super.getTemplate();

           ...

          }

       

      In the JPDL I have :

       

      <task assignee="kmoodley" g="210,250,92,52" name="User Review">
              <description>User Review Task Description</description>

              <notification  class="com.kevinmoodley.bpm.mail.CustomMailProducer" template="test-template"/>
              </notification>
              <transition g="-42,-18" name="CANCEL" to="end1"/>
              <transition g="-42,-18" name="RESTART" to="end2"/>
      </task>

       

      Here is the jbpm.mail.templates.xml

       

      <?xml version="1.0" encoding="UTF-8"?>
      <jbpm-configuration>
        <process-engine-context>
           <mail-template name="test_template">
                <to addresses="kevinmoodley@gmail.com" />
                <subject>Test Email via Notification</subject>
                <text>
                      This is a test email sent by the notification tag in a Human Task             
                </text>
           </mail-template> 
        </process-engine-context>
      </jbpm-configuration>

       

       

      But super.getTemplate() always returns null.
      Note: same jbpm.mail.templates.xml works fine when the custom MailProducer is not used.

       

      What else do I need to do to get this CustomMailProducer to pick up the template?

       

      Thanks

      Kevin

        • 1. Re: Mail template for custom MailProducer
          HuiSheng Xu Master

          Hi kevin,

            You could get MailTemplate using EnvironmentImpl. Like this:

           

           

          MailTemplateRegistry templateRegistry = EnvironmentImpl
             .getFromCurrent(MailTemplateRegistry.class);
          
          MailTemplate template = templateRegistry.getTemplate(
             "test-template");
          
          • 2. Re: Mail template for custom MailProducer
            Kevin Moodley Newbie

            Thanks Huishengrebody,

             

            Your technique to get the template worked but I was not sure where to put this code. For now, I  tested it by putting your code in the CustomMailProducer default constructor. I could move it into the produce() method, but I was wondering if there was a dynamic way to get the template name - maybe from the Execution?

             

            How does the default MailProducerImpl get the template name?

             

            Thanks

            Kevin

            • 3. Re: Mail template for custom MailProducer
              HuiSheng Xu Master

              Hi Kevin,

               

                 The codes I showed you comes from org.jbpm.jpdl.internal.xml.JpdlParser.java.  The PVM will parse mail-template and register them to MailTemplateRegistry, so we could find MailTemplate by template name from MailTemplateRegistry.  I cannot find a way to inject MailTemplate object by only using the XML configuration.  So I am afraid that we have to write these codes to get MailTemplate.

               

                And, here is development forum, you really should post this content to user forum. Thanks.

              • 4. Re: Mail template for custom MailProducer
                HuiSheng Xu Master

                Hi Kevin,

                  Fortunately, I saw a same issue in the JIRA.  You could add comment on it.

                • 5. Re: Mail template for custom MailProducer
                  Alejandro Guizar Master

                  Here is how you can specify a named template for a custom mail producer:

                  <mail name='send mail' class='org.example.CustomMailProducer'>
                    <field name='templateName'><string value='rectify-template'/></field>
                    <transition to='end' />
                  </mail>
                  

                  Here is the custom mail producer code:

                  public class CustomMailProducer extends MailProducerImpl {
                  
                    private String templateName;
                  
                    public Collection<Message> produce(Execution execution) {
                      MailTemplateRegistry templateRegistry = EnvironmentImpl
                        .getFromCurrent(MailTemplateRegistry.class);
                      setTemplate(templateRegistry.getTemplate(templateName));
                      return super.produce(execution);
                    }
                  }
                  

                  It should be possible to inject a reference to the template registry into the bean, but it is not currently possible due to the way the custom mail producer is parsed. I will fix this in JBPM-2844.

                  • 6. Re: Mail template for custom MailProducer
                    Alejandro Guizar Master

                    Huisheng, is JBPM-2844 the issue you refer to?

                    • 7. Re: Mail template for custom MailProducer
                      HuiSheng Xu Master

                      Hi Alejandro,

                        Yes, it is the JBPM-2844. I had seen you move it the jBPM-4.4. 

                       

                        But the priority of this issue is not very high.  I want to notice that there is a serious issue https://jira.jboss.org/jira/browse/JBPM-2794 that many people has met.

                       

                        Please reference http://community.jboss.org/thread/150965 and http://community.jboss.org/thread/150555.  If you have enough time, please review this, thank your very much.

                      • 8. Re: Mail template for custom MailProducer
                        Kevin Moodley Newbie

                        Hi,

                         

                        Thanks for looking into this.

                         

                        Will the field tag work inside a notification tag in a task?

                         

                            <task assignee="kmoodley" g="210,250,92,52" name="User Review">

                               <notification class="com.kevinmoodley.bpm.mail.CustomMailProducer">
                                    <field name="templateName"> <string value="test-template"/></field>
                               </notification>

                               <transition g="-42,-18" name="CANCEL" to="end1"/>
                                <transition g="-42,-18" name="RESTART" to="end2"/>
                            </task>

                         

                        I know that the xsd does not support it, but then again the class attribute in <notification> is not is the xsd either.

                         

                        By the way, most user only create create a custom MailProducer to bypass the requirement of the default MailProducerImpl that mail recepients  need to exist in the Indentity tables.

                         

                        ~~~~~~

                        List<User> users = identitySession.findUsersById(userIds);

                        ~~~~~

                         

                        Don't quite understand that, especially if I want the workflow to trigger an email to an external customer.

                         

                        Anyway, this is not a serious issue. Moving back to the User forum now.

                        Thanks

                        • 9. Re: Mail template for custom MailProducer
                          Alejandro Guizar Master

                          By the way, most user only create create a custom MailProducer to bypass the requirement of the default MailProducerImpl that mail recepients  need to exist in the Indentity tables.

                           

                          ~~~~~~

                          List<User> users = identitySession.findUsersById(userIds);

                          ~~~~~

                           

                          Don't quite understand that, especially if I want the workflow to trigger an email to an external customer.

                          The code you indicate is guarded by a condition:

                          String userList = fromTemplate.getUsers();
                          if (userList != null) {
                            String[] userIds = tokenizeActors(userList, execution);
                            List<User> users = identitySession.findUsersById(userIds);
                            email.addFrom(resolveAddresses(users, addressResolver));
                          }
                          

                          If you don't need users and groups from the identity tables, your template should only specify the addresses attribute to enumerate recipients. Avoiding the users and groups attributes also prevents access to the identity session. However, since you have looked at the source code you probably knew this already - am I missing something?