PageRenderTime 115ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/jieshuhuiyou/views/NotificationDisplayHelper.java

https://bitbucket.org/psjay/ants-bookbase
Java | 162 lines | 128 code | 33 blank | 1 comment | 3 complexity | 4437077ceb66a72f05e88d38da928264 MD5 | raw file
  1. package com.jieshuhuiyou.views;
  2. import com.alibaba.fastjson.JSON;
  3. import com.jieshuhuiyou.entity.Notification;
  4. import com.jieshuhuiyou.entity.notifications.AcceptBorrowRequestNotification;
  5. import com.jieshuhuiyou.entity.notifications.AcceptBorrowRequestToOtherNotification;
  6. import com.jieshuhuiyou.entity.notifications.CancleBorrowRequestNotification;
  7. import com.jieshuhuiyou.entity.notifications.EvaluatedNotification;
  8. import com.jieshuhuiyou.entity.notifications.NewBorrowRequestNotification;
  9. import com.jieshuhuiyou.entity.notifications.RejectBorrowRequestNotification;
  10. import com.jieshuhuiyou.entity.notifications.ReturnBookNotification;
  11. import com.jieshuhuiyou.entity.notifications.SharingDeletedNotification;
  12. public class NotificationDisplayHelper {
  13. private Notification notification;
  14. private String html;
  15. private static final String EMPTY_STRING = "";
  16. public NotificationDisplayHelper(Notification notification) {
  17. this.notification = notification;
  18. html = toHTML();
  19. }
  20. public String toHTML() {
  21. switch(notification.getType()) {
  22. case NEW_BORROW_REQUEST:
  23. return newBorrowRequestNotificationToHTML();
  24. case ACCEPT_BORROW_REQUEST:
  25. return acceptBorrowRequestNotificationToHTML();
  26. case REJECT_BORROW_REQEUST:
  27. return rejectBorrowRequestNotificationToHTML();
  28. case CANCLE_BORROW_REQUEST:
  29. return cancelBorrowRequestNotificationToHTML();
  30. case ACCEPT_BORROW_REQUEST_TO_OTHER:
  31. return acceptBorrowRequestToOtherNotificationToHTML();
  32. case RETURN_BOOK:
  33. return returnBookNotificationToHTML();
  34. case SHARING_DELETED:
  35. return sharingDeletedNotificationToHTML();
  36. case EVALUATE_FINISHED:
  37. return evaluatedNotificationToHTML();
  38. }
  39. return EMPTY_STRING;
  40. }
  41. private String newBorrowRequestNotificationToHTML() {
  42. NewBorrowRequestNotification n = JSON.parseObject(notification.getJsonContent()
  43. , NewBorrowRequestNotification.class);
  44. String html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" class=\"notification\" href=\"/borrowRequest/mine\">" + n.getRequest().getUser().getName() + "??????" + n.getRequest().getSharing().getBook().getTitle() + "?</a>";
  45. return html;
  46. }
  47. private String acceptBorrowRequestNotificationToHTML() {
  48. AcceptBorrowRequestNotification n = JSON.parseObject(notification.getJsonContent()
  49. , AcceptBorrowRequestNotification.class);
  50. String html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" class=\"notification\" href=\"/borrow/mine\">"
  51. + n.getBorrow().getSharing().getUser().getName() + "???????"
  52. + n.getBorrow().getSharing().getBook().getTitle() + "????</a>";
  53. return html;
  54. }
  55. private String returnBookNotificationToHTML() {
  56. ReturnBookNotification n = JSON.parseObject(notification.getJsonContent()
  57. , ReturnBookNotification.class);
  58. String html = null;
  59. if(notification.getUser().getId() == n.getBorrow().getSharing().getUser().getId()) {
  60. html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" href=\"/borrow/mine?type=1\" class=\"notification\">"
  61. + n.getBorrow().getUser().getName() +"????" + n.getBorrow().getSharing().getBook().getTitle() + "?????</a>";
  62. }
  63. else {
  64. html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" href=\"/borrow/mine\" class=\"notification\">"
  65. + n.getBorrow().getSharing().getUser().getName() +"???????" + n.getBorrow().getSharing().getBook().getTitle() + "?????</a>";
  66. }
  67. return html;
  68. }
  69. private String rejectBorrowRequestNotificationToHTML() {
  70. RejectBorrowRequestNotification n = JSON.parseObject(notification.getJsonContent()
  71. , RejectBorrowRequestNotification.class);
  72. int bookId = n.getRequest().getSharing().getBook().getId();
  73. String bookTitle = n.getRequest().getSharing().getBook().getTitle();
  74. String providerName = n.getRequest().getSharing().getUser().getName();
  75. String html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" class=\"notification\" href=\"/book/" + bookId + "\">"
  76. + providerName + "???????"
  77. + bookTitle + "????</a>";
  78. return html;
  79. }
  80. private String cancelBorrowRequestNotificationToHTML() {
  81. CancleBorrowRequestNotification n = JSON.parseObject(notification.getJsonContent()
  82. , CancleBorrowRequestNotification.class);
  83. int bookId = n.getRequest().getSharing().getBook().getId();
  84. String borrowerName = n.getRequest().getUser().getName();
  85. String bookTitle = n.getRequest().getSharing().getBook().getTitle();
  86. String html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" class=\"notification\" href=\"/book/" + bookId + "\">"
  87. + borrowerName + "????????"
  88. + bookTitle + "????</a>";
  89. return html;
  90. }
  91. private String acceptBorrowRequestToOtherNotificationToHTML() {
  92. AcceptBorrowRequestToOtherNotification n = JSON.parseObject(notification.getJsonContent()
  93. , AcceptBorrowRequestToOtherNotification.class);
  94. int bookId = n.getRequest().getSharing().getBook().getId();
  95. String bookTitle = n.getRequest().getSharing().getBook().getTitle();
  96. String providerName = n.getRequest().getSharing().getUser().getName();
  97. String html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" class=\"notification\" href=\"/book/" + bookId + "\">"
  98. + providerName + "??" + bookTitle + "??????????????????</a>";
  99. return html;
  100. }
  101. private String sharingDeletedNotificationToHTML() {
  102. SharingDeletedNotification n = JSON.parseObject(notification.getJsonContent()
  103. , SharingDeletedNotification.class);
  104. int bookId = n.getSharing().getBook().getId();
  105. String bookTitle = n.getSharing().getBook().getTitle();
  106. String providerName = n.getSharing().getUser().getName();
  107. String html = "<a target=\"_blank\" _nid=\"" + notification.getId() + "\" class=\"notification\" href=\"/book/" + bookId + "\">"
  108. + providerName + "????" + bookTitle + "??????????????</a>";
  109. return html;
  110. }
  111. private String evaluatedNotificationToHTML(){
  112. EvaluatedNotification n = JSON.parseObject(notification.getJsonContent()
  113. , EvaluatedNotification.class);
  114. return n.toHTML();
  115. }
  116. // setters and getters
  117. public Notification getNotification() {
  118. return notification;
  119. }
  120. public void setNotification(Notification notification) {
  121. this.notification = notification;
  122. }
  123. public String getHtml() {
  124. return html;
  125. }
  126. public void setHtml(String html) {
  127. this.html = html;
  128. }
  129. }