cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for a help to write UDF for sort and group records from the source

praveen_vanga3
Participant
0 Kudos

I would like to generate a target structure based on the source. in the source two records with the first 4 fields are same then considered to be header and then remaining are details.in the target two DeviceItems should be generated.the below will be the output.

Could you please provide suggestions for the below udf?

List<String> list = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
String key = "";
for (int i = 0; i < var1.length; i++) {
if (!var1[i].equals(ResultList.CC)) {
sb.setLength(0);
if (key.equals(""))
{
result1.addValue("");
result1.addValue(ResultList.CC);
result2.addValue(var1[i]);
result2.addValue(ResultList.CC);
result3.addValue(var2[i]);
result3.addValue(ResultList.CC);
result4.addValue(var3[i]);
result4.addValue(ResultList.CC);
result5.addValue(var4[i]);
result5.addValue(ResultList.CC);
result6.addValue("");
result7.addValue(var5[i]);
result7.addValue(ResultList.CC);
result8.addValue(var6[i]);
result8.addValue(ResultList.CC);
result9.addValue(var7[i]);
result9.addValue(ResultList.CC);
result10.addValue(var8[i]);
result10.addValue(ResultList.CC);
}
key = sb.append(var1[i]).append(var2[i]).append(var3[i]).append(var5[i]).toString();

if (list.contains(key))
{
//result6.addValue("");
result7.addValue(var5[i]);
result7.addValue(ResultList.CC);
result8.addValue(var6[i]);
result8.addValue(ResultList.CC);
result9.addValue(var7[i]);
result9.addValue(ResultList.CC);
result10.addValue(var8[i]);
result10.addValue(ResultList.CC);

}
else {
list.add(key);
result1.addValue("");
result1.addValue(ResultList.CC);
result2.addValue(var1[i]);
result2.addValue(ResultList.CC);
result3.addValue(var2[i]);
result3.addValue(ResultList.CC);
result4.addValue(var3[i]);
result4.addValue(ResultList.CC);
result5.addValue(var4[i]);
result5.addValue(ResultList.CC);
result6.addValue("");
result7.addValue(var5[i]);
result7.addValue(ResultList.CC);
result8.addValue(var6[i]);
result8.addValue(ResultList.CC);
result9.addValue(var7[i]);
result9.addValue(ResultList.CC);
result10.addValue(var8[i]);
result10.addValue(ResultList.CC);
}
}
}

Thank you

Praveen

Accepted Solutions (1)

Accepted Solutions (1)

former_member190293
Active Contributor

Hi Praveen!

Regards, Evgeniy.

praveen_vanga3
Participant
0 Kudos

Hi Evgeniy,

Oh! my good, Thanks for your efforts. you are one of the best developers, I used to read each thread answered by you from SCN.I have done mapping my own. Could you please check and provide your valuable comments?

I would like to just learn myself rather copied. Thank you very much.

Look forward for your comments.

Thanks

Praveen

former_member190293
Active Contributor
0 Kudos

Hi Praveen!

Thank you very much for your kind feedback!

Step 2: I guess you can't directly map header fields. Its order should be changed too according to sorting order implemented in Step 1.

Regards, Evgeniy.

former_member190293
Active Contributor
0 Kudos

Step 3: As you could see, I've used one design area to implement the mappings for all target nodes. If you take a look at your Step 1, you'll see that SplitByValue step produces the queue with exactly the same elements count and context changes you need to map to DeviceItems node. So, why not to use it instead of reproducing the same mapping again? 🙂

Regards, Evgeniy.

former_member190293
Active Contributor
0 Kudos

Step 4: since ocurrence of these fields is 0..1, you should use MapWithDefault before RemoveContexts to avoid loosing the empty contexts in case the source element doesn't exist.

Regards, Evgeniy.

praveen_vanga3
Participant
0 Kudos

Hi Evgeniy,

Thank you very much for your time. I have two more things need to added in the mapping.

1.Delete duplicates.

2 Drop records where status are not on the fixed values.

For the above I have created two separate mapping but I would like to create a single mapping and that too give no exception when there are no records.

a) Delete logic


List<String> list = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
String key = "";
for (int i = 0; i < var1.length; i++) {
if (!var1[i].equals(ResultList.CC)) {
sb.setLength(0);
key = sb.append(var1[i]).append(var2[i]).append(var3[i]).append(var4[i]).append(var5[i]).append(var6[i]).append(var7[i]).append(var8[i]).toString();
if (list.contains(key))
result.addSuppress();
else {
list.add(key);
result.addValue("");
}
}
}

a) Drop the records of the statuses not in fixed values.

For the above I have created two separate mappings, Could you please suggest me ? How do I include it into single a mapping?

Thanks

Praveen

former_member190293
Active Contributor
0 Kudos

Hi Praveen!

When you build the mapping for any of target fields, you can drag-n-drop another target element(s) to same mapping area using right mouse button.

Regards, Evgeniy.

praveen_vanga3
Participant
0 Kudos

Ok, Thank you... How to create a target based on values maintained in the fixed values? this should be happened to the mapping you have mentioned earlier. I tried above , I am unable to do it in the same mapping.

Thanks

Praveen

Answers (2)

Answers (2)

former_member366655
Active Participant

Hi Praveen,

As per my understanding , it can be achieved using Graphical Mapping only with proper context handling.

Regards,

Vivek Jain

praveen_vanga3
Participant
0 Kudos

Hi Vivek,

Thank you, yes I tied with Graphical mapping . in the mapping I have to sort records with first 4 fields, if the 2 records or 3 are same then first 4 fields considered as header and remaining are details.so I have written UDF. can you share your views ? how to sort with combination of 4 fields with Graphical mapping.

Thank you

Praveen

former_member190293
Active Contributor
0 Kudos

Hi Praveen!

Are you sure that you need UDF to fulfill your requirement?

Regards, Evgeniy.

praveen_vanga3
Participant
0 Kudos

Hi Evgeniy,

Thank you... I am not sure , I have tried with Graphical mapping and then used UDF . I have to sort the records with combination of first 4 fields and then it requires grouping.

Thanks

Praveen