Home

Custom Constraint

You are here

28 posts / 0 new
Last post
Custom Constraint

I'm a total newbie on the area of java. Nevertheless i wanted to try to create a custom constraint which implements databaselookups for a pick list. I found an example here on the forum.

I was able to get the java code compiled with javac against the Alfresco SDK. The result was a compiled .class file. I was not sure where to put it, so i put it in a new "classes" directory in my extensions root.

Next i created a test model, again based on the example i found, and i set the "type" parameter to my package name in the java code, extended with the name of the public class in the java code. In my case "org.contezza.customConstraints.ListOfValuesQueryConstraint".

Of course it did something wrong. When i try to deploy the model (it's a dynamic model in the data dictionary/models folder) i get the error message in the logging "Constraint type 'org.contezza.customConstraints.ListOfValuesQueryConstraint' on constraint 'test:CodeLabel' is not a well-known type or a valid Constraint implementation".

Because i was able to compile the java code against the SDK without error, i think this error has to do with the place i put the class file in. Can anyone give me a push into the right direction?

My environment is:
- Ubuntu 7.04
- Tomcat 5.5
- JDK 1.5
- MySQL 5

My (example) java code is as follows:

package org.contezza.customConstraints;
 
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
//import org.alfresco.i18n.I18NUtil;
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 = "org.gjt.mm.mysql.Driver";
        String serverName = "localhost";
        String mydatabase = "alfresco";
        String username = "alfresco";
        String password = "alfresco";
 
        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 code,label from codelabel");
            while (rs.next()) {
                av.add(rs.getString("code"));
                al.add(rs.getString("label"));
            }
        }
        catch (Exception e) {}
 
      super.setAllowedValues(av);
      this.setAllowedLabels(al);
   }
}

My example model is as follows:

<?xml version="1.0" encoding="UTF-8"?>
 
<model name="test:test" xmlns="http://www.alfresco.org/model/dictionary/1.0">
<description></description>
<author>Contezza InformatieManagement</author>
<version>0.1</version>
<imports>
<import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d" />
<import uri="http://www.alfresco.org/model/content/1.0" prefix="cm" />
</imports>
<namespaces>
<namespace uri="http://www.contezza.nl/model/test/1.0" prefix="test" />
</namespaces>
 
 
<constraints>
<constraint name="test:CodeLabel" type="org.contezza.customConstraints.ListOfValuesQueryConstraint">
<parameter name="allowedValues">
<list>
</list>
</parameter>
<parameter name="caseSensitive"><value>true</value></parameter>
</constraint>
</constraints>
 
<types>
<type name="test:codecontent">
<title>Contenu étendu</title>
<parent>cm:content</parent>
<properties>
<property name="test:codestr">
<title>Code str</title>
<type>d:text</type>
<constraints>
<constraint ref="test:CodeLabel" />
</constraints>
</property>
</properties>
</type>
</types>
</model>

Re: Custom Constraint

Hi,

I recognize my custom constraint here :D

You should have posted in the same topic for further readers.

After compilation you should place your .class file in
/webapps/alfresco/WEB-INF/classes/org/contezza/customConstraints/

Don't forget to change connection parameters to the database and to create a table codelabel with 2 fields : code and label.

Good luck.

Re: Custom Constraint

Hi Acis,

Thanks for your reply! I have it running since 10 minutes :)

It wasnt clear to me that i needed to put my class in the WEB-INF/lib directory. I thought that putting in somewhere in the classpath would be sufficient. When i tried WEB-INF/lib, it started working.

I also, in the process of troubleshooting, made a .jar file of it. And since i dont like to tamper in the deflated alfresco.war code, i create an AMP-file for it, so that i can install it nicely.

Now i have everything to deploy new classes, i can start customizing your java code. :P

Thanks a lot!

Kind regards,

Koen Bonnet

Re: Custom Constraint

Thanks! That was helpful!

and 1 more question.. Is it possible to, somehow, reload this list from database, each time I access this dropdown?

Because now, this list is static, in order to apply changes from database, i have to restart alfresco.

Any chance of making this more dynamical?

Re: Custom Constraint

I didn't try but I think it's possible by overriding the getAllowedValues() method like :

public List getAllowedValues() {
      this.loadDB();
      super.getAllowedValues();
}

Re: Custom Constraint

my question is how does "allowedLabels" in this code comes into the picture. I see that only allowed values are displayed on the properties page drop down list and the same after selection. do generators and converters come into play if i want to show both of them "label" and "values". Can anyone post a sample of the generator or describe how it is going to look like.

Thanks
Robain

