Search code examples
javartc

programmatically load repository workspace components into local workspace


i'm retriving my repository workspace programatically with this snippet:

IWorkspaceManager workspaceManager = SCMPlatform.getWorkspaceManager(teamRepository);
IWorkspaceSearchCriteria wsSearchCriteria = WorkspaceSearchCriteria.FACTORY.newInstance();
wsSearchCriteria.setKind(IWorkspaceSearchCriteria.WORKSPACES);
wsSearchCriteria.setExactOwnerName("ownerName"); //replaced with real parameter
List<IWorkspaceHandle> workspaceHandles = workspaceManager.findWorkspaces(wsSearchCriteria,Integer.MAX_VALUE, monitor);

 //so, here i got my repWorkSpace:
IRepositoryWorkspace myDesiredRepositoryWorkspace = workspaceHandles.get(0);

how can i programatically fetch/load components from the repository workspace into my eclipse workspace?

you can get your components by this snippet:

List<IComponent> componentList = new ArrayList<IComponent>();
for(Object componentHandle: myDesiredRepositoryWorkspace.getComponents() ){
    IItemHandle handle = (IItemHandle) componentHandle;
    IItemManager itemManager = teamRepository.itemManager();
    IComponent component = (IComponent) itemManager.fetchCompleteItem(handle, IItemManager.DEFAULT, monitor );
    componentList.add(component);
}

after that i have my repository workspace, i have all components from each workspace but i'm not able to load the repository workspace into a local workspace.

i'm developint a eclipse plugin, so you'll need the following plugins (or imports from the plain-java-api directly):

  • com.ibm.team.rtc.common
  • com.ibm.team.repository.client
  • com.ibm.team.scm.client
  • com.ibm.team.scm.common
  • com.ibm.team.process.common

Solution

  • ok, loading works with ILoadRule2, i found a way, but sadly it involves a bypass via xml-files. It requires access to the sandbox, but it's all described below ^_^

    let's assume we have our workspace and our components (as mentioned in the question), we can load the workspace with this snippet:

    ISharingManager sharingManager = FileSystemCore.getSharingManager();
    File workspaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();                               
    PathLocation pathlocation = new PathLocation(workspaceRoot.getAbsolutePath());
    ILocation sandBoxLocation = pathlocation.getCanonicalForm();
    ISandbox sandbox = sharingManager.getSandbox(sandBoxLocation, false);
    LoadDilemmaHandler p = LoadDilemmaHandler.getDefault();
    
    monitor.subTask("searching for load rules file");
    
    File f = LoadRuleUtility.createLoadRules(componentList);
    InputStream ins = new FileInputStream(f);
    Reader xmlReader = new InputStreamReader(ins);
    ILoadRule2 rule = ILoadRuleFactory.loadRuleFactory.getLoadRule(con, xmlReader, monitor);                                    
    ILoadOperation loadoperator = rule.getLoadOp(sandbox, p, monitor);
    monitor.subTask("loading files from RTC server...");
    loadoperator.run(monitor);
    

    ok, the magic of LoadRuleUtility.createLoadRules() is that it creates a xml file that describes the workspaces's components. (i'm using a DOM).

    it has to look like this:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <scm:sourceControlLoadRule eclipseProjectOptions="import" version="1" xmlns:scm="http://com.ibm.team.scm">
    
        <!-- for each component in your repository workspace -->
        <parentLoadRule>
            <component name="component_name"/>
            <parentFolder repositoryPath="/"/>
        </parentLoadRule>
    
    </scm:sourceControlLoadRule>
    

    to unload the components you simply have to delete them:

    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for(IProject project: projects){
        try {
            project.delete(true, true, monitor);
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }