Liferay Service Hook to override Liferay Service

Liferay Service Hook


aim

Liferay has many in built services. By Hook even we can override Liferay services. We simply call it as Liferay Service Hook. The main purpose of Liferay Service Hook is to override Liferay existing services.This article focuses on how to develop Liferay Service Hook and its basic use. For the beginners you should have basic knowledge of Liferay existing services to follow this article.  


Service Wrapper!!

Liferay generates dummy wrapper classes for all its services. For example BlogsEntryLocalServiceWrapper is created as a wrapper for BlogsEntryLocalService  a service for adding, removing, and retrieving blog entries. To modify the functionality of BlogsEntryLocalService from our hook, create a class that extends BlogsEntryLocalServiceWrapper , override the methods you want to modify, and instruct Liferay to use your service class instead of the original.

Tips:

1. Configure Liferay Source Code in your eclipse 

2. Press CTRL + SHIF + R from your key board

3. Enter *wrapper.java to see all the wrapper classes 

service-wrapper


Demo for this article

We are going to override updateUser method of UserLocalService by creating Liferay Service Hook. While updating any user from the control panel we want job title should be populated with some specific value, say its Analyst.   Follow the following steps 

Step 1: Create Liferay Plugin Project of type Hook. If you dont know how to create it follow the post 

https://proliferay.com/basics-liferay-hook-development/

Step 2: Open liferay-hook.xml and add the following 

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">

<hook>
	<service>
		<service-type>
			com.liferay.portal.service.UserLocalService
		</service-type>
		<service-impl>
			com.proliferay.demo.ExampleUserLocalServiceImpl
		</service-impl>
	</service>
</hook>

Here we are saying that use ExampleUserLocalServiceImpl instead of UserLocalService

Step 3Create ExampleUserLocalServiceImpl.java file which should extends UserLocalServiceWrapper.  Add the below content 

package com.proliferay.demo;

import java.util.List;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroupRole;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserLocalService;
import com.liferay.portal.service.UserLocalServiceWrapper;

/**
 *
 * @author Hamidul Islam
 *
 */

public class ExampleUserLocalServiceImpl extends UserLocalServiceWrapper{

	public ExampleUserLocalServiceImpl(UserLocalService userLocalService) {
		super(userLocalService);

	}

	@Override
	public User updateUser(long userId, String oldPassword,
			String newPassword1, String newPassword2, boolean passwordReset,
			String reminderQueryQuestion, String reminderQueryAnswer,
			String screenName, String emailAddress, long facebookId,
			String openId, String languageId, String timeZoneId,
			String greeting, String comments, String firstName,
			String middleName, String lastName, int prefixId, int suffixId,
			boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
			String smsSn, String aimSn, String facebookSn, String icqSn,
			String jabberSn, String msnSn, String mySpaceSn, String skypeSn,
			String twitterSn, String ymSn, String jobTitle, long[] groupIds,
			long[] organizationIds, long[] roleIds,
			List<UserGroupRole> userGroupRoles, long[] userGroupIds,
			ServiceContext serviceContext) throws PortalException,
			SystemException {

		jobTitle = "Analyst";

		return super.updateUser(userId, oldPassword, newPassword1, newPassword2,
				passwordReset, reminderQueryQuestion, reminderQueryAnswer, screenName,
				emailAddress, facebookId, openId, languageId, timeZoneId, greeting,
				comments, firstName, middleName, lastName, prefixId, suffixId, male,
				birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn, facebookSn,
				icqSn, jabberSn, msnSn, mySpaceSn, skypeSn, twitterSn, ymSn, jobTitle,
				groupIds, organizationIds, roleIds, userGroupRoles, userGroupIds,
				serviceContext);
	}

}

Step 4: Deploy the hook. 

Step 5: Now go to the Liferay Control Panel. Edit any user and save the changes. As soon as you update a user, job title will be automatically inserted into the database. 

Download Source Code

About The Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d