07-06-2010 09:00 AM
public class MyConstraint extends ListOfValuesConstraint {
private static ServiceRegistry registry;
@Override
public void initialize() {
loadData();
}
@Override
public List getAllowedValues() {
loadData();
return super.getAllowedValues();
}
@Override
public void setAllowedValues(List allowedValues) {
}
// Getters and setters for service registry and other things
protected void loadData() {
List<String> values = new LinkedList<String>();
String query = "+TYPE:\"cm:category\" +@cm\\:description:\"" + tipo + "\"";
StoreRef storeRef = new StoreRef("workspace://SpacesStore");
ResultSet resultSet = registry.getSearchService().query(storeRef, SearchService.LANGUAGE_LUCENE, query);
// … values.add(data obtained using searchService and nodeService) …
if (values.isEmpty()) {
values.add("-");
}
super.setAllowedValues(values);
}
}
If I only call "loadData()" from initialize(), it works fine: executes the Lucene query, gets the data, and the dropdown displays it correctly. Only that it's not dynamic: data doesn't get refreshed unless I restart the Alfresco server.07-22-2010 02:35 PM
10-06-2010 11:46 AM
12-17-2010 12:33 PM
public abstract class SearchBasedListConstraint extends ListOfValuesConstraint {
private static ServiceRegistry serviceRegistry = null;
protected String strStoreRef = "workspace://SpacesStore";
protected String query = null;
protected String sortField = null;
protected Boolean sortDescending = null;
@Override
public void initialize() {
//List<String> allowedValues = getSearchResult();
//super.setAllowedValues(allowedValues);
}
@Override
public List<String> getAllowedValues()
{
List<String> allowedValues = getSearchResult();
//super.setAllowedValues(allowedValues);
//return super.getAllowedValues();
return allowedValues;
}
protected abstract List<String> getSearchResult();
public ServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
public void setServiceRegistry(ServiceRegistry serviceRegistryParam) {
serviceRegistry = serviceRegistryParam;
}
public String getStrStoreRef() {
return strStoreRef;
}
public void setStrStoreRef(String strStoreRef) {
this.strStoreRef = strStoreRef;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public String getSortField() {
return sortField;
}
public void setSortField(String sortField) {
this.sortField = sortField;
}
public Boolean getSortDescending() {
return sortDescending;
}
public void setSortDescending(Boolean sortDescending) {
this.sortDescending = sortDescending;
}
}
public class LuceneSearchBasedListConstraint extends SearchBasedListConstraint {
protected List<String> getSearchResult() {
SearchService searchService = getServiceRegistry().getSearchService();
NodeService nodeService = getServiceRegistry().getNodeService();
List<String> allowedValues = new ArrayList<String>();
try {
StoreRef storeRef = new StoreRef(strStoreRef);
ResultSet resultSet = null;
if (sortField != null) {
SearchParameters sp = new SearchParameters();
sp.addStore(storeRef);
sp.setLanguage(SearchService.LANGUAGE_LUCENE);
sp.setQuery(query);
sp.addSort(sortField, (sortDescending!=null) ? !sortDescending : true);
resultSet = searchService.query(sp);
} else {
resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, query);
}
for (ResultSetRow row : resultSet) {
allowedValues.add((String)nodeService.getProperty(row.getNodeRef(), ContentModel.PROP_NAME));
}
} catch (Exception e) {
e.printStackTrace();
}
return allowedValues;
}
}
03-17-2011 07:07 AM
package org.alfresco.ryden;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.web.bean.generator.BaseComponentGenerator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.Serializable;
import javax.faces.model.SelectItem;
public class ListOfValuesQueryConstraint extends ListOfValuesConstraint implements Serializable {
private static Log logger = LogFactory.getLog(BaseComponentGenerator.class);
private static final long serialVersionUID=1;
private List<String> allowedLabels;
public void setAllowedValues(List allowedValues) {}
public void setCaseSensitive(boolean caseSensitive) {}
public void initialize() {
super.setCaseSensitive(false);
this.loadDB();
}
public List<String> getAllowedLabels() {
return this.allowedLabels;
}
public void setAllowedLabels(List<String> allowedLabels) {
this.allowedLabels=allowedLabels;
}
public List<SelectItem> getSelectItemList() {
List<SelectItem> result = new ArrayList<SelectItem>(this.getAllowedValues().size());
for(int i=0;i<this.getAllowedValues().size();i++) {
result.add(new SelectItem((Object)this.getAllowedValues().get(i),this.allowedLabels.get(i)));
}
return result;
}
protected void loadDB() {
String driverName = "com.mysql.jdbc.Driver";
String serverName = "localhost:3307";
String mydatabase = "propertyrecord";
String username = "propertyrecord";
String password = "rydenproperty";
List<String> av = new ArrayList<String>();
List<String> al=new ArrayList<String>();
try {
Connection connection = null;
Class.forName(driverName);
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select propertyRef from propertyrecord");
while (rs.next()) {
av.add(rs.getString("propertyRef"));
al.add(rs.getString("propertyRef"));
}
}
catch (Exception e) {}
super.setAllowedValues(av);
this.setAllowedLabels(al);
}
}
<constraint name="dl:PropertyRef" type="org.alfresco.ryden.ListOfValuesQueryConstraint" />
package org.alfresco.ryden;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
public class ListOfValuesQueryConstraint extends ListOfValuesConstraint {
@Override
public void initialize() {
}
@Override
public List<String> getAllowedValues()
{
List<String> allowedValues = loadDB();
//super.setAllowedValues(allowedValues);
//return super.getAllowedValues();
return allowedValues;
}
protected List<String> loadDB() {
String driverName = "com.mysql.jdbc.Driver";
String serverName = "localhost:3307";
String mydatabase = "propertyrecord";
String username = "propertyrecord";
String password = "rydenproperty";
List<String> av = new ArrayList<String>();
List<String> al=new ArrayList<String>();
try {
Connection connection = null;
Class.forName(driverName);
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select propertyRef from propertyrecord");
while (rs.next()) {
av.add(rs.getString("propertyRef"));
al.add(rs.getString("propertyRef"));
}
rs=null;
}
catch (Exception e) {}
return av;
}
}
and the constraint declaration is <constraint name="dl:PropertyRef" type="org.alfresco.ryden.ListOfValuesQueryConstraint" />
07-20-2011 02:25 AM
This post is rather old.
For documentation purposes, and because its an important question, here is the solution:Override initialize() and do nothing
Don't override setAllowedValues()
Don't put allowedValues in the super class, return the list directly
06-06-2012 10:48 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.