Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
BhaskarY
Participant
In this blog, we will be going through few B2BNodePool functions and the corresponding groovy scripts in SAP CPI which will help us to get the required outputs for the given inputs. This Blog can be considered as the part two of the below blog.

Part1: UDFNodePool Functions for SAP CPI with Groovy Scripting

In the part one, below are the list of B2B Node Pool functions discussed.

  • createIfExistsAndHasValue

  • createIfExistsAndHasOneOfSuchValues

  • passIfExistsAndHasValue

  • passIfExistsAndHasOneOfSuchValues

  • existsAndHasValue

  • existsAndHasOneOfSuchValues

  • simpleUseOneAsMany

  • deleteSuppress

  • getFirstContextValue

  • contextHasOneOfSuchValues

  • assignValueByCondition

  • getValueByIndex


In the current blog we will be going through the below B2B Node Pool functions.

  • concatContextValues

  • concatToOneQueue

  • concatTwoQueuesToOne

  • createMultipleCopies

  • createMultipleContextCopies

  • useOneContextAsMany

  • suppressMultipleContextValues

  • deleteMultipleContextValues

  • simpleUseOneAsManyAndSplitByEachValue

  • rearrangeByKey


concatContextValues:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void concatContextValues(String[] Input, String[] Separator, Output output)
{
StringBuilder result = new StringBuilder()
String delimiter = Separator[0];
if (Input[0] != null){
result.append(Input[0])
}
for (int i=1; i<Input.size();i++)
{
if (Input[i] != null && Input[i].trim().length() > 0 && !output.isSuppress(Input[i]))
{
result.append(delimiter).append(Input[i])
}
}
result.toString()
output.addValue(result)
}



 

concatToOneQueue:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void concatToOneQueue(String[] queue1, String[] queue2, String[] queue3, String[] queue4, String[] queue5, Output output) throws Exception{

if (queue1 != null && queue1.length > 0) {
for (int i = 0; i < queue1.length; i++) {
output.addValue(queue1[i])
}
} else {
output.addSuppress()
}
if (queue2 != null && queue2.length > 0) {
for (int i = 0; i < queue2.length; i++) {
output.addValue(queue2[i])
}
} else {
output.addSuppress()
}
if (queue3 != null && queue3.length > 0) {
for (int i = 0; i < queue3.length; i++) {
output.addValue(queue3[i])
}
} else {
output.addSuppress()
}
if (queue4 != null && queue4.length > 0) {
for (int i = 0; i < queue4.length; i++) {
output.addValue(queue4[i])
}
} else {
output.addSuppress()
}
if (queue5 != null && queue5.length > 0) {
for (int i = 0; i < queue5.length; i++) {
output.addValue(queue5[i])
}
} else {
output.addSuppress()
}
}




Note: The code here has been written for five different queues, but we can edit to our convenience. For example, if you want to concat only two queues, you can refer the next function.

concatTwoQueuesToOne:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void concatTwoQueuesToOne(String[] queue1, String[] queue2, Output output) throws Exception{
if (queue1 != null && queue1.length > 0) {
for (int i = 0; i < queue1.length; i++) {
output.addValue(queue1[i])
}
} else {
output.addSuppress()
}
output.addContextChange()
if (queue2 != null && queue2.length > 0) {
for (int i = 0; i < queue2.length; i++) {
output.addValue(queue2[i])
}
} else {
output.addSuppress()
}
}



 

createMultipleCopies:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void createMultipleCopies(String[] contextValues, String[] copyCounts, Output output) throws Exception {
if (contextValues == null || contextValues.length == 0) {
throw new Exception("createMultipleCopies: contextValues is null or empty")
}

if (copyCounts == null || copyCounts.length != contextValues.length) {
throw new Exception("createMultipleCopies: copyCounts is null or has a different length than contextValues")
}

for (int i = 0; i < contextValues.length; i++) {
String value = contextValues[i]
if (value != null && value.trim().length() > 0) {
try {
int count = Integer.parseInt(copyCounts[i])
if (count > 0) {
for (int j = 0; j < count; j++) {
output.addValue(value)
}
} else {
output.addSuppress()
}
} catch (NumberFormatException ex) {
throw new Exception("UDF createMultipleCopies: " + copyCounts[i] + " is not a valid number")
}
} else {
output.addSuppress()
}
}
}



 

