ITeye博客

Hiberante Interceptor

absolute · 2007-09-20 14:16 · Java记事本 · Markdown

java 代码

  1. public class CryptoInterceptor extends EmptyInterceptor {
  2. public boolean onLoad(Object entity, Serializable id, Object[] state,
  3. String[] propertyNames, Type[] types) {
  4. if (entity instanceof Worker) {
  5. for (int i = 0; i < propertyNames.length; i++) {
  6. if ("wkrPhone".equals(propertyNames[i])) {
  7. String phone = (String) state[i];
  8. if (phone != null && !"".equals(phone)) {
  9. state[i] = CryptoUtils.decrypt(phone);
  10. }
  11. }
  12. }
  13. }
  14. return true;
  15. }
  16. public boolean onFlushDirty(Object entity, Serializable id, Object[] state,
  17. String[] propertyNames, Type[] types) {
  18. if (entity instanceof Worker) {
  19. for (int i = 0; i < propertyNames.length; i++) {
  20. if ("wkrPhone".equals(propertyNames[i])) {
  21. String phone = (String) state[i];
  22. if (phone != null && !"".equals(phone)) {
  23. state[i] = CryptoUtils.encrypt(phone);
  24. }
  25. }
  26. }
  27. }
  28. return true;
  29. }
  30. public boolean onSave(Object entity, Serializable id, Object[] state,
  31. String[] propertyNames, Type[] types) {
  32. if (entity instanceof Worker) {
  33. for (int i = 0; i < propertyNames.length; i++) {
  34. if ("wkrPhone".equals(propertyNames[i])) {
  35. String phone = (String) state[i];
  36. if (phone != null && !"".equals(phone)) {
  37. state[i] = CryptoUtils.encrypt(phone);
  38. }
  39. }
  40. }
  41. }
  42. return true;
  43. }
  44. public int[] findDirty(Object entity, Serializable id,
  45. Object[] currentState, Object[] previousState,
  46. String[] propertyNames, Type[] types) {
  47. boolean forceUpdate = false;
  48. int[] results = null;
  49. int count = 0;
  50. if (entity instanceof Worker) {
  51. for (int i = 0; i < types.length; i++) {
  52. if (propertyNames[i].equalsIgnoreCase("wkrPhone")) {
  53. String currentPhone = (String) currentState[i];
  54. String previusPhone = (String) previousState[i];
  55. if (currentPhone != null && !"".equals(currentPhone)) {
  56. if (!currentPhone.equals(previusPhone)) {
  57. forceUpdate = true;
  58. currentState[i] = CryptoUtils.encrypt(currentPhone);
  59. }
  60. }
  61. }
  62. if (results == null) {
  63. results = new int[types.length];
  64. }
  65. results[count++] = i;
  66. }
  67. }
  68. if (count == 0) {
  69. return null;
  70. } else {
  71. int[] trimmed = new int[count];
  72. System.arraycopy(results, 0, trimmed, 0, count);
  73. if (forceUpdate) {
  74. return trimmed;
  75. } else {
  76. return null;
  77. }
  78. }
  79. }
  80. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。