Model generator for custom columns
Goal: Technical
Developer: Peter Takacs (CloudEmpiere)
Feature Ticket: IDEMPIERE-7161
Version: iDempiere 14
Summary
A plug-in that adds columns to a table it does not own, such as C_Order, can now generate an accessor class for its own columns. This example was tested on iDempiere 14.
C_Order_XYZ.of(order).setXYZ_Note("Pallet delivery");
The accessor wraps any PO or GridTab of the table. Core's I_, X_ and M classes stay as they are, and several plug-ins can each have their own accessor for the same table. The model generator also gets named command-line arguments and a --check option that fails a build when the generated classes no longer match the dictionary.
| Level | Table owner and column owners | Use |
|---|---|---|
| A | Core only, or a plug-in with its own tables | I_, X_ and M classes and a model factory. Unchanged. |
| B1 | Core and XYZ | Accessor C_Order_XYZ |
| B2 | Core, XYZ and ABC | One accessor per plug-in: C_Order_XYZ, C_Order_ABC |
| B3 | XYZ and KLM | KLM gets an accessor on a table that XYZ owns |
The rule at every level: each model stays with the owner of its entity type.
The problem
Implementations add fields to standard documents all the time: a delivery note on the Sales Order, a customs code on the Product. The model generator already covers tables that a plug-in owns:
- Core generates
I_andX_classes with entity typeDintoorg.compiere.model. Business logic is in theMclasses. - A plug-in with its own tables generates
I_andX_classes for its entity type into its own package, writesMclasses and registers a model factory.
Run it with the Generate Model process or the model.generator launch configuration in Eclipse. This is level A, and its output does not change.
Level A stops working as soon as a second owner adds columns to the same table:
| Option | Problem |
|---|---|
get_Value("XYZ_Note") | Untyped. A typo fails at run time. |
Partial I_C_Order or X_C_Order | Name clash with core. A partial X_ class breaks the model factory. |
Subclass MOrder and a model factory | Only one plug-in can do it. |
Columns in core's X_C_Order | A merge on every core upgrade. |
What changed
The model generator can write an accessor: a small class for one owner's columns. It wraps an existing PO or GridTab and has no data or logic of its own.
C_Order_XYZ.of(po)works on anyPOof the table, including anMOrderthat core created. It needs no model factory.C_Order_XYZ.of(mTab)works in a callout. Values go throughGridTab.setValue, so dependent callouts still run and the read-only rules of the field apply.- The getters and setters behave like the ones of the
X_classes: an_IDset below 1 stores null (below 0 for the columns where 0 is valid), Boolean columns use Y and N, and list columns get the value constants of theX_class. - On a
PO, a column that is not updateable is written withset_ValueNoCheck. On aGridTab, every value goes throughGridTab.setValue, so the read-only rules of the field apply also to a column that is not updateable. - A column the table does not have fails at the first write, so a renamed column is not ignored.
- The accessor is named after the table and the plug-in:
<Table>_<suffix>, for exampleC_Order_XYZ. Its interface, when generated in the same run, isI_C_Order_XYZ.
The command line of the model generator also accepts named arguments:
| Argument | What it does |
|---|---|
--folder | Source folder. It must exist. |
--package | Java package of the generated files. |
--tables | One table name pattern (LIKE, case-insensitive) or a comma-separated list of exact names. |
--table-entity-type | Entity types of the tables, comma separated. Default U,A. |
--column-entity-type | Only columns of these entity types, comma separated. Default all columns. |
--output | interface, class and/or accessor, comma separated. Default interface,class. |
--suffix | Owner of the columns, for example XYZ. Required for accessor. Not allowed with class. |
--columns | Only these columns, on top of --column-entity-type. Not allowed with class. |
--template | Read the values not given from the Generate Model Template with this name. |
--check | Compare instead of writing. See Keep the classes in sync. |
--help | Print the full description of the arguments. |
Write named arguments as --key=value. The form --key value is not supported.
The Generate Model process, the Swing dialog and the dictionary are not changed. The new options exist only on the command line.