[midPoint] How to seperate content with Velocity EmailTemplates

Patrik Sidler patrik.sidler at itconcepts.ch
Thu Jun 22 14:29:04 CEST 2023


Hi Community,

I have to implement several Email Templates with Velocity on midPoint 4.7.1.
One of this Email Templates is used to inform the Manager about Changes on Identity Attributes from one of his Team Members.

The following Handler is used in SystemConfiguration:

   <handler>
       <name>Simple User Notifier - Modify User</name>
       <description>Inform Manager about Attribute Change on Team Member</description>
       <operation>modify</operation>
       <simpleUserNotifier id="329">
           <name>Inform manager when user account changes</name>
           <expressionFilter>
               <script>
                   <code>import java.util.logging.Logger
                       import com.evolveum.midpoint.xml.ns._public.common.common_3.EventOperationType
                       import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType
                       import com.evolveum.midpoint.notifications.api.events.ModelEvent
                       import com.evolveum.midpoint.prism.delta.ObjectDelta
                       import com.evolveum.midpoint.prism.path.ItemPath
                       import com.evolveum.midpoint.prism.PrismValue

                       Logger logger = Logger.getLogger("Expression Filter")

                       if (event instanceof ModelEvent
                               && event.getFocusContext().getObjectTypeClass().getName().equals(UserType.getName())
                               && !event.isOperationType(EventOperationType.DELETE)){
                           //logger.info("event: " + event)
                           for (ObjectDelta delta : event.getFocusDeltas()) {
                               if (delta.isModify()) {
                                   //logger.info("delta: " + delta)
                                   for (ItemPath paths : delta.getModifiedItems()) {
                                       || paths.toString().equalsIgnoreCase("extension/idDa")
                                       || paths.toString().equalsIgnoreCase("extension/Da")
                                       || paths.toString().equalsIgnoreCase("extension/idStructuralUnit")
                                       || paths.toString().equalsIgnoreCase("extension/StructuralUnit")
                                       || paths.toString().equalsIgnoreCase("extension/idFonction")
                                       || paths.toString().equalsIgnoreCase("extension/Fonction")
                                       || paths.toString().equalsIgnoreCase("extension/mobile")
                                       || paths.toString().equalsIgnoreCase("emailAddress")
                                       || paths.toString().equalsIgnoreCase("extension/otherEmail")
                                       || paths.toString().equalsIgnoreCase("telephoneNumber")) {
                                           //logger.info("Relevant Attribute change: " + paths.toString() )
                                           return true
                                       }
                                   }
                               }
                           }
                       }
                       return false</code>
               </script>
           </expressionFilter>
           <recipientExpression>
               <script>
                   <code>import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;

String managerEmail = ""

String sIdStructuralUnit = basic.getExtensionPropertyValue(requestee, "idStructuralUnit")

if(sIdStructuralUnit != null && !basic.isEmpty(sIdStructuralUnit)) {
    OrgType requesteeOrg = midpoint.getOrgByName(sIdStructuralUnit)
    if(requesteeOrg != null) {
        managers = midpoint.getManagersOfOrg(requesteeOrg.getOid())
        if (managers != null && !basic.isEmpty(managers)) {
            for(manager in managers) {
                managerOid = midpoint.getUserByOid(manager.getOid())
                managerEmail += manager?.getEmailAddress()
            }
        } else {
            managerEmail = ""
        }
    }
}
if (!basic.isEmpty(managerEmail)) {
    return managerEmail
} else {
    return iam at mail.ch<mailto:iam at mail.ch>
}</code>
               </script>
           </recipientExpression>
           <messageTemplateRef oid="357c5817-c090-443d-be92-38d40854d251" relation="org:default" type="c:MessageTemplateType"/>
           <transport>mail</transport>
       </simpleUserNotifier>
   </handler>

The Email Template mentioned in the Handler:
<messageTemplate xmlns=http://midpoint.evolveum.com/xml/ns/public/common/common-3
                 xmlns:c=http://midpoint.evolveum.com/xml/ns/public/common/common-3
                 xmlns:icfs=http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3
                xmlns:org=http://midpoint.evolveum.com/xml/ns/public/common/org-3
                 xmlns:q=http://prism.evolveum.com/xml/ns/public/query-3
                 xmlns:ri=http://midpoint.evolveum.com/xml/ns/public/resource/instance-3
                 xmlns:t=http://prism.evolveum.com/xml/ns/public/types-3
                 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
                 oid="357c5817-c090-443d-be92-38d40854d251">
    <name>User Change Notification</name>
    <description>This message template is used, when an user has changes on one of his attributes</description>
    <defaultContent>
        <subjectExpression>
            <script>
                <language>http://midpoint.evolveum.com/xml/ns/public/expression/language#velocity</language>
                <code><![CDATA[
#if ($event.isSuccess())
[IAM] SUCCESS: User $event.getChangeType() operation succeeded for $event.requesteeDisplayName.toString() (${requestee.name})
#elseif ($event.isFailure())
[IAM] ERROR: User $event.getChangeType() operation failed for $event.requesteeDisplayName.toString() (${requestee.name})
#else
[IAM] IN PROGRESS: User $event.getChangeType() operation in progress for $event.requesteeDisplayName.toString() (${requestee.name})
#end
   ]]></code>
            </script>
        </subjectExpression>
        <bodyExpression>
            <script>
                <language>http://midpoint.evolveum.com/xml/ns/public/expression/language#velocity</language>
                <code>
                <![CDATA[
Dear Manager,
<br><br>
There were changes for one of your Team Members <b>$event.requesteeDisplayName.toString() (${requestee.name})</b>
<br><br>
The following changes were made:
<br>
$event.contentAsFormattedList
<br><br>
Thank you for your attention.<br>
Best regards,<br>
<br>
IAM Team<br>
<br>
Do no answer this Email
                    ]]>
                </code>
            </script>
        </bodyExpression>
        <contentType>text/html</contentType>
    </defaultContent>
</messageTemplate>


Does anyone know, hot to separate the key-value pairs from $event.contentAsFormattedList in Velocity?

Because now, the content looks wired:

******************************************
Dear Manager,

There were changes for one of your Team Members Beck Jessica (53803)

The following changes were made:
- Extension/Structural Unit: - REPLACE: Marketing - Extension/Employee Status: - REPLACE: active

Thank you for your attention.
Best regards,

IAM Team

Do no answer this Email
******************************************


I would like to have an output like:


******************************************
Dear Manager,

There were changes for one of your Team Members Beck Jessica (53803)

The following changes were made:

  *   Structural Unit: Marketing
  *   Employee Status: active

Thank you for your attention.
Best regards,

IAM Team

Do no answer this Email
******************************************

Thank you in advance for your help.

Best regards,
Patrik Sidler
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.evolveum.com/pipermail/midpoint/attachments/20230622/82349c54/attachment-0001.htm>


More information about the midPoint mailing list