Map message payloads by using tokens
About this task
A messaging plugin might need to convert items in the payload of a message, such as
typecodes, before sending the message on to the final destination. For many properties
governed by typelists, a typecode might have the same the meaning in both systems. However,
for typecodes that do not match across systems,, you need to map codes from one system to
another. For example, convert code A1 in PolicyCenter to the code XYZ for the external
system.
If you implement your plugin in Java, you can use a utility class included in the PolicyCenter Java API libraries that map the message payload by using
text substitution. The class, gw.pl.util.StringSubstitution, scans a
message payload to find any strings surrounded by delimiters that you define and then
substitutes a new value.
Procedure
-
Choose start and end delimiters for the text to replace.
For example, you can use the 2-character string “
**” as the start delimiter and end delimiter. For production code, you might want to use multiple special characters in a sequence that is forbidden in a real field. -
Put these delimiters around the original text that you need to map and replace.
For example, a Gosu template that generates the payload might include
"Injury=**${exposure.InjuryCode}**". This delimited text might generate text such as"Injury=**A1**"in the message payload. -
Implement a class that implements the inner interface
StringSubstitution.Exchanger.This exchanger class must translate exactly one token, not the entireStringobject for the payload. This class might use its own look-up table, a java.util.Map object, or look in a properties file. TheExchangerinterface has one method calledexchangethat translates the token. This method takes aStringobject (the token) and translates it and returns a newStringobject. If the input token requires no substitution, you must decide whether to quietly return the originalString, or throw an exception. -
Instantiate your class that implements Exchanger, and then
instantiate the StringSubstitution class with the constructor arguments
as follows.
- Start delimiter
- End delimiter
- Your Exchanger instance
- On the new StringSubstitution instance, call the substitute method to convert the message payload.
Example
The following example demonstrates this process. You can paste the following code into the Gosu Scratchpad in Studio.
uses gw.pl.util.StringSubstitution
class TestExchanger implements StringSubstitution.Exchanger {
public function exchange(s : String) : String {
print("exchange() method called with token: " + s)
if (s == "cat")
{ return "kitten"}
else if (s == "dog")
{ return "puppy"}
else return s
}
}
var origString = "wolf cat **cat** dog dog **dog** llama llama **llama**"
var myExchanger = new TestExchanger()
var mySub = new StringSubstitution("**", "**", myExchanger)
var output = mySub.substitute(origString)
print("final output is: " + output)
The example code prints the following output.
exchange() method called with token: cat
exchange() method called with token: dog
exchange() method called with token: llama
final output is: wolf cat kitten dog dog puppy llama llama llama
