AnsweredAssumed Answered

List of values constraint from lucene or database search

Question asked by alarocca on Jul 28, 2011
Latest reply on Sep 5, 2011 by pavaniakella
Here is the code I use to retrieve a list of values constraint from a lucene search. It can easily be modified in order to execute the search against a database.

I still have a problem: getAllowedValues is called just once at Alfresco startup. So the result is not dynamic as I would like. Anyone has useful information to solve this issue?

package my.alfresco.constraint;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;

public class LuceneSearchListConstraint extends ListOfValuesConstraint {
    private static NodeService nodeService;
    private static SearchService searchService;

    public void initialize() {

    public List<String> getAllowedValues() {
        List<String> allowedValues = getSearchResult();
        return allowedValues;

    private List<String> getSearchResult() {
        List<String> allowedValues = new ArrayList<String>();
        String query = "PATH:\"/cm:generalclassifiable//cm:MyCategories/*\"";

        ResultSet resultSet = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_LUCENE, query);
        for (ResultSetRow row : resultSet)
            allowedValues.add((String)nodeService.getProperty(row.getNodeRef(), ContentModel.PROP_NAME));


        if (allowedValues.size() == 0)

        return allowedValues;

    public void setAllowedValues(List allowedValues) {

    public void evaluateCollection(Collection<Object> collection) {

    public void setNodeService(NodeService nodeService) {
        LuceneSearchListConstraint.nodeService = nodeService;

    public void setSearchService(SearchService searchService) {
        LuceneSearchListConstraint.searchService = searchService;


Required service and/or parameters are injected through a spring bean.

   <bean id="LuceneSearchListConstraintInitializer" class="my.alfresco.constraint.LuceneSearchListConstraint">
      <property name="nodeService">
         <ref bean="nodeService"/>
      <property name="searchService">
         <ref bean="searchService"/>

Then the new constraint can be used within your model:

      <constraint name="my:myList" type="my.alfresco.constraint.LuceneSearchListConstraint">
         <parameter name="allowedValues">

Best regards,