1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.impl;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.persistence.UserUtil;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portlet.messageboards.BannedUserException;
34  import com.liferay.portlet.messageboards.NoSuchBanException;
35  import com.liferay.portlet.messageboards.model.MBBan;
36  import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
37  import com.liferay.portlet.messageboards.service.persistence.MBBanUtil;
38  import com.liferay.portlet.messageboards.util.MBUtil;
39  
40  import java.util.Date;
41  import java.util.List;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="MBBanLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
53  
54      public MBBan addBan(long userId, long plid, long banUserId)
55          throws PortalException, SystemException {
56  
57          User user = UserUtil.findByPrimaryKey(userId);
58          long groupId = PortalUtil.getPortletGroupId(plid);
59          Date now = new Date();
60  
61          long banId = CounterLocalServiceUtil.increment();
62  
63          MBBan ban = MBBanUtil.fetchByG_B(groupId, banUserId);
64  
65          if (ban == null) {
66              ban = MBBanUtil.create(banId);
67  
68              ban.setGroupId(groupId);
69              ban.setCompanyId(user.getCompanyId());
70              ban.setUserId(user.getUserId());
71              ban.setUserName(user.getFullName());
72              ban.setCreateDate(now);
73              ban.setBanUserId(banUserId);
74          }
75  
76          ban.setModifiedDate(now);
77  
78          MBBanUtil.update(ban);
79  
80          return ban;
81      }
82  
83      public void checkBan(long groupId, long banUserId)
84          throws PortalException, SystemException {
85  
86          if (hasBan(groupId, banUserId)) {
87              throw new BannedUserException();
88          }
89      }
90  
91      public void deleteBan(long plid, long banUserId)
92          throws PortalException, SystemException {
93  
94          long groupId = PortalUtil.getPortletGroupId(plid);
95  
96          try {
97              MBBanUtil.removeByG_B(groupId, banUserId);
98          }
99          catch (NoSuchBanException nsbe) {
100         }
101     }
102 
103     public void deleteBansByBanUserId(long banUserId) throws SystemException {
104         MBBanUtil.removeByBanUserId(banUserId);
105     }
106 
107     public void deleteBansByGroupId(long groupId) throws SystemException {
108         MBBanUtil.removeByGroupId(groupId);
109     }
110 
111     public void expireBans() throws SystemException {
112         long now = System.currentTimeMillis();
113 
114         int expireInterval = GetterUtil.getInteger(PropsUtil.get(
115             PropsUtil.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL));
116 
117         List bans = MBBanUtil.findAll();
118 
119         for (int i = 0; i < bans.size(); i++) {
120             MBBan ban = (MBBan)bans.get(i);
121 
122             long unbanDate = MBUtil.getUnbanDate(ban, expireInterval).getTime();
123 
124             if (now >= unbanDate) {
125                 if (_log.isDebugEnabled()) {
126                     _log.debug(
127                         "Auto expiring ban " + ban.getBanId() + " on user " +
128                             ban.getBanUserId());
129                 }
130 
131                 MBBanUtil.remove(ban);
132             }
133         }
134     }
135 
136     public List getBans(long groupId, int start, int end)
137         throws SystemException {
138 
139         return MBBanUtil.findByGroupId(groupId, start, end);
140     }
141 
142     public int getBansCount(long groupId) throws SystemException {
143         return MBBanUtil.countByGroupId(groupId);
144     }
145 
146     public boolean hasBan(long groupId, long banUserId)
147         throws SystemException {
148 
149         if (MBBanUtil.fetchByG_B(groupId, banUserId) == null) {
150             return false;
151         }
152         else {
153             return true;
154         }
155     }
156 
157     private static Log _log = LogFactory.getLog(MBBanLocalServiceImpl.class);
158 
159 }