It's all about the answers!

Ask a question

WorkItem : All releated information


Kartik Gajjar (112) | asked Nov 18 '10, 2:05 a.m.
Hi,

I am not a strong java developer but I needed work item related information.

I spent quite some time to achieve following information from various sources from this forum.

This is a big thanks for sharing, pl. find end code below..

Hth, cheers,




package jazz;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import com.ibm.team.build.common.model.IBuildResult;
import com.ibm.team.build.internal.common.model.impl.BuildResultHandleImpl;
import com.ibm.team.links.common.IReference;
import com.ibm.team.links.common.registry.IEndPointDescriptor;
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.ItemNotFoundException;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.workitem.client.IWorkItemClient;
import com.ibm.team.workitem.client.IWorkItemWorkingCopyManager;
import com.ibm.team.workitem.client.WorkItemWorkingCopy;
import com.ibm.team.workitem.common.internal.model.impl.WorkItemHandleImpl;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemHandle;
import com.ibm.team.workitem.common.model.IWorkItemReferences;

public class WIInfo2 {

/**
* @param args
*/

static ITeamRepository teamRepository = null;
static IProgressMonitor monitor = new NullProgressMonitor();
static Logger _log = Logger.getLogger("jazz");
static BufferedWriter out = null;

private static ITeamRepository getTeamRepository() {
return teamRepository;
}

private static void setTeamRepository(ITeamRepository teamRepositoryIn) {
teamRepository = teamRepositoryIn;
}

public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: RequestBuild <repository> <userId> <password>");
System.exit(1);
}

String repositoryURI = args[0];
String userId = args[1];
String password = args[2];

TeamPlatform.startup();
_log.info("Team Platform Startup");
try {
IProgressMonitor monitor = new NullProgressMonitor();
setTeamRepository(login(repositoryURI, userId, password, monitor));
_log.info("Logged In");

out = new BufferedWriter(new FileWriter("c:\\rtc.csv"));

for (int i=1; i < 1431; i++)
{
fetchWIInfo(i);
out.flush();
} // end for

out.close();

} catch (TeamRepositoryException e) {
_log.error("TeamRepositoryException : " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
_log.error(e.getMessage());
e.printStackTrace();
} finally {
TeamPlatform.shutdown();
_log.info("Logged out");
}
}

