Class NotificationServices


  • public class NotificationServices
    extends java.lang.Object
    Provides generic services related to preparing and delivering notifications via email.

    To use the NotificationService, a message specific service should be defined for a particular Freemarker Template mapping the required fields of the template to the required attributes of the service.

    This service definition should extend the sendNotificationInterface or the prepareNotificationInterface service interface and simply invoke the associated method defined in this class.

     
     <service name="sendPoPickupNotification" engine="java"
             location="org.apache.ofbiz.content.email.NotificationServices"
             invoke="sendNotification">
         <description>Sends notification based on a message template</description>
         <implements service="sendNotificationInterface"/>
         <attribute name="orderId" type="String" mode="IN" optional="false"/>
     </service>
     
     

    An optional parameter available to all message templates is baseUrl which can either be specified when the service is invoked or let the NotificationService attempt to resolve it as best it can, see setBaseUrl(Map) for details on how this is achieved.

    The following example shows what a simple notification message template, associated with the above service, might contain:
         Please use the following link to schedule a delivery date:
         ${baseUrl}/ordermgr/control/schedulepo?orderId=${orderId}"
     

    The template file must be found on the classpath at runtime and match the "templateName" field passed to the service when it is invoked.

    For complex messages with a large number of dynamic fields, it may be wise to implement a custom service that takes one or two parameters that can be used to resolve the rest of the required fields and then pass them to the prepareNotification(DispatchContext, Map) or sendNotification(DispatchContext, Map) methods directly to generate or generate and send the notification respectively.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String module  
      static java.lang.String resource  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Map<java.lang.String,​java.lang.Object> prepareNotification​(DispatchContext ctx, java.util.Map<java.lang.String,​? extends java.lang.Object> context)
      This will process the associated notification template definition with all the fields contained in the given context and generate the message body of the notification.
      static java.util.Map<java.lang.String,​java.lang.Object> sendNotification​(DispatchContext ctx, java.util.Map<java.lang.String,​? extends java.lang.Object> context)
      This will use the prepareNotification(DispatchContext, Map) method to generate the body of the notification message to send and then deliver it via the "sendMail" service.
      static void setBaseUrl​(Delegator delegator, java.lang.String webSiteId, java.util.Map<java.lang.String,​java.lang.Object> context)
      The expectation is that a lot of notification messages will include a link back to one or more pages in the system, which will require knowledge of the base URL to extrapolate.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • module

        public static final java.lang.String module
    • Constructor Detail

      • NotificationServices

        public NotificationServices()
    • Method Detail

      • sendNotification

        public static java.util.Map<java.lang.String,​java.lang.Object> sendNotification​(DispatchContext ctx,
                                                                                              java.util.Map<java.lang.String,​? extends java.lang.Object> context)
        This will use the prepareNotification(DispatchContext, Map) method to generate the body of the notification message to send and then deliver it via the "sendMail" service.

        If the "body" parameter is already specified, the message body generation phase will be skipped and the notification will be sent with the specified body instead. This can be used to combine both service calls in a decoupled manner if other steps are required between generating the message body and sending the notification.

        Parameters:
        ctx - The dispatching context of the service
        context - The map containing all the fields associated with the sevice
        Returns:
        A Map with the service response messages in it
      • prepareNotification

        public static java.util.Map<java.lang.String,​java.lang.Object> prepareNotification​(DispatchContext ctx,
                                                                                                 java.util.Map<java.lang.String,​? extends java.lang.Object> context)
        This will process the associated notification template definition with all the fields contained in the given context and generate the message body of the notification.

        The result returned will contain the appropriate response messages indicating success or failure and the OUT parameter, "body" containing the generated message.

        Parameters:
        ctx - The dispatching context of the service
        context - The map containing all the fields associated with the sevice
        Returns:
        A new Map indicating success or error containing the body generated from the template and the input parameters.
      • setBaseUrl

        public static void setBaseUrl​(Delegator delegator,
                                      java.lang.String webSiteId,
                                      java.util.Map<java.lang.String,​java.lang.Object> context)
        The expectation is that a lot of notification messages will include a link back to one or more pages in the system, which will require knowledge of the base URL to extrapolate. This method will ensure that the baseUrl field is set in the given context.

        If it has been specified a default baseUrl will be set using a best effort approach. If it is specified in the url.properties configuration files of the system, that will be used, otherwise it will attempt resolve the fully qualified local host name.

        Note: I thought it might be useful to have some dynamic way of extending the default properties provided by the NotificationService, such as the baseUrl, perhaps using the standard ResourceBundle java approach so that both classes and static files may be invoked.

        Parameters:
        context - The context to check and, if necessary, set the baseUrl.