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