Re: Custom Constraint

To add to my previous comment where does getSelectItemList() method come into play. I dont see it in any of the extended classes or the interface. but where or how is this method utilized.

Thanks
Robain.

Re: Custom Constraint

Hi robain !

Here is how it works :

Your defined a constraint : in that case we extended ListOfValuesConstraint to select the values from a database or anywhere else.

In your web-client-config-custom.xml you should have define that you want to display the property with the constraint.

<property-sheet>
<show-property name="tc:codestr" component-generator="ListOfValuesQueryGenerator" />
</property-sheet>

If you don't put the additional attribut "component-generator", Alfresco will use a list with the values as values and labels of the drop down list. But here we want to have values as values and labels as labels. So we have to create a component generator.

As you can see, I added an additional attribute "component-generator". This attribute means that when alfresco will display the property, it'll use ListOfValuesQueryGenerator to create the component. The Generator is a subclass of org.alfresco.web.bean.generator.TextFieldGenerator. You must override the createComponent and generate methods.
This componentGenerator will call getSelectedItems.

public UIComponent generate(FacesContext context, String id) {
 
	  UIOutput component = (UIOutput)context.getApplication().createComponent(
      ComponentConstants.JAVAX_FACES_OUTPUT);
      component.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
      FacesHelper.setupComponentId(context, component, id);
      return component;
   }
 
   protected UIComponent createComponent(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
 
      UIComponent component = null;
 
      if (propertySheet.inEditMode()) {
 
    	 ListOfValuesQueryConstraint constraint=this.getListOfValuesQuery(context, propertySheet, item);
 
         PropertyDefinition propDef = this.getPropertyDefinition(context, propertySheet.getNode(), item.getName());
 
         if (constraint != null && item.isReadOnly() == false && propDef != null && propDef.isProtected() == false) {
            component = context.getApplication().createComponent(UISelectOne.COMPONENT_TYPE);
            FacesHelper.setupComponentId(context, component, item.getName());
 
            UISelectItems itemsComponent = (UISelectItems)context.getApplication().createComponent("javax.faces.SelectItems");
 
            itemsComponent.setValue(constraint.getSelectItemList());
 
            component.getChildren().add(itemsComponent);
         }
      }
      else {
         component = generate(context, item.getName());
      }
 
      return component;
   }
 
protected ListOfValuesQueryConstraint getListOfValuesQuery(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
 
	  ListOfValuesQueryConstraint lovqConstraint = null;
      PropertyDefinition propertyDef = getPropertyDefinition(context, propertySheet.getNode(), item.getName());
 
      if (propertyDef != null) {
 
         List<ConstraintDefinition> constraints = propertyDef.getConstraints();
         for (ConstraintDefinition constraintDef : constraints) {
 
            Constraint constraint = constraintDef.getConstraint();
            if (constraint instanceof ListOfValuesQueryConstraint) {
            	lovqConstraint = (ListOfValuesQueryConstraint)constraint;
               break;
            }
         }
      }
 
      return lovqConstraint;
   }

Ok now the Edit mode will work perfectly. But the problem is to display the field in view mode. Alfresco will show the value and not the label. So here you have to use a converter.

In your generator add this :

protected void setupConverter(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem property, PropertyDefinition propertyDef, UIComponent component) {
 
super.setupConverter(context, propertySheet, property, propertyDef, component);
 
if(!propertySheet.inEditMode() && propertyDef != null && component instanceof UIOutput) {
((UIOutput)component).setConverter(new ListOfValuesQueryConverter(this.getListOfValuesQuery(context, propertySheet, property)));
}
}

and create the converter

public class ListOfValuesQueryConverter implements Converter,StateHolder {
 
	private ListOfValuesQueryConstraint constraint;
 
	public ListOfValuesQueryConverter(ListOfValuesQueryConstraint constraint) {
		this.constraint=constraint;
	}
 
	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		return new Object();
   }
 
   public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
 
	   if(value instanceof String) {
		   List<String> l = this.constraint.getAllowedValues();
		   	for(int i=0;i<l.size();i++) {
		   		if(l.get(i).equals((String)value)) {
		   			return this.constraint.getAllowedLabels().get(i);
		   		}
		   	}
	   }
	   else if(value instanceof List) {
		   String result="";
		   List<String> l = this.constraint.getAllowedValues();
		   for(String val : (List<String>)value) {
			   	for(int i=0;i<l.size();i++) {
			   		if(l.get(i).equals(val)) {
			   			result+=this.constraint.getAllowedLabels().get(i)+" ";
			   			break;
			   		}
			   	}
		   }
		   return result;
	   }
 
	   return new String();
   }
 
   public ListOfValuesQueryConverter() {}
   public Object saveState(FacesContext context) { return (Object)this.constraint; }
   public void restoreState(FacesContext context, Object state) { this.constraint=(ListOfValuesQueryConstraint)state; }
   public boolean isTransient() { return false; }
   public void setTransient(boolean newTransientValue) {}
}

