-1

I am working on a WSO2 integration that needs to create a flat file - so basically, a file with only 1 column, the column is 200 some characters wide, and there is a specific protocol for what values go into which positions. For example -

  • positions 1-3 = record type
  • position 4 = blank space
  • position 5-20 = First name
  • position 21 = blank space
  • positions 22-32 = Last name

etc etc.

So in that example, the last name is given 10 "digits," which means that if the person's last name is Smith, I need to pad the property with 5 subsequent extra spaces - so like 'Smith(followed by 5 spaces)'. I've found simple solutions for this using substring(concat('$ctx:LastName', ' '), 1, 10). Which seems to work just fine for smaller sizes of padded placeholders. However, the issue I'm running into is that some of the fields I'm padding need to have over 100 characters. And I REALLY don't want to count 100 spaces in between my single quotes there. Are there any elegant solutions to dealing with this type of thing in WSO2?

Thank you.

3 Answers3

1

code:

[#assign firstName1 = "john"]
[#assign lastName1 = "doe"]
[#assign firstName2 = "abcdefghijklmnopqrstuvwxyz"]
[#assign lastName2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
[#assign firstNameMaxLength = 20]
[#assign lastNameMaxLength = 10]
[#assign placeholder = "-"]

[#function getMyProperty input outputMaxLength placeholder]
    [#if input?length > outputMaxLength]
        [#return input?substring(0,outputMaxLength)]
    [/#if]
    [#return input?right_pad(outputMaxLength,placeholder)]
[/#function]

<br>code1:${getMyProperty(firstName1,firstNameMaxLength,placeholder)}${getMyProperty(lastName1,lastNameMaxLength,placeholder)}
<br>code2:${getMyProperty(firstName2,firstNameMaxLength,placeholder)}${getMyProperty(lastName2,lastNameMaxLength,placeholder)}

output:

code1:john----------------doe------- 
code2:abcdefghijklmnopqrstABCDEFGHIJ
T.Okrajni
  • 81
  • 3
0

Based on the approach in this answer, you can do something like:

substring(concat('Smith', string-join((1 to 50)!"_")), 1, 10)

Which outputs:

'smith____'

The expression basically creates a string of 50 (or whatever you want) spaces, joins it to 'Smith' and cuts the whole thing down to 10 characters.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Thanks Jack, this does look like the perfect solution to what I'm doing. I can't seem to get the string-join to be recognized by WSO2 though. I've uncommented the "synapse.xpath.dom.failover.enabled=true" line in my synapse.properties file, but every time I try and put this in, I get "Invalid XPath Expression for attribute expression". – Sean Wesche Feb 24 '20 at 16:02
  • @SeanWesche `string-join()` is an xpath 2.0 method. It's not really my area, but it seems xpath 2.0 needs to be explicitly enabled; take a look here: https://stackoverflow.com/a/55351665/9448090 – Jack Fleeting Feb 24 '20 at 17:18
0

I ended up using a smooks mediator to handle this.

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="padEntries-smooks-config" xmlns="http://ws.apache.org/ns/synapse">
    <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:file="http://www.milyn.org/xsd/smooks/file-routing-1.1.xsd" xmlns:fl="http://www.milyn.org/xsd/smooks/fixed-length-1.3.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
        <params>
            <param name="stream.filter.type">SAX</param>
            <param name="default.serialization.on">false</param>
        </params>
        <resource-config selector="Employee180,Employee181,Employee220Domestic,Employee220Foreign,Employee240">
            <resource>org.milyn.delivery.DomModelCreator</resource>
        </resource-config>
        <ftl:freemarker applyOnElement="Employee180">
            <ftl:template>
                <!--${.vars["Employee180"].RecordType?right_pad(3)}${.vars["Employee180"].BlankSpace1?right_pad(1)}${.vars["Employee180"].ContractNumber?right_pad(6)}${.vars["Employee180"].BlankSpace2?right_pad(1)}${.vars["Employee180"].EmployeeIDNumber?right_pad(9)?replace("-","")}${.vars["Employee180"].BlankSpace3?right_pad(1)}${.vars["Employee180"].EmployeeName?right_pad(24)}${.vars["Employee180"].DateOfBirth?right_pad(10)}${.vars["Employee180"].BlankSpace4?right_pad(1)}${.vars["Employee180"].DateOfEmployment?right_pad(10)}${.vars["Employee180"].BlankSpace5?right_pad(1)}${.vars["Employee180"].Sex?right_pad(1)}${.vars["Employee180"].BlankSpace6?right_pad(8)}${.vars["Employee180"].SocialSecurityNumber?right_pad(9)}${.vars["Employee180"].BlankSpace7?right_pad(133)}${.vars["Employee180"].BenefitEventDate?right_pad(10)}${.vars["Employee180"].BlankSpace8?right_pad(5)}${.vars["Employee180"].BenefitEventReason?right_pad(4)}${.vars["Employee180"].BlankSpace9?right_pad(3)}${'\r\n'} -->
            </ftl:template>
        </ftl:freemarker>
        <ftl:freemarker applyOnElement="Employee220Domestic">
            <ftl:template>
                <!--${.vars["Employee220Domestic"].RecordType?right_pad(3)}${.vars["Employee220Domestic"].BlankSpace1?right_pad(1)}${.vars["Employee220Domestic"].ContractNumber?right_pad(6)}${.vars["Employee220Domestic"].BlankSpace2?right_pad(1)}${.vars["Employee220Domestic"].EmployeeIDNumber?right_pad(9)}${.vars["Employee220Domestic"].BlankSpace3?right_pad(1)}${.vars["Employee220Domestic"].ForeignIndicator?right_pad(1)}${.vars["Employee220Domestic"].BlankSpace4?right_pad(1)}${.vars["Employee220Domestic"].AddressLine1?right_pad(50)}${.vars["Employee220Domestic"].AddressLine2?right_pad(50)}${.vars["Employee220Domestic"].AddressLine3?right_pad(50)}${.vars["Employee220Domestic"].City?right_pad(30)}${.vars["Employee220Domestic"].State?right_pad(2)}${.vars["Employee220Domestic"].Zip?right_pad(9)}${.vars["Employee220Domestic"].BlankSpace5?right_pad(26)}${'\r\n'} -->
            </ftl:template>
        </ftl:freemarker>
        <ftl:freemarker applyOnElement="Employee220Foreign">
            <ftl:template>
                <!--${.vars["Employee220Foreign"].RecordType?right_pad(3)}${.vars["Employee220Foreign"].BlankSpace1?right_pad(1)}${.vars["Employee220Foreign"].ContractNumber?right_pad(6)}${.vars["Employee220Foreign"].BlankSpace2?right_pad(1)}${.vars["Employee220Foreign"].EmployeeIDNumber?right_pad(9)}${.vars["Employee220Foreign"].BlankSpace3?right_pad(1)}${.vars["Employee220Foreign"].ForeignIndicator?right_pad(1)}${.vars["Employee220Foreign"].BlankSpace4?right_pad(1)}${.vars["Employee220Foreign"].AddressLine1?right_pad(50)}${.vars["Employee220Foreign"].AddressLine2?right_pad(50)}${.vars["Employee220Foreign"].AddressLine3?right_pad(50)}${.vars["Employee220Foreign"].AddressLine4?right_pad(50)}${.vars["Employee220Foreign"].BlankSpace5?right_pad(17)}${'\r\n'} -->
            </ftl:template>
        </ftl:freemarker>
        <ftl:freemarker applyOnElement="Employee240">
            <ftl:template>
                <!--${.vars["Employee240"].RecordType?right_pad(3)}${.vars["Employee240"].ContractNumber?right_pad(6)}${.vars["Employee240"].EmployeeIDNumber?right_pad(9)}${.vars["Employee240"].WorkEmailAddress?right_pad(75)}${.vars["Employee240"].PersonalEmailAddress?right_pad(75)}${.vars["Employee240"].WorkPhoneNumber?right_pad(17)}${.vars["Employee240"].WorkPhoneNumberExt?right_pad(7)}${.vars["Employee240"].HomePhoneNumber?right_pad(17)}${.vars["Employee240"].HomePhoneNumberExt?right_pad(7)}${.vars["Employee240"].MobilePhoneNumber?right_pad(18)}${'\r\n'} -->
            </ftl:template>
        </ftl:freemarker>
        <ftl:freemarker applyOnElement="Employee181">
            <ftl:template>
                <!--${.vars["Employee181"].RecordType?right_pad(3)}${.vars["Employee181"].BlankSpace1?right_pad(1)}${.vars["Employee181"].ContractNumber?right_pad(6)}${.vars["Employee181"].BlankSpace2?right_pad(1)}${.vars["Employee181"].EmployeeIDNumber?right_pad(9)}${.vars["Employee181"].BlankSpace3?right_pad(1)}${.vars["Employee181"].EmployeeName?right_pad(24)}${.vars["Employee181"].BlankSpace4?right_pad(31)}${.vars["Employee181"].SocialSecurityNumber?right_pad(9)}${.vars["Employee181"].BlankSpace5?right_pad(1)}${.vars["Employee181"].RehireDate?right_pad(10)}${.vars["Employee181"].BlankSpace6?right_pad(144)}${'\r\n'} -->
            </ftl:template>
        </ftl:freemarker>
    </smooks-resource-list>
</localEntry>