How best to compare attributes in an operation advisor?
Hi all (and Ralph in particular :) ),
Accepted answer
Hi Cliff,
I think this is one of the many questions that has the ominous answer "It depends".
I don't think there is a general answer that works for all. My experience (and my Java knowledge is also very limited in some areas).
1. Comparing the object usually only works if these Objects implement the necessary interfaces. I rarely use that.I usually at least make sure the object is not null and I can cast to what I expect.
2. If you have an IItemHandle, IItem or IAuditable you can try to get away with the UUID in the handle e.g. com.ibm.team.repository.common.IItemHandle.getItemId() and maybe the stateID com.ibm.team.repository.common.IItemHandle.getStateId() This would be for all kinds of stuff such as contributors. I have to confess that I only found that out recently and have not really used it that often. To be sure you can resolve the item and get the ID or value or what ever.
3. Strings, Dates etc often requires some kind of comparison/format change etc. so even if equals() works might need further considerations and tests
4. Enumerations, you can compare the literals by ID instead by the values only if the enumeration is the same.
5. Often you have different domains e.g. a user name as string and the contributor, or two different enumerations then you have to resolve and compare the specific values or have a mapping to help with that.
Comments
I can't seem to add a comment but to follow up a little:
1 vote
Yes. Makes sense. The contributor, if they are from within one CCM, the UUID should be OK, otherwise the ID is the best as you mention. Note the ID won't necessarily work across JTS's that is why the LDAP sync also looks at the name.
You might be interested in looking into the Work Item Command Line code, where I use similar approaches. e.g. it is possible to treat various string types similarly (Summary and HTML are different) and so forth.
It can give you at least some ideas of challenges I had....
https://rsjazz.wordpress.com/2017/03/29/the-work-item-command-line-is-now-open-source/
Cliff, note that for getting the UUID e.g. for a contributor object you only need the handle. So you don't always have to resolve the handle. This can save a lot of time.
Thanks again Ralph. We have a single JTS, a single CCM and validate against LDAP but I see what you mean about multiple instances. I'll have a play with the UUIDs, anything to improve efficiency is good news.
One other answer
If you know the type of the attributes is IContributor, you can simply cast the object instance from getValue() to that type:
IAttribute attribute1 = ...;
IContributor contributor1 = (IContributor) attribute1.getValue();
IAttribute attribute2 = ...;
IContributor contributor2 = (IContributor) attribute2.getValue();
if (contributor1.sameItemId(contributor2)) {
// do magic
}