How to Extend Models
Introduction
This document explains an example how to extend a model used in Web POS. The list of models that can be extended and the details needed to extend it are described in This document
The typical case a developer will require to extend an existing Web POS model is when in a new Openbravo module are added one or more columns to a table that is used in Web POS (for example Products, Business Partner, ...) and these new columns needs to be used also in the Web POS terminal.
This example shows how extend the products model adding two existing columns.
Extending server side
To extend the server side you must create a new java class that extends the class ModelExtension.
The class must be annotated with the java qualifier used to extend the products model and the columns added must belong to one of the tables retrieved by the server side model.
Each of the columns added is an instance of HQLProperty. This instance has two values hqlProperty that is an HQL expression based on the tables used by the model and jsonName the name used in the JSON object sent to the browser.
import java.util.Arrays; import java.util.List; import org.openbravo.client.kernel.ComponentProvider.Qualifier; import org.openbravo.mobile.core.model.HQLProperty; import org.openbravo.mobile.core.model.ModelExtension; import org.openbravo.retail.posterminal.master.Product; @Qualifier(Product.productPropertyExtension) public class NewProductProperties extends ModelExtension { @Override public List<HQLProperty> getHQLProperties(Object params) { return Arrays.asList( new HQLProperty("product.weight", "weight"), new HQLProperty("product.taxCategory.name", "taxcategoryname")); } }
Extending the client side
To extend the client side of the model you must add the new columns added in the server side.
In each of the columns added the property name must match with the jsonName value of the HQLProperty added in the server side. The rest of values describes the properties of each column in the client model:
- column: The name of the column of the client side table.
- primaryKey: Whether this column is part or not of the primary key.
- filter: Whether this column is added or not to the filter column in the client side table.
- type: The type used for the column in the client side table.
OB.Model.Product.addProperties([{ name: 'weight', column: 'weight', primaryKey: false, filter: false, type: 'NUMERIC' }, { name: 'taxcategoryname', column: 'taxcategoryname', primaryKey: false, filter: false, type: 'TEXT' }]);
If needed you can also add client side indexes based on the new columns added. Indexes are usually necessary if you want to execute queries that filter based on the new columns added.
This is an example of a new index for the Products model based on the new added column taxcategoryname.
OB.Model.Product.addIndex([{ name: 'inx_taxcategoryname', columns: [{ name: 'taxcategoryname', sort: 'asc' }] }]);