I need to know when the users modify some attribute in their profile.
I made a hook, but some fields like the birthday is the same value in onAfterUpdate and onBeforeUpdate function.
I don't know how I can do this for knowing when the user fields and custom fields have changed. I thought to extend the profile but I don't know how and if this is the only or the best solution.
This is the code of the portal.properties:
value.object.listener.com.liferay.portal.model.User=com.rulessegmentation.hook.profile.CreateAccountHook
And this is the code of the hook:
public void onAfterUpdate(User user) throws ModelListenerException {
System.out.println("Hello UPDATE After");
try {
System.out.println(UserLocalServiceUtil.getUser(user.getUserId()).getBirthday());
System.out.println(user.getBirthday());
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onBeforeUpdate(User user) throws ModelListenerException {
System.out.println("Hello UPDATE Before");
try {
System.out.println(UserLocalServiceUtil.getUser(user.getUserId()).getBirthday());
System.out.println(user.getBirthday());
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And this is the output:
Hello UPDATE Before Mon Feb 12 00:00:00 GMT 1990 Mon Feb 12 00:00:00 GMT 1990
Hello UPDATE After Mon Feb 12 00:00:00 GMT 1990 Mon Feb 12 00:00:00 GMT 1990
Thanks!
I override the UserLocalService to know the attributes that change and the same for the AddressLocalService. And it works, but I don't know if I did all right because other hook now doesn't work. I did a hook and in the liferay-hook.xml I put this:
<hook>
<service>
<service-type>com.liferay.portal.service.UserLocalService</service-type>
<service-impl>com.segmentationProjecthookUpdate.hook.MyUserLocalServiceImpl</service-impl>
</service>
<service>
<service-type>com.liferay.portal.service.AddressLocalService</service-type>
<service-impl>com.segmentationProjecthookUpdate.hook.MyAddressLocalServiceImpl</service-impl>
</service>
</hook>
And in MyUserLocalServiceImpl.java: public class MyUserLocalServiceImpl extends UserLocalServiceWrapper {
public MyUserLocalServiceImpl(UserLocalService userLocalService) {
super(userLocalService);
// TODO Auto-generated constructor stub
}
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) {
System.out.println("UPDATE USER");
return getWrappedService().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);
}
}
It works but the other hook that I have for the login doesn't work.
Thanks.
I joined the 2 hooks and it works.
Model Listeners for attribute changes are not really recommended: They are on the persistence level and should not contain any business logic. You should rather override the various update*
methods in UserLocalService
. In there it should be easier to access the required beans.
In case you nevertheless want to get to the root cause of your code not working, one possibility is to check if user
and UserLocalServiceUtil(user.getId())
are identical (e.g. due to a cached bean being returned instead of the actual database values). I didn't look it up, but it might be a reason for the behaviour you see.