How to create a defect(customised work item) in Project using OSLC Java Programming?
Hi,
Kindly guide me on creating customized work item(defect) by using OSLC Java programming. <o:p> </o:p>
As of now,i'm able to create default defect in sample project. <o:p> </o:p>
When i'm trying to create a defect(customised work item) in my project,i'm able to see that workitem-task is created instead of defect-work item. <o:p> </o:p>
Please find the below code used. <o:p> </o:p>
String webContextUrl = " "; <o:p> </o:p>
String user = " "; <o:p> </o:p>
String passwd = " "; <o:p> </o:p>
String projectArea = " "; <o:p> </o:p>
try { <o:p> </o:p>
//STEP 1: Initialize a Jazz rootservices helper and indicate we're looking for the ChangeManagement catalog <o:p> </o:p>
//RTC contains a service provider for CM and SCM, so we need to indicate our interest in CM <o:p> </o:p>
JazzRootServicesHelper helper = new JazzRootServicesHelper(webContextUrl,OSLCConstants.OSLC_CM_V2); <o:p> </o:p>
//STEP 2: Create a new Form Auth client with the supplied user/password <o:p> </o:p>
JazzFormAuthClient client = helper.initFormClient(user, passwd); <o:p> </o:p>
//STEP 3: Login in to Jazz Server <o:p> </o:p>
if (client.formLogin() == HttpStatus.SC_OK) { <o:p> </o:p>
System.out.println("Login Successful"); <o:p> </o:p>
//STEP 4: Get the URL of the OSLC ChangeManagement catalog <o:p> </o:p>
String catalogUrl = helper.getCatalogUrl(); <o:p> </o:p>
//STEP 5: Find the OSLC Service Provider for the project area we want to work with <o:p> </o:p>
String serviceProviderUrl = client.lookupServiceProviderUrl(catalogUrl, projectArea); <o:p> </o:p>
//STEP 6: Get the Query Capabilities URL so that we can run some OSLC queries <o:p> </o:p>
String queryCapability = client.lookupQueryCapability(serviceProviderUrl, <o:p> </o:p>
OSLCConstants.OSLC_CM_V2, <o:p> </o:p>
OSLCConstants.CM_CHANGE_REQUEST_TYPE); <o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
//SCENARIO D: RTC defect creation <o:p> </o:p>
ChangeRequest defect = new ChangeRequest(); <o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
defect.setTitle("Error logging in by Sujith Babu Bandla"); <o:p> </o:p>
defect.setDescription("An error occurred when I tried to log in with a user ID - sujithbabubandla, that contained the '@' symbol."); <o:p> </o:p>
defect.addTestedByTestCase(new Link(new URI("http://qmprovider/testcase-sujithbabu/3"), "Global Verifcation Test - sujith babu")); <o:p> </o:p>
defect.addDctermsType("defect"); <o:p> </o:p>
//Get the Creation Factory URL for change requests so that we can create one <o:p> </o:p>
CreationFactory defectCreation = client.lookupCreationFactoryResource( <o:p> </o:p>
serviceProviderUrl, OSLCConstants.OSLC_CM_V2, <o:p> </o:p>
defect.getRdfTypes()[0].toString(), OSLCConstants.OSLC_CM_V2 + "defect"); <o:p> </o:p>
String factoryUrl = defectCreation.getCreation().toString(); <o:p> </o:p>
//Determine what to use for the Filed Against attribute by requesting the resource shape for the creation factory. <o:p> </o:p>
String shapeUrl = defectCreation.getResourceShapes()[0].toString(); <o:p> </o:p>
ClientResponse shapeResponse = client.getResource(shapeUrl,OslcMediaType.APPLICATION_XML); <o:p> </o:p>
ResourceShape shape = shapeResponse.getEntity(ResourceShape.class); <o:p> </o:p>
//Look at the allowed values for Filed Against. This is generally a required field for defects. <o:p> </o:p>
Property filedAgainstProperty = shape.getProperty(new URI(RTC_NAMESPACE + RTC_FILED_AGAINST)); <o:p> </o:p>
if (filedAgainstProperty != null) { <o:p> </o:p>
URI allowedValuesRef = filedAgainstProperty.getAllowedValuesRef(); <o:p> </o:p>
ClientResponse allowedValuesResponse = client.getResource(allowedValuesRef.toString(),OslcMediaType.APPLICATION_XML); <o:p> </o:p>
AllowedValues allowedValues = allowedValuesResponse.getEntity(AllowedValues.class); <o:p> </o:p>
Object[] values = allowedValues.getValues().toArray(); <o:p> </o:p>
//If this fails, you might need to check that the value is <o:p> </o:p>
//not "Unassigned", which is an allowed value in some RTC <o:p> </o:p>
//project areas. Try the second value instead of the first, most project area processes <o:p> </o:p>
//create more than one category <o:p> </o:p>
defect.getExtendedProperties().put(new QName(RTC_NAMESPACE, RTC_FILED_AGAINST), (URI) values[1]); <o:p> </o:p>
} <o:p> </o:p>
//Create the change request <o:p> </o:p>
ClientResponse creationResponse = client.createResource( <o:p> </o:p>
factoryUrl, defect, <o:p> </o:p>
OslcMediaType.APPLICATION_RDF_XML, <o:p> </o:p>
OslcMediaType.APPLICATION_RDF_XML); <o:p> </o:p>
String defectLocation = creationResponse.getHeaders().getFirst(HttpHeaders.LOCATION); <o:p> </o:p>
if (creationResponse.getStatusCode() != HttpStatus.SC_CREATED) { <o:p> </o:p>
System.err.println("ERROR: Could not create the defect (status " + creationResponse.getStatusCode() + ")\n"); <o:p> </o:p>
System.err.println(creationResponse.getEntity(String.class)); <o:p> </o:p>
System.exit(1); <o:p> </o:p>
} <o:p> </o:p>
creationResponse.consumeContent(); <o:p> </o:p>
System.out.println("Defect created at location " + defectLocation); <o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
Any suggestion will be helpful.
Thanks in advance. <o:p> </o:p>
Regards, <o:p> </o:p>
Pankaj Kumar
2 answers
Not sure where that API comes from. I think the code to set or select the work item type, is this and uses "defect" as default. To create a different type you would have to set the type ID to the desired value.
CreationFactory defectCreation = client.lookupCreationFactoryResource( serviceProviderUrl, OSLCConstants.OSLC_CM_V2, defect.getRdfTypes()[0].toString(), OSLCConstants.OSLC_CM_V2 + "defect");
Hi Pankaj,