Child pages
  • Providing Converters and Validators from Plugins
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Column converters as well as validators can be provided via client plugins.

Column Converters

In order to build a column converter, you need to implement the interface IColumnConverterProvider to produce ITypedColumnConverter instances in the getColumnConverters() method. See API docs.

Note that IColumnConverter is considered to be deprecated, therefore in getColumnConverters() method do return an ITypedColumnConverter array.

An ITypedColumnConverter has 3 methods:

  • convertFromObject()
    Converts from dataprovider value to db value

  • convertToObject()
    Converts from db value to dataprovider value

  • type (one of TEXT, INTEGER, NUMBER, DATETIME or MEDIA)
    The dataprovider data type (so the resulting type of the convertToObject()).

For an example of a column converter, see the NrToJodaConverter class in the example given on Providing UI Converters from plugins page.

Column Validators

In order to build a column validator, you need to implement the interface IColumnValidatorProvider to produce IColumnValidator instances in the getColumnValidators() method. See API docs.

The validation rule of an IColumnValidator is defined in the validate() method.

An IColumnValidator can also define properties whose values will be set by the developer in UI, in the Validation tab.

Example

This is an example of validating the size of a column of type TEXT or MEDIA. The validation rule is that the value must be lower or equal to the value given by the developer by setting the length property in the Validation tab.

public class SizeValidator implements IColumnValidator
{
	private static final String
	LENGTH_PROPERTY = "length";

	public Map getDefaultProperties()
	{
		Map props = new HashMap();
		props.put(LENGTH_PROPERTY, "");
		return props;
	}

	public String getName()
   		{
        	return "servoy.SizeValidator";
    	}
    	public int[] getSupportedColumnTypes()
    	{
        	return new int[] { IColumnTypes.TEXT, IColumnTypes.MEDIA };
    	}

	public void validate(Map props, Object arg) throws IllegalArgumentException
	{
		String propLength = (String)props.get(LENGTH_PROPERTY);
		int length = new Double(propLength.replace(',', '.')).intValue();
		if (arg instanceof byte[] && ((byte[])arg).length > length)
		{
			throw new IllegalArgumentException();
		}
		else if (arg instanceof String && ((String)arg).length() > length)
		{
			throw new IllegalArgumentException();
		}
	}
}
  • No labels