Creating Content Beans
Introduction
To make custom document types available in a site, the backing JCR nodes must be wrapped in a HippoBean object. A Content Bean is a light-weight wrapper around such a node and a simple POJO. For a request, you can access the Content Bean mapped to the requested page via
HstRequestContext ctx = RequestContextProvider.get(); ctx.getContentBean()
See HstRequestContext documentation for more information.
For a typical document type created using the Document Type Editor, the corresponding content bean class is dynamically generated at runtime by default (as of version 13.2.0) or can optionally be generated using the Beanwriter tool in the Essentials setup application. However, in some cases it might be necessary to customize the generated class, or write one from scratch.
Document Type
For the mapping to work it must be annotated with the org.hippoecm.hst.content.beans.Node annotation. Set the mapped JCR node type with the jcrType parameter.
@Node(jcrType="myproject:textdocument") public class TextDocument extends HippoDocument{ ... }
Read the page about Automatic Scanning to see how to enable the actual scanning of the Bean.
Primitive Fields
To make the properties of the JCR node available in components and templates the bean must have appropriate getters. The return type of the getter for a property depends on the type of it.
An example of a typical getter for a property is:
public String getTitle() { return getSingleProperty("myproject:title"); }
Here is a list of Primitve fields as defined in the Document Type Editor in the CMS, the matching JCR property type and the corresponding types in Java.
Primitive CMS field | JCR property type | Java types | Remarks |
---|---|---|---|
Boolean | Boolean | boolean | |
String, Text, Label | String | java.lang.String | The Text field offers a multi line field in the CMS. |
Date, CalendarDate | Date | java.util.Calendar | |
Decimal Number | Double | double | |
Integer Number | Long | long | |
Docbase | String | java.lang.String | This CMS field provides a document picker. The JCR UUID of the choosen document is simply stored as a String. |
Formatted text | String | java.lang.String | Use the formattedText attribute of the <hst:html /> tag in your template. |
Password | String | java.lang.String | Note that this only implies a 'hidden' input in the cms. The entered value is not stored in a encrypted way. |
Compound Fields
For compound types in document types the information is stored in JCR as child nodes of the documents' node. The getter in the document Bean should then return the Bean type that maps the content of the child node.
Some examples of typical getters for compound type fields are:
public HippoBean getRelatedDocument() { return getLinkedBean("myproject:relateddocument", HippoBean.class); }
public List<HippoGalleryImageSet> getImages() { return getLinkedBeans("myproject:images", HippoGalleryImageSet.class); }
public HippoHtml getDescription() { return getHippoHtml("myproject:description"); }
For custom compounds you must write your own beans. For the compounds provided by Hippo these are the corresponding beans.
CMS Compound field | JCR node type | Matching Bean type | Remarks |
---|---|---|---|
Rich Text Editor | hippostd:html | org.hippoecm.hst.content.beans. standard.HippoHtml |
Use the <hst:html /> tag in your template. |
imagelink | hippogallerypicker:imagelink | org.hippoecm.hst.content.beans. standard.HippoGalleryImageSetBean | Use org.hippoecm.hst.content.beans.standard.HippoItem#getLinkedBean to get the target Bean. Use #getBean to retrieve the child node of the document that contains the link information. |
Resource | hippo:resource | org.hippoecm.hst.content.beans. standard.HippoResourceBean |
|
Link | hippo:mirror | org.hippoecm.hst.content.beans. standard.HippoMirror |
Prevent the Beanwriter from Modifying Custom Bean Methods
Even if a project contains custom content bean classes, the Beanwriter can still be useful. For example, to generate bean classes for newly added document types, or even to generate methods for fields added to existing document types. However, care must be taken to not let the Beanwriter modify any existing methods in existing bean classes.
To prevent the Beanwriter from making any modifications to a method in a bean class:
- Add the @HippoEssentialsGenerated annotation to the method.
- Set the annotation's internalName parameter to the JCR property name of the field.
- Set the annotation's allowModifications parameter to false.
Example:
@HippoEssentialsGenerated(internalName = "myproject:title", allowModifications = false) public String getTitle() { return getSingleProperty("myproject:title"); }
You can also prevent the whole bean class from being modified by the Beanwriter by updating the class level annotation in the same way.
Persistence
Typically content beans only have getters and consequently are "read-only". If a document type is used to store content submitted by site visitors its content bean must support persistence.
To support persistence a content bean must have setters and in addition must implement the org.hippoecm.hst.content.beans.ContentNodeBinder interface.