Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Charles de Courval
Help needed to register a custom model listener.
May 15, 2011 11:49 AM
Answer

Charles de Courval

Rank: Junior Member

Posts: 55

Join Date: July 31, 2010

Recent Posts

Hi,
I have an Enterprise model and I'd like to register a listener on that model to do some afterUpdate oprerations.

I tried defining the portal.properties in liferay-hook.xml of my portlet, and I've added the value.object.listener.com.acq.portlet.enterprise.model.Enterprise = com.acq.portlet.enterprise.model.EnterpriseListener to each of WEB-INF/src/portal.properties, WEB-INF/src/system.properties, WEB-INF/src/portal-ext.properties without success.

The clossest I got was a hotDeployException where the bean com.acq.portlet.enterprise.service.persistence.EnterprisePersistence could not be found. This seems to point to a classLoader problem where the Listener is being registered by the ROOT classLoader and not MyPortlet classLoader.

I even tryied using the following in my enterpriseLocalServiceImpl , but to no avail.
1
2public void setEnterprisePersistence(EnterprisePersistence enterprisePersistence) {
3    super.setEnterprisePersistence(enterprisePersistence);
4    enterprisePersistence.registerListener(new EnterpriseListener());
5}


Does anyone have an idea on how to register a listener on a custom model ?

cheers.
Nagendra Kumar Busam
RE: Help needed to register a custom model listener.
May 15, 2011 9:56 PM
Answer

Nagendra Kumar Busam

Rank: Liferay Master

Posts: 637

Join Date: July 7, 2009

Recent Posts

Hi Charles,

Does Enterprise model you developed is developed using plugins portlet OR ext?

Which version of Liferay you are using?

Regards,
- Nagendra Kumar
Sandeep Nair
RE: Help needed to register a custom model listener.
May 15, 2011 10:24 PM
Answer

Sandeep Nair

Rank: Liferay Legend

Posts: 1692

Join Date: November 5, 2008

Recent Posts

Hi,

Try creating the listener in portlet itself. You can declare the listener for ur custom model in portlet.properties of your portlet, as you do in hook.

Regards,
Sandeep
Charles de Courval
RE: Help needed to register a custom model listener.
May 16, 2011 3:45 AM
Answer

Charles de Courval

Rank: Junior Member

Posts: 55

Join Date: July 31, 2010

Recent Posts

Nagendra Kumar Busam:

Does Enterprise model you developed is developed using plugins portlet OR ext?

Which version of Liferay you are using?


The Enterprise model was developed using plugins portlet on Liferay 6.0.5.
Charles de Courval
RE: Help needed to register a custom model listener.
May 16, 2011 3:44 AM
Answer

Charles de Courval

Rank: Junior Member

Posts: 55

Join Date: July 31, 2010

Recent Posts

Sandeep Nair:

Try creating the listener in portlet itself. You can declare the listener for ur custom model in portlet.properties of your portlet, as you do in hook.
Sandeep

The Listener was created in the portlet (com.acq.portlet.enterprise.model.impl.EnterpriseListener) and declared in portlet.properties , but for some reason it never got registered
1
2public class EnterpriseListener implements ModelListener<Enterprise> {
3.....
4}

and
1value.object.listener.com.acq.portlet.enterprise.model.Enterprise = com.acq.portlet.enterprise.model.EnterpriseListener
Nagendra Kumar Busam
RE: Help needed to register a custom model listener.
May 16, 2011 4:51 AM
Answer

Nagendra Kumar Busam

Rank: Liferay Master

Posts: 637

Join Date: July 7, 2009

Recent Posts

Hi Charles,

The approach suggested by Sandeep will do needful.

Run you server in debug mode, try to debug to check whether the control is going to method you are looking for according to operation (CRUD) you are performing.

Regards,
- Nagendra Kumar
Sandeep Nair
RE: Help needed to register a custom model listener.
May 16, 2011 6:45 AM
Answer

Sandeep Nair

Rank: Liferay Legend

Posts: 1692

Join Date: November 5, 2008

Recent Posts

Hmm Strange,

Can you please check using debug in the method called afterPropertiesSet in your ModelPersitenceImpl class(in your case EnterprisePersistenceImpl) what is the value of listenerClassNames

Regards,
Sandeep
Charles de Courval
RE: Help needed to register a custom model listener.
June 3, 2011 7:22 AM
Answer

Charles de Courval

Rank: Junior Member

Posts: 55

Join Date: July 31, 2010

Recent Posts

Hi,
I finally succeded in registerring my custom listener.

There were 2 problems:

The first is related to bug LPS-10988 where the my listener impl class was not found. The workaround in the bug description solved my problem.

The second problem was that the value.object.listener was suppose to go into my portlet's system.properties and that was constantly overridden by the SDK. The solution can be found here


One thing to note though, due to some constraints in easyconf, service-ext.properties doesn't work as it should. So you need to put this property in your service.properties file. And re-add it back each time you rebuild your services cuz ant build-service will remove your entries.

I fixed this by overriding the build-service target in the build.xml from the portlet:
1<target name="build-service" depends="clean">
2    <ant antfile="../build-common-portlet.xml" target="build-service"/>
3
4    <concat destfile="${src}/service.properties" append="true" fixlastline="yes" eol="crlf">
5        <filelist dir="${src}" files="service-ext.properties" />
6    </concat>
7</target>



Thanks for all your help and I hope this will help other people.

cheers.
Mukul Kumar Gupta
RE: Help needed to register a custom model listener.
February 18, 2013 3:58 AM
Answer

Mukul Kumar Gupta

Rank: New Member

Posts: 1

Join Date: October 6, 2012

Recent Posts

The easiest solution of this issue is to just modify your bean's persistenceImpl class's method name afterPropertiesSet: -

/**
* Initializes the MyBean persistence.
*/
public void afterPropertiesSet() {
String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
com.liferay.portal.kernel.util.PropsUtil.get(
"value.object.listener.com.test.model.MyBean")));


if (listenerClassNames.length > 0) {
try {
List<ModelListener<MyBean>> listenersList = new ArrayList<ModelListener<MyBean>>();
for (String listenerClassName : listenerClassNames) {
listenersList.add((ModelListener<MyBean>)InstanceFactory.newInstance(PortletClassLoaderUtil.getClassLoader(), listenerClassName, (Class<?>[])null, (Object[])null));

}

listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
}
catch (Exception e) {
_log.error(e);
}
}
}

For completely fixed this issue just change the liferay.src\portal-impl\src\com\liferay\portal\tools\servicebuilder\dependencies\persistence_impl.ftl: -

/**
* Initializes the ${entity.humanName} persistence.
*/
public void afterPropertiesSet() {
String[] listenerClassNames = StringUtil.split(GetterUtil.getString(${propsUtil}.get("value.object.listener.${packagePath}.model.${entity.name}")));

if (listenerClassNames.length > 0) {
try {
List<ModelListener<${entity.name}>> listenersList = new ArrayList<ModelListener<${entity.name}>>();

for (String listenerClassName : listenerClassNames) {
listenersList.add((ModelListener<${entity.name}>)InstanceFactory.newInstance(PortletClassLoaderUtil.getClassLoader(), listenerClassName, (Class<?>[])null, (Object[])null));
}

listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
}
catch (Exception e) {
_log.error(e);
}
}

Cheers and happy coding............ emoticon