What
Model Listener - as the name specifies that there is some class that in continuously looking to a entity model in liferay. More technically its a call back class which is called when some operation happens on the model (operation can be add/edit/delete).
Model Listener
public interface ModelListener<T> {
public void onAfterAddAssociation(Object classPK, String associationClassName, Object associationClassPK) throws ModelListenerException;
public void onAfterCreate(T model) throws ModelListenerException;
public void onAfterRemove(T model) throws ModelListenerException;
public void onAfterRemoveAssociation(Object classPK, String associationClassName, Object associationClassPK) throws ModelListenerException;
public void onAfterUpdate(T model) throws ModelListenerException;
public void onBeforeAddAssociation(Object classPK, String associationClassName, Object associationClassPK) throws ModelListenerException;
public void onBeforeCreate(T model) throws ModelListenerException;
public void onBeforeRemove(T model) throws ModelListenerException;
public void onBeforeRemoveAssociation(Object classPK, String associationClassName, Object associationClassPK) throws ModelListenerException;
public void onBeforeUpdate(T model) throws ModelListenerException;
}
How
To implement own listener on some Model (for example User) yin your
portlet you need:
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>
Make an entry in file WEB-INF/src/portal.properties to define hook for specific model:
value.object.listener.com.liferay.portal.model.User=my.hook.UserListener
Implement class by inheriting it from BaseModelListener<T> in our case BaseModelListener<User> and implementing only
method we want to call-back:
public class GroupListener extends BaseModelListener<Group> {
@Override
public void onAfterUpdate(User u) throws ModelListenerException {
// do something here
}
}