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.util.PortalUtil;
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(long userId, long plid, long banUserId)
52 throws PortalException, SystemException {
53
54 User user = userPersistence.findByPrimaryKey(userId);
55 long groupId = PortalUtil.getScopeGroupId(plid);
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 plid, long banUserId) throws SystemException {
90 long groupId = PortalUtil.getScopeGroupId(plid);
91
92 try {
93 mbBanPersistence.removeByG_B(groupId, banUserId);
94 }
95 catch (NoSuchBanException nsbe) {
96 }
97 }
98
99 public void deleteBansByBanUserId(long banUserId) throws SystemException {
100 mbBanPersistence.removeByBanUserId(banUserId);
101 }
102
103 public void deleteBansByGroupId(long groupId) throws SystemException {
104 mbBanPersistence.removeByGroupId(groupId);
105 }
106
107 public void expireBans() throws SystemException {
108 long now = System.currentTimeMillis();
109
110 List<MBBan> bans = mbBanPersistence.findAll();
111
112 for (MBBan ban : bans) {
113 long unbanDate = MBUtil.getUnbanDate(
114 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
115
116 if (now >= unbanDate) {
117 if (_log.isDebugEnabled()) {
118 _log.debug(
119 "Auto expiring ban " + ban.getBanId() + " on user " +
120 ban.getBanUserId());
121 }
122
123 mbBanPersistence.remove(ban);
124 }
125 }
126 }
127
128 public List<MBBan> getBans(long groupId, int start, int end)
129 throws SystemException {
130
131 return mbBanPersistence.findByGroupId(groupId, start, end);
132 }
133
134 public int getBansCount(long groupId) throws SystemException {
135 return mbBanPersistence.countByGroupId(groupId);
136 }
137
138 public boolean hasBan(long groupId, long banUserId)
139 throws SystemException {
140
141 if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
142 return false;
143 }
144 else {
145 return true;
146 }
147 }
148
149 private static Log _log =
150 LogFactoryUtil.getLog(MBBanLocalServiceImpl.class);
151
152 }