简体   繁体   中英

How to update the DTOSteps node values based on the logic in xpr using dataweave 2.0

In my case, I have two payloads: domDRC and domPayload (2nd payload).

I have to iterate over the layer (child node) and update the DTOStep value of DTOCoverage and DTORateArea in domPayload based on values in layer of domDRC payload with InclFlag equal to "Y".

Logic:

nodes = domDRC.getRootElement().selectNodes("/Envelope/Body/rateResponse/RateResult/Layers/Layer","LayerNum");
for (Node node : nodes) {
    if (node.valueOf("InclFlag") == 'Y') {
        if ( (int) node.valueOf("LayerPremNonAuto") > 0) {      
                // NonAuto Layer
                //add the DTOStep in CEL DTOCoverage of 2 nd payload
                //add the DTOStep in DTORateArea of 2 nd payload and DTORateArea attributes values(AreaName, Description, FullTermAmt, FinalPremiumAmt, WrittenPremiumAmt)
                
        }
        if ( (int) node.valueOf("LayerPremAuto") > 0) {
                // Auto Layer
                //add the DTOStep in CEL DTOCoverage of 2 nd payload
                //add the DTOStep in DTORateArea of 2 nd payload and DTORateArea attributes values(AreaName, Description, FullTermAmt, FinalPremiumAmt, WrittenPremiumAmt)
        
        }
    }
}

Entire XPR code: https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/auto_split_update_DtoSteps.xpr

DomDRC Payload (input): https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/domDRC_payload.xml

Dom payload (2nd payload) (input): https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/dom_payload(2nd).xml

Expected output: https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/Expected_updated_dtosteps.xml

Logic of xpr: https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/logic_of_auto_split_xpr.txt

I tried dataweave: https://github.com/Manikandan99/java-code-to-dataweave-2/blob/main/transform_DOM_payload.dwl

experts please go through the links.

In mule 3, I've used expression to transform the dom payload. while doing migration, due to unavailability of expression component in mule 4. I wanna go with dataweave 2.0. I've attached the dataweave tried but i couldn't solve the iteration over the domDRC and get the values from Layer node in domDRC payload, then update to it dom payload DTOSTEPS.

please help me on this and thanks in advance.

Any ideas on how to convert the auto_split xpr to dataweave 2.0?

My two cents: don't expect a straightforward migration from XPR (whatever that is) to DataWeave. I'd strongly recommend you to do it from scratch without those Java functions you're using right now (I don't think they are necessary at all). Looking to other similar questions you did before, that @aled has kindly help you with, it seems that what you really need is to understand how DataWeave 2 works and the features it has to solve the requirements you have.

So, I'd recommend you to take a look at the following content:

The official documentation has quite useful information to help you with this.

N.netheless, looking at the snippet logic you're sharing in here (no the ones in GitHub, since with the examples and scripts it's not clear at all), you can start by doing something like:

%dw 2.0
output application/xml 
var layers = payload.Envelope.Body.rateResponse.RateResult.Layers.*Layer
fun processLayers(layers) = do {
    var layersWithInclFlagY = layers filter ((item, index) -> item.InclFlag == 'Y')
    // var layersWithInclFlagY = layers[?$.InclFlag == 'Y'] //The same as above but with inline filter
    --- 
    layersWithInclFlagY map ((layer, index) -> processLayer(layer))
}

fun processLayer(layer) = processAutoLayer(processNonAutoLayer(layer))

fun processAutoLayer(layer) = 
if (layer.LayerPremAuto > 0)
    layer //DO SOMETHING
else layer

fun processNonAutoLayer(layer) = 
if (layer.LayerPremNonAuto > 0)
    layer //DO SOMETHING
else layer
---
Layers: {
    (Layer: processLayers(layers)) if (!isEmpty(layers))   
}

I also recommend you to try to abstract the problems and be specific (specific questions/problems, since this is a Q&A site, don't expect someone else to develop something from a code you share), reduce the examples for the input/expected data so the community in here can help you.

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