It's all about the answers!

Ask a question

How to check for user existence in jazz repository using Java API


1
1
Kaushambi Singh (37139979) | asked Jul 11 '13, 3:21 a.m.
 I can add users to members section in team area. 
But before the user get added , I want to put a check if the user is valid user and exists in jazz repository. How to put this check if the user exists in jazz repository ? Any example syntax would be of great help.

Part of code which adds the user:

ITeamArea TA = (ITeamArea)teamRepository.itemManager().fetchCompleteItem(newTAHandle,ItemManager.DEFAULT,monitor);
 IContributor contributor = teamRepository.contributorManager().fetchContributorByUserId("user_id",monitor);
 IProcessAreaWorkingCopy areaWc = (IProcessAreaWorkingCopy)service.getWorkingCopyManager().createPrivateWorkingCopy(TA); 

//adding the user here and want to put a check here 
        areaWc.getTeam().addContributorsSettingRoleCast(
                                    new IContributor[] {contributor}, 
                                    new IRole[] {roles}); 

Accepted answer


permanent link
Ralph Schoon (58.0k23642) | answered Jul 11 '13, 4:08 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
If the User with that ID would not exist, your .fetchContributorByUserId() call would not return a value and probably trow an exception you could catch.
Kaushambi Singh selected this answer as the correct answer

Comments
Kaushambi Singh commented Jul 11 '13, 8:18 a.m.

 Thanks! Ralph.


Kaushambi Singh commented Jul 12 '13, 6:02 a.m.

 Hi Ralph, your answer fits for my questions completely but I also wanted to know how to make that check in general for any other scenario? How can we check if userId exists in jazz repository or not?


Ralph Schoon commented Jul 12 '13, 7:18 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

As I see it, you have the teamRepository.contributorManager() available. You can either use teamRepository.contributorManager().fetchAllContributors(monitor) to fetch all contributors that are registered to the repository, this contains archived users as well. Or you have some ID and can use fetchContributorByID and you will get the contributor if one exists for the ID or an exception if not.

Or am I misunderstanding the question?

One other answer



permanent link
Albert Yao (5411118) | answered Jul 11 '17, 6:14 a.m.

Why the return value of fetchContributorByUserId() is not null(in fact, it is never null by design) but throwing an exception when the input parameter "userID" does not exist/match in the repository?

The design of RTC user management API is poor!


Comments
Ralph Schoon commented Jul 11 '17, 6:29 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

To document that in case of possible errors no null value is returned and to throw exceptions in error cases is a very clean design. Exceptions can also be easily used together with try/catch by a capable developer to implement control flows.


Albert Yao commented Jul 12 '17, 12:43 a.m.

3ks, Ralph!

I know the way of controling flows using together with try/catch in Java exception,
but I just do not like it, it looks like intricately and not directly.


Your answer


Register or to post your answer.