It's all about the answers!

Ask a question

get custom attribute on precondition use java


Pavel Cikota (136) | asked May 30 '16, 9:34 a.m.
I'm newer on RTC API and Plain Java.
I try to get some values of custom attributes in saved work item. RTC get this exception "Exception checking recondition.
An unhandled exception occurred during "<my_precondition_plugin_name>".
org.eclipse.emf.ecore.util.EObjectResolvingEList$Unsettable incompatible with com.ibm.team.workitem.common.model.IAttributeHandle."

I need make server-side plugin. First time I try get all names of custom attributes in "For"

long list_count = workItem.getCustomAttributes().size();
for (long inx=0; inx < list_count; inx++) {
int i = (int) inx;
com.ibm.team.workitem.common.model.IAttributeHandle wi_c_handle = workItem.getCustomAttributes().get(i);
...
}

then I try code of example:
com.ibm.team.workitem.common.model.IAttributeHandle wi_c_handle = workItem.getCustomAttributes().get(0);

but RTC request some exception.

full code is here (output on file - it's some kind of "debug" and "watch"):
package br.com.bello;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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

import com.ibm.team.process.common.IProcessConfigurationElement;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.process.common.advice.AdvisableOperation;
import com.ibm.team.process.common.advice.IAdvisorInfo;
import com.ibm.team.process.common.advice.IAdvisorInfoCollector;
import com.ibm.team.process.common.advice.runtime.IOperationAdvisor;
import com.ibm.team.repository.common.IAuditable;
import com.ibm.team.repository.common.IItemHandle;
import com.ibm.team.repository.common.TeamRepositoryException;

import com.ibm.team.repository.service.AbstractService;
import com.ibm.team.repository.service.IRepositoryItemService;
import com.ibm.team.workitem.common.ISaveParameter;
import com.ibm.team.workitem.common.internal.model.impl.AttributeImpl;
import com.ibm.team.workitem.common.internal.scripting.WorkItemAPIType;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.IAttributeHandle;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.WorkItemLinkTypes;

import com.ibm.team.workitem.common.IWorkItemCommon;

import com.ibm.team.repository.*;

import java.net.URI; 
import java.util.Arrays; 

import com.ibm.team.foundation.common.text.XMLString; 
import com.ibm.team.process.common.IProjectArea; 
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.IWorkItemHandle; 
import com.ibm.team.workitem.common.model.IWorkItemType; 


import com.ibm.team.workitem.api.common.WorkItemAttributes;


public class CheckSummaryAdvisor extends AbstractService implements
IOperationAdvisor { //q1

public void run(AdvisableOperation operation,
IProcessConfigurationElement advisorConfiguration,
IAdvisorInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException { //q2
Object data = operation.getOperationData();

// The action is save
if (data instanceof ISaveParameter) { //q3
// Get state of auditable item
ISaveParameter saveParameter = (ISaveParameter) data;
IAuditable auditable = saveParameter.getNewState();
//if everything is correct, the auditable is an WorkItem
if (auditable instanceof IWorkItem) { //q4
IWorkItem workItem = (IWorkItem) auditable;
// Getting workitem summary
String summary = workItem.getHTMLSummary().getPlainText();
if (summary.contains("#") ) { //q5
// Illegal characters, create a problem and add it collector
IAdvisorInfo createProblemInfo = collector.createProblemInfo("Illegal character", "The WI summary cannont contain character '#'", "error");
collector.addInfo(createProblemInfo);
} //q5
IAttributeHandle custom_attr = (IAttributeHandle) workItem.getCustomAttributes();
FileWriter writeFile = null;
try { //q6
  File logFile = new File("c:\\log\\out.txt");

  writeFile = new FileWriter(logFile);

 
  String out_to_file="";
 
  String wi_state = "State: " + workItem.getState2().toString() + "\nScope: " + workItem.getState2().getScope() +
  "\nScopedIdentifer: " + workItem.getState2().getScopedIdentifier() + "\nStringIdentifer: " + workItem.getState2().getStringIdentifier();
  String wi_summary = workItem.getHTMLSummary().toString();
 
  String wi_full = "\nWI: " + workItem.toString() + "\n\n";
 
  String wi_history = "\nHistory: " + workItem.hasHistory();
  String wi_is = "WI is ";
 
  if (workItem.hasHistory()) { //p1
  wi_is = wi_is + "create";
  } //p1
  else { //p2
  wi_is = wi_is + "update";
  } //p2
 
 
  com.ibm.team.workitem.common.model.IAttributeHandle wi_c_handle = workItem.getCustomAttributes().get(0);
  IRepositoryItemService itemService = getService(IRepositoryItemService.class);
  com.ibm.team.workitem.common.model.IAttribute attribute = (IAttribute) itemService.fetchItem(wi_c_handle, IRepositoryItemService.COMPLETE);

  if ((wi_state == null) || (wi_state=="")) { wi_state="Inicialize, new WI";}
  out_to_file = "WorkItem Summary: " + wi_summary + "\nWorkItem States Type:\n" + wi_state + "\n\n" + wi_full + "\n\n" + wi_history + "\n\n" + wi_is + "\n\nDISP NAME: " + attribute.getDisplayName();
 
  writeFile.write(out_to_file);

} //q6
catch (IOException e) { //q7
e.printStackTrace();
} // q7
finally { //q8
if(writeFile != null) { //q9
       try { //q10
           writeFile.close();
       } //q10
       catch (IOException e) { //q11
           e.printStackTrace();
       } //q11
   } //q9
} //q8
} //q4
} //q3
} //q2
} //q1

One answer



permanent link
Ralph Schoon (55.5k23642) | answered May 30 '16, 9:52 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
edited May 30 '16, 11:18 a.m.
I would like to suggest you get started here: https://rsjazz.wordpress.com/2015/09/30/learning-to-fly-getting-started-with-the-rtc-java-apis/ . Follow it and do the workshop. Don't do it with 6.0.2 because the SDK 6.0.2 currently has a problem.

Look at the references posts about how to work with work item attributes in the client API. See https://rsjazz.wordpress.com/2013/01/02/working-with-work-item-attributes/  and https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/ for some hints for the client and server work item API.

Note, there is pretty much no difference in the client and server API when getting attributes. Use IWorkItemCommon.findAttribute(,attributeID as String,) or iterate the custom attributes and take the one you want.


There are many examples on the blog that should get you going. Note, you can download the code of many of the examples from there too.

Your answer


Register or to post your answer.