简体   繁体   中英

how to reuse the xpr file in Mule 4

In my case, I'm doing a migration from Mule 3 to Mule 4 .

This flow, which includes transformers like DOM to XML, XML to DOM, and an expression component , needs to be migrated.

In Mule 4 , I want to reuse the xrp file .

My flow for XML payload transform uses an XPR file by the expression component in Mule 3.

<flow name="rate-dtostepFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/dtostep" allowedMethods="POST" doc:name="HTTP"/>
        <mulexml:xml-to-dom-transformer returnClass="org.dom4j.Document" doc:name="XML to DOM"/>
        <set-variable variableName="domPayload" value="#[payload]" doc:name="set domPayload "/>
        <expression-component file="xpr/responseStubCannasure.xpr" doc:name="Expression"/>
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
</flow>

Input XML: https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml

Output XML: https://github.com/Manikandan99/rate-dtostep/blob/master/response.xml

my MULE 3 application: https://github.com/Manikandan99/rate-dtostep/tree/master/rate-dtostep

ResponseStubcannsure xpr file:

import org.dom4j.*;
import java.util.*;
import java.util.logging.Logger;

Logger logger = Logger.getLogger("");

dtoCoverageStepsNodes = flowVars.domPayload.selectNodes("//DTOCoverage[@Status=\'Active\']/DTOSteps");
for (Node node : dtoCoverageStepsNodes){
    //logger.info("inside: detach");
    node.detach();
}

dtoCoverageNodes = flowVars.domPayload.selectNodes("//DTOCoverage[@Status=\'Active\']");
int i = 500;
for (Node node : dtoCoverageNodes){

    //node.detach();
    //logger.info("inside: assign prem");
    node.addAttribute("FullTermAmt", Integer.toString(i));

    node.addElement("DTOSteps");

    stepNode = node.selectSingleNode("DTOSteps");
    stepNode.addElement("DTOStep")
        .addAttribute("Order","1")
        .addAttribute("Name","Final Premium")
        .addAttribute("Desc","Final Premium Desc")
        .addAttribute("Operation","=")
        .addAttribute("Factor",Integer.toString(i))
        .addAttribute("Value",Integer.toString(i));

    i+=1;
}

The xpr file transform the xml payload in the following ways:

  • updated the value of the DTOStep node.
  • The attribute value of DTOStep is autoincremented from 500 each time.

Please assist me.

You need to migrate the complete flow to Mule 4. The file responseStubCannasure.xpr is just a script in MEL (Mule 3 expression language). The extension is irrelevant, it could have been anything. MEL is very similar to Java so you could reuse the logic by encapsulating it into a Java class. You will need to add to the Java code the conversion to DOM4J from the input XML because Mule 4 doesn't support it.

Alternatively you could just delete the last 4 operations of the flow and replace them with a DataWeave transformation. Using a recursive function to navigate the keys and value recursively, using a condition to check if we are in the element DTOCoverage, with attribute Status == "Active" and then replace the nested element with the DTOSteps/DTOStep combination. Which is what your script does.

Example:

%dw 2.0
output application/xml
var startingValue=499
fun transformSteps(x, index)=
    x match {
      case is Object -> x mapObject 
        if ($$ as String == "DTOCoverage" and $$.@Status == "Active")
            { DTOCoverage: DTOSteps: DTOStep @(Order:1, Factor: $$$ + startingValue, Value: $$$ + startingValue, Name:"Final Premiun", Operation:"=", Desc: "Final Premium Desc"): null }
        else 
            (($$): transformSteps($, index+1)) 
      else -> $
    }
---
transformSteps(payload,1)

This solution doesn't completely resolve the Value and Factor (why two attributes with the same value?) sequential increase. You may need to do an additional transformation, or use Groovy or Java code to renumber them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM