001
014
015 package com.liferay.portlet.messageboards.service.impl;
016
017 import com.liferay.portal.kernel.annotation.Propagation;
018 import com.liferay.portal.kernel.annotation.Transactional;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.util.PropsValues;
026 import com.liferay.portlet.messageboards.BannedUserException;
027 import com.liferay.portlet.messageboards.NoSuchBanException;
028 import com.liferay.portlet.messageboards.model.MBBan;
029 import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
030 import com.liferay.portlet.messageboards.util.MBUtil;
031
032 import java.util.Date;
033 import java.util.List;
034
035
038 public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
039
040 public MBBan addBan(
041 long userId, long banUserId, ServiceContext serviceContext)
042 throws PortalException, SystemException {
043
044 User user = userPersistence.findByPrimaryKey(userId);
045 long groupId = serviceContext.getScopeGroupId();
046 Date now = new Date();
047
048 long banId = counterLocalService.increment();
049
050 MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
051
052 if (ban == null) {
053 ban = mbBanPersistence.create(banId);
054
055 ban.setGroupId(groupId);
056 ban.setCompanyId(user.getCompanyId());
057 ban.setUserId(user.getUserId());
058 ban.setUserName(user.getFullName());
059 ban.setCreateDate(serviceContext.getCreateDate(now));
060 ban.setBanUserId(banUserId);
061 }
062
063 ban.setModifiedDate(serviceContext.getModifiedDate(now));
064
065 mbBanPersistence.update(ban, false);
066
067 return ban;
068 }
069
070 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
071 public void checkBan(long groupId, long banUserId)
072 throws PortalException, SystemException {
073
074 if (hasBan(groupId, banUserId)) {
075 throw new BannedUserException();
076 }
077 }
078
079 public void deleteBan(long banUserId, ServiceContext serviceContext)
080 throws SystemException {
081
082 long groupId = serviceContext.getScopeGroupId();
083
084 try {
085 mbBanPersistence.removeByG_B(groupId, banUserId);
086 }
087 catch (NoSuchBanException nsbe) {
088 }
089 }
090
091 public void deleteBansByBanUserId(long banUserId) throws SystemException {
092 mbBanPersistence.removeByBanUserId(banUserId);
093 }
094
095 public void deleteBansByGroupId(long groupId) throws SystemException {
096 mbBanPersistence.removeByGroupId(groupId);
097 }
098
099 public void expireBans() throws SystemException {
100 if (PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL <= 0) {
101 return;
102 }
103
104 long now = System.currentTimeMillis();
105
106 List<MBBan> bans = mbBanPersistence.findAll();
107
108 for (MBBan ban : bans) {
109 long unbanDate = MBUtil.getUnbanDate(
110 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
111
112 if (now >= unbanDate) {
113 if (_log.isDebugEnabled()) {
114 _log.debug(
115 "Auto expiring ban " + ban.getBanId() + " on user " +
116 ban.getBanUserId());
117 }
118
119 mbBanPersistence.remove(ban);
120 }
121 }
122 }
123
124 public List<MBBan> getBans(long groupId, int start, int end)
125 throws SystemException {
126
127 return mbBanPersistence.findByGroupId(groupId, start, end);
128 }
129
130 public int getBansCount(long groupId) throws SystemException {
131 return mbBanPersistence.countByGroupId(groupId);
132 }
133
134 public boolean hasBan(long groupId, long banUserId)
135 throws SystemException {
136
137 if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
138 return false;
139 }
140 else {
141 return true;
142 }
143 }
144
145 private static Log _log = LogFactoryUtil.getLog(
146 MBBanLocalServiceImpl.class);
147
148 }