2013-09-25 30 views
1

我需要知道用戶何時修改其配置文件中的某些屬性。如何知道Liferay中的配置文件更改?

我做了一個鉤子,但像生日那樣的一些字段在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(); 
    } 
} 

}

而這是輸出:

你好UPDATE之前 週一2月12日00:00:00 GMT 1990年 週一年02月12日00:00:00 GMT 1990年

你好更新後 週一年02月12日00:00:00 GMT 1990年 週一年02月12日00:00:00 GMT 1990

謝謝!


我覆蓋了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個鉤子,它的工作原理。

回答

1

不真正推薦用於屬性更改的模型監聽器:它們位於持久層,不應包含任何業務邏輯。您應該重寫UserLocalService中的各種update*方法。在那裏它應該更容易訪問所需的bean。

如果你仍然想要去的你的代碼不工作的根本原因,一種可能性是,以檢查是否userUserLocalServiceUtil(user.getId())(由於緩存豆例如被返回的實際數據庫值代替)是相同的。我沒有看到它,但它可能是你看到的行爲的原因。

+0

謝謝!它可以工作,但例如我無法比較的地址,因爲如果我這樣做: User oldUser = UserLocalServiceUtil.getUser(userId); System.out.println(oldUser.getAddresses()); User aux = getWrappedService()。updateUser(...); System.out.println(aux.getAddresses()); 輸出是一樣的。地址被修改。我怎樣才能做到這一點? 謝謝。 – sandra

+0

很難看到你在評論中做什麼,沒有格式。你能編輯你的問題嗎? –

相關問題