Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSON to EDI Data Binding in REST client #55

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 99 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Usually, organizations have to work with many EDI formats, and integration devel
The below command can be used to generate Ballerina records, parser and util functions, and a REST connector for a given collection of EDI schemas organized into a Ballerina package:

```
bal edi libgen -p <package name> -i <input schema folder> -o <output folder>
bal edi libgen -p <organization-name/package-name> -i <input schema folder> -o <output folder>
```

The Ballerina package will be generated in the output folder. This package can be built and published by issuing "bal pack" and "bal push" commands from the output folder. Then the generated package can be imported into any Ballerina project and generated utility functions of the package can be invoked to parse EDI messages into Ballerina records.
Expand Down Expand Up @@ -251,58 +251,109 @@ It is quite common for different trading partners to use variations of standard

EDI libraries generated in the previous step can also be compiled to a jar file (using the ```bal build``` command) and executed(using the ```bal run``` command) as a standalone Ballerina service that processes EDI files via a REST interface. This is useful for microservice environments where the EDI processing functionality can be deployed as a separate microservice.

For example, the "citymart" package generated in the above step can be built and executed as a jar file. Once executed, it will expose a REST service to work with X12 850, 810, 820, and 855 files. Converting of X12 850 EDI text to JSON using the REST service is shown below:
For example, the "citymart" package generated in the above step can be built and executed as a jar file. Once executed, it will expose a REST service to work with X12 850, 810, 820, and 855 files.

#### Converting of X12 850 EDI text to JSON using the REST service

The below REST call can be used to convert an X12 850 EDI text to JSON using the REST service generated from the "citymart" package:

```
curl --request POST \
--url http://localhost:9090/porderParser/edis/850 \
--header 'Content-Type: text/plain' \
--data 'ST*834*12345*005010X220A1~
BGN*00*12456*20020601*1200****~
REF*38*ABCD012354~
AMT*cc payment*467.34*~
N1*P5**FI*999888777~
N1*IN**FI*654456654~
INS*Y*18*025**A***FT~
REF*0F*202443307~
REF*1L*123456001~
NM1*IL*1*SMITH*WILLIAM****ZZ*202443307~
HD*025**DEN~
DTP*348*D8*20020701~
SE*12*12345~'
curl --location 'http://localhost:9090/porderParser/edis/850' \
--header 'Content-Type: text/plain' \
--data-raw 'GS*PO*SENDERID*RECEIVERID*20240802*1705*1*X*004010~
ST*850*0001~
BEG*00*NE*4500012345**20240802~
REF*DP*038~
PER*BD*John Doe*TE*1234567890*EM*[email protected]~
FOB*CC~
ITD*01*3*2**30**31~
DTM*002*20240902~
N1*ST*SHIP TO NAME*92*SHIP TO CODE~
N3*123 SHIP TO ADDRESS~
N4*CITY*STATE*12345*US~
PO1*1*10*EA*15.00**BP*123456789012*VP*9876543210*UP*123456789012~
PID*F****PRODUCT DESCRIPTION~
PO4*1*CA*20*LB~
CTT*1~
SE*16*0001~
GE*1*1~
IEA*1*000000001~'
```

The above REST call will return a JSON response like the below:

```
{
"Transaction_Set_Header": {
"Transaction_Set_Identifier_Code": "834",
"Transaction_Set_Control_Number": "12345",
"Implementation_Convention_Reference": "005010X220A1"
},
"Beginning_Segment": {
"Transaction_Set_Purpose_Code": "00",
"Reference_Identification": "12456",
"Date": "20020601",
"Time": "1200"
},
"Reference_Information": [
{
"Reference_Identification_Qualifier": "38",
"Reference_Identification": "ABCD012354"
}
],
"Date_or_Time_or_Period": [],
"Monetary_Amount_Information": [
{
"Amount_Qualifier_Code": "cc payment",
"Monetary_Amount": 467.34
"X12_FunctionalGroup": {
"FunctionalGroupHeader": {
"code": "GS",
"GS01__FunctionalIdentifierCode": "PO",
"GS02__ApplicationSendersCode": "SENDERID",
"GS03__ApplicationReceiversCode": "RECEIVERID",
... // Other fields
}
],...
... // Other fields
},
"InterchangeControlTrailer": {
"code": "IEA",
"IEA01__NumberofIncludedFunctionalGroups": 1.0,
"IEA02__InterchangeControlNumber": 1.0
}
}
```

#### Converting of JSON to X12 850 EDI text using the REST service

The below REST call can be used to convert a JSON to X12 850 EDI text using the REST service generated from the "citymart" package:

```
curl --location 'http://localhost:9090/ediParser/objects/850' \
--header 'Content-Type: application/json' \
--data-raw '{
"X12_FunctionalGroup": {
"FunctionalGroupHeader": {
"code": "GS",
"GS01__FunctionalIdentifierCode": "PO",
"GS02__ApplicationSendersCode": "SENDERID",
"GS03__ApplicationReceiversCode": "RECEIVERID",
"GS04__Date": "20240802",
"GS05__Time": "1705",
"GS06__GroupControlNumber": 1.0,
... // Other fields
},
... // Other fields
},
"InterchangeControlTrailer": {
"code": "IEA",
"IEA01__NumberofIncludedFunctionalGroups": 1.0,
"IEA02__InterchangeControlNumber": 1.0
}
}'
```

