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.setUuid(serviceContext.getUuid());
056 ban.setGroupId(groupId);
057 ban.setCompanyId(user.getCompanyId());
058 ban.setUserId(user.getUserId());
059 ban.setUserName(user.getFullName());
060 ban.setCreateDate(serviceContext.getCreateDate(now));
061 ban.setBanUserId(banUserId);
062 }
063
064 ban.setModifiedDate(serviceContext.getModifiedDate(now));
065
066 mbBanPersistence.update(ban);
067
068 return ban;
069 }
070
071 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
072 public void checkBan(long groupId, long banUserId)
073 throws PortalException, SystemException {
074
075 if (hasBan(groupId, banUserId)) {
076 throw new BannedUserException();
077 }
078 }
079
080 public void deleteBan(long banId) throws PortalException, SystemException {
081 MBBan ban = mbBanPersistence.findByPrimaryKey(banId);
082
083 deleteBan(ban);
084 }
085
086 public void deleteBan(long banUserId, ServiceContext serviceContext)
087 throws SystemException {
088
089 long groupId = serviceContext.getScopeGroupId();
090
091 try {
092 MBBan ban = mbBanPersistence.findByG_B(groupId, banUserId);
093
094 deleteBan(ban);
095 }
096 catch (NoSuchBanException nsbe) {
097 }
098 }
099
100 public void deleteBan(MBBan ban) throws SystemException {
101 mbBanPersistence.remove(ban);
102 }
103
104 public void deleteBansByBanUserId(long banUserId) throws SystemException {
105 List<MBBan> bans = mbBanPersistence.findByBanUserId(banUserId);
106
107 for (MBBan ban : bans) {
108 deleteBan(ban);
109 }
110 }
111
112 public void deleteBansByGroupId(long groupId) throws SystemException {
113 List<MBBan> bans = mbBanPersistence.findByGroupId(groupId);
114
115 for (MBBan ban : bans) {
116 deleteBan(ban);
117 }
118 }
119
120 public void expireBans() throws SystemException {
121 if (PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL <= 0) {
122 return;
123 }
124
125 long now = System.currentTimeMillis();
126
127 List<MBBan> bans = mbBanPersistence.findAll();
128
129 for (MBBan ban : bans) {
130 long unbanDate = MBUtil.getUnbanDate(
131 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
132
133 if (now >= unbanDate) {
134 if (_log.isDebugEnabled()) {
135 _log.debug(
136 "Auto expiring ban " + ban.getBanId() + " on user " +
137 ban.getBanUserId());
138 }
139
140 mbBanPersistence.remove(ban);
141 }
142 }
143 }
144
145 public List<MBBan> getBans(long groupId, int start, int end)
146 throws SystemException {
147
148 return mbBanPersistence.findByGroupId(groupId, start, end);
149 }
150
151 public int getBansCount(long groupId) throws SystemException {
152 return mbBanPersistence.countByGroupId(groupId);
153 }
154
155 public boolean hasBan(long groupId, long banUserId) throws SystemException {
156 if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
157 return false;
158 }
159 else {
160 return true;
161 }
162 }
163
164 private static Log _log = LogFactoryUtil.getLog(
165 MBBanLocalServiceImpl.class);
166
167 }