aperak edi file in mule dataweaver - mule

I am trying to create aperak-edifact file from mule data weaver component.
Here is my piece of code
<edifact-edi:config name="EDIFACT_EDI" delimiterUsage="USE_SPECIFIED_FOR_WRITES" doc:name="EDIFACT EDI" interchangeIdPartner="YYYY" interchangeIdSelf="XXXX">
<edifact-edi:schemas>
<edifact-edi:schema>/edifact/d98b/APERAK.esl</edifact-edi:schema>
</edifact-edi:schemas>
</edifact-edi:config>
<flow name="new1Flow">
<file:inbound-endpoint path="C:\Users\Desktop" responseTimeout="10000" doc:name="File">
<file:filename-regex-filter pattern="aperak.xml" caseSensitive="true"/>
</file:inbound-endpoint>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Messages: {
D98B: {
APERAK: [{
Interchange: {
UNB0201: "XXX",
UNB0301: "YYYY"
},
MessageHeader: {
UNH01: "1"
},
Heading: {
"0020_BGM": {
BGM0101: "7"
}
}
}]
}
}
}]]></dw:set-payload>
</dw:transform-message>
<edifact-edi:write config-ref="EDIFACT_EDI" doc:name="EDIFACT EDI"/>
<file:outbound-endpoint path="C:\Users\Desktop" outputPattern="out.json" responseTimeout="10000" doc:name="File"/>
</flow>
Output
UNB+UNOB:4+XXX+YYY+05042016:0948+1'UNH+2+APERAK:D:98B:UN'BGM+7'UNT+3+2'UNZ+1+1'
But the output created is all in one line where I want it to be appeared line by line. So how can I introduce new line for edifact
Any help is appreciated
Expected Output
UNB+UNOB:4+XXX+YYY+05042016:0948+1'
UNH+2+APERAK:D:98B:UN'
BGM+7'
UNT+3+2'UNZ+1+1'

Set the lineEnding configuration attribute of edifact-edi:config to output a CRLF.

Related

Add one more element to the object array inside for each loop in mule 3

I have Input as below
[{Name=ABC, ID=123},{Name=XYZ, ID=345}]
I would iterate over this collection in a for-each loop and add one more element Age to each object.
My expected output would be like
[{Name=ABC, ID=123, Age=23},{Name=XYZ, ID=345, Age=24}]
Any help would be highly appreciated. Thanks in advance.
HTH..
%dw 2.0
output application/json
var inp = [
{
"Name":"ABC",
"ID":"123"
},
{
"Name":"XYZ",
"ID":"345"
}
]
---
inp map {
($),
age: (23 + ($$)) // or your logic to derive age
}
It was not possible with DW as because payload inside for-each was modified. So I tried using Expression component. Below is my configuration XML.
<flow name="add-one-more-element-to-the-original-payload">
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="30" timeUnit="SECONDS"/>
<logger message="Pooling Started" level="INFO" doc:name="Log"/>
</poll>
<dw:transform-message doc:name="Transform Payload">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
%var collection = [{"Name":"ABC","ID":123},{"Name":"XYZ","ID":345}]
---
collection]]></dw:set-payload>
</dw:transform-message>
<set-variable variableName="outputList" value="#[new java.util.ArrayList()]"
doc:name="Set Variable"/>
<foreach doc:name="For Each" collection="#[payload]">
<dw:transform-message doc:name="CurrentPayload">
<dw:set-variable variableName="currentPayload"><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-variable>
</dw:transform-message>
<flow-ref name="get-age-subflow" doc:name="Get Age"/>
<expression-component doc:name="Expression"><!
[CDATA[flowVars.currentPayload.Age=payload.Age;]]></expression-component>
<expression-transformer expression="#
[flowVars.outputList.add(flowVars.currentPayload)]" doc:name="Expression"/>
</foreach>
<logger message="#[flowVars.outputList]" level="INFO" doc:name="Logger"/>
</flow>
<sub-flow name="get-age-subflow">
<dw:transform-message doc:name="Transform Age">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Age:24
}]]></dw:set-payload>
</dw:transform-message>
</sub-flow>

Invoke Component in message Enricher and how to define the target value for Message Enricher

