Solr search result ContentBeanBinder - BloomReach Experience - Open Source CMS

This article covers a Hippo CMS version 11. There's an updated version available that covers our most recent release.

04-07-2016

Solr search result ContentBeanBinder

Concept: When you have executed a HippoQuery and you have a HippoQueryResult, you can choose the bind the ContentBean in the Hit back to its original provider. This is done through aContentBeanBinder.

As a developer, you can invoke for this #setContentBeanBinders:

HippoQueryResult result = hippoQuery.execute();
result.setContentBeanBinders();

or #setContentBeanBinders(List<ContentBeanBinder> binders)

HippoQueryResult result = hippoQuery.execute();
result.setContentBeanBinders(List<ContentBeanBinder> binders);

When using result.setContentBeanBinders(), all available ContentBeanBinders from HippoSolrClient#getContentBeanBinders() are used: Typically only one of the available binders can be used for a specific hit to bind it back to its original provider. Developers can inject through Spring configuration their own ContentBeanBinders into the HippoSolrClient. The JcrContentBeanBinder available in the HippoSolrClientImpl implementation.

When you have invoked result.setContentBeanBinders() or result.setContentBeanBinders(List<ContentBeanBinder> binders) , then, when you call Hit#getBean(), one of the available providers tries to bind the bean back to its original provider. This is done by looping through all the ContentBeanBinders, and if there is found a suitable ContentBeanBinder for the bean, that one gets its #bind(IdentifiableContentBean) invoked. The code in the HitImpl that does this is:

for (ContentBeanBinder contentBeanBinder : contentBeanBinders) {
    if (!contentBeanBinder.canBind(identifiableContentBean.getClass())) {
       continue;
    }
    try {
       contentBeanBinder.bind(identifiableContentBean);
       binded = true;
       break;
     } catch (BindingException e) {
       // log warn
     }
}

If you create and configure your own ContentBeanBinder, then the #bind method with the IdentifiableContentBean instance will be invoked. A developer can then populate the IdentifiableContentBean. For example, the JcrContentBeanBinder looks something like:

public class JcrContentBeanBinder implements ContentBeanBinder {
    @Override
    public boolean canBind(
                    final Class<? extends IdentifiableContentBean> clazz) {
        return (HippoBean.class.isAssignableFrom(clazz));
    }

    @Override
    public void bind(final IdentifiableContentBean identifiableContentBean)
                                                  throws BindingException {
        if (!(identifiableContentBean instanceof HippoBean)) {
            // log warn
            return;
        }
        HippoBean bean = (HippoBean) identifiableContentBean;
        if (RequestContextProvider.get() == null) {
            // log warn
        }
        try {
            Node node = RequestContextProvider.get().getSession().
                   getNodeByIdentifier(bean.getIdentifier());
            bean.setNode(node);
        } catch (ItemNotFoundException e) {
            // log warn
        } catch (RepositoryException e) {
            // log warn
        }
    }
}
Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?