Monday, 18 September 2017

Liferay Service Hook to override Liferay Existing Service

Today we will see how to write service wrapper in Liferay 6.2. We override User service getUserCount().Here we create a class that extend the particular wrapper class and just provide the entries of our custom class in liferay-hook.xml so that liferay use our services instead of Original.

Step 1:-Create a liferay Plug project
Right Click in Project Explorer and Click New -> Liferay plugin project. Now give the Project name and select hook in plugin type:-







Step 2:-Create a hook in the project
Right Click on Project ->New->Liferay Hook Configuration->Select Services


Click Next--->Click Add-->Select Service Type(UserLocalService)


For Impl Class

Click New-->Give Package Name and your Impl Class Name

Then Click Finish

This step automatically Create the entry in  liferay-hook.xml. 

<hook>
<service>
<service-type>com.liferay.portal.service.UserLocalService</service-type>
<service-impl>com.ipg.MyUserLocalService</service-impl>
</service>
</hook>






Step 3:-Override the getUserCount()
Open MyUserLocalService and override getUserCount()

package com.ipg;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.UserLocalServiceWrapper;
import com.liferay.portal.service.UserLocalService;
public class MyUserLocalService extends UserLocalServiceWrapper {
public MyUserLocalService(UserLocalService userLocalService) {
super(userLocalService);
}
@Override
public int getUsersCount() throws SystemException {
System.out.println("This text is from getUsersCount ");
return super.getUsersCount();
}
}

Step 4:-Deploy the Hook
Deploy the hook and call getUsersCount() from your portlet .

public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
try {
UserLocalServiceUtil.getUsersCount();
} catch (SystemException e) {
e.printStackTrace();
}
super.doView(renderRequest, renderResponse);
}
view rawDemo.java hosted with ❤ by GitHub

Output:-This text is from getUsersCount 

TEST

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 Test.