It's all about the answers!

Ask a question

IWorkItemCommon.findAttribute not working for built-in attributes


Lukas Steiger (3131422) | asked Oct 25 '13, 3:01 a.m.
edited Oct 25 '13, 3:08 a.m.
 I'm writing a tool which should later import workitems from a text file. to get to know how to work with the jazz java api, I made a little test application

I can dynamilcally set the custom attributes of a custom work item type by passing the id and the value. but this procedure fails if i want to set a built-in attribute like the work item description.

the method findAttributes does return null for all built-in attributes, but works for all my custom attributes (I tried it with various fields with different data types).

Does findAttributes only work for custom attributes?

Below the code of my (simplified) application

EDIT: for some reason, " string, string " is shown as " string, string="" ", i have no idea why

String workItemSummary = "this is my summary";
String categories = "Projects/Hardware";
// get the category handle
List<String> categoryPath = Arrays.asList(CATEGORIES.split("/"));
ICategoryHandle categoryHandle = workItemClient.findCategoryByNamePath(projectArea, categoryPath, null);
// define key/value pairs for custom attributes
List<AbstractMap.SimpleEntry<String, string>> attributeValueList = new ArrayList<AbstractMap.SimpleEntry<String,String>>();
// built-in attribute, which cannot be found
attributeValueList.add(new SimpleEntry<String, string>("com.ibm.team.workitem.attribute.description", "this is the project info"));
// custom attribute which can be found and set
attributeValueList.add(new SimpleEntry<String, string>("com.mycompany.division.attr.managementaction", "this is the management action"));
// initialize the work item
WorkItemInitialization operation = new WorkItemInitialization(workItemSummary, categoryHandle, attributeValueList, workItemClient, projectArea, teamRepository);
// save the work item and return the id of the saved work item
IWorkItemHandle handle = operation.run(workItemType, null);
IWorkItem workItem = auditableClient.resolveAuditable(handle, IWorkItem.FULL_PROFILE, null);
return workItem.getId();
Work Item Initialization:
private static class WorkItemInitialization extends WorkItemOperation {
    private String fSummary;
    private ICategoryHandle fCategory;
    private List<AbstractMap.SimpleEntry<String, string>> fAttributes;
    private IProjectArea fProjectArea;
    private IWorkItemClient fWorkItemClient;
    private ITeamRepository fTeamRepository;
    
    public WorkItemInitialization(String summary, ICategoryHandle category, List<AbstractMap.SimpleEntry<String, String>> attributes, IWorkItemClient workItemClient, IProjectArea projectArea, ITeamRepository teamRepo) {
        fSummary = summary;
        fCategory = category;
        fAttributes = attributes;
        fWorkItemClient = workItemClient;
        fProjectArea = projectArea;
        fTeamRepository = teamRepo;
    }
    
    @Override
    protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException {
        IWorkItem workItem = workingCopy.getWorkItem();
        // set summary and category (working)
        workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
        workItem.setCategory(fCategory);
        
        // try to set the attributes
        if(fAttributes != null) {
            for(AbstractMap.SimpleEntry<String, String> item : fAttributes) {
                String attributeKey = item.getKey();
                String attributeValue = item.getValue();
                // get attribute definition (only working for the custom attribute)
                IAttribute myAttribute = fWorkItemClient.findAttribute(fProjectArea, attributeKey, monitor);
                if(myAttribute == null) {
                    // the built-in attributes are null here!!!!!
                    printMessage(attributeKey + " is null");
                }
                Object value = null;
                String type = myAttribute.getAttributeType();
                if(type != null) {
                    case "html":
                        value = XMLString.createFromPlainText(attributeValue);
                        break;
                    // other case's removed for better readablity
                    default:
                        value = attributeValue;
                        break;
                    }
                    if(value != null) {
                        workItem.setValue(myAttribute, value);
                    }
                }
            }
        }
    }
}

2 answers



permanent link
Ralph Schoon (57.0k23642) | answered Oct 25 '13, 3:08 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
This has always worked for me. See https://rsjazz.wordpress.com/2013/01/02/working-with-work-item-attributes/ and use the interface IWorkItem to get the correct ID's e.g. IWorkItem.DURATION_PROPERTY

Comments
Lukas Steiger commented Oct 25 '13, 3:25 a.m. | edited Oct 25 '13, 3:25 a.m.

Hi Ralph 


Does it work for you if you pass "com.ibm.team.workitem.attribute.description" instead of IWorkItem.DURATION_PROPERTY? 
To make my importer dynamic, I want to achieve it with the ID of the attribute


Lukas Steiger commented Oct 25 '13, 3:30 a.m.

The values (attributeValueList) shall be read dynamically in the future, so it would be easier if I could use the ID (in string format) instead of mapping the ID to the different interface properties (e.g. IWorkItem.DURATION_PROPERTY)


Ralph Schoon commented Oct 25 '13, 3:40 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I believe I have successfully done that. I have no time to test it for you. I would try to check the value of the IWorkItem attribute ID, you can pass that. To evaluate your code works at all, I would start with the IWorkItem interface. Make sure it works and then stat looking at ID Strings.


Lukas Steiger commented Oct 25 '13, 3:55 a.m.

 The code works for me except for the built-in attributes when i pass the ID. So in the example above, the work item is being created with summary, category and managementaction set.
If i set the description by using the interface, it works as well

IAttribute myAttribute2 = fWorkItemClient.findAttribute(fProjectArea, IWorkItem.DESCRIPTION_PROPERTY, monitor);
workItem.setValue(myAttribute2, "my description");
could that be a bug in the interface?

Or is "com.ibm.team.workitem.attribute.description" not the same as IWorkItem.DESCRIPTION_PROPERTY?


permanent link
Lukas Steiger (3131422) | answered Oct 25 '13, 8:36 a.m.
 I have found the solution. The string ID of the built-in attributes are different than those ID's which are shown in  the jazz eclipse client.

To find out the ID do the following:
- Open the project area in the eclipse client
- Choose 'Process Configuration' tab
- Navigate to  Project Configuration -> Configuration Data -> Work Items -> Types and Attributes
- click a built-in attribute and choose Edit
- instead of the whole ID path (like 'com.ibm.team.workitem.attribute.modified'), only use the last part ('modified').
---> for custom attributes, you have to use the whole path (like in the source code above)

I don't know if that is a bug or feature, but in my opinion, it is kind of confusing.

Comments
Ralph Schoon commented Oct 25 '13, 8:44 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

From my personal perspective, I would consider this a defect - the ID's presented in the UI should be the ones that work. I remember I had similar issues in the past, but I am unsure if I could solve this to use the full ID's.



Lukas Steiger commented Nov 09 '17, 1:43 p.m.

 the related defect is resolved as 'wont fix', so my answer is still valid


Ralph Schoon commented Nov 09 '17, 1:48 p.m. | edited Nov 09 '17, 1:49 p.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

@lukas, I had forgotten but I filed a similar defect aeons ago. It is still alive, I think. You can not use the external ids from the eclipse client. You have to use the internal ones from the web UI. Note, the WCL has a command to print the real ids for the built in and custom attributes of a type.


Ralph Schoon commented Nov 10 '17, 2:28 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Your answer


Register or to post your answer.