cancel
Showing results for 
Search instead for 
Did you mean: 

Message Mapping - Initialize counter every context change

roger_alluivall
Participant
0 Kudos

Hi all,

is there any way to develop a counter customer function in a graphical message mapping so every context change of a field the counter initializes?

Imagine i have the following structure:

1 DATA

1.1 DETAIL

1.2 DETAIL

2 DATA

2.1 DETAIL

2.2 DETAIL

[...]

and i wanna count DETAIL fields but initialize the counter every DATA field. Is there any way to do it?

Thanks a lot.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

If your Source structure is


<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_DATA xmlns:ns0="http://yash.com.YH1309">
   <DATA>
      <DETAIL/>
      <DETAIL/>
   </DATA>
   <DATA>
      <DETAIL/>
      <DETAIL/>
      <DETAIL/>
   </DATA>
</ns0:MT_DATA>

and You want the Target as


<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_DATA xmlns:ns0="http://yash.com.YH1309">
   <DATA>
      <DETAIL>COUNTER=1</DETAIL>
      <DETAIL>COUNTER=2</DETAIL>
   </DATA>
   <DATA>
      <DETAIL>COUNTER=1</DETAIL>
      <DETAIL>COUNTER=2</DETAIL>
      <DETAIL>COUNTER=3</DETAIL>
   </DATA>
</ns0:MT_DATA>

Create the below UDF with one argument (DETAIL) and select Execution type all values of a Context


for (int i =1;i<=DETAIL.length;i++)
{
result.addValue("COUNTER=" + i);
}

but if you want Your Target as


<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_DATA xmlns:ns0="http://yash.com.YH1309">
   <DATA>
      <DETAIL>1</DETAIL>
      <DETAIL>2</DETAIL>
   </DATA>
   <DATA>
      <DETAIL>1</DETAIL>
      <DETAIL>2</DETAIL>
      <DETAIL>3</DETAIL>
   </DATA>
</ns0:MT_DATA>

So please Follow abhishek salvi's reply

roger_alluivall
Participant
0 Kudos

Ok, this is what i did:

UDF - Execution Type: All values of a context

public void counterContext(String[] CONTEXT, String[] ELEMENT, ResultList result, Container container) throws StreamTransformationException{

for (int i =1;i<=CONTEXT.length;i++)
{
for (int j = 1; j<=ELEMENT.length;j++)
{
result.addValue(j);
}
}
}

And it's working perfect!

Thank you very much for your help.

Former Member
0 Kudos

I don't know why the Std INDEX function did not work...

It looks like the same as u had in ur UDF...

-SM

Former Member
0 Kudos

Hi,

INDEX doesn't work and returns The first value (1) for all lines, I solved it by adding Split by value (parameter each value) std function.

So INDEX (with parameter reset index) std function can achieve this with no UDF.



Answers (3)

Answers (3)

former_member208856
Active Contributor
0 Kudos

Use function Statistic- Count for mapping Detail.

Also change the Context leve of Detail for Header.

roger_alluivall
Participant
0 Kudos

Well, maybe i didn't explain what i want to achieve properly.

If i have this message:

...DATA

......DETAIL

......DETAIL

......DETAIL

...DATA

......DETAIL

......DETAIL

i want to convert it as follows:

...DATA

......DETAIL = COUNTER = 1

......DETAIL = COUNTER = 2

......DETAIL = COUNTER = 3

...DATA

......DETAIL = COUNTER = 1

......DETAIL = COUNTER = 2

What is happening now is this:

...DATA

......DETAIL = COUNTER = 1

......DETAIL = COUNTER = 2

......DETAIL = COUNTER = 3

...DATA

......DETAIL = COUNTER = 4

......DETAIL = COUNTER = 5

is any way to create a counter that initializes it's starting value every context change?

Thank you in advance!

Former Member
0 Kudos

As Grube suggested, Index function (in statistic) will solve your requrirement.

No UDF required.

-SM

former_member200962
Active Contributor
0 Kudos

Source --> INDEX --> Target

Double click on INDEX function and fill the below properties:

Initial Value: 1

Increment Value: 1

Reset Index: Reset Index to initial value with each new context change.

Regards,

Abhishek.

former_member208856
Active Contributor
0 Kudos

As per my old message, change the contex level of DETAIL node for DATA.

I hope it will work.

stefan_grube
Active Contributor
0 Kudos

Use function index. No UDF required.

former_member187339
Active Contributor
0 Kudos

Hi Roger,

Try using this udf:


UDF should be an advanced one and shoudl for all values in a context

Provide Details as the input to this udf
int counter=0;

for (int i=0;i<input.length;i++)
  result.addValue(counter++);

Regards

Suraj