留言板

Hooks

krishna rao,修改在9 年前。

Hooks

New Member 帖子: 10 加入日期: 14-7-13 最近的帖子
Hi,

Anyone can say how to add a new method to UserLocalServiceUtil . I tried creating hook for it but it dint work

Liferay-hook.xml

<service>
<service-type>com.liferay.portal.service.UserLocalService</service-type>
<service-impl>com.liferay.sample.hook.action.MyUserLocalServiceImpl</service-impl>
</service>

My class file

package com.liferay.sample.hook.action;


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

public class MyUserLocalServiceImpl extends UserLocalServiceWrapper {

public MyUserLocalServiceImpl(UserLocalService userLocalService) {
super(userLocalService);
// TODO Auto-generated constructor stub
}
@Override
public User getUserById(long userId)
throws PortalException, SystemException {

System.out.println(
"## MyUserLocalServiceImpl.getUserById(" + userId + ")");

return super.getUserById(userId);
}
//New method
public String customMethod(String name)
throws PortalException, SystemException {

System.out.println("new method to user service");

return name;
}

}


Now i created a new Portlet plugin and tried to access my method it says not found
UserLocalServiceUtil.customMethod("name"); this line shows error in my Portlet application .
thumbnail
Olaf Kock,修改在9 年前。

RE: Hooks

Liferay Legend 帖子: 6403 加入日期: 08-9-23 最近的帖子
Quick answer: You don't. You can't change the interface of a core service. What you can do is to create your own service and have that delegate to UserLocalService instead.

In service.xml define your own service (or entity), e.g. "ExtendedUser", but don't give any columns - e.g. make it an empty entity.

In ExtendedUserLocalService, implement like this (pseudocode):


public class ExtendedUserLocalServiceImpl ... {
  //New method
  public String customMethod(long userId)
    throws PortalException, SystemException {
      User user = UserLocalServiceUtil.getUserById(userId);
      // do something custom with user
      return user.getName()
   }
}


The interface UserLocalService is defined in portal-service.jar. You simply can't change it (and you wouldn't want to do it because it would introduce all kinds of compatibility and maintenance issues. Just delegating to UserLocalService does the trick without all the negative side effects.
krishna rao,修改在9 年前。

RE: Hooks

New Member 帖子: 10 加入日期: 14-7-13 最近的帖子
Thanks Olaf,

Is this only way or is there any way via Ext plugin if so Ext plugin approach is better or the approach described you is better
thumbnail
Olaf Kock,修改在9 年前。

RE: Hooks

Liferay Legend 帖子: 6403 加入日期: 08-9-23 最近的帖子
krishna rao:
...Is this only way or is there any way via Ext plugin...?


While there might be a way with ext, I strongly advise against it. Do you want a solution that "only" works technically or one that you can easily maintain in future, on updates to Liferay or even on version change? Everything that can be done in a hook should be done in a hook. Keep your ext as small as possible for maintenance reasons.

This task can be done in a hook (as outlined above), so it definitely should be done this way, and not in ext.
thumbnail
Subhash Pavuskar,修改在9 年前。

RE: Hooks

Regular Member 帖子: 234 加入日期: 12-3-13 最近的帖子
Have a Look on following links
https://www.liferay.com/community/forums/-/message_boards/message/4257623

Click Here for Wiki
krishna rao,修改在9 年前。

RE: Hooks

New Member 帖子: 10 加入日期: 14-7-13 最近的帖子
Thanks subhash
thumbnail
Olaf Kock,修改在9 年前。

RE: Hooks

Liferay Legend 帖子: 6403 加入日期: 08-9-23 最近的帖子
Subhash Pavuskar:
Have a Look on following links...


IMHO both links are old and outdated. You shouldn't follow the practices outlined there.