handle any input csv file in groovy to add update remove lines based on condition
groovy code
/* Refer the link below to learn more about the use cases of script.
https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/148851bf8192412cba1f9d2c17f4bd25.html
If you want to know more about the SCRIPT APIs, refer the link below
https://help.sap.com/doc/a56f52e1a58e4e2bac7f7adbf45b2e26/Cloud/en-US/index.html */
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
def inputData = message.getBody(java.lang.String)
def processedLines = ""
def cLines = []
inputData.eachLine { line ->
line = line.trim()
cLines << line
}
for (int i = 0; i < cLines.size(); i++) {
def cLine = cLines[i]
// Check if the current line starts with 'C' and the previous line starts with 'Z'
if (i > 0 && cLine.startsWith("C") && cLines[i - 1].startsWith("Z")) {
processedLines += cLines[i - 1] + "\n"
processedLines += cLine + "\n"
// Check if the current line starts with 'C' and the next line starts with 'E'
if (i < cLines.size() - 1 && cLines[i + 1].startsWith("E")) {
processedLines += cLines[i + 1] + "\n"
}
}
// Check if the current line starts with 'C' and the next line starts with 'E'
else if (i < cLines.size() - 1 && cLine.startsWith("C") && cLines[i + 1].startsWith("E")) {
processedLines += cLine + "\n"
processedLines += cLines[i + 1] + "\n"
}
// Display all C lines
else if (cLine.startsWith("C")) {
processedLines += cLine + "\n"
}
}
println("Processed Lines:")
println(processedLines)
//
message.setBody(processedLines.trim()) // Set the processed payload as the message body
return message
}
----------------
java code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String inputData = "Z99@MDNET0145J8391750181750300 XQ000310 00145\n" +
"EOF@MDNET0145 XQ000310000001\n" +
"Z99@MDNET0145J8391750137576400 XQ000310 00145\n" +
"C1120712641 375764002023062983917501 000000000 0000000014987439072388 0000600000004650000000001\n" +
"C1113062901471817503132023062983917501 0000000014987439076607 0000120000000000000000002\n" +
"C1113062901471817503132023062983917501 0000000014987439076607 0000120000000000000000002\n" +
"EOF@MDNET0145 XQ000310000001";
BufferedReader reader = new BufferedReader(new StringReader(inputData));
String line;
List<String> cLines = new ArrayList<>();
try {
while ((line = reader.readLine()) != null) {
line = line.trim();
cLines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < cLines.size(); i++) {
String cLine = cLines.get(i);
// Check if the current line starts with 'C' and the previous line starts with 'Z'
if (cLine.startsWith("C") && i > 0 && cLines.get(i - 1).startsWith("Z")) {
System.out.println(cLines.get(i - 1));
System.out.println(cLine);
// Check if the current line starts with 'C' and the next line starts with 'E'
if (i < cLines.size() - 1 && cLines.get(i + 1).startsWith("E")) {
System.out.println(cLines.get(i + 1));
}
}
// Check if the current line starts with 'C' and the next line starts with 'E'
else if (cLine.startsWith("C") && i < cLines.size() - 1 && cLines.get(i + 1).startsWith("E")) {
System.out.println(cLine);
System.out.println(cLines.get(i + 1));
}
// Display all C lines
else if (cLine.startsWith("C")) {
System.out.println(cLine);
System.out.println("");
}
}
}
}