we could have use the "converter=" attribute in the web-client-config-custom.xml but good luck to do it. I tried hard and it didn't work out.

Re: Custom Constraint

Escuse me for my english, when i compile the 'customConstraints' i need to import the following :

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;

where do i find these ?

Thanks
Alex--70

Re: Custom Constraint

Download Alfresco SDK to get the libs.

Re: Custom Constraint

Thank acis.
The example run correctly, but this list is static, in order to apply changes from database, i have to restart alfresco.
I have try to modify the getAllowedValues metod, as you told, but nothing.

Have you other ideas ?

Thansk lot
alex--70

Re: Custom Constraint

I didn't check the suggestion I gave so I don't know if it's working.

but it's strange that the list is not refreshing if you are calling back the loadDb function because the allowedValues list is changed in taht function

maybe you can check if the web interface don't have the list in cache and try to refresh it

Re: Custom Constraint

I have refresh the page but the result is that the list remain with same values (static value)
I used the followed code :

public List getAllowedValues() {
this.loadDB();
super.getAllowedValues();
}

Can you try your code ?

Thank a lot

Re: Custom Constraint

Questiong for "msvoren" , you had the my same problem.

Have you put the new item into the custom constraint (combo box) after reload the record from database ?

Thanks

alex--70

Re: Custom Constraint

How can I force the refresh from db (this.loadDb()) without user interact???

Where I have to put the loadDb call?

I need to write a property by web service code. I write the value I need but if the database changes I got an error. The writing work well if I click on dropdownlist to force the values list to refresh with database value. How can I force the refresh (constraint values) itself (without clikking on the dropdown list? Where I have to put the call "this.loadDB()"??

Please help me.

Thanks!

package org.contezza.customConstraints;

import java.util.ArrayList;
import java.util.List;
import java.sql.*;
//import org.alfresco.i18n.I18NUtil;
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 allowedLabels;

public void setAllowedValues(List allowedValues) {}
public void setCaseSensitive(boolean caseSensitive) {}

public void initialize() {
super.setCaseSensitive(false);
this.loadDB();
}

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 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 code,label from codelabel");
while (rs.next()) {
av.add(rs.getString("code"));
al.add(rs.getString("label"));
}
}
catch (Exception e) {}

super.setAllowedValues(av);
this.setAllowedLabels(al);
}
}

How can I force the refresh from db (this.loadDb()) without user interact???

Where I have to put the loadDb call?

I need to write a property by web service code. I write the value I need but if the database changes I got an error. The writing work well if I click on dropdownlist to force the values list to refresh with database value. How can I force the refresh (constraint values) itself (without clikking on the dropdown list? Where I have to put the call "this.loadDB()"??

Please help me.

Thanks!

Re: Custom Constraint

acis wrote:

we could have use the "converter=" attribute in the web-client-config-custom.xml but good luck to do it. I tried hard and it didn't work out.

Thats because you have to declare your converter in the faces-config-custom.xml. Of course, that is not undocumented but simply implied to be a known fact.
But unfortunately that won't help you either, since the converter is used for the value, not the label.
In the edit view, that is, not in the properties view, where it looks correct. Holy jesus on a bicycle....

Re: Custom Constraint

Hello,

Once I figured out the imports I was able to get this code to work, I think... I have one remaining issue to resolve and I wasn't sure if anyone else has faced this issue:

    Details page shows the label correct for the property value Drop downs show the label correctly in edit mode
    However, my "selected items" shows the value and not the label.

In catalina.out the warning is:

WARN [bean.generator.BaseComponentGenerator] ListOfValuesQueryConverter could not be applied

In web-client-config-custom.xml the property is:

In faces-config-custom my beans are:

Dropdown with lable and value
ListOfValuesQueryGenerator
org.contezza.customConstraints.ListOfValuesQueryGenerator
request

Dropdown with lable and value
ListOfValuesQueryConverter
org.contezza.customConstraints.ListOfValuesQueryConverter
request

Any ideas?

Re: Custom Constraint

nice thread....

I am able to populate drop down from database table and also when I add new value in database it gets reflected in Alfresco.

Re: Custom Constraint

Hi Nikes,
can you summarize your solution for us as example in the wiki and post a link here ? Idealy, you can put your lessonslearned in there as well ?

That would be great,
Norgan

"Reading alone is not enough, sometimes, understanding is helpful as well"(roughly after Kurt Tucholsky ;-) )

