1
22
23 package com.liferay.portlet.messageboards.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.annotation.Propagation;
28 import com.liferay.portal.kernel.annotation.Transactional;
29 import com.liferay.portal.kernel.log.Log;
30 import com.liferay.portal.kernel.log.LogFactoryUtil;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.ServiceContext;
33 import com.liferay.portal.util.PropsValues;
34 import com.liferay.portlet.messageboards.BannedUserException;
35 import com.liferay.portlet.messageboards.NoSuchBanException;
36 import com.liferay.portlet.messageboards.model.MBBan;
37 import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
38 import com.liferay.portlet.messageboards.util.MBUtil;
39
40 import java.util.Date;
41 import java.util.List;
42
43
49 public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
50
51 public MBBan addBan(
52 long userId, long banUserId, ServiceContext serviceContext)
53 throws PortalException, SystemException {
54
55 User user = userPersistence.findByPrimaryKey(userId);
56 long groupId = serviceContext.getScopeGroupId();
57 Date now = new Date();
58
59 long banId = counterLocalService.increment();
60
61 MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
62
63 if (ban == null) {
64 ban = mbBanPersistence.create(banId);
65
66 ban.setGroupId(groupId);
67 ban.setCompanyId(user.getCompanyId());
68 ban.setUserId(user.getUserId());
69 ban.setUserName(user.getFullName());
70 ban.setCreateDate(now);
71 ban.setBanUserId(banUserId);
72 }
73
74 ban.setModifiedDate(now);
75
76 mbBanPersistence.update(ban, false);
77
78 return ban;
79 }
80
81 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
82 public void checkBan(long groupId, long banUserId)
83 throws PortalException, SystemException {
84
85 if (hasBan(groupId, banUserId)) {
86 throw new BannedUserException();
87 }
88 }
89
90 public void deleteBan(long banUserId, ServiceContext serviceContext)
91 throws SystemException {
92
93 long groupId = serviceContext.getScopeGroupId();
94
95 try {
96 mbBanPersistence.removeByG_B(groupId, banUserId);
97 }
98 catch (NoSuchBanException nsbe) {
99 }
100 }
101
102 public void deleteBansByBanUserId(long banUserId) throws SystemException {
103 mbBanPersistence.removeByBanUserId(banUserId);
104 }
105
106 public void deleteBansByGroupId(long groupId) throws SystemException {
107 mbBanPersistence.removeByGroupId(groupId);
108 }
109
110 public void expireBans() throws SystemException {
111 long now = System.currentTimeMillis();
112
113 List<MBBan> bans = mbBanPersistence.findAll();
114
115 for (MBBan ban : bans) {
116 long unbanDate = MBUtil.getUnbanDate(
117 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
118
119 if (now >= unbanDate) {
120 if (_log.isDebugEnabled()) {
121 _log.debug(
122 "Auto expiring ban " + ban.getBanId() + " on user " +
123 ban.getBanUserId());
124 }
125
126 mbBanPersistence.remove(ban);
127 }
128 }
129 }
130
131 public List<MBBan> getBans(long groupId, int start, int end)
132 throws SystemException {
133
134 return mbBanPersistence.findByGroupId(groupId, start, end);
135 }
136
137 public int getBansCount(long groupId) throws SystemException {
138 return mbBanPersistence.countByGroupId(groupId);
139 }
140
141 public boolean hasBan(long groupId, long banUserId)
142 throws SystemException {
143
144 if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
145 return false;
146 }
147 else {
148 return true;
149 }
150 }
151
152 private static Log _log =
153 LogFactoryUtil.getLog(MBBanLocalServiceImpl.class);
154
155 }