title: Dynamic list constraint not updating in alfresco on a datalist
body: <p>I tried to create a dynamic list constraint. The data in the drop down is not getting refreshed when an item is added to the database.</p>
  
  <p><strong>ListOfValuesQueryConstraint.java</strong></p>
  
  <pre><code>package org.alfresco.ryden;
  
  import java.util.ArrayList;
  
  import java.util.List;
  import java.io.Serializable;
  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 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 allowedLabels;
  
  public void setAllowedValues(List allowedValues) {}
  public void setCaseSensitive(boolean caseSensitive) {}
  
  public void initialize() {
  super.setCaseSensitive(false);
  this.loadDB();
  }
  public List getAllowedValues() {
  this.loadDB();
  return super.getAllowedValues(); // In earlier post there is no return statement..
  //return this.getAllowedValues();
  }
  public List getAllowedLabels() {
  return this.allowedLabels;
  }
  
  public void setAllowedLabels(List allowedLabels) {
  this.allowedLabels=allowedLabels;
  }
  
  public List getSelectItemList() {
  List result = new ArrayList(this.getAllowedValues().size());
  for(int i=0;i&lt;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 av = new ArrayList();
  List al=new ArrayList();
  
  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”));
  System.out.println(“value of prop pavani “+rs.getString(“propertyRef”));
  logger.debug(“value of prop pavani “+rs.getString(“propertyRef”));
  }
  rs=null;
  }
  catch (Exception e) {}
  
  super.setAllowedValues(av);
  this.setAllowedLabels(al);
  }
  }
  </code></pre>
  
  <p><strong>CustomListComponentGenerator.java</strong></p>
  
  <pre><code>package org.alfresco.ryden;
  
  import java.util.List;
  
  import javax.faces.component.UIComponent;
  import javax.faces.component.UISelectOne;
  import javax.faces.context.FacesContext;
  
  import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
  import org.alfresco.service.cmr.dictionary.Constraint;
  import org.alfresco.service.cmr.dictionary.ConstraintDefinition;
  import org.alfresco.service.cmr.dictionary.PropertyDefinition;
  import org.alfresco.web.bean.generator.TextFieldGenerator;
  import org.alfresco.web.ui.repo.component.property.PropertySheetItem;
  import org.alfresco.web.ui.repo.component.property.UIPropertySheet;
  import org.apache.log4j.Logger;
  
  import org.alfresco.ryden.ListOfValuesQueryConstraint;
  
  public class CustomListComponentGenerator extends TextFieldGenerator {
  private static Logger log = Logger.getLogger(CustomListComponentGenerator.class);
  
  // private String tutorialQuery =
  // “( TYPE:\”{http://www.alfresco.org/model/content/1.0}content\” AND
  // (@\\{http\\://www.alfresco.org/model/content/1.0\\}name:\”tutorial\”
  // TEXT:\”tutorial\”))”
  // ;
  
  private boolean autoRefresh = false;
  
  public boolean isAutoRefresh() {
  return autoRefresh;
  }
  
  /**
  * This gets set from faces-config-beans.xml, and allows some drop downs to
  * be automaticlaly refreshable (i.e. country), and others not (i.e. city).
  */
  public void setAutoRefresh(boolean autoRefresh) {
  this.autoRefresh = autoRefresh;
  }
  
  @Override
  @SuppressWarnings(“unchecked”)
  protected UIComponent createComponent(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
  UIComponent component = super.createComponent(context, propertySheet, item);
  log.info(“********************** ” + item + ” &gt;” + component + ” &gt;” + (component instanceof UISelectOne) + ” ” + isAutoRefresh());
  if (component instanceof UISelectOne &amp;&amp; isAutoRefresh()) {
  component.getAttributes().put(“onchange”, “submit()”);
  }
  return component;
  }
  
  /**
  * Retrieves the list of values constraint for the item, if it has one
  *
  * @param context
  * FacesContext
  * @param propertySheet
  * The property sheet being generated
  * @param item
  * The item being generated
  * @return The constraint if the item has one, null otherwise
  */
  protected ListOfValuesConstraint getListOfValuesConstraint(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
  ListOfValuesConstraint lovConstraint = null;
  
  log.info(“propertySheet: ” + propertySheet.getNode() + ” item: ” + item.getName());
  // get the property definition for the item
  PropertyDefinition propertyDef = getPropertyDefinition(context, propertySheet.getNode(), item.getName());
  
  if (propertyDef != null) {
  // go through the constaints and see if it has the
  // list of values constraint
  List constraints = propertyDef.getConstraints();
  for (ConstraintDefinition constraintDef : constraints) {
  Constraint constraint = constraintDef.getConstraint();
  //log.info(“constraint: ” + constraint);
  if (constraint instanceof ListOfValuesQueryConstraint) {
  //Node currentNode = (Node) propertySheet.getNode();
  // This is a workaround for the fact that constraints do not
  // have a reference to Node.
  //((ListOfValuesQueryConstraint) constraint).setNode(currentNode);
  lovConstraint = (ListOfValuesQueryConstraint) constraint;
  break;
  }
  
  if (constraint instanceof ListOfValuesConstraint) {
  lovConstraint = (ListOfValuesConstraint) constraint;
  break;
  }
  }
  }
  return lovConstraint;
  }
  
  }
  </code></pre>
  
  <p><strong>custom-model.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;!-- Definition of Property Base Model --&gt;
  
  &lt;model name="cdl:customdatalist" xmlns="http://www.alfresco.org/model/dictionary/1.0"&gt;
  
     &lt;!-- Optional meta-data about the model --&gt;
     &lt;description&gt;Custom Data Model&lt;/description&gt;
     &lt;author&gt;Lalitha Akella&lt;/author&gt;
     &lt;version&gt;1.0&lt;/version&gt;
  
     &lt;!-- Imports are required to allow references to definitions in other models --&gt;
     &lt;imports&gt;
        &lt;!-- Import Alfresco Dictionary Definitions --&gt;
        &lt;import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/&gt;
        &lt;!-- Import Alfresco Content Domain Model Definitions --&gt;
        &lt;import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/&gt;
         &lt;import uri="http://www.alfresco.org/model/datalist/1.0" prefix="dl"/&gt;
     &lt;/imports&gt;
  
     &lt;!-- Introduction of new namespaces defined by this model --&gt;
     &lt;namespaces&gt;
        &lt;namespace uri="cdl.model" prefix="cdl"/&gt;
     &lt;/namespaces&gt;
       &lt;constraints&gt;
          &lt;constraint name="cdl:PropertyRef" type="org.alfresco.ryden.ListOfValuesQueryConstraint" &gt;
           &lt;parameter name="allowedValues"&gt;
           &lt;list&gt;
           &lt;/list&gt;
          &lt;/parameter&gt;
            &lt;parameter name="caseSensitive"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/parameter&gt;
          &lt;/constraint&gt;
        &lt;/constraints&gt;
      &lt;types&gt;
  
          &lt;type name="cdl:applicationform"&gt;
              &lt;title&gt;Custom Application Form&lt;/title&gt;
              &lt;parent&gt;dl:dataListItem&lt;/parent&gt;
              &lt;properties&gt;
                  &lt;property name="cdl:applicationpropertyRef"&gt;
                      &lt;title&gt;Property Reference&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;true&lt;/mandatory&gt;
                      &lt;constraints&gt;
                          &lt;constraint ref="cdl:PropertyRef" /&gt;
                      &lt;/constraints&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:applicationpropAddress"&gt;
                      &lt;title&gt;Property Address&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
  
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:apcreateddate"&gt;
                      &lt;title&gt;Created Date&lt;/title&gt;
                      &lt;type&gt;d:date&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:apcreatedby"&gt;
                      &lt;title&gt;Created By&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:applicationstatus"&gt;
                      &lt;title&gt;Application Status&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:applicationlink"&gt;
                      &lt;title&gt;Application Workflow Link&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
              &lt;/properties&gt;
          &lt;associations&gt;
              &lt;association name="cdl:applicationassignee"&gt;
                 &lt;title&gt;Assignee&lt;/title&gt;
                 &lt;source&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;true&lt;/many&gt;
                 &lt;/source&gt;
                 &lt;target&gt;
                    &lt;class&gt;cm:person&lt;/class&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;false&lt;/many&gt;
                 &lt;/target&gt;
              &lt;/association&gt;
              &lt;association name="cdl:applicationattachments"&gt;
                 &lt;title&gt;Attachments&lt;/title&gt;
                 &lt;source&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;true&lt;/many&gt;
                 &lt;/source&gt;
                 &lt;target&gt;
                    &lt;class&gt;cm:cmobject&lt;/class&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;true&lt;/many&gt;
                 &lt;/target&gt;
              &lt;/association&gt;
           &lt;/associations&gt;
        &lt;/type&gt;   
        &lt;type name="cdl:terminationform"&gt;
              &lt;title&gt;Custom Termination Form&lt;/title&gt;
              &lt;parent&gt;dl:dataListItem&lt;/parent&gt;
              &lt;properties&gt;
                  &lt;property name="cdl:terminationpropertyRef"&gt;
                      &lt;title&gt;Property Reference&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;true&lt;/mandatory&gt;
                      &lt;constraints&gt;
                          &lt;constraint ref="cdl:PropertyRef" /&gt;
                      &lt;/constraints&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:trcreateddate"&gt;
                      &lt;title&gt;Created Date&lt;/title&gt;
                      &lt;type&gt;d:date&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:trcreatedby"&gt;
                      &lt;title&gt;Created By&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:terminationstatus"&gt;
                      &lt;title&gt;Termination Status&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
                  &lt;property name="cdl:terminationlink"&gt;
                      &lt;title&gt;Termination Workflow Link&lt;/title&gt;
                      &lt;type&gt;d:text&lt;/type&gt;
                      &lt;mandatory&gt;false&lt;/mandatory&gt;
                  &lt;/property&gt;
              &lt;/properties&gt;
          &lt;associations&gt;
              &lt;association name="cdl:terminationassignee"&gt;
                 &lt;title&gt;Assignee&lt;/title&gt;
                 &lt;source&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;true&lt;/many&gt;
                 &lt;/source&gt;
                 &lt;target&gt;
                    &lt;class&gt;cm:person&lt;/class&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;false&lt;/many&gt;
                 &lt;/target&gt;
              &lt;/association&gt;
              &lt;association name="cdl:terminationattachments"&gt;
                 &lt;title&gt;Attachments&lt;/title&gt;
                 &lt;source&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;true&lt;/many&gt;
                 &lt;/source&gt;
                 &lt;target&gt;
                    &lt;class&gt;cm:cmobject&lt;/class&gt;
                    &lt;mandatory&gt;true&lt;/mandatory&gt;
                    &lt;many&gt;true&lt;/many&gt;
                 &lt;/target&gt;
              &lt;/association&gt;
           &lt;/associations&gt;
        &lt;/type&gt;
      &lt;/types&gt;
  
  &lt;/model&gt;
  </code></pre>
  
  <p><strong>web-client-config-custom.xml</strong></p>
  
  <pre><code>&lt;config evaluator="node-type" condition="cdl:assignationform"&gt;
      &lt;property-sheet&gt;
          &lt;show-property name="cdl:assignationpropertyRef" component-generator="CustomListComponentGenerator" /&gt;
      &lt;/property-sheet&gt;
     &lt;/config&gt;
  </code></pre>
  
  <p><strong>faces-config-beans.xml</strong></p>
  
  <pre><code>&lt;managed-bean&gt;
                  &lt;description&gt;
                          Bean that generates a custom generator component
                  &lt;/description&gt;
                  &lt;managed-bean-name&gt;
                          CustomListComponentGenerator
                  &lt;/managed-bean-name&gt;
                  &lt;managed-bean-class&gt;
                          org.alfresco.ryden.CustomListComponentGenerator
                  &lt;/managed-bean-class&gt;
                  &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;
                   &lt;managed-property&gt;
                          &lt;property-name&gt;autoRefresh&lt;/property-name&gt;
                          &lt;value&gt;true&lt;/value&gt;
                  &lt;/managed-property&gt;
          &lt;/managed-bean&gt;
  </code></pre>
  
  <p>I don't know whether I should be changing any other files or some thing is wrong in the code above.
  I am new To alfresco. Any help is deeply appreciated.</p>
  
  <p>Thanks,
  Pavani</p>