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