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: 
former_member182412
Active Contributor

Introduction

There is a common requirement to write user defined functions in the message mapping for string functions, normally we write our own functions when we get the requirement, instead of writing our own functions use StringUtils functions in PI/PRO because if we write the function we need to test properly but StringUtils functions already well tested by numerous people.

In this blog i want to show you how to use StringUtils functions in PI/PRO.

Create StringFunctions Function Library

Create common software component like below and create imported archive and import the commons lang jar.

Create StringFunctions function library.

Add commons lang imported archive in StringFunctions function library under dependencies.

And add the methods in StringFunctions function library.

leftPad function


@LibraryMethod(title = "leftPad", description = "", category = "StringFunctions", type = ExecutionType.SINGLE_VALUE)
  public String leftPad(@Argument(title = "") String str, @Parameter(title = "") int size, @Parameter(title = "") String pad,
  Container container) throws StreamTransformationException {
  return StringUtils.leftPad(str, size, pad);
  }

stripStart function


@LibraryMethod(title = "stripStart", description = "", category = "StringFunctions", type = ExecutionType.SINGLE_VALUE)
  public String stripStart(@Argument(title = "") String str, @Parameter(title = "") String remove, Container container)
  throws StreamTransformationException {
  return StringUtils.stripStart(str, remove);
  }

substring function


@LibraryMethod(title = "substring", description = "Gets a substring from the specified String avoiding exceptions", category = "StringFunctions", type = ExecutionType.SINGLE_VALUE)
  public String substring(@Argument(title = "") String source, @Parameter(title = "") int start,
  @Parameter(title = "") int end, Container container) throws StreamTransformationException {
  return StringUtils.substring(source, start, end);
  }

replacePattern function


@LibraryMethod(title = "replacePattern", description = "Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern.DOTALL option", category = "StringFunctions", type = ExecutionType.SINGLE_VALUE)
  public String replacePattern(@Parameter(title = "") String regex, @Parameter(title = "") String replacement,
  @Argument(title = "") String source, Container container) throws StreamTransformationException {
  return StringUtils.replacePattern(source, regex, replacement);
  }

isBlank function


@LibraryMethod(title = "isBlank", description = "Checks if a String is whitespace, empty (\"\") or null", category = "StringFunctions", type = ExecutionType.SINGLE_VALUE)
  public String isBlank(@Argument(title = "") String input, Container container) throws StreamTransformationException {
  return String.valueOf(StringUtils.isBlank(input));
  }

right function


@LibraryMethod(title = "right", description = "", category = "StringFunctions", type = ExecutionType.SINGLE_VALUE)
  public String right(@Argument(title = "") String str, @Parameter(title = "") int len, Container container)
  throws StreamTransformationException {
  return StringUtils.right(str, len);
  }

Use StringFunction in Message Mapping

Add common software component which we created above to the original software component.

Once we created the dependency then we can see them under functions.

Testing

The most common requirement in message mapping is add leading zero's, we can do this easily with leftPad function without writing any code.

The another most common requirement is to remove the leading zero's in message mapping, this we can do easily with function stripFirst.


If we want to get the characters from the end of the string we can use right function.

Normal substring function will throw an exception if the source string is less than what we configured in the substring function but StringUtils substring function will not throw an exception.

If the given string is blank or not you can use isBlank function from StringUtils.

If you want to replace using regular expression.

Conclusion

By creating StringFunctions in a common software component by wrapping StringUtils fuctions we no need to write the user defined functions from scratch, we can use ready made functions from StringUtils class. I hope this helps to the community.

1 Comment
Labels in this area