It's all about the answers!

Ask a question

Execute Query using Java API


Pankaj Sharma (40341) | asked Feb 04 '16, 6:22 a.m.
 Hello Team,

I want to execute a query using Java API and use the result for further processing, I refrered 
https://rsjazz.wordpress.com/2012/10/29/using-work-item-queris-for-automation/
While executing the following code, I am getting execption for ITeamRepository teamRepository

public static IQueryDescriptor findSharedQuery(	IProjectArea projectArea, 
		List sharingTargets, String queryName,  IProgressMonitor monitor)
		throws TeamRepositoryException {
	// Get the required client libraries
	ITeamRepository teamRepository = (ITeamRepository)projectArea.getOrigin();
	IWorkItemClient workItemClient = (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
	IQueryClient queryClient = workItemClient.getQueryClient();
	IQueryDescriptor queryToRun = null;
	List queries = queryClient.findSharedQueries(projectArea.getProjectArea(),
		sharingTargets, QueryTypes.WORK_ITEM_QUERY,
	IQueryDescriptor.FULL_PROFILE, monitor);
	// Find a query with a matching name
	for (Iterator iterator = queries.iterator(); iterator.hasNext();) {
		IQueryDescriptor iQueryDescriptor = (IQueryDescriptor) iterator.next();
		if (iQueryDescriptor.getName().equals(queryName)) {
			queryToRun = iQueryDescriptor;
			break;
		}
	}
	return queryToRun;
}
	
Please help me.

2 answers



permanent link
Ralph Schoon (58.2k23642) | answered Feb 04 '16, 6:52 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Without context, about what an exception and what is around and passed, no one will be able to help.
Maybe see https://rsjazz.wordpress.com/2013/03/20/understanding-and-using-the-rtc-java-client-api/ and set your system up for debugging? E.g. find out some classes are null or something like that?

Comments
Pankaj Sharma commented Feb 04 '16, 9:16 a.m.

 Hello Ralph,


If we use the code ITeamRepository teamRepository = (ITeamRepository)workItem.getOrigin();

We are getting the value of  as "null" of teamRepository.

If we use the code ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI());

We are getting the exception " java.lang.NoClassDefFoundError: com.ibm.team.repository.transport.client.IInternalRawRestServiceClient".

Please help on this.


permanent link
Ralph Schoon (58.2k23642) | answered Feb 05 '16, 1:40 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Usually, you don't need to get the ITeamRepository from the work item, because you should already be logged in.
The second method is more like it, however, for it to be able to run, it needs the Plain Java Client libraries to be in the classpath, either by creating a user library as described in Understanding and Using the RTC Java Client API section Setting Up Your Workspace for Plain Java Development, or by providing it in the classpath as described in Understanding and Using the RTC Java Client API in the section Run From Command Line.

A valid base program that should do the login looks like below.


/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2008. All Rights Reserved. 
 *
 * Note to U.S. Government Users Restricted Rights:  Use, duplication or 
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *******************************************************************************/
package com.ibm.js.team.workitem.automation.examples;

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

import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.TeamRepositoryException;

/**
 * Skeleton for a plain-Java client, see
 * https://jazz.net/wiki/bin/view/Main/ProgrammaticWorkItemCreation.
 */
public class PlainJavaClient {

	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) {

		boolean result;
		TeamPlatform.startup();
		try {
			result = run(args);
		} catch (TeamRepositoryException x) {
			x.printStackTrace();
			result = false;
		} finally {
			TeamPlatform.shutdown();
		}

		if (!result)
			System.exit(1);

	}

	private static boolean run(String[] args) throws TeamRepositoryException {

		if (args.length != 3) {
			System.out
					.println("Usage: PlainJavaClient [repositoryURI] [userId] [password]");
			return false;
		}

		IProgressMonitor monitor = new NullProgressMonitor();
		String repositoryURI = args[0];
		String userId = args[1];
		String password = args[2];

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

		System.out.println("Logged in as: "
				+ teamRepository.loggedInContributor().getName());
		
		// your code goes here


		teamRepository.logout();

		return true;
	}
}

Comments
Pankaj Sharma commented Feb 05 '16, 8:41 a.m.

 Hello Ralph,

I have used the below code to get IWorkItemClient object and IQueryClient object from teamRepository. However I am getting as null for both the objects.

ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI());
        
IWorkItemClient workItemClient = (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
        
IQueryClient queryClient = (IQueryClient) teamRepository.getClientLibrary(IQueryClient.class);

Note: The java class is implementing IOperationAdvisor.

Please help on this.

Your answer


Register or to post your answer.