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.model.User;
28 import com.liferay.portal.util.PortalUtil;
29 import com.liferay.portal.util.PropsValues;
30 import com.liferay.portlet.messageboards.BannedUserException;
31 import com.liferay.portlet.messageboards.NoSuchBanException;
32 import com.liferay.portlet.messageboards.model.MBBan;
33 import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
34 import com.liferay.portlet.messageboards.util.MBUtil;
35
36 import java.util.Date;
37 import java.util.List;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42
48 public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
49
50 public MBBan addBan(long userId, long plid, long banUserId)
51 throws PortalException, SystemException {
52
53 User user = userPersistence.findByPrimaryKey(userId);
54 long groupId = PortalUtil.getPortletGroupId(plid);
55 Date now = new Date();
56
57 long banId = counterLocalService.increment();
58
59 MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
60
61 if (ban == null) {
62 ban = mbBanPersistence.create(banId);
63
64 ban.setGroupId(groupId);
65 ban.setCompanyId(user.getCompanyId());
66 ban.setUserId(user.getUserId());
67 ban.setUserName(user.getFullName());
68 ban.setCreateDate(now);
69 ban.setBanUserId(banUserId);
70 }
71
72 ban.setModifiedDate(now);
73
74 mbBanPersistence.update(ban, false);
75
76 return ban;
77 }
78
79 public void checkBan(long groupId, long banUserId)
80 throws PortalException, SystemException {
81
82 if (hasBan(groupId, banUserId)) {
83 throw new BannedUserException();
84 }
85 }
86
87 public void deleteBan(long plid, long banUserId)
88 throws PortalException, SystemException {
89
90 long groupId = PortalUtil.getPortletGroupId(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 = LogFactory.getLog(MBBanLocalServiceImpl.class);
150
151 }