• Liferay Service Hook to override Liferay Service

    Posted on December 1, 2014 by Hamidul Islam in Liferay.

    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 

    http://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

    Post Tagged with , ,
Comments are closed.
Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close