Difference between revisions of "Developing Plug-Ins - IModelFactory"
| Line 1: | Line 1: | ||
[[Category:Plug-In Development]] | [[Category:Plug-In Development]] | ||
__TOC__ | __TOC__ | ||
| − | + | This tutorial was brought to you by Jan Thielemann from evenos GmbH (www.evenos.de). If you have questions, criticism or improvement suggestions, feel free to visit me ([[User:Jan.thielemann|Jan.thielemann]]) or write me [mailto:[email protected] an email] | |
=Goal of this tutorial= | =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. | 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. | ||
Revision as of 16:48, 4 March 2013
This tutorial was brought to you by Jan Thielemann from evenos GmbH (www.evenos.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:
- 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
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:
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:
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")