The above REST call will return an X12 850 EDI text response like the below:

```
GS*PO*SENDERID*RECEIVERID*20240802*1705*1*X*004010~
ST*850*0001~
BEG*00*NE*4500012345**20240802~
REF*DP*038~
PER*BD*John Doe*TE*1234567890*EM*[email protected]~
FOB*CC~
ITD*01*3*2**30**31~
DTM*002*20240902~
N1*ST*SHIP TO NAME*92*SHIP TO CODE~
N3*123 SHIP TO ADDRESS~
N4*CITY*STATE*12345*US~
PO1*1*10*EA*15.00**BP*123456789012*VP*9876543210*UP*123456789012~
PID*F****PRODUCT DESCRIPTION~
PO4*1*CA*20*LB~
CTT*1~
SE*16*0001~
GE*1*1~
IEA*1*1~
```

## Schema conversion

Instead of writing Ballerina EDI schema from scratch, the Ballerina EDI tool also supports converting various EDI schema formats to Ballerina EDI schema format.
Expand All @@ -318,9 +369,11 @@ bal edi convertX12Schema -H <enable headers mode> -c <enable collection mode > -
```

Example:

```
$ bal edi convertX12Schema -i input/schema.xsd -o output/schema.json
```

### EDIFACT schema to Ballerina EDI schema

EDIFACT, which stands for Electronic Data Interchange For Administration, Commerce, and Transport, is an international EDI standard developed by the United Nations. It's widely used in Europe and many other parts of the world. EDIFACT provides a common syntax for exchanging business documents electronically between trading partners, facilitating global trade and improving efficiency in supply chain management.
Expand All @@ -332,9 +385,11 @@ bal edi convertEdifactSchema -v <EDIFACT version> -t <EDIFACT message type> -o <
```

Example:

```
$ bal edi convertEdifactSchema -v d03a -t ORDERS -o output/schema.json
```

### ESL to Ballerina EDI schema

ESL, or Electronic Shelf Labeling, is a technology used in retail stores to display product pricing and information electronically. Instead of traditional paper price tags, ESL systems use digital displays that can be updated remotely, allowing retailers to change prices in real-time and automate pricing strategies.
Expand All @@ -346,9 +401,11 @@ bal edi convertESL -b <segment definitions file path> -i <input ESL schema file/
```

Example:

```
$ bal edi convertESL -b segment_definitions.yaml -i esl_schema.esl -o output/schema.json
```

## Issues and projects

The **Issues** and **Projects** tabs are disabled for this repository as this is part of the Ballerina library. To report bugs, request new features, start new discussions, view project boards, etc., visit the Ballerina library [parent repository](https://github.com/ballerina-platform/ballerina-library).
Expand Down Expand Up @@ -407,7 +464,7 @@ All the contributors are encouraged to read the [Ballerina Code of Conduct](http

## Useful links

* For more information go to the [EDI package](https://ballerina.io/learn/edi-tool/).
* For more information go to the [EDI Tool documentation](https://ballerina.io/learn/edi-tool/).
* For example demonstrations of the usage, go to [Ballerina By Examples](https://ballerina.io/learn/by-example/).
* Chat live with us via our [Discord server](https://discord.gg/ballerinalang).
* Post all technical questions on Stack Overflow with the [#ballerina](https://stackoverflow.com/questions/tagged/ballerina) tag.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ public void execute() {
printStream.println(stringBuilder.toString());
return;
}
if (!packageName.matches("^[a-zA-Z0-9_]+/[a-zA-Z0-9_]+$")) {
printStream.println("Invalid package name. Package name should be in the format orgname/packagename");
if (!packageName.matches("^[a-zA-Z0-9_]{1,256}/[a-zA-Z0-9_.]{1,256}$")) {
printStream.println(
"Invalid package name. Package name should be in the format orgname/packagename."+
" The orgname part must contain only alphanumeric characters or underscores and be 1 to 256 characters long."+
" The packagename part must contain only alphanumeric characters, underscores, or periods and be 1 to 256 characters long.");
return;
}
try {
Expand Down
2 changes: 1 addition & 1 deletion edi-tools-package/BalTool.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
id = "edi"

[[dependency]]
path = "resources/edi-tools-cli-2.0.2.jar"
path = "resources/edi-tools-cli-2.0.3.jar"
2 changes: 1 addition & 1 deletion edi-tools-package/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "editoolspackage"
version = "2.0.2"
version = "2.0.3"
authors = ["Ballerina"]
keywords = ["edi"]
license = ["Apache-2.0"]
Expand Down
2 changes: 1 addition & 1 deletion edi-tools/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "editools"
version = "2.0.2"
version = "2.0.3"
authors = ["Ballerina"]
keywords = ["edi"]
license = ["Apache-2.0"]
Expand Down
2 changes: 1 addition & 1 deletion edi-tools/modules/codegen/transformer_gen.bal
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ isolated function transformRead(${mainRecordName} data) returns InternalType =>
# + content - Ballerina record to be converted
# + return - EDI string or error
public isolated function transformToEdiString(anydata content) returns string|error {
${mainRecordName} data = transformWrite(check content.ensureType());
${mainRecordName} data = transformWrite(check content.cloneWithType());
return toEdiString(data);
}

Expand Down
Loading