Developing Plug-Ins - IModelFactory
Goal of this tutorial
The goal of this tutorial is to show, how you use the IModelFactory service in you plug-in project. The reason why you want to use the model factory service is, that you can provide your own model for custom tables and iDempiere will make use of them (e. G. the beforeSave method gets called). Also if you want to use the Query class, it is necessary to provide your own models. Otherwise, a Query will return a "GenericPO" instead of your model class.
Prerequisites
Before you start this tutorial, you should take a look at the follwing tutorials:
- Developing Plug-Ins without affecting the Trunk
- Developing Plug-Ins - Get your Plug-In started
- Developing Plug-Ins - ModelValidator
The workflow
After creating the plug-in and setting its dependencies, click on New>New>Others and select "Component Definition" from the Plug-in Development section.
Click on next. Select your plug-in project and chose a file name and a general name. We often use "defaultmodelfactory.xml" and "my.domain.model.factory" for the filename/name but every other name is okay too. You don't need to set the class name yet because we haven't implemented one. Click on finish.
The next things we will need is:
- A Model
- A ModelFactory (which is just a class which implements IModelFactory
So crete two new classes. For our tutorial, we will use a custom subclass of MOrder, so create a new class which inherits from MOrder and implement the default constructors:
The second class only implements IModelFactory and is called MyModelFactory. In getClass() we check if the table name is equal to our custom models table name and return the model class if it is. In the both getPO() methods, we use our custom models constructors to create new instances.
Open the mymodelfactory.xml and click on the Browse button to select the MyModelFactory class. Also add a integer property called "service.ranking" with a positive value (>0).
On the Services tab, add the IModelFactory to the "Proviced Services" section.
Basically thats all you need to do. To see that we realy did someting, open your custom model class again and add a console log in beforeSave()
Make sure that your plug-in is active in the run configurations
Start the client and make sure your plug-in is active
Now log in as GardenAdmin/GardenWorld and create a new Sales Order fill in all mandatory fields and click on save. Take a look at your console log, you should see the log from your custom model class:
