It's all about the answers!

Ask a question

Setting enum value through JAVA API


Joseph Simon Arokiaraj (21414845) | asked Dec 20 '11, 6:32 a.m.
Hi All,

I am trying to creating a workitem through java API and got strucked in setting the value for enum attributes. Basically I am from perl background and not expert in Java.

Source Code:

CreateWI.java

package rtctestlogin;

import java.net.URI;
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.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.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.IAttributeHandle;
import com.ibm.team.workitem.common.model.ICategoryHandle;
import com.ibm.team.workitem.common.model.IEnumeration;
import com.ibm.team.workitem.common.model.ILiteral;
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 CreateWI {

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 String fDesc;
private ICategoryHandle fCategory;



public WorkItemInitialization(String summary, ICategoryHandle category, String description) {
super("Initializing Work Item");
fSummary= summary;
fCategory= category;
fDesc= description;

}

@Override
protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem= workingCopy.getWorkItem();
workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
workItem.setCategory(fCategory);
workItem.setHTMLDescription(XMLString.createFromPlainText(fDesc));

}


}

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 != 8) {
System.out.println("Usage: CreateWI <repositoryURI> <userId> <password> <projectArea> <workItemType> <summary> <category> <description>");
return false;
}

String repositoryURI= args;
String userId= args;
String password= args;
String projectAreaName= args;
String typeIdentifier= args;
String summary= args;
String categoryName= args;
String desc= args;



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<String> path= Arrays.asList(categoryName.split("/"));
ICategoryHandle category= workItemClient.findCategoryByNamePath(projectArea, path, null);
if (category == null) {
System.out.println("Category not found.");
return false;
}
//IAttribute attribute0= workItemClient.findAttribute(projectArea, "rolelist_A", null);

WorkItemInitialization operation= new WorkItemInitialization(summary, category, desc);
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;
}
}


To run the program:

CreateWI <repositoryURI> <userId> <password> <projectArea> <workItemType> <summary> <category> <description>

Eg:

CreateWI xxxxxx joel xxxxx SDAgile Request TestingSummary DS Testingdescrioption

Now I need to include one additional enum parameter.

attribute name: roleslist_A which points to enum roles_A

CreateWI xxxxxx joel xxxxx SDAgile Request TestingSummary DS Testingdescrioption Projectmanager

Could anyone please update what needs to be changed in the source code to set values for the enum attribute?

Cheers,
Jose

Be the first one to answer this question!


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.