You can easily add Callback classes in order to complete persist, merge or remove processes in the EJB. For example, how to automatically set value for lastUpdate property in Film class when a record is created or updated.

A callback class must extends class org.adichatz.common.ejb.AEntityCallback. You can override preMerge, prePersist, preRemove, postMerge, postPersist or postRemove methods to finalize processing. Zero, one or more callback classes can be specified to be executed during saving data changes. If you declare several callback classes for an entity,

Step 1: Create the Callback class

Create class org.mycompany.myproject.model.callback.FilmCallback as below:

package org.mycompany.myproject.model.callback;

import java.util.Date;

import javax.persistence.EntityManager;

import org.adichatz.common.ejb.AEntityCallback;
import org.mycompany.myproject.model.Film;

public class FilmCallback extends AEntityCallback<Film> {
	public FilmCallback(EntityManager entityManager) {
		super(entityManager);
	}
	@Override
	public void preMerge(Film bean) {
		bean.setLastUpdate(new Date());
	}
	@Override
	public void prePersist(Film bean) {
		bean.setLastUpdate(new Date());
	}
}

Remarks:

  • Line 15-17: Sets value for lastUpdate property when bean is updated.
  • Line 19-21: Sets value for lastUpdate property when bean is inserted.



Step 2: Change 'FilmMM.axml' file

Open file $projectDirectory/resources/xml/model/film/FilmDIMM.xml.

Following XML elements:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entityTree xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            entityURI="adi://myproject/model.film/FilmMM" idFieldName="filmId"
            xsi:noNamespaceSchemaLocation="http://www.adichatz.org/xsd/v0.8.7/generator/entityTree.xsd">
    <propertyField mandatory="true" id="filmId">
    ...
    ...


Change XML elements

  • Copy FilmMMGENERATED.axml file to FilmMM.axml which will become the reference for generated code.
  • Replace above XML lines with the following ones:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entityTree xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            entityURI="adi://myproject/model.film/FilmMM" idFieldName="filmId"
            callbackClassNames="org.mycompany.myproject.model.callback.FilmCallback"
            xsi:noNamespaceSchemaLocation="http://www.adichatz.org/xsd/v0.8.7/generator/entityTree.xsd">
    <propertyField mandatory="true" id="filmId">
    ...

Remark:
Line 4: a Callback class is defined.

if you want to set muliple callback classes set callbackClassNames property with class names separated by comma:
callbackClassNames=“org.mycompany.myproject.model.callback.FilmCallback, org.mycompany.myproject.model.callback.CommonCallback”



Step 3: Change scenario.xml file for adding callback resources when building EJB

Open file $projectDirectory/resources/xml/scenario.xml.

    <actionResources>
    	...
        <copyResource sourceURI="#PLUGINHOME()/src/org/mycompany/myproject/call" targetURI="#EJBJARFILE()!org/mycompany/myproject/call" actionWhen="WHEN_BUILDING_EJB_JAR" relative="false" throwError="false"/>
    </actionResources>

Remark:
Line 3: package org.mycompany.myproject.call, containing callback classes is added to EJB.



Step 4: Build and deploy EJB

 Generate EJB

  • Open file $projectDirectory/resources/xml/Scenario.xml with Adichatz editor.
  •  Generate scenario Select Generate scenario at top right of the editor.
  • Check 'Generate EJB' and 'DeployEJB on application server' options and select 'OK' button.



Step 5: Execute

  • Open editor for Film 1.
  • Change a value and save editor.
  • Check that lastUpdate property has changed.



Callback classes are implemented on the server side. The same principle can be used on the client side using extensions of the class org.adichatz.common.ejb.AEntityForeback and setting class names in callforeClassNames property