cancel
Showing results for 
Search instead for 
Did you mean: 

UDF Help required

former_member200386
Active Participant
0 Kudos

Dear Experts,

I am working on a mapping logic where i will get the input data in the string , i need to generate a new field for every 5 characters.

My source structure :

MT_Source:

<config>

      <product>

         <feature id="BAYAADWAA9IDBA9ABCD3"/>

      </product>

</config>



Desired Target structure


MT_Target

<config>

      <product>

         <feature id="BAYAA"/>

         <feature id="DWAA9"/>

         <feature id="IDBA9"/>

         <feature id="ABCD3"/>

      </product>

   </config>

Please provide your valuable inputs.

Thanks,

Pavan T

Accepted Solutions (1)

Accepted Solutions (1)

vinaymittal
Contributor
0 Kudos

take input one field as "field" and one output as "result" udf type is all values of a context.

int len = field[0].length();

String str = field[0];

int number = (int)Math.ceil(len/5.0);

for(int i=0; i<number; i++)

{

if(i!=number-1)

     result.addValue(str.substring(i*5,(i+1)5));

else

     result.addValue(str.substring(i*5,str.length());

}

ex

ABCDE

FGHIJ

KLMNO

P               15-16

len 16

num = 4

ABCDEFGHIJKLMNOP

map the udf to target node featureID

former_member200386
Active Participant
0 Kudos

HI Viany,

I am getting below error while testing the mapping.

  Source text of object Message Mapping: MM_Request | http://Gforce_POC_Retrive_Catalog_Codes has syntax errors:
vinaymittal
Contributor
0 Kudos

You should use code given by sahithi its better and faster

but just to correct the typing mistakes i have corrected the code

int len = field[0].length();

String str = field[0];

int number = (int)Math.ceil(len/5.0);

for(int i=0; i<number; i++)

{

if(i!=number-1)

     result.addValue(str.substring(i*5,(i+1)*5));

else

     result.addValue(str.substring(i*5,str.length()));

}

former_member200386
Active Participant
0 Kudos

Hi Vinay,

I am getting only first field the response. Please find the mapping and test results screen shots

vinaymittal
Contributor
0 Kudos

Insert a splitbyeachvalue before mapping to the target i thought all the target nodes are in same context....

former_member200386
Active Participant
0 Kudos

Hi Vinay,

I tired the split value but no use.

vinaymittal
Contributor
0 Kudos

Can you show display queue of udf

former_member200386
Active Participant
0 Kudos
vinaymittal
Contributor
0 Kudos

Ohh the reason is that feature node is coming only once you need to map the output of udf to both id and feature

you will get the result now....

but for correct answer it should be sahithi's code thats better and correct

former_member200386
Active Participant
0 Kudos
vinaymittal
Contributor
0 Kudos

For features split by value is not required as they all fall in same context, if it doesnt work then try removing the split by value of id too but in any case splitbyvalue will not come in feature node

former_member200386
Active Participant
0 Kudos

Hi Vinay,

I Removed split value for for feature. I am getting id values in feature also. i want values to be populate in "id" field only.

<?xml version="1.0" encoding="UTF-8" ?>

- <ns1:DescribeConfigRequest xmlns:ns1="urn:ford/Describe">

<ns1:Catalog id="" />

- <ns1:Config>

- <ns2:Product xmlns:ns2="urn:ford/Common/DescribeModel">

<ns2:Feature id="BAYAA">BAYAA</ns2:Feature>

<ns2:Feature id="DWAA9">DWAA9</ns2:Feature>

<ns2:Feature id="IDBA9">IDBA9</ns2:Feature>

<ns2:Feature id="ABCD3">ABCD3</ns2:Feature>

</ns2:Product>

</ns1:Config>

- <ns1:ContentSets>

- <ns2:ContentSet xmlns:ns2="urn:ford/Common/DescribeModel" id="" targetType="">

<ns2:Item target="" name="" />

</ns2:ContentSet>

</ns1:ContentSets>

</ns1:DescribeConfigRequest>

vinaymittal
Contributor
0 Kudos

usually it doesnt happens the node doesnt show the value because its a node not a field in this case please do as below

use createif , exists node function after the output of title udf while mapping ONLY for feature node''

Title--->Exists-->createIf--->Features   

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Pavan,

Here Feature is not a node, it is a field which is having id as its attribute.

Can you check the solution provided by vinay and let us know if you still facing any issues.

former_member200386
Active Participant
0 Kudos

Hi Vinay,

Thanks a ton for your help. Now  i am able to generate the data id field only. I am closing theread.

@ Vishnu, Sahithi thanks for your valuable inputs.

Thnaks,

Pavan T

Answers (3)

Answers (3)

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Pavan,

You can use the code given by Sahithi as it is looking optimized one if you ask me.

Just in the code replace input with input[0] and use the same exact code as it is.

Here, for target Feature node, give the output of the udf as it is.

For Id target attribute, use split by value after the udf and pass to the target.

@Sahithi: I assume you don't mind on this. I have just though of writing the code using if and else but saw the code given by you in the mean time, so just told the minor correction which you have forgotten..

sahithi_moparthi
Contributor
0 Kudos

Achieve using below UDF:

int size = 5;

for (int i = 0; i < input.length(); i += size){

   int end = i + size > input.length() ? input.length() : i + size;

   result.addValue(input.substring(i, end));

}

former_member186851
Active Contributor
0 Kudos

Pavan,

I guess you cant use the same names for mutliple fields in XML.

if your requirement is to achieve something like below

   <feature id="BAYAA"/>

         <feature id1="DWAA9"/>

         <feature id2="IDBA9"/>

         <feature id3="ABCD3"/>

you can use Substring function in mapping function to achieve the requirment.

Feature Id = substring(0,5)

Feature Id 1 = substring(5,5)

Feature Id 2 = substring(10,5)

Feature Id 3 = substring ( 15.5)

vinaymittal
Contributor
0 Kudos

>I guess you cant use the same names for mutliple fields in XML.


It can have a occurence 0.unbounded and we can have the same name occuring multiple times

former_member186851
Active Contributor
0 Kudos

Ya ..Agreed..Got a bit confused.

vinaymittal
Contributor
0 Kudos

just a reminder pavan u havent closed this thread yet

its important to close a thread and mark correct answer once the problem is solved/you get the answer