I am practicing on salesforce to mulesoft connection. I am trying to query the account from salesforce, do some logic using Invoke Component and update the Account name in Salesforce. Please find the XML code below. I get the following error when i invoke..
Execution of the expression "Variable:Fname=__object_for_enrichment" failed. (org.mule.api.expression.ExpressionRuntimeException).
XML:
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8092" doc:name="HTTP Listener Configuration"/>
<sfdc:config name="Salesforce__Basic_Authentication" username="username" password="*******" securityToken="Token" doc:name="Salesforce: Basic Authentication" url="https://login.salesforce.com/services/Soap/u/38.0">
<reconnect-forever/>
</sfdc:config>
<spring:beans>
<spring:bean id="Bean" name="UpdateAcc" class="com.pack.salesforceconnect.SFJava"/>
</spring:beans>
<flow name="salesforceconnectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/salesforce" doc:name="HTTP"/>
<sfdc:query-single config-ref="Salesforce__Basic_Authentication" query="dsql:SELECT Id,Name,ShippingCity FROM Account ORDER BY Name DESC" doc:name="Salesforce"/>
<dw:transform-message doc:name="Transform Message">
<dw:input-payload doc:sample="sample_data\Account.dwl"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Id: payload.Id,
Name: payload.Name,
ShippingCity: payload.ShippingCity
}]]></dw:set-payload>
</dw:transform-message>
<enricher source="#[payload.Name]" target="#[Variable:Fname]" doc:name="Message Enricher">
<invoke name="Invoke" object-ref="UpdateAcc" method="ChangeSCity" methodArguments="#[payload.Name]" metadata:id="id"/>
</enricher>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
[{
Id: payload.Id,
Name: variable:FName,
ShippingCity: payload.ShippingCity
}]]]></dw:set-payload>
</dw:transform-message>
<set-payload value="#[payload]" doc:name="Set Payload"/>
</flow>
</mule>
Class:
package com.pack.salesforceconnect;
public class SFJava {
public String ChangeSCity(String Fname){
//String ShippingCity;
System.out.print("Account Name received : "+Fname);
if (Fname == null){
Fname = "New Account";
}else{
Fname = "Washington Industries Inc.,";
}
System.out.print("Account Name: "+Fname);
return Fname;
}
}
Syntax wrong .. it would be target="#[variable:Fname]" instead of target="#[Variable:Fname]"
Or simply you can use target="#[flowVars.Fname]"

How to handle if else nested statment in dataweave in mulesoft

OptIn: "Yes" when payload.country==true and payload.state==false otherwise "No" when payload.country==false and payload.state==true otherwise "NOT VALID" when payload.country==false and payload.state==false otherwise ""
This can be achieve through following example :-
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="DataweaveFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/poc" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[ %dw 1.0
%input payload application/json
%output application/json
---
{ value1: "Yes" } when payload.country==true and payload.state==false
otherwise (
{ value1: "No" } when payload.country==false and payload.state==true
otherwise (
{ value1: "NOT VALID" } when payload.country==false and payload.state==false
otherwise "your value"
))]]></dw:set-payload>
</dw:transform-message>
<logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger" />
</flow>
Where I tested this with following input :-
case 1:-
{
"country":true,
"state":false
}
case 2:-
{
"country":false,
"state":true
}
case 3:-
{
"country":false,
"state":false
}
case 4:-
{
"country":true,
"state":"default value"
}

Dataweave equivalent of SQL join

