Friday, April 4, 2008

[Java Plug-In Development] How to Add Your Own Column Validators and Column Converters with the Plug-In API

While in the Dataproviders area of Servoy, there are column properties that allow you to choose Validation, or Conversion for that column.  In this post, I'll show you how to create plug-ins to add your own items to those lists.

Column Validation

Servoy has several built-in validators that are included in the default_validators.jar plugin that ships with Servoy.  In addition, I have a free plugin at Servoy Guy:  Validator Pro. However, you may want to use the Java Plugin-API to add your own items to that list, so here we go...
  • Make a new class, lets call it ValidatorPlugin.java and have the class implement IClientPlugin, and IColumnValidatorProvider. (com.servoy.j2db.plugins.IColumnValidatorProvider)
  • If you have eclipse automatically include the required methods from those classes, you'll have a few methods you'll need to implement.  Most are self-explanatory, but the main one is "public IColumnValidators[] getColumnValidators()".  That method needs to return back an array of classes that implement IColumnValidator.  So, onto our next class...
  • Make a new class, lets call it MyValidator.java, and have the class implement IColumnValidator. (com.servoy.j2db.dataprocessing.IColumnValidator)  Again, if you have eclipse move in the required methods, you'll see several to implement
  • First, there is "Public Map getDefaultProperties()".  This needs to return back a Map of properties that will be shown to the user when they choose this validator.  For example, the servoy.SizeValidator shows "length" as a property.  So, here you can have it return back the properties you want the user to be able to input
  • Second, there is "public String getName()".  This returns back the name that shows up in the validator.  So that your validator doesn't conflict with any others use a notation like companyName.validatorname.
  • Third, there is "public in[] getSupportedColumnTypes()".  This returns back an array representing the different column types that this validator can be used on, like TEXT, INTEGER, NUMBER, etc.  To get those integers you can use com.servoy.j2db.persistence.IColumnTypes .  For example com.servoy.j2db.persistence.IColumnTypes.TEXT
  • Finally we get to the meat of this class.  There is "public void validate(Map props, Object arg) throws IllegalArgumentException".  This passes in the a Map as the props variable which allows you to know what properties the user entered for those available properties.  It also passes in arg as an Object, which represents the value in the column being validated.  Here is where you'll put you own business logic to determine if the input is valid.  If it isn't valid you must throw an IllegalArgumentException
  • So, now lets go back to our ValidatorPlugin.java class, and have it return back a new set array, like "return new IColumnValidator[]{MyValidator}

Column Conversion

Again Servoy has a JSON column conversion built in allowing you to serialize objects. In addition, I have a free plugin at Servoy Guy: Column Converter Pro.  Again however, you may want to use the Java Plugin-API to add your own items to that list.
  • Make a new class, lets call it ColumnConverterPlugin.java and have the class implement IClientPlugin and IColumnConverterProvider. (com.servoy.j2db.plugins.IColumnConverterProvider)
  • You'll now need to implement "public IColumnConverter[] getColumnConverters()".  Just like before, this needs to return back an array of objects, but this time they are IColumnConverter classes.
  • So make a new class, lets call it MyColumnConverter.java, and have it implement IColumnConverter (com.servoy.j2db.dataprocessing.IColumnConverter)
  • First, there is "Public Map getDefaultProperties()". This needs to return back a Map of properties that will be shown to the user when they choose this validator. For example, the StringSerializer shows "length" as a property. So, here you can have it return back the properties you want the user to be able to input.
  • Second, there is "public String getName()". This returns back the name that shows up in the converter. So that your validator doesn't conflict with any others use a notation like companyName.convertername.
  • Third, there is "public in[] getSupportedColumnTypes()". This returns back an array representing the different column types that this converter can be used on, like TEXT, INTEGER, NUMBER, etc. To get those integers you can use com.servoy.j2db.persistence.IColumnTypes . For example com.servoy.j2db.persistence.IColumnTypes.TEXT
  • Next, there are 2 methods that do the bulk of the work...
  • "public Object convertFromObject(Map props, int columnType, Object obj)": passes in the properties that the user defined, an integer representing the column type, and an object representing the value of the data in the column.  Here you can write your own business logic to determine what you want returned back to convertFromObject.
  • "public Object convertToObject(Map props, int columnType, Object obj)": passes in the properties that the user defined, an integer representing the column type, and an object representing the value of the data in the column. Here you can write your own business logic to determine what you want returned back to convertToObject.
  • Finally, lets go back to our ColumnConverterPlugin.java class.  Our getColumnConverters needs to return back our converter class.  Such as "return new IColumnConverter[]{new MyColumnConverter.java}".  In your example, you may choose to return back a new array of IColumnConverter objects, or just return back a static set stored in a variable.
So, after compilation, and added to the plugin directory, you should now have your plugin working with Column Converters and Column Validators.  Don't forget to create your jnlp file if you need this working in Smart Client.

Author: Scott Butler, Senior Sales Engineer, Servoy USA

0 comments:

Post a Comment