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.util.GetterUtil;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.util.PortalUtil;
30 import com.liferay.portal.util.PropsUtil;
31 import com.liferay.portlet.messageboards.BannedUserException;
32 import com.liferay.portlet.messageboards.NoSuchBanException;
33 import com.liferay.portlet.messageboards.model.MBBan;
34 import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
35 import com.liferay.portlet.messageboards.util.MBUtil;
36
37 import java.util.Date;
38 import java.util.List;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
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.getPortletGroupId(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);
76
77 return ban;
78 }
79
80 public void checkBan(long groupId, long banUserId)
81 throws PortalException, SystemException {
82
83 if (hasBan(groupId, banUserId)) {
84 throw new BannedUserException();
85 }
86 }
87
88 public void deleteBan(long plid, long banUserId)
89 throws PortalException, SystemException {
90
91 long groupId = PortalUtil.getPortletGroupId(plid);
92
93 try {
94 mbBanPersistence.removeByG_B(groupId, banUserId);
95 }
96 catch (NoSuchBanException nsbe) {
97 }
98 }
99
100 public void deleteBansByBanUserId(long banUserId) throws SystemException {
101 mbBanPersistence.removeByBanUserId(banUserId);
102 }
103
104 public void deleteBansByGroupId(long groupId) throws SystemException {
105 mbBanPersistence.removeByGroupId(groupId);
106 }
107
108 public void expireBans() throws SystemException {
109 long now = System.currentTimeMillis();
110
111 int expireInterval = GetterUtil.getInteger(PropsUtil.get(
112 PropsUtil.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL));
113
114 List bans = mbBanPersistence.findAll();
115
116 for (int i = 0; i < bans.size(); i++) {
117 MBBan ban = (MBBan)bans.get(i);
118
119 long unbanDate = MBUtil.getUnbanDate(ban, expireInterval).getTime();
120
121 if (now >= unbanDate) {
122 if (_log.isDebugEnabled()) {
123 _log.debug(
124 "Auto expiring ban " + ban.getBanId() + " on user " +
125 ban.getBanUserId());
126 }
127
128 mbBanPersistence.remove(ban);
129 }
130 }
131 }
132
133 public List getBans(long groupId, int start, int end)
134 throws SystemException {
135
136 return mbBanPersistence.findByGroupId(groupId, start, end);
137 }
138
139 public int getBansCount(long groupId) throws SystemException {
140 return mbBanPersistence.countByGroupId(groupId);
141 }
142
143 public boolean hasBan(long groupId, long banUserId)
144 throws SystemException {
145
146 if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
147 return false;
148 }
149 else {
150 return true;
151 }
152 }
153
154 private static Log _log = LogFactory.getLog(MBBanLocalServiceImpl.class);
155
156 }