How can I do the equivalent of a SQL join in Dataweave? I'm basically implementing a lookup. I have CSV data and JSON data in flow variables. I'm able to use both of them, but I can't figure out how connect them. Let's say the CSV has two columns:
Name,ExternalId
Foo,1
Bar,2
Baz,2
The JSON data is:
{
ExternalEntities: [
{ "Id": 1, "Name": "One", "Description": "Thing One" }
, { "Id": 2, "Name": "Two", "Description": "Thing Two" }
]
}
In the end, I'd like a List<Hashmap> with the following (expressed here in JSON format for convenience.)
[
{ "Name": "Foo", "ExternalName": "One", "ExternalDescription": "Thing One" }
, { "Name": "Bar", "ExternalName": "Two", "ExternalDescription": "Thing Two" }
, { "Name": "Baz", "ExternalName": "Two", "ExternalDescription": "Thing Two" }
]
So here is a sample using the json as the payload and the csv as a lookup table. The first flows parses the json and does a flow lookup passing the 'Id' as the payload. The second flow loads the csv as an array and does a search by the Id passed in. This returns a map of the CSV record found where you can then extract the 'Name' field.
It is possible to do this in one transformer, but for demo sake this was easier. You can also reverse the lookup with some tinkering if you want the csv as your payload and the json as your lookup.
<flow name="lookuptest" processingStrategy="synchronous">
<poll doc:name="Poll">
<logger level="INFO" doc:name="Logger" />
</poll>
<set-payload
value="{ "ExternalEntities": [ { "Id": 1, "Name": "One", "Description": "Thing One" } , { "Id": 2, "Name": "Two", "Description": "Thing Two" } ] }"
doc:name="Set Payload" mimeType="application/json"></set-payload>
<dw:transform-message doc:name="Transform Message">
<dw:input-payload doc:sample="string_2.dwl" />
<dw:set-payload><![CDATA[%dw 1.0
%input payload application/json
%output application/json
---
payload.ExternalEntities map ((value , index) -> {
Name: lookup("NameLookup", [value.Id as :string]).Name
})]]></dw:set-payload>
</dw:transform-message>
<object-to-string-transformer doc:name="Object to String"/>
<logger level="ERROR" message="#[payload]" doc:name="Logger"/>
</flow>
<flow name="NameLookup" processingStrategy="synchronous">
<set-variable value="#[payload[0]]" variableName="ExternalId"
doc:name="Variable" />
<set-payload
value="#[Thread.currentThread().getContextClassLoader().getResourceAsStream('lookuptables/namelookup.csv')]"
mimeType="application/csv" doc:name="Variable" />
<dw:transform-message doc:name="Transform Message">
<dw:input-payload doc:sample="string_2.dwl" />
<dw:set-payload><![CDATA[%dw 1.0
%input payload application/csv
%output application/java
---
(payload[?($.ExternalId == flowVars['ExternalId'])])[0]
]]></dw:set-payload>
</dw:transform-message>
</flow>
Here's a second solution that works, though I accepted #Ryan Carter's answer.
%dw 1.0
%output application/json
---
flowVars.myCsv map (
(row0, i) -> using (lookupElement = (flowVars.jsonLookup.ExternalEntities filter ((obj1) -> row0.ExternalId ~= obj1.Id))[0]) {
Name: row0.Name
, ExternalName: lookupElement.Name
, ExternalDescription: lookupElement.Description
} mapObject ({"$$": $, (StartsWithB:$[0] == "B") when $$ ~= "Name"})
)

Can not Extract json array properly in message Enricher mule esb

I tried to extract json array data using message enricher. But in output I got some object data.How can I fetch properly?
Following is my flow
<flow name="readfileFlow1" doc:name="readfileFlow1">
<file:inbound-endpoint path="Test" moveToDirectory="Backup" responseTimeout="10000" doc:name="File"/>
<enricher source="#[(name in payload.data)]" target="#[flowVars.myMap]" doc:name="Message Enricher">
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
</enricher>
<logger message="#[payload.toString()]" level="INFO" doc:name="Logger"/>
</flow>
When I print in logger then I got following output
INFO 2015-04-29 13:01:20,409 [[readfile].readfileFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor: org.mule.transport.file.ReceiverFileInputStream#15bc5cc
How can I extract properly all name
I tried using for each . Using for each I can extract succesfully. But want to use message enricher. How can I do this using message enricher MVEL??
My json data is as follow..
{
"data":[
{
"id" : "1",
"name": "AAA"
},
{
"id" : "5",
"name": "DDD"
},
{
"id" : "6",
"name": "CCC"
},
]
}
I have done this..
silly mistake I made..
Following is my answer
<logger message="#[flowVars.myMap]" level="INFO" doc:name="Logger"/>
This is how you can achieve to extract name and id from your JSON payload .. you will get it in logger
<flow name="readfileFlow1" doc:name="readfileFlow1">
<file:inbound-endpoint path="Test" moveToDirectory="Backup" responseTimeout="10000" doc:name="File"/>
<enricher source="#[message.payload]" target="#[flowVars.myMap]" doc:name="Message Enricher">
<processor-chain doc:name="Processor Chain">
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
<foreach collection="#[payload.data]">
<logger level="INFO" message="Name :- #[payload.name] and Id:- #[payload.id]"/>
</foreach>
</processor-chain>
</enricher>
<logger level="INFO" message="#[flowVars.myMap]"/>
</flow>
One more thing you JSON payload is not valid.. try to remove an extra , at the end .. so your valid JSON file will be :-
{
"data":[
{
"id" : "1",
"name": "AAA"
},
{
"id" : "5",
"name": "DDD"
},
{
"id" : "6",
"name": "CCC"
}
]
}