Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mira.viewcount;
- import org.mirapolis.core.Context;
- import org.mirapolis.data.bean.reflect.ReflectDataBean;
- import org.mirapolis.exception.CoreException;
- import org.mirapolis.orm.DataObject;
- import org.mirapolis.orm.EntityManager;
- import org.mirapolis.orm.ORM;
- import org.mirapolis.user.User;
- import org.mirapolis.util.StringHelper;
- import org.mirapolis.util.WebHelper;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Счетчик просмотров
- *
- * @author Anatoly Petrov
- * @since 20.02.2017
- */
- public class ViewCounter {
- private DataObject viewCountDataObject;
- private DataObject userViewDataObject;
- private String type;
- ViewCounter(Class<? extends ReflectDataBean> beanClass) {
- DataObject beanDataObject = ORM.getInstance().getDataObject(beanClass);
- type = beanDataObject.getName();
- viewCountDataObject = ORM.getInstance().getDataObject(ViewCountBean.getViewCountDataObjectName(beanDataObject.getName()));
- if (viewCountDataObject == null) {
- throw new CoreException("ViewCounter for " + beanDataObject.getName() + " not registered");
- }
- userViewDataObject = ORM.getInstance().getDataObject(UserViewBean.getUserViewDataObjectName(beanDataObject.getName()));
- }
- /**
- *
- * @param objectId id объекта
- * @return количество просмотров объекта
- */
- public Integer view(String objectId) {
- User user = Context.get().getUser();
- ViewCountBean viewCountBean = getViewCountBean(objectId);
- if (user.isAnonymous()) {
- Set<String> viewedObjectIds = getViewObjectIdsFromCookies();
- if (viewedObjectIds.contains(objectId)) {
- return viewCountBean.getViewCount();
- } else {
- viewedObjectIds.add(objectId);
- saveViewObjectIdsToCookies(viewedObjectIds);
- viewCountBean.increaseViewCount();
- }
- } else {
- if (isViewedByUser(objectId, user.getId())) {
- return viewCountBean.getViewCount();
- } else {
- viewCountBean.increaseViewCount();
- EntityManager.insert(createUserViewBean(objectId, user.getId()));
- }
- }
- EntityManager.insert(viewCountBean);
- return viewCountBean.getViewCount();
- }
- private ViewCountBean getViewCountBean(String objectId) {
- ViewCountBean viewCountBean = new ViewCountBean(viewCountDataObject);
- viewCountBean.setObject(objectId);
- viewCountBean = EntityManager.get(viewCountBean);
- if (viewCountBean == null) {
- viewCountBean = new ViewCountBean(viewCountDataObject);
- viewCountBean.setObject(objectId);
- viewCountBean.setViewCount(0);
- }
- return viewCountBean;
- }
- private Set<String> getViewObjectIdsFromCookies() {
- return new HashSet<>(StringHelper.splitList(WebHelper.getCookie(Context.get().getRequest(), type)));
- }
- private void saveViewObjectIdsToCookies(Set<String> ids) {
- WebHelper.setCookie(type, StringHelper.joinWithComa(ids), Context.get().getResponse());
- }
- private UserViewBean createUserViewBean(String objectId, String personId) {
- UserViewBean userViewBean = new UserViewBean(userViewDataObject);
- userViewBean.setObject(objectId);
- userViewBean.setPerson(personId);
- return userViewBean;
- }
- private boolean isViewedByUser(String objectId, String personId) {
- UserViewBean userViewBean = createUserViewBean(objectId, personId);
- userViewBean = EntityManager.get(userViewBean);
- return userViewBean != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement