1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.PropsValues;
29  import com.liferay.portlet.messageboards.BannedUserException;
30  import com.liferay.portlet.messageboards.NoSuchBanException;
31  import com.liferay.portlet.messageboards.model.MBBan;
32  import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
33  import com.liferay.portlet.messageboards.util.MBUtil;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * <a href="MBBanLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
45  
46      public MBBan addBan(long userId, long plid, long banUserId)
47          throws PortalException, SystemException {
48  
49          User user = userPersistence.findByPrimaryKey(userId);
50          long groupId = PortalUtil.getScopeGroupId(plid);
51          Date now = new Date();
52  
53          long banId = counterLocalService.increment();
54  
55          MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
56  
57          if (ban == null) {
58              ban = mbBanPersistence.create(banId);
59  
60              ban.setGroupId(groupId);
61              ban.setCompanyId(user.getCompanyId());
62              ban.setUserId(user.getUserId());
63              ban.setUserName(user.getFullName());
64              ban.setCreateDate(now);
65              ban.setBanUserId(banUserId);
66          }
67  
68          ban.setModifiedDate(now);
69  
70          mbBanPersistence.update(ban, false);
71  
72          return ban;
73      }
74  
75      public void checkBan(long groupId, long banUserId)
76          throws PortalException, SystemException {
77  
78          if (hasBan(groupId, banUserId)) {
79              throw new BannedUserException();
80          }
81      }
82  
83      public void deleteBan(long plid, long banUserId) throws SystemException {
84          long groupId = PortalUtil.getScopeGroupId(plid);
85  
86          try {
87              mbBanPersistence.removeByG_B(groupId, banUserId);
88          }
89          catch (NoSuchBanException nsbe) {
90          }
91      }
92  
93      public void deleteBansByBanUserId(long banUserId) throws SystemException {
94          mbBanPersistence.removeByBanUserId(banUserId);
95      }
96  
97      public void deleteBansByGroupId(long groupId) throws SystemException {
98          mbBanPersistence.removeByGroupId(groupId);
99      }
100 
101     public void expireBans() throws SystemException {
102         long now = System.currentTimeMillis();
103 
104         List<MBBan> bans = mbBanPersistence.findAll();
105 
106         for (MBBan ban : bans) {
107             long unbanDate = MBUtil.getUnbanDate(
108                 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
109 
110             if (now >= unbanDate) {
111                 if (_log.isDebugEnabled()) {
112                     _log.debug(
113                         "Auto expiring ban " + ban.getBanId() + " on user " +
114                             ban.getBanUserId());
115                 }
116 
117                 mbBanPersistence.remove(ban);
118             }
119         }
120     }
121 
122     public List<MBBan> getBans(long groupId, int start, int end)
123         throws SystemException {
124 
125         return mbBanPersistence.findByGroupId(groupId, start, end);
126     }
127 
128     public int getBansCount(long groupId) throws SystemException {
129         return mbBanPersistence.countByGroupId(groupId);
130     }
131 
132     public boolean hasBan(long groupId, long banUserId)
133         throws SystemException {
134 
135         if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
136             return false;
137         }
138         else {
139             return true;
140         }
141     }
142 
143     private static Log _log =
144          LogFactoryUtil.getLog(MBBanLocalServiceImpl.class);
145 
146 }