简体   繁体   中英

for loops in Mule 3.8.4 | Dataweave 1.0

I need to do a string comparison on the incoming payload string with the existing list of product names from cache, as there will not be a exact match, first I need to remove last 3 chars from the incoming payload string and do a comparison with all the existing strings/product Names. If I find a match then do other transformation. else remove one more char from incoming payload string and compare again. I need to repeat this till the string size of incoming payload is =3 (no need to compare if the string size is less than 3)

Summary: i need to remove chars from right to left and do a string comparison

Imput Payload is as below:

{
  "productPartNo": "MPN-400110",
  "supplier": "70058",
  "productCode": "02",
}

Need to compare the string "productPartNo" against the "partNumber" field from the below existing product list

{
  "Product": [
    {
      "productNumber": "420475",
      "created": "2012-10-28",
      "partNumber": "C1F2PNEX71K9",
      "codeNo": "7712",
      "manufacturer": ""
    },
    {
      "productNumber": "478376",
      "created": "2017-12-12",
      "partNumber": "N77-C77-RMK=",
      "codeNo": "1589",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478381",
      "created": "2017-12-12",
      "partNumber": "CON-U-C1F2PXNE",
      "codeNo": "1586",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478384",
      "created": "2017-12-12",
      "partNumber": "PMPN4070",
      "codeNo": "1585",
      "manufacturer": "50884"
    }
 ]
}

In this case, my input string "productPartNo": "MPN-400110" should be compared with all the PartNumber. After removing chars from last, the string 'MPN' will have the match/contain with the "partNumber": "PMPN4070" hence I should get the "codeNo": "1585" associated with partNumber-PMPN4070 as my output field.

How can I implement this logic in %dw 1.0

This needed a little thinking and its late, not much brain power left, in the morning I may revise but its a good start given your sample data. Just let me know if you have any further clarifications.

Just copy and paste inside a Transform Message processor and turn on the preview.

%dw 1.0
%output application/dw

%var products = {
  "Product": [
    {
      "productNumber": "420475",
      "created": "2012-10-28",
      "partNumber": "C1F2PNEX71K9",
      "codeNo": "7712",
      "manufacturer": ""
    },
    {
      "productNumber": "478376",
      "created": "2017-12-12",
      "partNumber": "N77-C77-RMK=",
      "codeNo": "1589",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478381",
      "created": "2017-12-12",
      "partNumber": "CON-U-C1F2PXNE",
      "codeNo": "1586",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478384",
      "created": "2017-12-12",
      "partNumber": "PMPN4070",
      "codeNo": "1585",
      "manufacturer": "50884"
    }
 ]
}


%var inputPayload = {
  "productPartNo": "MPN-400110",
  "supplier": "70058",
  "productCode": "02"
}
---
// Get a list with all the substrings to search with--this give you the following array
// ["MPN-400","MPN-40","MPN-4","MPN-","MPN"]
(4 to (sizeOf inputPayload.productPartNo ) - 2 map inputPayload.productPartNo[0 to -$]
map (
    // Filter the list of products on the substring, 
    // this should either give you a matches or the empty list
    // Finally, take the first match.  I am not sure if this what you want.
    (products.Product filter (e) -> (e.partNumber contains $))[0]
) 
filter ($ != null))[0].codeNo

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