Building Your First Pattern

Building Your First Pattern

This page will help you get started with building your first pattern. If you already are familiar with patterns and have built your first pattern, you can skip ahead to Building Advance Patterns 

Let's understand the basics of a Pattern and then move on to building our first pattern.
  • Basics - Building Blocks of a  Pattern
    • Templates -
                      Templates are the actual Mule Configuration xml content with some placeholders, which would be replaced dynamically based on the project configuration
Placeholders are denoted using double flower braces i.e. {{ value }} 
    • User Interface -
                        Each pattern can have an optional action configuration screen where users can configure the placeholder values defined in the template
    • Actions
                        Actions denotes the execution flow of a pattern i.e. how & where the Templates would be applied in a Mule project
  • Pattern Types
    • Source
                     Source Patterns indicate the Mule - source connectors/components.  
    • Processor
                     Processor Patterns indicate one or more Mule - processor connectors/components. 
    • Application 
                     Application Patterns indicate the Mule project/application level configurations.
                     Example: Adding API auto discovery, Munit tests for the generated flows, exception handling, adding environment specific property files.
                               
  • Building Our First Pattern

Lets consider a simple use case where we have to build 10 - 20 interfaces for a project and all the interfaces will interact with Salesforce, print out a log messages before & after the flow and a Transform Message component before & after the Salesforce connector. So, our Patten should should contain (Logger -> DW -> Salesforce -> DW -> Logger) 

1: Navigate to "Pattern Builder" menu of IZ Accelerators.
2:Click on "Create New" option 
    • Basic Details 
Lets call our Pattern "Project X Salesforce", Version "1.0.0", System Type "3.8.x", select Pattern Type as "Processor Pattern" and click on Next to move on to "Pattern Implementation"
    • Configuring Templates
Let's configure our (Logger -> DW -> Salesforce -> DW -> Logger)  pattern.
1: Edit the default template's name "template.mst" to "project_x_salesforce.mst"
2: Delete the default configuration & add the below Mule xml configuration code 

<logger level="INFO" doc:name="Logger"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
}]]></dw:set-payload>
        </dw:transform-message>
        <sfdc:query-all config-ref="Salesforce__Basic_Authentication" query="dsql:" doc:name="Salesforce"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
}]]></dw:set-payload>
        </dw:transform-message>
<logger level="INFO" doc:name="Logger"/>

1: Add a new template called "sfdcconfig.mst", which is our Salesforce connector's configuration
2: Delete the default configuration and add the below Mule xml configuration 
<sfdc:config name="Salesforce__Basic_Authentication" username="{{details.username}}" password="{{details.password}}" doc:name="Salesforce: Basic Authentication"/>
NOTE: The values for "username" & "password" are {{details.username}} and {{details.password}} respectively. These values will be derived from the "UI Configuration" which we are going to explore in the next section.
    • Configuring User Interface
Lets configure some properties for our pattern where users will be able to define them dynamically.
In our example, lets allow users to configure Salesforce "Username" & "Password"

1: Click on the "UI Configuration" tab
2: In the "Basic Form Builder" section:
    2.1: Drag & Drop a "Text Field" from the selection panel -> hover over the dropped "Text Field" and click on edit icon
    2.2: Change the Label property to "Salesforce Username", which is the display name
    2.3: Change the Name property to "username", which is the actual placeholder
    2.4: Chnage the Value property to "{{data.username}}"

Final configuration should similar to the below screenshot.


Configure another UI property for "Salesforce Password" in the same way with following properties
2.2: Change the Label property to "Salesforce Password", which is the display name
2.3: Change the Name property to "password", which is the actual placeholder
2.4: Chnage the Value property to "{{data.password}}"
Finally click on the "Preview" button to visualize the actual screen that comes up in the pattern properties editor.
Preview should look similar to below screenshot

    • Configuring Actions
The final part is the actions i.e how the pattern will be executed
Click on the "Main Configuration" tab.

Here we will have 3 sections namely
1. Action Types - List of available action types that be used to configure the pattern, which can be dragged & dropped into "Action Sequence"
2. Action Sequence - Sequence in which the configured actions will be executed
3. Action Configuration - Configuration section of an action
Lets start configuring actions our Pattern

Configuring "Execution" action
Description: Project X Salesforce
Execute When: - can be left blank. This is basically the condition to check if the pattern's actions should be executed. By default it evaluates to true.
Execution Phase: Before Adding Flows

Preview should look similar to below screenshot


Configuring "Namespace" action. Namespace action adds the namespace to Mule configuration file.
Drag & Drop "Namespace" action from the "Action Types" section to "Action Sequence"

Target Folder: {{{flowDetails.targetFolder}}}   - Target folder of the configuration file
Target File Name: {{{flowDetails.targetFileName}}}   - Target file to add the namespace
Namespace Prefix: xmlns:dw

Click on "Add Another Action" and add the following properties

Target Folder: {{{flowDetails.targetFolder}}}   - Target folder of the configuration file
Target File Name: {{{flowDetails.targetFileName}}}   - Target file to add the namespace
Namespace Prefix: xmlns:sfdc

Click on "Add Another Action" and add the following properties

Target Folder: src/main/app     - Target folder of the configuration file
Target File Name: global.xml    - Target file to add the namespace
Namespace Prefix: xmlns:sfdc

Preview should look similar to below screenshot


Configuring "Global Config" action. Adds the contents of "sfdcconfig.mst" to global.xml Mule configuration.
Drag & Drop "Global Config" action from the "Action Types" section to "Action Sequence"

