cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic contraint loading query with searchService

anita_platos
Champ in-the-making
Champ in-the-making
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
10 REPLIES 10

genesy
Champ in-the-making
Champ in-the-making
Hi Anita,
I had the same yours problem and I resolved him with your suggestions about counter!
Like you said this is not a very elegant solution.
Can you give me an example about the better solution with ftl control you were talking about?

Thank you very much!