AnsweredAssumed Answered

Dynamic contraint loading query with searchService

Question asked by anita.platos on Aug 26, 2013
Latest reply on Sep 5, 2013 by anita.platos
I am trying to have a constraint loading its data through a searchService-query. The problem is that when I start the Alfresco, it keeps locked in the query, but it never starts.
This is the code I´m using:
- The java code for constraint:

package org.myproject.mycontraints;
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 LoadingMyCombo 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:\"/app:company_home/cm:DOCUMENTS\"";
        ResultSet resultSet = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_SOLR_FTS_ALFRESCO, 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) {
       LoadingMyCombo.nodeService = nodeService;
    }

    public void setSearchService(SearchService searchService) {
       LoadingMyCombo.searchService = searchService;
    }

}

- The constraint declared in the model:

   <constraint name="prfx:load-my-combo" type="org.myproject.mycontraints.LoadingMyCombo" >
         <parameter name="allowedValues">
               <list>
               </list>
         </parameter>
         <parameter name="caseSensitive"><value>true</value></parameter>
   </constraint>

- The bean to associate with the nodeService

   <bean id="mytests.myconstraints.ConstraintInitializer" class="org.myproject.mycontraints.LoadingMyCombo">
      <property name="nodeService">
         <ref bean="nodeService"/>
      </property>
      <property name="searchService">
         <ref bean="searchService"/>
      </property>
   </bean>

Somebody knows if I´m doing something wrong or why it gets locked indefinitely  in the line where it executes the query?. I know that the query is well-done because I tested OK using the Node Browser console.
Thank you in advance

Outcomes