Source Template: template/sfdcconfig.mst  - Template name configured in Templates section
Target Folder: - src/main/app   - Target folder of the configuration file
Target File Name: global.xml  - Target file to add the namespace
Duplicate Check: sfdc:config[name='Salesforce__Basic_Authentication']

Preview should look similar to below screenshot




Configuring "Append To Flow" action. Adds the contents of project_x_salesforce.mst to the project's Mule configuration file
Drag & Drop "Append To Flow" action from the "Action Types" section to "Action Sequence"

Source Template Path : template/project_x_salesforce.mst  - Template name configured in Templates section
Target Directory: - {{{flowDetails.targetFolder}}}   - Target folder of the configuration file
Target File: {{flowDetails.targetFileName}}  - Target file to add the namespace
Target Flow: {{flowDetails.targetFlowName}}  - Target flow name. This field is optional and will be derived dynamically based on the project

Preview should look similar to below screenshot


Configuring "Maven Dependencies" action. Adds the Salesforce connector maven dependencies to pom.xml
Drag & Drop "Maven Dependencies" action from the "Action Types" section to "Action Sequence"

Pom Location : pom.xml    - Localtion of pom.xml in the project
Group Id: - org.mule.modules  - Maven dependency group id
Artifact Id: mule-module-sfdc  - Maven dependency artifact id
Version: 8.3.0

Preview should look similar to below screenshot


Configuring "Maven Inclusion" action. Adds the maven inclusion to pom.xml. The "Group Id" & "Artifact Id" here are same as the values in dependencies section.
Drag & Drop "Maven Inclusion" action from the "Action Types" section to "Action Sequence"

Pom Location : pom.xml    - Localtion of pom.xml in the project
Group Id: - org.mule.modules  - Maven dependency group id
Artifact Id: mule-module-sfdc  - Maven dependency artifact id

Preview should look similar to below screenshot



  • Creating/Uploading the Pattern
Click on the "Create Pattern" to upload the new pattern to your current exchange section.
Once the pattern is uploaded, click on the generate project menu and configure the processor with our new pattern (Project X Salesforce will be populated as part of the processors list).

Preview should look similar to below screenshot




Hover over the "Project X Salesforce" pattern and click on edit icon to view list of configurable properties.

Preview should look similar to below screenshot  



Once the pattern is configured, click on "Next" and generate the project (Download Local).
Unzip the project & import the pom.xml in Anypoint Studio.

Preview should look similar to below screenshot  




With this we have built a new pattern with (Logger -> DW -> Salesforce -> DW -> Logger) components within.

  • Enhance the pattern to comply with standards
Now that we have our first pattern ready for use, we still can enhance the pattern to comply with standards. In our example, we have our Salesforce username & password directly placed in the Mule configuration xml. Ideally we need to place the values in a property file and refer the property key in our Mule Configuration xml.

Lets edit our pattern to achieve this.
Note: As a prerequisite, you should have already seeded "Enterprise Properties <version>" pattern from Seed Patterns menu.
1: Navigate to to "Pattern Builder" menu of IZ Accelerators.
2: Search for "Project X Salesforce" pattern which we just created.
3: Click on the "Edit Pattern" icon in Actions column.
4: Update the patter version to "1.0.1" and click on Next to move "Pattern Implementation" section.
5: Click on "sfdcconfig.mst" template and update the values of username & password to:
      5.1: username: ${app.salesforce.username}
      5.2: password: ${app.salesforce.password}

Now the template should look similar to:

<sfdc:config name="Salesforce__Basic_Authentication" username="${app.salesforce.username}" password="${app.salesforce.password}" doc:name="Salesforce: Basic Authentication"/>

1: Move on to "Main Configuration" tab
2: Drag & Drop "Properties" action from "Action Types" section to "Action Sequence" section
3: Configure the "Properties" action with below values
  • Target Directory: src/main/resources
  • Property Name: app.salesforce.username 
  • Property Value:  {{details.username}}
Preview should look similar to below screenshot  


1: Configure the "password" property in the same way.
2: Click on "Add Another Action" and configure the below values
  • Target Directory: src/main/resources
  • Property Name: app.salesforce.password
  • Property Value:  {{details.password}}
3: Click on "Update Pattern" to publish the changes.

  • Generating the Project to validate the chnages

Generate the project as we did earlier with "Project X Salesforce" processor. 
1: Click on "Generate Project" menu option
2: Add a source of ypur choice
3: Add "Project X Salesforce" processor and edit the pattern properties with below values 
  • Salesforce Username: admin
  • Salesforce Password: changeme
4: Expand the Application Patterns and add "ACME Enterprise Properties" from Application Patterns section on the right.
Once the project is generated, unzip the archive & open the same in Anypoint studio. Now when we open the Salesforce config, username and password field refer to "${app.salesforce.username}" and "${app.salesforce.password}" respectively.



In the Package Explorer, expand "src/main/resources" section. We should have "dev", "qa" and "prod" property files.



Open "dev.properties" file and verify that the property values are same as what we defined in the pattern's property editor.



  • Conclusion
If you have reached here by following the tutorial steps, then you should be familiar with building your own Patterns based on the business case.
Next, you might want to check out Building Advance Patterns, where we will concentrate on adding custom Javascript and HTML code in our Pattern UI editor.

    • Related Articles

    • Building Advance Pattern

      In this topic we will cover how to build advance Patterns by adding few lines of Javascript. This tutorial assumes that you are already familiar with basics of how the pattern is built, if not it is recommended to get familiar with Building Your ...