Class NotificationServices

java.lang.Object
org.apache.ofbiz.common.email.NotificationServices

public class NotificationServices extends 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.

  • Constructor Details

    • NotificationServices

      public NotificationServices()
  • Method Details

    • setBaseUrl

      public static void setBaseUrl(Delegator delegator, String webSiteId, Map<String,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.