Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Execute Query using Java API

 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.

0 votes



2 answers

Permanent link
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?

0 votes

Comments

 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
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;
	}
}

0 votes

Comments

 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 log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 11,030

Question asked: Feb 04 '16, 6:22 a.m.

Question was seen: 4,486 times

Last updated: Feb 05 '16, 8:41 a.m.

Confirmation Cancel Confirm