How to access repository content through the Magnolia API
Overview
This is just to get some of the fundamentals established. The Magnolia API has been evolving very quickly so describing it is like trying to hit a moving target. I have tried to avoid the deprecated parts of the API and focussed on the parts that appear to be here for good.
The objective of this documentation is to give you a basic command of how you can fundamentally access information in the repository.
Associating a request with content in the repository
When a request is received Magnolia has a filter class, info.magnolia.cms.filters.MgnlCmsFilter, that looks at the URI and determines what content it relates to. The content is then assembled and placed in the request scope where the template used to render it can access it. Practically the content is available as a info.magnolia.cms.core.Content object stored in the request scope under the key Aggregator.ACTPAGE.
The following code would get a reference to the Content object:
Content pageContent = (Content) req.getAttribute(Aggregator.ACTPAGE);
Rather than get the Content object directly from the request scope the class info.magnolia.context.MgnlContext is provided. Using the class is advised because implementation details might change with other classes and there are other convenience services provided by this class. MgnlContext is a Singleton,'(although the constructor is public)', and all the methods are static. Hence, you use the class directly. There are a number of methods provided by this class and you should familiarize yourself with them. To get the Content object you would use the following code:
Content pageContent = MgnlContext.getActivePage();
Once you have a Content object there are a number of methods available, for example:
| Method |
Description |
|
| Content getContent(String name) |
this returns a Content object representing a child node with the supplied name |
|
| String getTemplate() |
returns the name of the template configuration for the page |
|
| String getName() |
returns the name of the content node |
|
| String getNodeData("name").getString() |
gets the node data with the supplied name |
|
| String getTitle() |
convenience method to return the value of the Title property. Actually uses the preceding method |
|
| Collection getChildren() |
various getChildren methods exist but the default, no arg, method returns child nodes of the same type as the parent |
|
| String getHandle() |
get a handle representing a path relative to the content repository |
|
| int getLevel() |
gets the level the content node appears relative to the ROOT |
|
| String getUUID() |
gets the UUID of the node referenced by this object |
|
Working with the current Content node
The current content node refers to the node associated with URI of the current request. The template being used for that URI will be able to get a reference to this content node using:
Content pageContent = MgnlContext.getActivePage();
A Content object is going to have two fundamental components; data nodes and child content nodes. To find out if a Content object has child nodes use the boolean method as follows:
pageContent.hasChildren()
A JSP snippet that uses some of the Content types basic methods would look like:
URI Handle: <%=pageContent.getHandle()%> <br/>
Name: <%=pageContent.getName()%> <br/>
Template: <%=pageContent.getTemplate()%> <br/>
UUDI: <%=pageContent.getUUID()%> <br/>
Title: <%=pageContent.getTitle()%> <br/>
Level: <%=pageContent.getLevel()%> <br/
If you wanted to process this Content object children you would get a collection of Content objects representing each of the Content objects child content nodes. For example:
<hr/>
<%Collection children = pageContent.getChildren()
Iterator it = children.iterator();
while(it.hasNext()) {
Content child = (Content) it.next();
%>
<hr/>
Handle: <%=child.getHandle()%> <br/>
Name: <%=child.getName()%> <br/>
Template: <%=child.getTemplate()%> <br/>
UUDI: <%=child.getUUID()%> <br/>
Title: <%=child.getTitle()%> <br/>
Level: <%=child.getLevel()%> <br/>
<%}%>
<hr/>
Working with Content in the Repository elsewhere
The MgnlContext class provides services for the Content object for the current request. To have more flexibility over what you access in the content repository you can use the info.magnolia.cms.core.HierarchyManager. The MgnlContext provides a method, 'getHierachyManager(ContentRepository.WEBSITE)', to get an instance of a HierarchyManager. You can obtain a Content object for a specified URI using the 'getContent(String uri)' method on the HierarchyManager so obtained. For example:
HierarchyManager mgr = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
Content uriContent = mgr.getContent("/en/about-magnolia");
This would provide a Content object representing /en/about-magnolia. You could iterate over this Content objects child nodes in exactly the same way as above. I won't teach you to suck eggs but obviously this basic functionality enables you to navigate down the whole content tree in the same way.
Content Node Data
Each Content object has child node and data nodes. To get a Content objects data node you use the Content types 'getNodeDataCollection()' method. Each element of the collection will be a NodeData type object. NodeData provides various methods for working with a Content nodes data. For example:
for(Iterator i = child.getNodeDataCollection().iterator(); i.hasNext();) {
NodeData nodeData = (NodeData) i.next();
out.println(" ");
out.println(nodeData.getName());
}
Summary
A page template, at runtime, is provided with a reference to a Content object that represents the repository content for the URI provided in the http request. The MgnlContext class and its static .getActivePage() method will return that Content object. The Content object has two components: child nodes and data nodes. Methods are provided for navigating both types of related node content. If you want to access other parts of the repository explicitly you can get a Content node for a given URI from a HierarchyManager. The MgnlContext object also provides a method to get an instance of a HierarchyManager.
In its previous incarnation on JspWiki, this page was last edited on Sep 19, 2007 6:42:26 PM by Interition