Did I shorten your search, gimme a point, I want one too :-D ... oh ... and update the Wiki PLEASE.
---
Developer Tutorials: http://www.ecmarchitect.com
Share Login vs Alfresco Login: Forum Thread

Re: Custom Constraint

Please refer below refined code. Its working properly on my implementation.

package org.customConstraints;
 
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 getAllowedValues() {
this.loadDB();
return super.getAllowedValues(); // In earlier post there is no return statement..
//return this.getAllowedValues();
}
 
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 = "org.gjt.mm.mysql.Driver";
String serverName = "localhost";
String mydatabase = "alfresco";
String username = "root";
String password = "admin1";
 
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 custName from Customers");
while (rs.next()) {
av.add(rs.getString("custName"));
al.add(rs.getString("custName"));
}
rs=null;
}
catch (Exception e) {}
 
super.setAllowedValues(av);
this.setAllowedLabels(al);
}
}

Re: Custom Constraint

Hi everyone
I tried to do the same thing, i compiled the class ListOfValuesQueryConstraint -as it is- successfully and put it into "C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\org\contezza\customConstraints".
then I compied the model, but when i try to test it, i get the following error :

Testing dictionary model definitions...
alfresco/model/dictionaryModel.xml
alfresco/model/systemModel.xml
org/alfresco/repo/security/authentication/userModel.xml
alfresco/model/contentModel.xml
alfresco/model/wcmModel.xml
alfresco/model/applicationModel.xml
alfresco/model/bpmModel.xml
alfresco/extension/test.xml
Found an invalid model...
00210002 Could not import bootstrap model alfresco/extension/test.xml
00210001 Failed to compile model test:test
00210000 d_dictionary.constraint.list_of_values.no_values

do you know what the error means and what I should do to handle it?
thanks in advance.

Re: Custom Constraint

HI Guys,
I am having the same problem. Did anyone manage to get this working?

Thanks,
Croc

Re: Custom Constraint

Hi,

Can this Dynamic List Constraint be applied to a data list property?
I am not able to update the drop down upon addition of a row in the database.

Code I created is here.

http://stackoverflow.com/questions/5353145/dynamic-list-constraint-not-updating-in-alfresco-on-a-datalist
Please guide me.
Thanks in advance

Regards,
Pavani

Re: Custom Constraint

acis wrote:
Hi,

I recognize my custom constraint here :D

You should have posted in the same topic for further readers.

After compilation you should place your .class file in
/webapps/alfresco/WEB-INF/classes/org/contezza/customConstraints/

Don't forget to change connection parameters to the database and to create a table codelabel with 2 fields : code and label.

Good luck.

Thanks it works! :arrow: Im using alfresco 4.0.b

Christopher Jiménez Valverde
Rivet Logic

Blog: http://cjimenez2581.blogspot.com/

Re: Custom Constraint

Nikes wrote:
nice thread....

I am able to populate drop down from database table and also when I add new value in database it gets reflected in Alfresco.

how could you insert a new value in a database?

Thanks

Christopher Jiménez Valverde
Rivet Logic

Blog: http://cjimenez2581.blogspot.com/

Re: Custom Constraint

Where can I place the java class within tomcat/shared so it can stay backed up with all other custom configurations?

Re: Custom Constraint

acis wrote:
Hi robain !

Here is how it works :

Your defined a constraint : in that case we extended ListOfValuesConstraint to select the values from a database or anywhere else.

In your web-client-config-custom.xml you should have define that you want to display the property with the constraint.

<property-sheet>
<show-property name="tc:codestr" component-generator="ListOfValuesQueryGenerator" />
</property-sheet>

If you don't put the additional attribut "component-generator", Alfresco will use a list with the values as values and labels of the drop down list. But here we want to have values as values and labels as labels. So we have to create a component generator.

As you can see, I added an additional attribute "component-generator". This attribute means that when alfresco will display the property, it'll use ListOfValuesQueryGenerator to create the component. The Generator is a subclass of org.alfresco.web.bean.generator.TextFieldGenerator. You must override the createComponent and generate methods.
This componentGenerator will call getSelectedItems.

