Developing Plug-Ins - IModelFactory

From iDempiere en

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:

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.

Pluginmodelfactory1.png

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.

Pluginmodelfactory2.png

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:

Pluginmodelfactory3.png

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.

Pluginmodelfactory4.png

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).

Pluginmodelfactory5.png

On the Services tab, add the IModelFactory to the "Proviced Services" section.

Pluginmodelfactory6.png

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()

Pluginmodelfactory7.png

Make sure that your plug-in is active in the run configurations

Pluginmodelfactory8.png

Start the client and make sure your plug-in is active

Pluginmodelfactory9.png

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:

Pluginmodelfactory10.png

Cookies help us deliver our services. By using our services, you agree to our use of cookies.