我需要知道用户何时修改其个人资料中的某些属性。
我做了一个钩子,但是像birthday一样的字段在onAfterUpdate和onBeforeUpdate函数中是相同的值。
我不知道如何知道用户字段和自定义字段何时发生了变化。我想扩展配置文件,但我不知道如何,如果这是唯一或最好的解决方案。
这是portal.properties的代码:
value.object.listener.com.liferay.portal.model.User=com.rulessegmentation.hook.profile.CreateAccountHook
这是钩子的代码:
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();
}
}
}
这是输出:
Hello UPDATE之前 1990年2月12日00:00:00 GMT 1990 1990年2月12日00:00:00 GMT
Hello UPDATE After 1990年2月12日00:00:00 GMT 1990 1990年2月12日00:00:00 GMT
谢谢!
我覆盖UserLocalService以了解更改的属性以及AddressLocalService的相同属性。它有效,但我不知道我是否做得好,因为其他钩现在不起作用。 我做了一个钩子,在liferay-hook.xml中我把它放了:
<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>
在MyUserLocalServiceImpl.java中: 公共类MyUserLocalServiceImpl扩展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);
}
}
它可以工作,但我登录的另一个钩子不起作用。
感谢。
我加入了2个钩子并且有效。
答案 0 :(得分:1)
不建议使用模型侦听器进行属性更改:它们位于持久性级别,不应包含任何业务逻辑。您应该覆盖UserLocalService
中的各种update*
方法。在那里,访问所需的bean应该更容易。
如果您仍然希望找到代码不起作用的根本原因,则可以检查user
和UserLocalServiceUtil(user.getId())
是否相同(例如,由于返回了缓存的bean而不是实际的数据库值)。我没有查找,但这可能是你看到的行为的原因。