Studio Developer Manual / Version 2107
Table Of ContentsWhen working with the metadata tree, you have two data structures to your convenience:
com.coremedia.cms.editor.sdk.preview.metadata.MetadataTree
: This data structure represents the whole tree and, for example, offers methods for accessing specific nodes (by their ID) or getting a list of all tree nodes (in breadth-first order).com.coremedia.cms.editor.sdk.preview.metadata.MetadataTreeNode
: This data structure represents a single metadata tree node. It offers a range of methods like retrieving the parent or the children of a node, finding specific parent nodes upwards in the hierarchy or specific child nodes downwards in the hierarchy or accessing properties of a metadata tree node.
In the following you will find two examples of how to use the metadata tree. Suppose that the JSP templates on the CAE side have been prepared to include metadata about content. At different points throughout the JSP templates the code might look as follows:
... <cm:metadata var="contentMetadata"> <cm:property name="contentInfo"> <cm:property name="title" value="${self.content.title}"/> <cm:property name="keywords" value="${self.content.keywords}"/> </cm:property> </cm:metadata> <div ${contentMetadata}> ... </div> ...
In a preview document there might be multiple of such content-related metadata chunks attached to different DOM nodes. Suppose you want to gather the titles of all the contents that are included in such metadata chunks. One way to gather these titles in an array is the following:
var metadataService:IMetadataService = ... ; var metadataTree:MetadataTree = metadataService.getMetadataTree(); var result:Array = []; if (metadataTree.getRoot()) { var nodesToProcess:Array = [metadataTree.getRoot()]; } var arrayIndex:int = 0; while (arrayIndex < nodesToProcess.length) { var currentNode:MetadataTreeNode = nodesToProcess[arrayIndex]; if (currentNode.getProperty("contentInfo")) { var title:String = currentNode.getProperty("contentInfo").title; result.push(title); } if (currentNode.getChildren()) { nodesToProcess = nodesToProcess.concat(currentNode.getChildren()); } arrayIndex++; }
In this example, the whole metadata tree is traversed in a breadth-first manner. For each
node it has to be checked whether it has the contentInfo
property as there
might be metadata nodes with completely other information.
The code can be simplified considerably if a filtered metadata tree is retrieved:
var metadataService:IMetadataService = ... ; var metadataTree:MetadataTree = metadataService.getMetadataTree(["contentInfo"]); var result:Array = []; var metadataNodesList:Array = metadataTree.getAsList(); metadataNodesList.forEach(function (node:MetadataTreeNode) { result.push(node.getProperty("contentInfo").title); }
In this case, the metadata tree is filtered on retrieval, namely for metadata nodes that
contain the contentInfo
property. Now it is sufficient to get all metadata tree
nodes as an array, walk through it and gather the content titles.