It's all about the answers!

Ask a question

Is it possible to rename a component via API?


hidetaka ochiai (32) | asked Aug 28 '15, 9:03 a.m.
I'm looking for  API to change the component name in the stream.
It was not to SCM CLI.

Is it possible to rename a component via  API?

Any help is appreciated.
Thanks.

Accepted answer


permanent link
Kohji Ohsawa (595138) | answered Aug 28 '15, 12:34 p.m.
JAZZ DEVELOPER
Hi Ochiai-san,

Yes, it would be possible by using IWorkspaceManager#renameComponent. Here is a sample. This sample finds all project areas, streams and components from a repository, but I think you can arrange it with defining query criteria. Hopefully it would help you.

*****

package update.sample;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import com.ibm.team.build.client.ITeamBuildClient;
import com.ibm.team.build.common.model.BuildState;
import com.ibm.team.build.common.model.IBuildDefinition;
import com.ibm.team.build.common.model.IBuildEngine;
import com.ibm.team.build.common.model.IBuildResult;
import com.ibm.team.build.common.model.IBuildResultContribution;
import com.ibm.team.build.common.model.query.IBaseBuildDefinitionQueryModel;
import com.ibm.team.build.common.model.query.IBaseBuildResultQueryModel;
import com.ibm.team.build.common.model.query.IBaseBuildDefinitionQueryModel.IBuildDefinitionQueryModel;
import com.ibm.team.build.internal.common.model.query.BaseBuildDefinitionQueryModel.BuildDefinitionQueryModel;
import com.ibm.team.process.client.IProcessItemService;
import com.ibm.team.process.common.IProcessAreaHandle;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.process.common.ITeamArea;
import com.ibm.team.process.common.ITeamAreaHandle;
import com.ibm.team.process.common.ITeamAreaHierarchy;
import com.ibm.team.repository.client.IItemManager;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.common.IFetchResult;
import com.ibm.team.repository.common.IItemHandle;
import com.ibm.team.repository.common.ItemNotFoundException;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.common.query.IItemQuery;
import com.ibm.team.repository.common.query.IItemQueryPage;
import com.ibm.team.repository.common.query.ast.IItemHandleInputArg;
import com.ibm.team.repository.common.query.ast.IPredicate;
import com.ibm.team.repository.common.service.IQueryService;
import com.ibm.team.repository.common.transport.ConnectionException;
import com.ibm.team.scm.client.IWorkspaceConnection;
import com.ibm.team.scm.client.IWorkspaceManager;
import com.ibm.team.scm.client.SCMPlatform;
import com.ibm.team.scm.common.IChangeSet;
import com.ibm.team.scm.common.IChangeSetHandle;
import com.ibm.team.scm.common.IComponent;
import com.ibm.team.scm.common.IComponentHandle;
import com.ibm.team.scm.common.IWorkspace;
import com.ibm.team.scm.common.IWorkspaceHandle;
import com.ibm.team.scm.common.dto.IChangeSetSearchCriteria;
import com.ibm.team.scm.common.dto.IWorkspaceSearchCriteria;
import com.ibm.team.scm.common.internal.dto.WorkspaceSearchCriteria;
import com.ibm.team.workitem.client.IAuditableClient;
import com.ibm.team.workitem.client.IQueryClient;
import com.ibm.team.workitem.common.expression.AttributeExpression;
import com.ibm.team.workitem.common.expression.Expression;
import com.ibm.team.workitem.common.expression.IQueryableAttribute;
import com.ibm.team.workitem.common.expression.QueryableAttributes;
import com.ibm.team.workitem.common.model.AttributeOperation;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.query.IQueryResult;
import com.ibm.team.workitem.common.query.IResolvedResult;

public class RenameComponentSample {
   
    private static class LoginHandler implements ILoginHandler, ILoginInfo {
       
        private String fUserId;
        private String fPassword;
       
        private LoginHandler(String userId, String password) {
            fUserId= userId;
            fPassword= password;
        }
       
        public String getUserId() {
            return fUserId;
        }
       
        public String getPassword() {
            return fPassword;
        }
       
        public ILoginInfo challenge(ITeamRepository repository) {
            return this;
        }
    }
   
    public static void main(String[] args) {
       
        String repositoryURI= "https://localhost:9443/ccm";
        String userId= "clmadmin";
        String password= "password";
       
        try {
            TeamPlatform.startup();
            ITeamRepository repository= login(repositoryURI, userId, password);
           
            findAllProjectAreas(repository);
           
        } catch (ItemNotFoundException e) {
            System.err.println(e.getMessage());
        } catch (ConnectionException e) {
            System.err.println(e.getMessage());
        } catch (TeamRepositoryException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            TeamPlatform.shutdown();
        }
    }
   
