Foren

Hooks

krishna rao, geändert vor 9 Jahren.

Hooks

New Member Beiträge: 10 Beitrittsdatum: 13.07.14 Neueste Beiträge
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, geändert vor 9 Jahren.

RE: Hooks

Liferay Legend Beiträge: 6403 Beitrittsdatum: 23.09.08 Neueste Beiträge
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, geändert vor 9 Jahren.

RE: Hooks

New Member Beiträge: 10 Beitrittsdatum: 13.07.14 Neueste Beiträge
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, geändert vor 9 Jahren.

RE: Hooks

Liferay Legend Beiträge: 6403 Beitrittsdatum: 23.09.08 Neueste Beiträge
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, geändert vor 9 Jahren.

RE: Hooks

Regular Member Beiträge: 234 Beitrittsdatum: 13.03.12 Neueste Beiträge
Have a Look on following links
https://www.liferay.com/community/forums/-/message_boards/message/4257623

Click Here for Wiki
krishna rao, geändert vor 9 Jahren.

RE: Hooks

New Member Beiträge: 10 Beitrittsdatum: 13.07.14 Neueste Beiträge
Thanks subhash
thumbnail
Olaf Kock, geändert vor 9 Jahren.

RE: Hooks

Liferay Legend Beiträge: 6403 Beitrittsdatum: 23.09.08 Neueste Beiträge
Subhash Pavuskar:
Have a Look on following links...


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