First of all - interesting problem. I never had to get the Keyword
itself before, so I'm very curious why you need it. I would appreciate if you gave more details about what you are trying to accomplish.
I couldn't find a simple straight forward solution (and by that I mean a one or two lines Broker API call to read the Keyword
). Instead I came up with the helper method below (Java code):
public Keyword getKeyword(ComponentMeta meta, String fieldName) {
CustomMeta custom = meta.getCustomMeta();
Map<String, NameValuePair> map = custom.getNameValues();
NameValuePair pair = map.get(fieldName);
String value = (String) pair.getFirstValue();
Category[] categories = meta.getCategories();
for (Category category : categories) {
for (Keyword keyword : category.getKeywordList()) {
if (keyword.getKeywordName().equals(value)) {
return keyword;
}
}
}
return null;
}
Obviously there are plenty of checks missing from the code-above, but I hope the idea is clear: that is, we read the CustomMeta
first and get the String value of the Keyword metadata field. Then we match this value to all keywords available under the getCategories() method. This is a list of Category object that the current ComponentMeta is tagged against. One word of caution here: since we are talking about several Categories, there might be the risk of a Keyword title clash (i.e. there might be more Keywords with the same title, since they belong to different Categories). So you would be better off passing in a parameter with the Category name, such that you don't need to iterate over all Categories.
You would use the sample above in the following manner:
// get somehow a ComponentMeta object
ComponentMetaFactory factory = new ComponentMetaFactory(PUBLICATION_ID);
ComponentMeta meta = factory.getMeta(COMPONENT_ID);
// call the helper method
Keyword keyword = getKeyword(meta, "process-type");