    private static void findAllProjectAreas(ITeamRepository repository) throws TeamRepositoryException {
        IProcessItemService itemService = (IProcessItemService) repository.getClientLibrary(IProcessItemService.class);
        List projectAreaList = itemService.findAllProjectAreas(null, null);
        for (Object obj: projectAreaList) {
            IProjectArea projectArea = (IProjectArea)obj;
            findAllStreams(projectArea);
        }
    }
   
    private static void findAllStreams(IProjectArea projectArea) throws TeamRepositoryException {
        IProgressMonitor monitor = new NullProgressMonitor();
        ITeamRepository repository = (ITeamRepository)projectArea.getOrigin();
        IWorkspaceManager workspaceManager = SCMPlatform.getWorkspaceManager(repository);
        IWorkspaceSearchCriteria wsSearchCriteria = WorkspaceSearchCriteria.FACTORY.newInstance();
        wsSearchCriteria.setKind(IWorkspaceSearchCriteria.ALL);
        wsSearchCriteria.setPartialOwnerNameIgnoreCase(projectArea.getName());
   
        List <IWorkspaceHandle> workspaceHandles = workspaceManager.findWorkspaces(wsSearchCriteria, Integer.MAX_VALUE, monitor);
        for (IWorkspaceHandle workspaceHandle: workspaceHandles) {
            IWorkspace workspace = (IWorkspace)repository.itemManager().fetchCompleteItem(workspaceHandle, IItemManager.DEFAULT, monitor);
            System.out.println("Workspace: " + workspace.getName() + " (Project Area:" + projectArea.getName() + ")");
           
            findAllComponents(repository, workspaceHandle);
        }
       
        List teamAreaList = projectArea.getTeamAreas();
        for(Object obj: teamAreaList){
            ITeamAreaHandle teamAreaHandle = (ITeamAreaHandle)obj;
            ITeamArea teamArea = (ITeamArea)repository.itemManager().fetchCompleteItem(teamAreaHandle, IItemManager.DEFAULT, monitor);
            wsSearchCriteria = WorkspaceSearchCriteria.FACTORY.newInstance();
            wsSearchCriteria.setKind(IWorkspaceSearchCriteria.ALL);
            wsSearchCriteria.setPartialOwnerNameIgnoreCase(teamArea.getName());
       
            workspaceHandles = workspaceManager.findWorkspaces(wsSearchCriteria, Integer.MAX_VALUE, monitor);
            for (IWorkspaceHandle workspaceHandle: workspaceHandles) {
                IWorkspace workspace = (IWorkspace)repository.itemManager().fetchCompleteItem(workspaceHandle, IItemManager.DEFAULT, monitor);
                System.out.println("Workspace: " + workspace.getName() + " (Project Area:" + teamArea.getName() + ")");
               
                findAllComponents(repository, workspaceHandle);
            }
        }
    }
   
    private static void findAllComponents(ITeamRepository repository, IWorkspaceHandle workspaceHandle) throws TeamRepositoryException {
        IProgressMonitor monitor = new NullProgressMonitor();
        IWorkspaceManager workspaceManager = SCMPlatform.getWorkspaceManager(repository);
        IWorkspaceConnection workspaceConnection = workspaceManager.getWorkspaceConnection(workspaceHandle, null);
        List componentList = workspaceConnection.getComponents();

        for (Object obj : componentList) {
            int i = 0;
            IComponentHandle componentHandle = (IComponentHandle)obj;
            IComponent component = (IComponent)repository.itemManager().fetchCompleteItem(componentHandle, IItemManager.DEFAULT, monitor);
            System.out.println("Component: " + component.getName() + ", Component UUID: " + componentHandle.getItemId());
            workspaceManager.renameComponent(componentHandle, "renamed1", null);
        }
    }
       
    private static ITeamRepository login(String repositoryURI, String userId, String password) throws TeamRepositoryException {
        ITeamRepository teamRepository= TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
        teamRepository.registerLoginHandler(new LoginHandler(userId, password));
        teamRepository.login(null);
        return teamRepository;
    }
}


hidetaka ochiai selected this answer as the correct answer

One other answer



permanent link
hidetaka ochiai (32) | answered Aug 31 '15, 12:14 p.m.

Hi Ohsawa-san,

Thank you for the sample code.
By reference to this sample code, it is going to result to expect.

Best regards,

Your answer


Register or to post your answer.