Count Attribute Values

A LinkedIn member asked how to count LDAP group members with TDI.

An LDAP group is an entry with a member or uniqueMember attribute. The attribute has one value for each group member.

At first thought, I suggested using an attribute-value loop with a trivial script to increment a counter. I then thought some more, and realised that there is a much neater way to do it as we need a script anyway:

work.getAttribute("member").getValues();

That will return a Java array of objects, so counting them is easy:

work.getAttribute("member").getValues().length;

Aren't Wikis wonderful? As soon as I posted this, Eddie came up with an even better way to do it:

work.getAttribute("member").size();

The advantage here is that TDI does not have to create an array of values just so that we can ask for its length. Oddly, Eclipse does not seem to know about the size() method so it does not appear in the prompt-list.

To handle the case where there are no members and so the attribute does not exist at all it is wise to split the expression into stages:

count = 0;
memberAtt = work.getAttribute("member");
if (memberAtt) {
   count = memberAtt.size();
}

You might then want to put the value of count into a new attribute for use in the rest of the assembly line.
work.setAttribute("count", count);

-- AndrewFindlay - 12 Nov 2010
Topic revision: r2 - 17 Nov 2010, AndrewFindlay - This page was cached on 04 Aug 2023 - 19:30.

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TDI Users? Send feedback