Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package mira.viewcount;
  2.  
  3. import org.mirapolis.core.Context;
  4. import org.mirapolis.data.bean.reflect.ReflectDataBean;
  5. import org.mirapolis.exception.CoreException;
  6. import org.mirapolis.orm.DataObject;
  7. import org.mirapolis.orm.EntityManager;
  8. import org.mirapolis.orm.ORM;
  9. import org.mirapolis.user.User;
  10. import org.mirapolis.util.StringHelper;
  11. import org.mirapolis.util.WebHelper;
  12.  
  13. import java.util.HashSet;
  14. import java.util.Set;
  15.  
  16. /**
  17. * Счетчик просмотров
  18. *
  19. * @author Anatoly Petrov
  20. * @since 20.02.2017
  21. */
  22. public class ViewCounter {
  23.  
  24. private DataObject viewCountDataObject;
  25. private DataObject userViewDataObject;
  26. private String type;
  27.  
  28. ViewCounter(Class<? extends ReflectDataBean> beanClass) {
  29. DataObject beanDataObject = ORM.getInstance().getDataObject(beanClass);
  30. type = beanDataObject.getName();
  31. viewCountDataObject = ORM.getInstance().getDataObject(ViewCountBean.getViewCountDataObjectName(beanDataObject.getName()));
  32. if (viewCountDataObject == null) {
  33. throw new CoreException("ViewCounter for " + beanDataObject.getName() + " not registered");
  34. }
  35. userViewDataObject = ORM.getInstance().getDataObject(UserViewBean.getUserViewDataObjectName(beanDataObject.getName()));
  36. }
  37.  
  38. /**
  39. *
  40. * @param objectId id объекта
  41. * @return количество просмотров объекта
  42. */
  43. public Integer view(String objectId) {
  44. User user = Context.get().getUser();
  45. ViewCountBean viewCountBean = getViewCountBean(objectId);
  46. if (user.isAnonymous()) {
  47. Set<String> viewedObjectIds = getViewObjectIdsFromCookies();
  48. if (viewedObjectIds.contains(objectId)) {
  49. return viewCountBean.getViewCount();
  50. } else {
  51. viewedObjectIds.add(objectId);
  52. saveViewObjectIdsToCookies(viewedObjectIds);
  53. viewCountBean.increaseViewCount();
  54. }
  55. } else {
  56. if (isViewedByUser(objectId, user.getId())) {
  57. return viewCountBean.getViewCount();
  58. } else {
  59. viewCountBean.increaseViewCount();
  60. EntityManager.insert(createUserViewBean(objectId, user.getId()));
  61. }
  62. }
  63. EntityManager.insert(viewCountBean);
  64. return viewCountBean.getViewCount();
  65. }
  66.  
  67. private ViewCountBean getViewCountBean(String objectId) {
  68. ViewCountBean viewCountBean = new ViewCountBean(viewCountDataObject);
  69. viewCountBean.setObject(objectId);
  70. viewCountBean = EntityManager.get(viewCountBean);
  71. if (viewCountBean == null) {
  72. viewCountBean = new ViewCountBean(viewCountDataObject);
  73. viewCountBean.setObject(objectId);
  74. viewCountBean.setViewCount(0);
  75. }
  76. return viewCountBean;
  77. }
  78.  
  79. private Set<String> getViewObjectIdsFromCookies() {
  80. return new HashSet<>(StringHelper.splitList(WebHelper.getCookie(Context.get().getRequest(), type)));
  81. }
  82.  
  83. private void saveViewObjectIdsToCookies(Set<String> ids) {
  84. WebHelper.setCookie(type, StringHelper.joinWithComa(ids), Context.get().getResponse());
  85. }
  86.  
  87. private UserViewBean createUserViewBean(String objectId, String personId) {
  88. UserViewBean userViewBean = new UserViewBean(userViewDataObject);
  89. userViewBean.setObject(objectId);
  90. userViewBean.setPerson(personId);
  91. return userViewBean;
  92. }
  93.  
  94. private boolean isViewedByUser(String objectId, String personId) {
  95. UserViewBean userViewBean = createUserViewBean(objectId, personId);
  96. userViewBean = EntityManager.get(userViewBean);
  97. return userViewBean != null;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement