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) throws SystemException {
88 long groupId = PortalUtil.getPortletGroupId(plid);
89
90 try {
91 mbBanPersistence.removeByG_B(groupId, banUserId);
92 }
93 catch (NoSuchBanException nsbe) {
94 }
95 }
96
97 public void deleteBansByBanUserId(long banUserId) throws SystemException {
98 mbBanPersistence.removeByBanUserId(banUserId);
99 }
100
101 public void deleteBansByGroupId(long groupId) throws SystemException {
102 mbBanPersistence.removeByGroupId(groupId);
103 }
104
105 public void expireBans() throws SystemException {
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 = LogFactory.getLog(MBBanLocalServiceImpl.class);
148
149 }