createMultipleContextCopies:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void createMultipleContextCopies(String[] contextValues, String[] copyCounts, Output output) throws Exception{

if (copyCounts.length == 0) {
throw new Exception("UDF createMultipleContextCopies: "
+ " copyCounts has length 0")
}
int count = 0;
try {
count = Integer.parseInt(copyCounts[0])
} catch (Exception ex) {
throw new Exception("UDF createMultipleContextCopies: "
+ copyCounts[0] + " is not a number")
}
if (count > 0) {
for (int i = 0; i < contextValues.length; i++) {
output.addValue(contextValues[i])
}
for (int i = 1; i < count; i++) {
output.addContextChange();
for (int k = 0; k < contextValues.length; k++) {
output.addValue(contextValues[k])
}
}
} else {
for (int i = 0; i < contextValues.length; i++) {
output.addSuppress()
}
}
}



 

useOneContextAsMany:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void useOneContextAsMany(String[] contextValues, String[] secondContext, Output output) throws Exception{

if (contextValues != null && contextValues.length > 0) {
if (secondContext != null && secondContext.length > 0) {
for (int i = 0; i < secondContext.length - 1; i++) {
for (int j=0; j < contextValues.length; j++) {
output.addValue(contextValues[j])
}
output.addContextChange()
}
// the last context
for (int j=0; j < contextValues.length; j++) {
output.addValue(contextValues[j])
}
}
}
}



 

suppressMultipleContextValues:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void suppressMultipleContextValues(String[] contextValues, Output output) throws Exception{

List values = new ArrayList()
if (contextValues != null && contextValues.length > 0) {
for (int i = 0; i < contextValues.length; i++) {
if (output.isSuppress(contextValues[i])) {
output.addSuppress()
} else if (!values.contains(contextValues[i])) {
values.add(contextValues[i])
output.addValue(contextValues[i])
} else {
output.addSuppress()
}
}
}
}



 

deleteMultipleContextValues:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void deleteMultipleContextValues(String[] contextValues, Output output) throws Exception{

List values = new ArrayList()
if (contextValues != null && contextValues.length > 0) {
for (int i = 0; i < contextValues.length; i++) {
if (!values.contains(contextValues[i])) {
values.add(contextValues[i])
output.addValue(contextValues[i])
}
}
}
}



 

simpleUseOneAsManyAndSplitByEachValue:



import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.mapping.*;

def void simpleUseOneAsManyAndSplitByEachValue(String[] contextValues, String[] secondContext, Output output) throws Exception{


if (contextValues != null && contextValues.length > 0
&& secondContext != null && secondContext.length > 0) {
String value = null;
if (contextValues.length == 1) {
value = contextValues[0]
} else {
for (int i = 0; i < contextValues.length; i++) {
if (!output.isSuppress(contextValues[i])) {
value = contextValues[i]
break;
}
}
if (value == null) {
value = contextValues[0]
}
}
output.addValue(value)
for (int i = 1; i < secondContext.length; i++) {
output.addContextChange()
output.addValue(value)
}
}
}



 

rearrangeByKey:



import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.it.api.mapping.*;
def void rearrangeByKey(String[] arrangedSetOfKeys, String[] allKeys, String[] valuesToRearrange, Output output) throws Exception{

if (allKeys == null || valuesToRearrange == null
|| allKeys.length != valuesToRearrange.length) {
throw new Exception("UDF rearrangeByKey: "
+ "allKeys and valuesToRearrange have different lengths")
}

if (valuesToRearrange == null || valuesToRearrange.length == 0) {
for (int i = 0; i < arrangedSetOfKeys.length; i++) {
output.addSuppress()
}
} else {
if (arrangedSetOfKeys == null || arrangedSetOfKeys.length == 0) {
throw new Exception(
"UDF rearrangeByKey: there is no arrangedKeySet")
}

Set indexSet = new HashSet()
for (int i = 0; i < allKeys.length; i++) {

if (output.isSuppress(allKeys[i])
|| allKeys[i].trim().length() == 0) {
// keep suppress values
output.addSuppress()
} else {
// iterate over the arranged key set
for1 : for (int k = 0; k < arrangedSetOfKeys.length; k++) {
for (int m = 0; m < allKeys.length; m++) {
if (indexSet.contains(new Integer(m))) {
// value is already processed; ignore
continue;
}
if (output.isSuppress(allKeys[m])
|| allKeys[m].trim().length() == 0) {
continue;
}
if (arrangedSetOfKeys[k].equals(allKeys[m])) {
// value is not yet already processed;
// add to output list and keep in mind by
// putting onto a set
output.addValue(valuesToRearrange[m])
indexSet.add(Integer.valueOf(m + ""))
break for1;
}
}
}
}
}
}
}



 

Hope you have a great read and had a better understanding on the functions from the above details.

Please comment and provide your feedback for the same.

Useful Links:

SAP B2B User Define Functions Official Release

B2B UDFNodePool Explained in Easy Example in SAP PO

 
2 Comments
Labels in this area