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