Difference between revisions of "Developing Plug-Ins - IModelFactory"

From iDempiere en
Line 12: Line 12:
  
 
=The workflow=
 
=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.
+
After creating the plug-in and setting its dependencies, click on File>New>Other... and select "Component Definition" from the Plug-in Development section.
  
 
[[File:Pluginmodelfactory1.png|320px]]
 
[[File:Pluginmodelfactory1.png|320px]]
Line 54: Line 54:
  
 
[[File:Pluginmodelfactory10.png|320px]]
 
[[File:Pluginmodelfactory10.png|320px]]
−
 
−
 
−
 
  
 
=Troubleshooting=
 
=Troubleshooting=

Revision as of 15:00, 3 August 2014

This tutorial is brought to you by Jan Thielemann from evenos GmbH (www.evenos-consulting.de). If you have questions, criticism or improvement suggestions, feel free to visit me (Jan.thielemann) or write me an email

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 File>New>Other... 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

Notice that your model factory and your model class must be implemented in different classes. It will not work if you try to implement IModelFactory in one of your models. The good news is, you only need one model factory for all your custom models. For our tutorial, we will use a custom subclass of MOrder. Create the class MOrder_New 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

Troubleshooting

  • Make sure that in your modelfactory.xml you have a unique name (my.domainname.pluginname.model.factory is a good idea) and that you selected the right class
  • Make sure there is no other model factory with a higher service.ranking property
  • Make sure you have a seperate model/model factory class
  • Make sure your plug-in is activated before you log in
  • Make sure your MANIFEST.MF contains a row with "Service-Component: " and the path to your modelfactory.xml (e. g. "Service-Component: mymodelfactory.xml")
Cookies help us deliver our services. By using our services, you agree to our use of cookies.