public UIComponent generate(FacesContext context, String id) {
 
	  UIOutput component = (UIOutput)context.getApplication().createComponent(
      ComponentConstants.JAVAX_FACES_OUTPUT);
      component.setRendererType(ComponentConstants.JAVAX_FACES_TEXT);
      FacesHelper.setupComponentId(context, component, id);
      return component;
   }
 
   protected UIComponent createComponent(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
 
      UIComponent component = null;
 
      if (propertySheet.inEditMode()) {
 
    	 ListOfValuesQueryConstraint constraint=this.getListOfValuesQuery(context, propertySheet, item);
 
         PropertyDefinition propDef = this.getPropertyDefinition(context, propertySheet.getNode(), item.getName());
 
         if (constraint != null && item.isReadOnly() == false && propDef != null && propDef.isProtected() == false) {
            component = context.getApplication().createComponent(UISelectOne.COMPONENT_TYPE);
            FacesHelper.setupComponentId(context, component, item.getName());
 
            UISelectItems itemsComponent = (UISelectItems)context.getApplication().createComponent("javax.faces.SelectItems");
 
            itemsComponent.setValue(constraint.getSelectItemList());
 
            component.getChildren().add(itemsComponent);
         }
      }
      else {
         component = generate(context, item.getName());
      }
 
      return component;
   }
 
protected ListOfValuesQueryConstraint getListOfValuesQuery(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
 
	  ListOfValuesQueryConstraint lovqConstraint = null;
      PropertyDefinition propertyDef = getPropertyDefinition(context, propertySheet.getNode(), item.getName());
 
      if (propertyDef != null) {
 
         List<ConstraintDefinition> constraints = propertyDef.getConstraints();
         for (ConstraintDefinition constraintDef : constraints) {
 
            Constraint constraint = constraintDef.getConstraint();
            if (constraint instanceof ListOfValuesQueryConstraint) {
            	lovqConstraint = (ListOfValuesQueryConstraint)constraint;
               break;
            }
         }
      }
 
      return lovqConstraint;
   }

Ok now the Edit mode will work perfectly. But the problem is to display the field in view mode. Alfresco will show the value and not the label. So here you have to use a converter.

In your generator add this :

protected void setupConverter(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem property, PropertyDefinition propertyDef, UIComponent component) {
 
super.setupConverter(context, propertySheet, property, propertyDef, component);
 
if(!propertySheet.inEditMode() && propertyDef != null && component instanceof UIOutput) {
((UIOutput)component).setConverter(new ListOfValuesQueryConverter(this.getListOfValuesQuery(context, propertySheet, property)));
}
}

and create the converter

public class ListOfValuesQueryConverter implements Converter,StateHolder {
 
	private ListOfValuesQueryConstraint constraint;
 
	public ListOfValuesQueryConverter(ListOfValuesQueryConstraint constraint) {
		this.constraint=constraint;
	}
 
	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		return new Object();
   }
 
   public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
 
	   if(value instanceof String) {
		   List<String> l = this.constraint.getAllowedValues();
		   	for(int i=0;i<l.size();i++) {
		   		if(l.get(i).equals((String)value)) {
		   			return this.constraint.getAllowedLabels().get(i);
		   		}
		   	}
	   }
	   else if(value instanceof List) {
		   String result="";
		   List<String> l = this.constraint.getAllowedValues();
		   for(String val : (List<String>)value) {
			   	for(int i=0;i<l.size();i++) {
			   		if(l.get(i).equals(val)) {
			   			result+=this.constraint.getAllowedLabels().get(i)+" ";
			   			break;
			   		}
			   	}
		   }
		   return result;
	   }
 
	   return new String();
   }
 
   public ListOfValuesQueryConverter() {}
   public Object saveState(FacesContext context) { return (Object)this.constraint; }
   public void restoreState(FacesContext context, Object state) { this.constraint=(ListOfValuesQueryConstraint)state; }
   public boolean isTransient() { return false; }
   public void setTransient(boolean newTransientValue) {}
}

we could have use the "converter=" attribute in the web-client-config-custom.xml but good luck to do it. I tried hard and it didn't work out.

What package is this?

Re: Custom Constraint

I have found another problem. I have noticed the list length can reach maximum of 80 before it errors with the following popup

Internal Form Error: form with id of 'template_x002e_documentlist_x002e_repository_x0023_default-editDetails-alf-id18-form' could not be located, ensure the form is created after the form element is available.

In the log file i get

 2012-11-22 15:12:51,999  ERROR [scripts.forms.FormUIGet] [http-apr-8080-exec-26] org.alfresco.service.cmr.dictionary.DictionaryException: 10220043 The list of allowed values is empty

How can I increase it, as I need the list to hold up to 300