It's all about the answers!

Ask a question

How to remove an administrator from a project area programmatically


Berthold Kröger (7841923) | asked Oct 17 '14, 7:45 a.m.
Hi, I'm working on an application that manages users in project and team areas. I use following code to remove a normal member from a project area:
            IContributor user = fetchUser(repository, userId, false);
            if (user != null) {
                IContributorHandle userHandle = (IContributorHandle) user.getItemHandle();
                // Zugriff auf die Project-Area
                IAuditableCommon auditableService = getAuditableCommon(repository);
                IProjectAreaHandle projectHandle = getProjectArea(auditableService, projectAreaName);
                if (projectHandle != null && projectHandle.isAuditable()) {
                    IProjectArea projectArea = (IProjectArea) repository.itemManager().
                            fetchCompleteItem(projectHandle, IItemManager.REFRESH, monitor);
                    if (projectArea != null && !projectArea.isArchived()) {
                        // Ist der User ein Member in dieser Project-Area?
                        if (projectArea.hasMember(userHandle)) {
                            logger.fine("User "+userId+" found in Project area "+projectAreaName+"!");
                            ProjectAreaWorkingCopy area = new ProjectAreaWorkingCopy(projectArea);
                            area.removeMembers(new IContributor[] {user});
                            area.save(monitor);
                            // Kopie freigeben
                            area.dispose();
                        }

But how can I remove an adminstrator from a project area. There is no method "removeAdministrator()" for ProjectAreaWorkingCopy. The removeAdministrator() is only defined for the type IProcessArea. But if I try to do it this way:
                        if (projectArea.hasAdministrator(userHandle)) {
                            ProjectAreaWorkingCopy area = new ProjectAreaWorkingCopy(projectArea);
                            IProcessArea processArea = area.getUnderlyingProcessArea();
                            processArea.addAdministrator(userHandle);
                            area.save(monitor);
                            area.dispose();
                        }
I'll get a com.ibm.team.repository.common.internal.ImmutablePropertyException. I don't understand what I did wrong!

Comments
Berthold Kröger commented Oct 17 '14, 7:47 a.m.

Sorry there was a copy/paste error: of course the method was
processArea.removeAdministrator(userHandle) instead of
"addAdministrator()".

2 answers



permanent link
Berthold Kröger (7841923) | answered Nov 19 '14, 9:00 a.m.
After some unsuccessful attempts I finally found it: you must remove the contributor from the list of administrators in the project area working copy, e.g.

                        if (projectArea.hasAdministrator(contributorHandle)) {
                            ProjectAreaWorkingCopy projectAreaWC = new ProjectAreaWorkingCopy(projectArea);
                            IContributorListWorkingCopy adminList = projectAreaWC.getAdministrators();
                            adminList.removeContributors(new IContributor[] { contributor });
                            adminList.setDirty(true);
                            projectAreaWC.save(monitor);
                            projectAreaWC.dispose();
                        }


permanent link
sam detweiler (12.4k6179201) | answered Oct 17 '14, 8:45 a.m.
edited Oct 17 '14, 9:12 a.m.

            IProcessArea p;
            p=(IProcessArea) p.getWorkingCopy();
            p..removeAdministrator(userHandle);
           p. newitemlist.add(p);  // see below
etc

fixed
private static ITeamRepository Server = null;
private static IProcessItemService processItemClient = null;
processItemClient = (IProcessItemService) server.getClientLibrary(IProcessItemService.class);
                ArrayList<IProcessItem> newitemlist = new ArrayList<IProcessItem>();
                // allocate space for the array
                IProcessItem[] ilist = new IProcessItem[newitemlist.size()];
                // build the arrary
                ilist = newitemlist.toArray(ilist);
                // call save for the list of changed process items
                processItemClient.save(ilist, null);

processItemClient.save() takes an array of item handles. in my utility I do not know how many items will be changed in advance.. so I create a list, save the handles in the list, then turn that list into an array.

Comments
Berthold Kröger commented Oct 17 '14, 9:02 a.m.

Hi Sam, thank you for your quick answer. But there is no save() method for IProcessArea!

Regards,
Berthold

Your answer


Register or to post your answer.