It's all about the answers!

Ask a question

Creating WorkItems from Custom UI using plain java libs


Imran Akbar (56278) | asked Mar 15 '12, 10:24 a.m.
Hi,

i'm developing a editor/View in RTC to get inputs and create them as several work items. initially i tried creating a standalone application with help of programmatic way creating work items tutorial ,which can be run from Main and it worked, now i created a view with two text fields and a submit button, once the submit button is clicked the data is passed and workitems has to be created, but i'm getting following error when i try to launch as plug in:

java.lang.IllegalStateException: Already started from a different context. Current context = plain java

at com.ibm.team.repository.common.internal.util.InternalTeamPlatform.start(InternalTeamPlatform.java:87)
at com.ibm.team.repository.client.TeamPlatform.startup(TeamPlatform.java:49)
at createwi.views.CreateWorkItem.<init>(CreateWorkItem.java:88)
at createwi.views.SampleView$2.handleEvent(SampleView.java:77)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3910)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3503)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2405)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2369)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


This is my working stand alone application




import java.net.URI;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;

import com.ibm.team.foundation.common.text.XMLString;
import com.ibm.team.process.client.IProcessClientService;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IProjectAreaHandle;

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.TeamRepositoryException;
import com.ibm.team.repository.common.model.RepositoryPackage;

import com.ibm.team.workitem.client.IAuditableClient;
import com.ibm.team.workitem.client.IWorkItemClient;
import com.ibm.team.workitem.client.WorkItemOperation;
import com.ibm.team.workitem.client.WorkItemWorkingCopy;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.ICategoryHandle;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemHandle;
import com.ibm.team.workitem.common.model.IWorkItemType;

public class CreateWorkItem {

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

}

private static class WorkItemInitialization extends WorkItemOperation {

private String fSummary;
private ICategoryHandle fCategory;
private IAttribute iattribute;

public WorkItemInitialization(String summary, ICategoryHandle category,
IAttribute ia) {
super("Initializing Work Item");

fSummary = summary;
fCategory = category;
iattribute = ia;
}

protected void execute(WorkItemWorkingCopy workingCopy,
IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem = workingCopy.getWorkItem();
workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
Timestamp ts = new Timestamp(30110205);
workItem.setCategory(fCategory);
workItem.setDueDate(ts);
workItem.setValue(iattribute, "2");
}

}

public CreateWorkItem() {
}

public void Testcall() {
System.out.println("inside class");

String[] args = null;

boolean result;
// RepositoryPackage.eINSTANCE.eContents();
// TeamPlatform.startup();

ITeamRepository repository = null;
try {
repository = RepositoryManager.getInstance().getCurrentRepository();
} catch (RepositoryOperationException e) {
e.printStackTrace();
// do nothing
}
try {
result = run1(args);
} catch (TeamRepositoryException x) {
x.printStackTrace();
result = false;
} finally {
// TeamPlatform.shutdown();
}

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

}

public static IAttribute getAttribute(IWorkItem workItem,
String attributeName, IWorkItemClient workItemClient)
throws TeamRepositoryException {
IProjectAreaHandle projectAreaHandle = workItem.getProjectArea();
return getAttribute(projectAreaHandle, attributeName, workItemClient);
}

public static IAttribute getAttribute(IProjectAreaHandle projectAreaHandle,
String attributeName, IWorkItemClient workItemClient)
throws TeamRepositoryException {
IAttribute attribute = workItemClient.findAttribute(projectAreaHandle,
attributeName, null);
return attribute;
}

private static boolean run1(String[] args) throws TeamRepositoryException {
System.out.println("inside run");
// if (args.length != 7) {
// System.out
// .println("Usage: CreateWorkItem <repositoryURI> <userId> <password> <projectArea> <workItemType> <summary> <category>");
// return false;
// }
IProgressMonitor monitor = null;
// monitor = new SysoutProgressMonitor();
String repositoryURI = "https://localhost:9443/ccm/";
String userId = "rtc";
String password = "passwd";
String projectAreaName = "Development-3.1";
String typeIdentifier = "enhancement";
String summary = "Test Workitem Java Plain";
String categoryName = "ABC";

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

IProcessClientService processClient = (IProcessClientService) teamRepository
.getClientLibrary(IProcessClientService.class);
IAuditableClient auditableClient = (IAuditableClient) teamRepository
.getClientLibrary(IAuditableClient.class);
IWorkItemClient workItemClient = (IWorkItemClient) teamRepository
.getClientLibrary(IWorkItemClient.class);

URI uri = URI.create(projectAreaName.replaceAll(" ", "%20"));
IProjectArea projectArea = (IProjectArea) processClient
.findProcessArea(uri, null, null);
if (projectArea == null) {
System.out.println("Project area not found.");
return false;
}

IWorkItemType workItemType = workItemClient.findWorkItemType(
projectArea, typeIdentifier, null);
if (workItemType == null) {
System.out.println("Work item type not found.");
return false;
}

List path = Arrays.asList(categoryName.split("/"));
ICategoryHandle category = workItemClient.findCategoryByNamePath(
projectArea, path, null);
if (category == null) {
System.out.println("Category not found.");
return false;
}
IAttribute ia = null;
ia = getAttribute(projectArea, "Dev.nummberOfTestCasesReceived",
workItemClient);
// ia = getAttribute(projectArea,
// "com.ibm.team.workitem.attribute.duedate", workItemClient);

WorkItemInitialization operation = new WorkItemInitialization(summary,
category, ia);
IWorkItemHandle handle = operation.run(workItemType, null);
IWorkItem workItem = auditableClient.resolveAuditable(handle,
IWorkItem.FULL_PROFILE, null);
System.out.println("Created work item " + workItem.getId() + ".");

teamRepository.logout();

return true;
}

}


i have added the plain java libs to Runtime classpath, has anyone faced similar kind of error or does any one point out what could be the issue.

Thanks in advance

Imran Akbar

2 answers



permanent link
sam detweiler (12.5k6195201) | answered Mar 15 '12, 10:58 a.m.
If your new code is running INSIDE the eclipse client, then u cannot use the plainjava classes. You need to move to the SDK classes.

there are three different models

standlone java app (plainjava)
Eclipse client extension (SDK)
Server extension plugin (sdk and others)

sam

permanent link
Imran Akbar (56278) | answered Mar 16 '12, 8:41 a.m.
If your new code is running INSIDE the eclipse client, then u cannot use the plainjava classes. You need to move to the SDK classes.

there are three different models

standlone java app (plainjava)
Eclipse client extension (SDK)
Server extension plugin (sdk and others)

sam


Thanks a lot Sam for pointing out.

After removing all plain java libs and adding required libraries in Dependencies tab it worked. i removed the login handler and TeamPlatform.startup from the code to make it run.

Thanks again.

Your answer


Register or 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.