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.

importedArchive.png

Create StringFunctions function library.

functionLib.png

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

/wp-content/uploads/2016/09/dependency_1037818.png

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.

dependencyToSoftwareComponent.png

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

/wp-content/uploads/2016/09/functions_1037820.png

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.

leftPad.png

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

removeLeading.png

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

/wp-content/uploads/2016/09/right_1037821.png

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.

/wp-content/uploads/2016/09/substring_1037822.png

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

isBlank.png

If you want to replace using regular expression.

replacePattern.png

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.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply