Get values for ValueListProvider from value list document

Hello,

I am trying to create a ValueListProvider to show a dropdown of Blog Categories from the same value list that we use in the document type. I have tried two ways to retrieve the value list document but both cause the same error which is at the bottom of this post.
I have tried using SelectionUtil.getValueListByIdentifier to get the value list which is defined in the valuseListManager.xml as well as manually querying for it which is commented out in the code below.

Any help would be appreciated since we would like to not have to have one central list of categories update all places it is used.

    package com.staplesconnect.cms.components;

import org.hippoecm.hst.container.RequestContextProvider;

import org.hippoecm.hst.core.parameters.ValueListProvider;

import org.onehippo.forge.selection.hst.contentbean.ValueList;
import org.onehippo.forge.selection.hst.util.SelectionUtil;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class ArticleCategoryValueListProvider implements ValueListProvider {

    private static final Map<String, String> CATEGORY_DISPLAY_VALUES_MAP = new LinkedHashMap<>();
    private static final List<String> CATEGORY_DISPLAY_VALUES_LIST;

    static {
        try {
//            HstRequestContext context = RequestContextProvider.get();
//            Node rootNode = context.getSession().getRootNode();
//            Node valueListNode = JcrUtils.getNodeIfExists(rootNode, "content/documents/staplesconnect/blog/blog-categories");
//            HstQueryResult result = context.getQueryManager().createQuery(valueListNode, ValueList.class).execute();
//            ValueList valueList = (ValueList) result.getHippoBeans().nextHippoBean();
//            if (valueList != null) {
//                valueList.getItems().forEach( valueListItem -> {
//                    System.out.println("Key: " + valueListItem.getKey() + " label: "+ valueListItem.getLabel());
//                    CATEGORY_DISPLAY_VALUES_MAP.put(valueListItem.getKey(), valueListItem.getLabel());
//                });
//            }else {
//                System.out.println("############## list is null");
//            }

            ValueList valueList = SelectionUtil.getValueListByIdentifier("blog-categories", RequestContextProvider.get());
            //  return SelectionUtil.valueListAsMap(valueList);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        CATEGORY_DISPLAY_VALUES_MAP.put("big-ideas", "Big Ideas");
//        CATEGORY_DISPLAY_VALUES_MAP.put("beyond-the-books", "Beyond The Books");
//        CATEGORY_DISPLAY_VALUES_MAP.put("business-boosts", "Business Boosts");

        CATEGORY_DISPLAY_VALUES_LIST = Collections.unmodifiableList(new LinkedList<>(CATEGORY_DISPLAY_VALUES_MAP.keySet()));
    }

    @Override
    public List<String> getValues() {
        return CATEGORY_DISPLAY_VALUES_LIST;
    }

    @Override
    public String getDisplayValue(final String value) {
        return getDisplayValue(value, null);
    }

    @Override
    public String getDisplayValue(final String value, final Locale locale) {
        final String displayValue = CATEGORY_DISPLAY_VALUES_MAP.get(value);
        return (displayValue != null) ? displayValue : value;
    }

}

Error
java.lang.IllegalStateException: No content bean annotation class resource path found.
[INFO] [talledLocalContainer] at org.hippoecm.hst.content.tool.DefaultContentBeansTool.getAnnotatedClasses(DefaultContentBeansTool.java:122)
[INFO] [talledLocalContainer] at org.hippoecm.hst.content.tool.DefaultContentBeansTool.getObjectConverter(DefaultContentBeansTool.java:84)
[INFO] [talledLocalContainer] at org.hippoecm.hst.site.request.HstRequestContextImpl.getObjectConverter(HstRequestContextImpl.java:926)
[INFO] [talledLocalContainer] at org.hippoecm.hst.site.request.HstRequestContextImpl.createObjectBeanManager(HstRequestContextImpl.java:918)
[INFO] [talledLocalContainer] at org.hippoecm.hst.site.request.HstRequestContextImpl.getObjectBeanManager(HstRequestContextImpl.java:836)
[INFO] [talledLocalContainer] at org.hippoecm.hst.site.request.HstRequestContextImpl.getObjectBeanManager(HstRequestContextImpl.java:822)
[INFO] [talledLocalContainer] at org.hippoecm.hst.site.request.HstRequestContextImpl.getSiteContentBaseBean(HstRequestContextImpl.java:784)
[INFO] [talledLocalContainer] at org.onehippo.forge.selection.hst.util.SelectionUtil.getValueListByIdentifier(SelectionUtil.java:66)
[INFO] [talledLocalContainer] at com.staplesconnect.cms.components.ArticleCategoryValueListProvider.(ArticleCategoryValueListProvider.java:48)
[INFO] [talledLocalContainer] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[INFO] [talledLocalContainer] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[INFO] [talledLocalContainer] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[INFO] [talledLocalContainer] at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[INFO] [talledLocalContainer] at java.lang.Class.newInstance(Class.java:442)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.model.ParametersInfoProcessor.createPropertyMap(ParametersInfoProcessor.java:232)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.model.ParametersInfoProcessor.getProperties(ParametersInfoProcessor.java:75)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.model.ParametersInfoProcessor.getPopulatedProperties(ParametersInfoProcessor.java:89)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.services.ContainerItemComponentServiceImpl.represent(ContainerItemComponentServiceImpl.java:244)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.services.ContainerItemComponentServiceImpl.getVariant(ContainerItemComponentServiceImpl.java:79)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.services.ContainerItemComponentResource.getVariant(ContainerItemComponentResource.java:126)
[INFO] [talledLocalContainer] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO] [talledLocalContainer] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[INFO] [talledLocalContainer] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] [talledLocalContainer] at java.lang.reflect.Method.invoke(Method.java:498)
[INFO] [talledLocalContainer] at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179)
[INFO] [talledLocalContainer] at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
[INFO] [talledLocalContainer] at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:193)
[INFO] [talledLocalContainer] at org.hippoecm.hst.jaxrs.cxf.AroundProcessableJAXRSInvoker.invoke(AroundProcessableJAXRSInvoker.java:94)
[INFO] [talledLocalContainer] at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:103)
[INFO] [talledLocalContainer] at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
[INFO] [talledLocalContainer] at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
[INFO] [talledLocalContainer] at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
[INFO] [talledLocalContainer] at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
[INFO] [talledLocalContainer] at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:267)
[INFO] [talledLocalContainer] at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234)
[INFO] [talledLocalContainer] at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208)
[INFO] [talledLocalContainer] at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160)
[INFO] [talledLocalContainer] at org.hippoecm.hst.jaxrs.cxf.CXFJaxrsService.invoke(CXFJaxrsService.java:128)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.cxf.CXFJaxrsHstConfigService.invoke(CXFJaxrsHstConfigService.java:187)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.JaxrsRestServiceValve.invoke(JaxrsRestServiceValve.java:38)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.HstSitePipeline$Invocation.invokeNext(HstSitePipeline.java:288)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.GenericResponseHeadersValve.invoke(GenericResponseHeadersValve.java:83)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.HstSitePipeline$Invocation.invokeNext(HstSitePipeline.java:288)
[INFO] [talledLocalContainer] at org.hippoecm.hst.pagecomposer.jaxrs.container.PageComposerSecurityValve.invoke(PageComposerSecurityValve.java:74)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.HstSitePipeline$Invocation.invokeNext(HstSitePipeline.java:288)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.InitializationValve.invoke(InitializationValve.java:37)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.HstSitePipeline$Invocation.invokeNext(HstSitePipeline.java:288)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.HstSitePipeline.invokeValves(HstSitePipeline.java:173)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.HstSitePipeline.invoke(HstSitePipeline.java:155)
[INFO] [talledLocalContainer] at org.hippoecm.hst.core.container.HstRequestProcessorImpl.processRequest(HstRequestProcessorImpl.java:79)
[INFO] [talledLocalContainer] at org.hippoecm.hst.container.HstDelegateeFilterBean.doFilter(HstDelegateeFilterBean.java:465)
[INFO] [talledLocalContainer] at org.hippoecm.hst.container.DelegatingFilter.doFilter(DelegatingFilter.java:68)
[INFO] [talledLocalContainer] at org.hippoecm.hst.container.HstFilter.doFilter(HstFilter.java:51)
[INFO] [talledLocalContainer] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
[INFO] [talledLocalContainer] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
[INFO] [talledLocalContainer] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
[INFO] [talledLocalContainer] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
[INFO] [talledLocalContainer] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
[INFO] [talledLocalContainer] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
[INFO] [talledLocalContainer] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
[INFO] [talledLocalContainer] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
[INFO] [talledLocalContainer] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
[INFO] [talledLocalContainer] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
[INFO] [talledLocalContainer] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
[INFO] [talledLocalContainer] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
[INFO] [talledLocalContainer] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
[INFO] [talledLocalContainer] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
[INFO] [talledLocalContainer] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
[INFO] [talledLocalContainer] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[INFO] [talledLocalContainer] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[INFO] [talledLocalContainer] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
[INFO] [talledLocalContainer] at java.lang.Thread.run(Thread.java:745)

Hi,
the thing is that you can’t use HST Queries there because the code is running in the CMS webapp. You can use JCR Queries there or just iterate through the nodes hierarchy to read the valuelist key/values.
HTH
Saimir

Note that you need to setup a few things in the cms webapp to get hold of a valuelist object as bean:
in your web.xml

  <context-param>
    <param-name>hst-beans-annotated-classes</param-name>
    <param-value>classpath*:org/onehippo/forge/selection/hst/contentbean/*.class</param-value>
  </context-param>

Add this to your cms-dependencies.xml

    <dependency>
      <groupId>org.onehippo.cms7</groupId>
      <artifactId>hippo-plugin-selections-hst-client</artifactId>
    </dependency>

And try again.

Hi @bcanvural,

I have tried the suggested changes and get a few different errors now.

When I use
ValueList valueList = SelectionUtil.getValueListByIdentifier("blog-categories", RequestContextProvider.get());

I get a null pointer on what I think is trying to find the blog-categories entry in the valueListManager.xml.

 java.lang.NullPointerException
[INFO] [talledLocalContainer]   at org.onehippo.forge.selection.hst.manager.DefaultValueListManager.getValueList(DefaultValueListManager.java:78)
[INFO] [talledLocalContainer]   at org.onehippo.forge.selection.hst.util.SelectionUtil.getValueListByIdentifier(SelectionUtil.java:69)
[INFO] [talledLocalContainer]   at com.staplesconnect.cms.components.ArticleCategoryValueListProvider.<clinit>(ArticleCategoryValueListProvider.java:42)

When I use the more manual way of querying for the document I get the following error.
org.hippoecm.hst.content.beans.query.exceptions.QueryException: Cannot find primaryNodeType for ‘java.lang.Class’.
[INFO] [talledLocalContainer] at org.hippoecm.hst.content.beans.query.HstQueryManagerImpl.createQuery(HstQueryManagerImpl.java:158)
[INFO] [talledLocalContainer] at org.hippoecm.hst.content.beans.query.HstQueryManagerImpl.createQuery(HstQueryManagerImpl.java:138)
[INFO] [talledLocalContainer] at com.staplesconnect.cms.components.ArticleCategoryValueListProvider.(ArticleCategoryValueListProvider.java:31)

I am trying to implement @saimir.muco suggestion now to see if I can get that working.

Thanks,
Tim

This is how i did it in another project:

ObjectConverter converter = HstServices.getComponentManager().getComponent(ObjectConverter.class);
ValueList valueList = (ValueList) converter.getObject(session, "/content/documents/administration/value-lists/my-value-list");

That did it! Thanks for all the help!

1 Like