Internal metadata structure and basic usage (v 3.0)
- Almuth Boehme [Communardo]
The Metadata add-on has two basic structure elements: Metadata sets and Metadata fields. A set contains one ore more fields. The basic set and field handling is available via the "com.communardo.confluence.metadata.service.MetadataManager".
Just inject it into your classes with constructor injection or setter injection. The injection itself only works, if you have defined a correct component-import in your atlassian-plugin.xml (please see Quick Setup of Metadata API (v 3.0)).
Loading all available metadata sets
List<MetadataSet> sets = metadataManager.loadMetadataSets();
This should give you the space metadata sets as well as the global metadata sets.
Or just load the metadata sets of a specific space:
//load the metadata sets of a space with the spacekey "demo" List<MetadataSet> sets = metadataManager.loadMetadataSets("demo");
Iterate through metadata fields of a set
for (MetadataField metadataField : metadataset.getMetadataFields()) { }
Read a metadata field value on a page or blog post
Each metadata field stores data objects, but only the metadata field type knows how to handle them. Field types are Checkbox; Textfield; Userfield and so on. So, first we need to load the data object. We use the "loadContentMetadataValue" method for that purpose.
DataObject dataObject = metadataManager.loadContentMetadataValue(ceo, metadataField);
Now that we have the data object, we can ask the field type of the metadata field to render it for us.
metadataField.renderData(dataObject, ceo);
Because the field type knows all the things internally, we can also ask it directly for the view representation.
metadatafield.getType().renderView(ceo);