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.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.systemevent.SystemEvent;
021 import com.liferay.portal.kernel.transaction.Propagation;
022 import com.liferay.portal.kernel.transaction.Transactional;
023 import com.liferay.portal.model.SystemEventConstants;
024 import com.liferay.portal.model.User;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portal.util.PropsValues;
027 import com.liferay.portlet.messageboards.exception.BannedUserException;
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 @Override
041 public MBBan addBan(
042 long userId, long banUserId, ServiceContext serviceContext)
043 throws PortalException {
044
045 User user = userPersistence.findByPrimaryKey(userId);
046 long groupId = serviceContext.getScopeGroupId();
047
048 long banId = counterLocalService.increment();
049
050 MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
051
052 if (ban == null) {
053 Date now = new Date();
054
055 ban = mbBanPersistence.create(banId);
056
057 ban.setUuid(serviceContext.getUuid());
058 ban.setGroupId(groupId);
059 ban.setCompanyId(user.getCompanyId());
060 ban.setUserId(user.getUserId());
061 ban.setUserName(user.getFullName());
062 ban.setCreateDate(serviceContext.getCreateDate(now));
063 ban.setModifiedDate(serviceContext.getModifiedDate(now));
064 ban.setBanUserId(banUserId);
065 }
066
067 mbBanPersistence.update(ban);
068
069 return ban;
070 }
071
072 @Override
073 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
074 public void checkBan(long groupId, long banUserId) throws PortalException {
075 if (hasBan(groupId, banUserId)) {
076 throw new BannedUserException("Banned user " + banUserId);
077 }
078 }
079
080 @Override
081 public void deleteBan(long banId) throws PortalException {
082 MBBan ban = mbBanPersistence.findByPrimaryKey(banId);
083
084 mbBanLocalService.deleteBan(ban);
085 }
086
087 @Override
088 public void deleteBan(long banUserId, ServiceContext serviceContext) {
089 long groupId = serviceContext.getScopeGroupId();
090
091 MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
092
093 if (ban != null) {
094 mbBanLocalService.deleteBan(ban);
095 }
096 }
097
098 @Override
099 @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
100 public void deleteBan(MBBan ban) {
101 mbBanPersistence.remove(ban);
102 }
103
104 @Override
105 public void deleteBansByBanUserId(long banUserId) {
106 List<MBBan> bans = mbBanPersistence.findByBanUserId(banUserId);
107
108 for (MBBan ban : bans) {
109 mbBanLocalService.deleteBan(ban);
110 }
111 }
112
113 @Override
114 public void deleteBansByGroupId(long groupId) {
115 List<MBBan> bans = mbBanPersistence.findByGroupId(groupId);
116
117 for (MBBan ban : bans) {
118 mbBanLocalService.deleteBan(ban);
119 }
120 }
121
122 @Override
123 public void expireBans() {
124 if (PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL <= 0) {
125 return;
126 }
127
128 long now = System.currentTimeMillis();
129
130 List<MBBan> bans = mbBanPersistence.findAll();
131
132 for (MBBan ban : bans) {
133 Date unbanDate = MBUtil.getUnbanDate(
134 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL);
135
136 long unbanTime = unbanDate.getTime();
137
138 if (now >= unbanTime) {
139 if (_log.isDebugEnabled()) {
140 _log.debug(
141 "Auto expiring ban " + ban.getBanId() + " on user " +
142 ban.getBanUserId());
143 }
144
145 mbBanPersistence.remove(ban);
146 }
147 }
148 }
149
150 @Override
151 public List<MBBan> getBans(long groupId, int start, int end) {
152 return mbBanPersistence.findByGroupId(groupId, start, end);
153 }
154
155 @Override
156 public int getBansCount(long groupId) {
157 return mbBanPersistence.countByGroupId(groupId);
158 }
159
160 @Override
161 public boolean hasBan(long groupId, long banUserId) {
162 if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
163 return false;
164 }
165 else {
166 return true;
167 }
168 }
169
170 private static final Log _log = LogFactoryUtil.getLog(
171 MBBanLocalServiceImpl.class);
172
173 }