001
014
015 package com.liferay.portlet.messageboards.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.transaction.Propagation;
022 import com.liferay.portal.kernel.transaction.Transactional;
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 banId) throws PortalException, SystemException {
080 MBBan ban = mbBanPersistence.findByPrimaryKey(banId);
081
082 deleteBan(ban);
083 }
084
085 public void deleteBan(long banUserId, ServiceContext serviceContext)
086 throws SystemException {
087
088 long groupId = serviceContext.getScopeGroupId();
089
090 try {
091 MBBan ban = mbBanPersistence.findByG_B(groupId, banUserId);
092
093 deleteBan(ban);
094 }
095 catch (NoSuchBanException nsbe) {
096 }
097 }
098
099 public void deleteBan(MBBan ban) throws SystemException {
100 mbBanPersistence.remove(ban);
101 }
102
103 public void deleteBansByBanUserId(long banUserId) throws SystemException {
104 List<MBBan> bans = mbBanPersistence.findByBanUserId(banUserId);
105
106 for (MBBan ban : bans) {
107 deleteBan(ban);
108 }
109 }
110
111 public void deleteBansByGroupId(long groupId) throws SystemException {
112 List<MBBan> bans = mbBanPersistence.findByGroupId(groupId);
113
114 for (MBBan ban : bans) {
115 deleteBan(ban);
116 }
117 }
118
119 public void expireBans() throws SystemException {
120 if (PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL <= 0) {
121 return;
122 }
123
124 long now = System.currentTimeMillis();
125
126 List<MBBan> bans = mbBanPersistence.findAll();
127
128 for (MBBan ban : bans) {
129 long unbanDate = MBUtil.getUnbanDate(
130 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
131
132 if (now >= unbanDate) {
133 if (_log.isDebugEnabled()) {
134 _log.debug(
135 "Auto expiring ban " + ban.getBanId() + " on user " +
136 ban.getBanUserId());
137 }
138
139 mbBanPersistence.remove(ban);
140 }
141 }
142 }
143
144 public List<MBBan> getBans(long groupId, int start, int end)
145 throws SystemException {
146
147 return mbBanPersistence.findByGroupId(groupId, start, end);
148 }
149
150 public int getBansCount(long groupId) throws SystemException {
151 return mbBanPersistence.countByGroupId(groupId);
152 }
153
154 public boolean hasBan(long groupId, long banUserId)
155 throws SystemException {
156
157 if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
158 return false;
159 }
160 else {
161 return true;
162 }
163 }
164
165 private static Log _log = LogFactoryUtil.getLog(
166 MBBanLocalServiceImpl.class);
167
168 }