/**
* To print all information about the work item
* @param workItemNumber
* @throws IOException
* @throws TeamRepositoryException
*/
private static void fetchWIInfo(int workItemNumber) throws IOException {

try {
IWorkItemClient itemClient = (IWorkItemClient) getTeamRepository().getClientLibrary(IWorkItemClient.class);
IWorkItemWorkingCopyManager copyManager = itemClient.getWorkItemWorkingCopyManager();
IWorkItemHandle handle = itemClient.findWorkItemById(workItemNumber,IWorkItem.FULL_PROFILE, monitor);

if (handle ==null)
{
System.out.println(workItemNumber + ",,,,,");
return;
}
copyManager.connect(handle, IWorkItem.FULL_PROFILE, monitor);
WorkItemWorkingCopy itemCopy= copyManager.getWorkingCopy(handle);
IWorkItem workItemDetail= itemCopy.getWorkItem();
IWorkItemReferences workItemReferences= itemCopy.getReferences();

//IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(getTeamRepository());
//IVersionableManager vm = wm.versionableManager();
//IVersionedContentManager contentManager = SCMPlatform.getContentManager(getTeamRepository());

Timestamp wiCreateDate = workItemDetail.getCreationDate();
String wiItemType = workItemDetail.getWorkItemType();
int parent = -1;
int totalChangeSet = -1;
ArrayList<String> builds = new ArrayList<String>();


List<IEndPointDescriptor> types = workItemReferences.getTypes();

for (IEndPointDescriptor endPointDesc : types)
{
_log.debug(workItemNumber + " > IEndPointDescriptor : " + endPointDesc.getId() + "," + endPointDesc.getLinkType().getLinkTypeId()+","+endPointDesc.getLinkType().getComponentId() + "," + endPointDesc.getDisplayName());
_log.info(workItemNumber + " > IEndPointDescriptor : " + endPointDesc.getId());

List<IReference> refList = workItemReferences.getReferences(endPointDesc);

if (endPointDesc.getId().equalsIgnoreCase("parent")) {
IWorkItem parentWI = (IWorkItem) getTeamRepository().itemManager().fetchCompleteItem((WorkItemHandleImpl) refList.get(0).resolve(), IItemManager.DEFAULT, monitor);
parent=parentWI.getId();
}
else if(endPointDesc.getId().equalsIgnoreCase("com.ibm.team.scm.ChangeSet")) {
/*
for (int i=0; i < refList.size(); i++) {
IChangeSet changeSetWI = (IChangeSet) getTeamRepository().itemManager().fetchCompleteItem((ChangeSetHandleImpl) refList.get(0).resolve(), IItemManager.DEFAULT, monitor);
List<IChange> changes = changeSetWI.changes();
for (IChange change : changes) {
IFileItemHandle after = (IFileItemHandle) change.afterState();
if( after != null ) {
IFileItem f = (IFileItem) vm.fetchCompleteState(after, null);
IContributor x = (IContributor) getTeamRepository().itemManager().fetchCompleteItem(f.getModifiedBy(), IItemManager.DEFAULT, monitor);
_log.info("changed : " + f.getName() + x.getName());
} // end if
} // end for
} // end for
*/
totalChangeSet = refList.size();
}
else if(endPointDesc.getId().equalsIgnoreCase("com.ibm.team.build.common.link.includedInBuilds")) {
for (int i=0; i < refList.size(); i++) {
try {
IBuildResult buildResultWI = (IBuildResult) getTeamRepository().itemManager().fetchCompleteItem((BuildResultHandleImpl) refList.get(i).resolve(), IItemManager.DEFAULT, monitor);
builds.add(buildResultWI.getLabel());
_log.info(buildResultWI.getLabel());
}
catch (ItemNotFoundException e)
{
_log.warn(refList.get(i).getComment());
}
} // end for
_log.info("included in builds..." + refList.size());
}
} // end for

if (builds.size() == 0) {
System.out.println(workItemNumber + "," + wiCreateDate + "," + wiItemType + "," + parent + "," + totalChangeSet + ",");
out.write(workItemNumber + "," + wiCreateDate + "," + wiItemType + "," + parent + "," + totalChangeSet + ",\n");
}
else
{
for (int i=0; i < builds.size(); i++){
System.out.println(workItemNumber + "," + wiCreateDate + "," + wiItemType + "," + parent + "," + totalChangeSet + "," + builds.get(i));
out.write(workItemNumber + "," + wiCreateDate + "," + wiItemType + "," + parent + "," + totalChangeSet + "," + builds.get(i) + "\n");
} // end for
} // end if
} catch (TeamRepositoryException e) {
_log.error("TeamRepositoryException !!" + workItemNumber + "!! " + e.getMessage());
e.printStackTrace();
}
}

/**
*
* @param repositoryURI
* @param userId
* @param password
* @param monitor
* @return
* @throws TeamRepositoryException
*/
public static ITeamRepository login(String repositoryURI, String userId,
String password, IProgressMonitor monitor)
throws TeamRepositoryException {

ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepository.registerLoginHandler(new LoginHandler(userId, password));
teamRepository.login(monitor);
return teamRepository;
}

/**
* LoginHandler required by login function
*
*/
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;
}
}
}



log4j.properties


log4j.rootLogger=WARN, CONSOLE

# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %C{1} : %m%n

# LIMIT CATEGORIES
log4j.logger.jazz=ERROR

2 answers



permanent link
Brent Barkman (156) | answered Nov 18 '10, 11:37 a.m.
JAZZ DEVELOPER
There's even more goodness in our SDK ;-)

https://jazz.net/wiki/bin/view/Main/RtcSdk20

permanent link
Sudhakar Frederick (80113431) | answered Nov 18 '10, 2:53 p.m.
JAZZ DEVELOPER
Nice one Kartik.
Thanks
On 18/11/2010 6:08 PM, kartikgajjar wrote:
Hi,

I am not a strong java developer but I needed work item related
information.

I spent quite some time to achieve following information from various
sources from this forum.

This is a big thanks for sharing, pl. find end code below..

Hth, cheers,

Your answer


Register or to post your answer.