cancel
Showing results for 
Search instead for 
Did you mean: 

List of values constraint from lucene or database search

alarocca
Champ in-the-making
Champ in-the-making
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;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.cmr.search.SearchService;

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

    @Override
    public void initialize() {
    }

    @Override
    public List<String> getAllowedValues() {
        List<String> allowedValues = getSearchResult();
        super.setAllowedValues(allowedValues);
        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));

        Collections.sort(allowedValues);

        if (allowedValues.size() == 0)
            allowedValues.add("");

        return allowedValues;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void setAllowedValues(List allowedValues) {
    }

    @Override
    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>
      <property name="searchService">
         <ref bean="searchService"/>
      </property>
   </bean>

Then the new constraint can be used within your model:


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

Best regards,
Alessandro
2 REPLIES 2

pavaniakella
Champ in-the-making
Champ in-the-making
Hi,

Did you find any solution? If so please post the solution

Regards,
Pavani Akella

Hi,

Is this approach working for anyone? Populatinig the dropdown using List of Values Constraint code from datalist.