Class TemplateDataResolver

java.lang.Object
com.bonitasoft.processbuilder.extension.TemplateDataResolver

public final class TemplateDataResolver extends Object
Utility class for resolving template variables in notification messages.

This class provides methods to resolve standard template variables like recipient information (firstname, lastname, email) using the Bonita Identity API. It is designed to work with PBStringUtils.resolveTemplateVariables(String, BiFunction) method.

The resolver supports two variable formats:

  • {{dataName}} - Simple variable without prefix (refStep will be null)
  • {{refStep:dataName}} - Variable with step reference prefix

Usage Example (Groovy Script):


 // Create the base resolver for standard variables
 BiFunction<String, String, String> resolver = TemplateDataResolver.createResolver(
     identityAPI,
     recipientUserId,
     hostUrl,
     humanTaskId,
     // Custom resolver for BDM-specific data
     { refStep, dataName ->
         // Your custom BDM lookup logic here
         return myCustomValue
     }
 );

 // Resolve template variables
 String result = PBStringUtils.resolveTemplateVariables(template, resolver);
 
Since:
1.0
Author:
Bonitasoft
See Also:
  • Method Details

    • getUserFirstName

      public static Optional<String> getUserFirstName(org.bonitasoft.engine.api.IdentityAPI identityAPI, Long userId)
      Gets the first name of a user.
      Parameters:
      identityAPI - the Bonita Identity API
      userId - the user ID
      Returns:
      Optional containing the first name if found
    • getUserLastName

      public static Optional<String> getUserLastName(org.bonitasoft.engine.api.IdentityAPI identityAPI, Long userId)
      Gets the last name of a user.
      Parameters:
      identityAPI - the Bonita Identity API
      userId - the user ID
      Returns:
      Optional containing the last name if found
    • getUserEmail

      public static Optional<String> getUserEmail(org.bonitasoft.engine.api.IdentityAPI identityAPI, Long userId)
      Gets the email of a user.
      Parameters:
      identityAPI - the Bonita Identity API
      userId - the user ID
      Returns:
      Optional containing the email if found
    • getUserFullName

      public static Optional<String> getUserFullName(org.bonitasoft.engine.api.IdentityAPI identityAPI, Long userId)
      Gets the full name (first + last) of a user.
      Parameters:
      identityAPI - the Bonita Identity API
      userId - the user ID
      Returns:
      Optional containing the full name if found
    • generateTaskLink

      public static String generateTaskLink(String hostUrl, Long taskId)
      Generates a task link HTML anchor tag.
      Parameters:
      hostUrl - the base host URL (e.g., "https://bonita.example.com")
      taskId - the human task ID
      Returns:
      the HTML link string
    • generateTaskUrl

      public static String generateTaskUrl(String hostUrl, Long taskId)
      Generates a plain task URL (without HTML anchor).
      Parameters:
      hostUrl - the base host URL
      taskId - the human task ID
      Returns:
      the URL string
    • createResolver

      public static BiFunction<String,String,String> createResolver(org.bonitasoft.engine.api.IdentityAPI identityAPI, Long recipientUserId, String hostUrl, Long humanTaskId, BiFunction<String,String,String> customResolver)
      Creates a complete BiFunction resolver for template variables.

      This method creates a resolver that handles standard variables (DataResolverType) and delegates unknown variables to a custom fallback resolver.

      Parameters:
      identityAPI - the Bonita Identity API for user lookups
      recipientUserId - the user ID of the recipient (for recipient_* variables)
      hostUrl - the base host URL (for task_link variable)
      humanTaskId - the human task ID (for task_link variable)
      customResolver - optional custom resolver for BDM-specific or step-based variables. Called when standard variables don't match. May be null.
      Returns:
      BiFunction resolver for use with PBStringUtils.resolveTemplateVariables(java.lang.String, java.util.function.BiFunction<java.lang.String, java.lang.String, java.lang.String>)
    • createRecipientResolver

      public static BiFunction<String,String,String> createRecipientResolver(org.bonitasoft.engine.api.IdentityAPI identityAPI, Long recipientUserId)
      Creates a simple resolver for recipient-only variables.

      Use this when you only need to resolve recipient_firstname, recipient_lastname, and recipient_email variables without any custom BDM lookups.

      Parameters:
      identityAPI - the Bonita Identity API
      recipientUserId - the user ID of the recipient
      Returns:
      BiFunction resolver for recipient variables only
    • createResolverWithTaskLink

      public static BiFunction<String,String,String> createResolverWithTaskLink(org.bonitasoft.engine.api.IdentityAPI identityAPI, Long recipientUserId, String hostUrl, Long humanTaskId)
      Creates a resolver with task link support.
      Parameters:
      identityAPI - the Bonita Identity API
      recipientUserId - the user ID of the recipient
      hostUrl - the base host URL
      humanTaskId - the human task ID
      Returns:
      BiFunction resolver for recipient and task link variables
    • createStepDataResolver

      public static <T> BiFunction<String,String,String> createStepDataResolver(Function<String,T> stepLookup, Function<T,String> usernameExtractor, Function<T,String> statusExtractor)
      Creates a step-based data extractor function.

      This helper creates a function that can be used as part of the custom resolver to handle step_user_name and step_status variables from BDM step data.

      Usage Example (Groovy Script):

      
       // Define step data lookup
       Function<String, Object> stepLookup = { refStep ->
           def steps = pBStepProcessInstanceDAO.findLastByRefStepAndRootProcessInstanceId(
               rootProcessInstanceId, refStep, 0, 1)
           return steps?.isEmpty() ? null : steps.get(0)
       }
      
       // Create step data resolver
       BiFunction<String, String, String> stepResolver = TemplateDataResolver.createStepDataResolver(
           stepLookup,
           { step -> step.getUsername() },
           { step -> step.getStepStatus() }
       );
       
      Type Parameters:
      T - the type of step object returned by the lookup
      Parameters:
      stepLookup - function that takes refStep and returns the step object (or null)
      usernameExtractor - function to extract username from step object
      statusExtractor - function to extract status from step object
      Returns:
      BiFunction that resolves step_user_name and step_status variables